added a bunch of features, preparing to read from obj files for display

This commit is contained in:
InventorXtreme 2024-03-10 01:58:05 -05:00
parent 6b04973314
commit 34e70de2f9
11 changed files with 567 additions and 377 deletions

View file

@ -1 +1,2 @@
IndentWidth: 4 IndentWidth: 4
ColumnLimit: 80

View file

@ -1,12 +1,12 @@
CC = gcc CC = clang #Set compiler
CFLAGS = -pg -g -Wall CFLAGS = -pg -g -Wall -O2 -pg #set compiler flags
LDFLAGS = -pg -g LDFLAGS = -pg -g #set linker flags
LDLIBS = -lraylib -lm LDLIBS = -lraylib -lm #set libraries to use
objects = c3d.o objects = c3d.o reader.o arrayfuncs.o vecfunc.c #list all object files
c3d: $(objects) c3d: $(objects) #State that to make the c3d executeable, we need all objects
$(objects): %.o: %.c $(objects): %.o: %.c #State that to make each object, we use its respective c file
clean: clean: #to clean remove all object files and the executable
rm -f *.o c3d rm -f *.o c3d

11
arrayfuncs.c Normal file
View file

@ -0,0 +1,11 @@
#include "c3dtypes.h"
void TriArrayAppend(TriArray *tarr, Tri t) {
tarr->arr[tarr->length] = t;
tarr->length = tarr->length + 1;
}
void Tri2DArrayAppend(Tri2DArray *tarr, Tri2D t) {
tarr->arr[tarr->length] = t;
tarr->length = tarr->length + 1;
}

8
arrayfuncs.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef ARRAYFUNCS_HEADER
#define ARRAYFUNCS_HEADER
void TriArrayAppend(TriArray *tarr, Tri t);
void Tri2DArrayAppend(Tri2DArray *tarr, Tri2D t);
#endif

655
c3d.c
View file

@ -1,8 +1,13 @@
#include "raylib.h"
#include "reader.h"
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include "raylib.h" #include <string.h>
#include "c3dtypes.h"
#include "arrayfuncs.h"
#include "vecfunc.h"
const int RENDERWIDTH = 1920; const int RENDERWIDTH = 1920;
@ -11,206 +16,170 @@ const int RENDERHEIGHT = 1080;
const int HALFWIDTH = RENDERWIDTH / 2; const int HALFWIDTH = RENDERWIDTH / 2;
const int HALFHEIGHT = RENDERHEIGHT / 2; const int HALFHEIGHT = RENDERHEIGHT / 2;
const int SCREENWIDTH = 1280; const int SCREENWIDTH = 1280;
const int SCREENHEIGHT = 720; const int SCREENHEIGHT = 720;
const float WIDTHSCALE = (float)RENDERWIDTH / SCREENWIDTH; const float WIDTHSCALE = (float)RENDERWIDTH / SCREENWIDTH;
const float HEIGHTSCALE = (float) RENDERHEIGHT / SCREENHEIGHT; const float HEIGHTSCALE = (float)RENDERHEIGHT / SCREENHEIGHT;
const float fov = 70.0 / 180.0 * 3.14159265369; const float fov = 70.0 / 180.0 * 3.14159265369;
const float half_fov = fov / 2; const float half_fov = fov / 2;
float proj; //HALFWIDTH / tan(half_fov) Inited later float proj; // HALFWIDTH / tan(half_fov) Inited later
double Min(double a, double b) {
struct LocalCam { if (a > b) {
Vector3 position; return b;
Vector3 velocity;
Vector3 acceleration;
Vector3 angles;
Vector3 angleVelocity;
Vector3 angleAcceleration;
};
typedef struct LocalCam LocalCam;
struct Tri {
Vector3 a;
Vector3 b;
Vector3 c;
Color color;
};
typedef struct Tri Tri;
struct Tri2D{
Vector2 a;
Vector2 b;
Vector2 c;
Color color;
};
typedef struct Tri2D Tri2D;
struct Zee {
int depth;
Tri2D * triangle;
};
typedef struct Zee Zee;
struct TriArray {
int length;
Tri* arr;
};
typedef struct TriArray TriArray;
struct Tri2DArray {
int length;
Tri2D* arr;
};
typedef struct Tri2DArray Tri2DArray;
double Min(double a, double b){
if (a>b) {
return b;
} }
return a; return a;
} }
double Max(double a, double b){ double Max(double a, double b) {
if (a<b){ if (a < b) {
return b; return b;
} }
return a; return a;
} }
static inline int IndexOfZBuff(int row, int col) { static inline int IndexOfZBuff(int row, int col) {
return row + RENDERWIDTH*col; return row + RENDERWIDTH * col;
} }
// sort triangle verts so that point A is the "highest" point (lowest y val) and
//sort triangle verts so that point A is the "highest" point (lowest y val) and point C is the "lowest" pont (highest y val) // point C is the "lowest" pont (highest y val)
void Tri2DSortByY(Tri2D * t){ void Tri2DSortByY(Tri2D *t) {
Vector2 temp; Vector2 temp;
if (t->a.y > t->b.y) { if (t->a.y > t->b.y) {
temp = t->a; temp = t->a;
t->a = t->b; t->a = t->b;
t->b = temp; t->b = temp;
} }
if (t->b.y > t->c.y){ if (t->b.y > t->c.y) {
temp = t->b; temp = t->b;
t->b = t->c; t->b = t->c;
t->c = temp; t->c = temp;
} }
if (t->a.y > t->b.y) { if (t->a.y > t->b.y) {
temp = t->a; temp = t->a;
t->a = t->b; t->a = t->b;
t->b = temp; t->b = temp;
} }
} }
//Draws triangle with a flat top. Note A and B must be the top points with C being the bottom "spike" // Draws triangle with a flat top. Note A and B must be the top points with C
void FillTopFlatZbuffer(Zee * zee , Tri2D* t, Tri2D * tp) { // being the bottom "spike"
void FillTopFlatZbuffer(Zee *zee, Tri2D *t, Tri2D *tp) {
if (t->b.x < t->a.x) { if (t->b.x < t->a.x) {
Vector2 e = t->b; Vector2 e = t->b;
t->b = t->a; t->b = t->a;
t->a = e; t->a = e;
} }
//Becasue we are trying to get the x values in terms of the y values, we need inverse slope // Becasue we are trying to get the x values in terms of the y values, we
float atocslopeinv = (t->c.x - t->a.x) / (t->c.y - t->a.y); //dif in x from start to end with a and c // need inverse slope
float btocslopinv = (t->c.x - t->b.x) / (t->c.y - t->b.y); //dif in x from start to end with b and c float atocslopeinv =
(t->c.x - t->a.x) /
(t->c.y - t->a.y); // dif in x from start to end with a and c
float btocslopinv =
(t->c.x - t->b.x) /
(t->c.y - t->b.y); // dif in x from start to end with b and c
//start at bottom of triangle (point c) so that we do not need to determine which vertex is on which side and increment it with its proper slope // start at bottom of triangle (point c) so that we do not need to determine
// which vertex is on which side and increment it with its proper slope
double curx1 = t->c.x; double curx1 = t->c.x;
double curx2 = t->c.x; double curx2 = t->c.x;
for(int scanline=t->c.y; scanline >= t->a.y; scanline--){ for (int scanline = t->c.y; scanline >= t->a.y; scanline--) {
if (0 <= scanline && scanline < RENDERHEIGHT){ if (0 <= scanline && scanline < RENDERHEIGHT) {
for (int i = Max(curx1,0); i < Min(curx2,RENDERWIDTH); i++) { for (int i = Max(curx1, 0); i < Min(curx2, RENDERWIDTH); i++) {
zee[IndexOfZBuff(i,scanline) ].triangle = tp; zee[IndexOfZBuff(i, scanline)].triangle = tp;
} }
} }
curx1 -= atocslopeinv; //subtract because we are working backwards (reason why we start with point c in slope equtn) curx1 -=
curx2 -= btocslopinv; atocslopeinv; // subtract because we are working backwards (reason
// why we start with point c in slope equtn)
curx2 -= btocslopinv;
} }
} }
void PrintTri2D(Tri2D t) { void PrintTri2D(Tri2D t) {
printf("{(TRI2D) A: (%f, %f), B: (%f, %f), C:(%f,%f) }\n ",t.a.x,t.a.y,t.b.x, t.b.y, t.c.x, t.c.y); printf("{(TRI2D) A: (%f, %f), B: (%f, %f), C:(%f,%f) }\n ", t.a.x, t.a.y,
t.b.x, t.b.y, t.c.x, t.c.y);
} }
//Draws triangle with a flat bottomp. Note B and C must be the bottom points with A being the top "spike" // Draws triangle with a flat bottomp. Note B and C must be the bottom points
void FillBottomFlatZbuffer(Zee * zee, Tri2D* t, Tri2D * tp) { // with A being the top "spike"
void FillBottomFlatZbuffer(Zee *zee, Tri2D *t, Tri2D *tp) {
if (t->c.x < t->b.x) { if (t->c.x < t->b.x) {
Vector2 e = t->c; Vector2 e = t->c;
t->c = t->b; t->c = t->b;
t->b = e; t->b = e;
} }
//Becasue we are trying to get the x values in terms of the y values, we need inverse slope // Becasue we are trying to get the x values in terms of the y values, we
float atobslopeinv = (t->b.x - t->a.x) / (t->b.y - t->a.y); //dif in x from start to end with a and c // need inverse slope
float atocslopeinv = (t->c.x - t->a.x) / (t->c.y - t->a.y); //dif in x from start to end with b and c float atobslopeinv =
(t->b.x - t->a.x) /
(t->b.y - t->a.y); // dif in x from start to end with a and c
float atocslopeinv =
(t->c.x - t->a.x) /
(t->c.y - t->a.y); // dif in x from start to end with b and c
//start at top of triangle (point c) so that we do not need to determine which vertex is on which side and increment it with its proper slope // start at top of triangle (point c) so that we do not need to determine
// which vertex is on which side and increment it with its proper slope
double curx1 = t->a.x; double curx1 = t->a.x;
double curx2 = t->a.x; double curx2 = t->a.x;
for(int scanline=t->a.y; scanline < t->c.y; scanline++){ for (int scanline = t->a.y; scanline < t->c.y; scanline++) {
if (0 <= scanline && scanline < RENDERHEIGHT){ if (0 <= scanline && scanline < RENDERHEIGHT) {
for (int i = Max(curx1,0); i < Min(curx2,RENDERWIDTH); i++) { for (int i = Max(curx1, 0); i < Min(curx2, RENDERWIDTH); i++) {
zee[ IndexOfZBuff(i,scanline) ].triangle = tp; zee[IndexOfZBuff(i, scanline)].triangle = tp;
} }
} }
curx1 += atobslopeinv; curx1 += atobslopeinv;
curx2 += atocslopeinv; curx2 += atocslopeinv;
} }
} }
void DrawTriZuff(Zee *zbuf, Tri2D *t) {
void DrawTriZuff(Zee * zbuf, Tri2D * t){
Tri2DSortByY(t); Tri2DSortByY(t);
if (t->b.y == t->c.y) { // if bottom of triangle is flat if (t->b.y == t->c.y) { // if bottom of triangle is flat
FillBottomFlatZbuffer(zbuf,t,t); FillBottomFlatZbuffer(zbuf, t, t);
} else if (t->a.y == t->b.y) { // if top of triangle is flat
FillTopFlatZbuffer(zbuf, t, t);
} else { // funny split tri
Vector2 v4; // v4 is the vertex on the line between a and c. It is used
// to split the triangle into a top and bottom
v4.y = t->b.y;
float slope = (float)((t->c.x) - (t->a.x)) /
((float)(t->c.y - t->a.y)); // get slope in run over rise
// becasue we need to find x
float changeiny = (float)(t->b.y - t->a.y);
float officalxpos = t->a.x + (slope * changeiny);
v4.x = officalxpos;
Tri2D bottomflattrires;
bottomflattrires.a = t->a;
bottomflattrires.b = t->b;
bottomflattrires.c = v4;
bottomflattrires.color = t->color;
Tri2D topflattrires;
topflattrires.a = t->b;
topflattrires.b = v4;
topflattrires.c = t->c;
topflattrires.color = t->color;
FillBottomFlatZbuffer(zbuf, &bottomflattrires, t);
FillTopFlatZbuffer(zbuf, &topflattrires, t);
} }
else if (t->a.y == t->b.y) { //if top of triangle is flat
FillTopFlatZbuffer(zbuf,t,t);
} else{ //funny split tri
Vector2 v4; //v4 is the vertex on the line between a and c. It is used to split the triangle into a top and bottom
v4.y = t->b.y;
float slope = (float)((t->c.x) - (t->a.x)) /((float)(t->c.y - t->a.y)); //get slope in run over rise becasue we need to find x
float changeiny = (float) (t->b.y - t->a.y);
float officalxpos = t->a.x + (slope*changeiny);
v4.x = officalxpos;
Tri2D bottomflattrires;
bottomflattrires.a = t->a;
bottomflattrires.b = t->b;
bottomflattrires.c = v4;
bottomflattrires.color = t->color;
Tri2D topflattrires;
topflattrires.a = t->b;
topflattrires.b = v4;
topflattrires.c = t->c;
topflattrires.color = t->color;
FillBottomFlatZbuffer(zbuf, &bottomflattrires, t);
FillTopFlatZbuffer(zbuf, &topflattrires, t);
}
} }
float Sign(Vector2* v1, Vector2* v2, Vector2* v3) { float Sign(Vector2 *v1, Vector2 *v2, Vector2 *v3) {
return (v1->x - v3->x) * (v2->y - v3->y) - (v2->x - v3->x) * (v1->y - v3->y); return (v1->x - v3->x) * (v2->y - v3->y) -
(v2->x - v3->x) * (v1->y - v3->y);
} }
bool IsInTri(Tri2D tri, Vector2 p) { static inline bool IsInTri(Tri2D tri, Vector2 p) {
float d1, d2, d3; float d1, d2, d3;
bool has_neg, has_pos; bool has_neg, has_pos;
@ -224,58 +193,7 @@ bool IsInTri(Tri2D tri, Vector2 p) {
return !(has_neg && has_pos); return !(has_neg && has_pos);
} }
void TriArrayAppend(TriArray * tarr, Tri t) {
tarr->arr[tarr->length] = t;
tarr->length = tarr->length + 1;
}
void Tri2DArrayAppend(Tri2DArray * tarr, Tri2D t) {
tarr->arr[tarr->length] = t;
tarr->length = tarr->length + 1;
}
Vector3 Vector3Sum(Vector3 v1, Vector3 v2) {
Vector3 retvec;
retvec.x = v1.x + v2.x;
retvec.y = v1.y + v2.y;
retvec.z = v1.z + v2.z;
return retvec;
}
void Vector3Print(Vector3 v) {
printf("VX: %f, VY: %f, FZ: %f\n",v.x,v.y,v.z);
}
Vector3 Vector3Scale(Vector3 v1, float scale) {
Vector3 retvec;
retvec.x = v1.x * scale;
retvec.y = v1.y * scale;
retvec.z = v1.z * scale;
return retvec;
}
Vector3 RotateAboutX(Vector3 v, double radians){
Vector3 rotatedvector;
rotatedvector.x = 1*v.x + (0) + (0);
rotatedvector.y = (0) + cos(radians)*v.y + (-sin(radians)*v.z);
rotatedvector.z = 0 + sin(radians)*v.y + cos(radians)*v.z;
return rotatedvector;
}
Vector3 RotateAboutY(Vector3 v, double radians){
Vector3 rotatedvector;
rotatedvector.x = cos(radians)*v.x + (0) + sin(radians)*v.z;
rotatedvector.y = (0) + 1*v.y + (0);
rotatedvector.z = -sin(radians)*v.x + 0 + cos(radians)*v.z;
return rotatedvector;
}
Vector3 RotateAboutZ(Vector3 v, double radians){
Vector3 rotatedvector;
rotatedvector.x = cos(radians)*v.x + (-sin(radians)*v.y) + (0);
rotatedvector.y = sin(radians)*v.x + cos(radians)*v.y + (0);
rotatedvector.z = 0 + 0 + 1*v.z;
return rotatedvector;
}
void CtrlLocalCam(LocalCam *cam, float time) { void CtrlLocalCam(LocalCam *cam, float time) {
cam->velocity.x = 0; cam->velocity.x = 0;
@ -286,63 +204,65 @@ void CtrlLocalCam(LocalCam *cam, float time) {
cam->angleVelocity.z = 0; cam->angleVelocity.z = 0;
if (IsKeyDown(KEY_W)) { if (IsKeyDown(KEY_W)) {
Vector3 forceForward = (Vector3){0,0,-500}; Vector3 forceForward = (Vector3){0, 0, -500};
Vector3 rotatedforce = RotateAboutY(forceForward,cam->angles.y); Vector3 rotatedforce = RotateAboutY(forceForward, cam->angles.y);
printf("%f", cam->angles.y); // printf("%f", cam->angles.y);
Vector3Print(rotatedforce); // Vector3Print(rotatedforce);
cam->velocity = Vector3Sum(cam->velocity,rotatedforce); cam->velocity = Vector3Sum(cam->velocity, rotatedforce);
//cam->velocity.z = cam->velocity.z + 50000*time*sin(cam->angles.y); // cam->velocity.z = cam->velocity.z + 50000*time*sin(cam->angles.y);
//cam->velocity.x = cam->velocity.x + 500*time*cos(cam->angles.y); // cam->velocity.x = cam->velocity.x + 500*time*cos(cam->angles.y);
} }
if (IsKeyDown(KEY_S)) { if (IsKeyDown(KEY_S)) {
Vector3 forceForward = (Vector3){0,0,500}; Vector3 forceForward = (Vector3){0, 0, 500};
Vector3 rotatedforce = RotateAboutY(forceForward,cam->angles.y); Vector3 rotatedforce = RotateAboutY(forceForward, cam->angles.y);
cam->velocity = Vector3Sum(cam->velocity,rotatedforce); cam->velocity = Vector3Sum(cam->velocity, rotatedforce);
//cam->velocity.z = cam->velocity.z + 50000*time*sin(cam->angles.y); // cam->velocity.z = cam->velocity.z + 50000*time*sin(cam->angles.y);
//cam->velocity.x = cam->velocity.x + 500*time*cos(cam->angles.y); // cam->velocity.x = cam->velocity.x + 500*time*cos(cam->angles.y);
} }
if (IsKeyDown(KEY_A)) { if (IsKeyDown(KEY_A)) {
Vector3 forceForward = (Vector3){-500,0,0}; Vector3 forceForward = (Vector3){-500, 0, 0};
Vector3 rotatedforce = RotateAboutY(forceForward,cam->angles.y); Vector3 rotatedforce = RotateAboutY(forceForward, cam->angles.y);
cam->velocity = Vector3Sum(cam->velocity,rotatedforce); cam->velocity = Vector3Sum(cam->velocity, rotatedforce);
//cam->velocity.z = cam->velocity.z + 50000*time*sin(cam->angles.y); // cam->velocity.z = cam->velocity.z + 50000*time*sin(cam->angles.y);
//cam->velocity.x = cam->velocity.x + 500*time*cos(cam->angles.y); // cam->velocity.x = cam->velocity.x + 500*time*cos(cam->angles.y);
} }
if (IsKeyDown(KEY_D)) { if (IsKeyDown(KEY_D)) {
Vector3 forceForward = (Vector3){500,0,0}; Vector3 forceForward = (Vector3){500, 0, 0};
Vector3 rotatedforce = RotateAboutY(forceForward,cam->angles.y); Vector3 rotatedforce = RotateAboutY(forceForward, cam->angles.y);
cam->velocity = Vector3Sum(cam->velocity,rotatedforce); cam->velocity = Vector3Sum(cam->velocity, rotatedforce);
//cam->velocity.z = cam->velocity.z + 50000*time*sin(cam->angles.y); // cam->velocity.z = cam->velocity.z + 50000*time*sin(cam->angles.y);
//cam->velocity.x = cam->velocity.x + 500*time*cos(cam->angles.y); // cam->velocity.x = cam->velocity.x + 500*time*cos(cam->angles.y);
} }
if(IsKeyDown(KEY_SPACE)) { if (IsKeyDown(KEY_SPACE)) {
cam->velocity.y = cam->velocity.y - 50; cam->velocity.y = cam->velocity.y - 50;
} }
if(IsKeyDown(KEY_LEFT_SHIFT)) { if (IsKeyDown(KEY_LEFT_SHIFT)) {
cam->velocity.y = cam->velocity.y + 50; cam->velocity.y = cam->velocity.y + 50;
} }
if (IsKeyDown(KEY_Q)){ if (IsKeyDown(KEY_Q)) {
cam->angleVelocity.y = cam->angleVelocity.y + time*10; cam->angleVelocity.y = cam->angleVelocity.y + time * 10;
} }
if (IsKeyDown(KEY_E)){ if (IsKeyDown(KEY_E)) {
cam->angleVelocity.y = cam->angleVelocity.y - time*10; cam->angleVelocity.y = cam->angleVelocity.y - time * 10;
} }
if (IsKeyDown(KEY_G)) { if (IsKeyDown(KEY_G)) {
printf("PosX: %f PosY: %f PosZ: %f\n",cam->position.x,cam->position.y,cam->position.z); printf("PosX: %f PosY: %f PosZ: %f\n", cam->position.x, cam->position.y,
printf("RotX; %f RotY: %f RotZ: %f\n",cam->angles.x,cam->angles.y,cam->angles.z); cam->position.z);
printf("RotX; %f RotY: %f RotZ: %f\n", cam->angles.x, cam->angles.y,
cam->angles.z);
} }
} }
void LocalCamApplyVelo(LocalCam *cam,float time){ void LocalCamApplyVelo(LocalCam *cam, float time) {
cam->position.x = cam->position.x + cam->velocity.x * time; cam->position.x = cam->position.x + cam->velocity.x * time;
cam->position.y = cam->position.y + cam->velocity.y * time; cam->position.y = cam->position.y + cam->velocity.y * time;
cam->position.z = cam->position.z + cam->velocity.z * time; cam->position.z = cam->position.z + cam->velocity.z * time;
@ -350,13 +270,9 @@ void LocalCamApplyVelo(LocalCam *cam,float time){
cam->angles.x = cam->angles.x + cam->angleVelocity.x * time; cam->angles.x = cam->angles.x + cam->angleVelocity.x * time;
cam->angles.y = cam->angles.y + cam->angleVelocity.y * time; cam->angles.y = cam->angles.y + cam->angleVelocity.y * time;
cam->angles.z = cam->angles.z + cam->angleVelocity.z * time; cam->angles.z = cam->angles.z + cam->angleVelocity.z * time;
//printf("%f %f %f\n",cam->angles.x, cam->angles.y, cam->angles.z); // printf("%f %f %f\n",cam->angles.x, cam->angles.y, cam->angles.z);
} }
Vector2 Conv3Dto2D(Vector3 v) { Vector2 Conv3Dto2D(Vector3 v) {
Vector2 returnvector; Vector2 returnvector;
returnvector.x = proj * v.x / (-v.z); returnvector.x = proj * v.x / (-v.z);
@ -371,31 +287,33 @@ Vector2 Conv2DCenteredToScreen(Vector2 v) {
return returnvector; return returnvector;
} }
Vector3 TransformWithCam(Vector3 v, LocalCam * cam) { static inline Vector3 TransformWithCam(Vector3 v, LocalCam *cam) {
Vector3 returnvector; Vector3 returnvector;
returnvector.x = v.x - cam->position.x; returnvector.x = v.x - cam->position.x;
returnvector.y = v.y - cam->position.y; returnvector.y = v.y - cam->position.y;
returnvector.z = v.z - cam->position.z; returnvector.z = v.z - cam->position.z;
returnvector = RotateAboutZ(returnvector,cam->angles.z); returnvector = RotateAboutZ(returnvector, cam->angles.z);
returnvector = RotateAboutY(returnvector,-cam->angles.y); returnvector = RotateAboutY(returnvector, -cam->angles.y);
returnvector = RotateAboutX(returnvector,-cam->angles.x); returnvector = RotateAboutX(returnvector, -cam->angles.x);
//printf("Before: %f %f %f, After: %f %f %f\n", v.x, v.y, v.z, returnvector.x, returnvector.y, returnvector.z); // printf("Before: %f %f %f, After: %f %f %f\n", v.x, v.y, v.z,
// returnvector.x, returnvector.y, returnvector.z);
return returnvector; return returnvector;
} }
Tri TriTransformWithCam(Tri *t, LocalCam * cam) { Tri TriTransformWithCam(Tri *t, LocalCam *cam) {
Tri rettri; Tri rettri;
rettri.a = TransformWithCam(t->a,cam); rettri.a = TransformWithCam(t->a, cam);
rettri.b = TransformWithCam(t->b,cam); rettri.b = TransformWithCam(t->b, cam);
rettri.c = TransformWithCam(t->c,cam); rettri.c = TransformWithCam(t->c, cam);
rettri.color = t->color; rettri.color = t->color;
return rettri; return rettri;
} }
Tri2D ConvertTriToTri2D(Tri* t){ Tri2D ConvertTriToTri2D(Tri *t) {
Tri2D rettri2d; Tri2D rettri2d;
rettri2d.a = Conv3Dto2D(t->a); rettri2d.a = Conv3Dto2D(t->a);
rettri2d.b = Conv3Dto2D(t->b); rettri2d.b = Conv3Dto2D(t->b);
@ -409,14 +327,13 @@ Tri2D ConvertTriToTri2D(Tri* t){
return rettri2d; return rettri2d;
} }
int main() { int main() {
printf("%d\n", TestFunc(5));
proj = HALFWIDTH / tan(half_fov); proj = HALFWIDTH / tan(half_fov);
SetConfigFlags(FLAG_WINDOW_UNDECORATED); SetConfigFlags(FLAG_WINDOW_UNDECORATED);
InitWindow(SCREENWIDTH, SCREENHEIGHT, "raylib [core] example - basic window"); InitWindow(SCREENWIDTH, SCREENHEIGHT,
"raylib [core] example - basic window");
// SetWindowPosition(0,1080); // SetWindowPosition(0,1080);
Vector2 a = GetMonitorPosition(0); Vector2 a = GetMonitorPosition(0);
@ -427,32 +344,34 @@ int main() {
printf("mh:%d mw:%d w:%d h:%d\n", mh, mw, w, h); printf("mh:%d mw:%d w:%d h:%d\n", mh, mw, w, h);
SetWindowPosition(a.x + (0.5 * mw) - (w / 2), a.y + (0.5 * mh) - (0.5 * h)); SetWindowPosition(a.x + (0.5 * mw) - (w / 2), a.y + (0.5 * mh) - (0.5 * h));
RenderTexture2D uiraylibtexture = LoadRenderTexture(RENDERWIDTH,RENDERHEIGHT); RenderTexture2D uiraylibtexture =
LoadRenderTexture(RENDERWIDTH, RENDERHEIGHT);
RenderTexture2D render3dtexture = LoadRenderTexture(RENDERWIDTH,RENDERHEIGHT);
RenderTexture2D render3dtexture =
LoadRenderTexture(RENDERWIDTH, RENDERHEIGHT);
Texture2D directaccesstex; Texture2D directaccesstex;
// Init cube model // Init cube model
// TODO: Load from obj // TODO: Load from obj
LocalCam camera; LocalCam camera;
camera.position = (Vector3){0,0,0}; camera.position = (Vector3){0, 0, 0};
camera.acceleration = (Vector3){0,0,0}; camera.acceleration = (Vector3){0, 0, 0};
camera.angleAcceleration = (Vector3){0,0,0}; camera.angleAcceleration = (Vector3){0, 0, 0};
camera.angles = (Vector3){0,0,0}; camera.angles = (Vector3){0, 0, 0};
camera.velocity = (Vector3){0,0,0}; camera.velocity = (Vector3){0, 0, 0};
camera.angleVelocity = (Vector3){0,0,0}; camera.angleVelocity = (Vector3){0, 0, 0};
Vector3 point = (Vector3){0,0,-10}; Vector3 point = (Vector3){0, 0, -10};
Tri internaltriarray[50]; Tri internaltriarray[50];
TriArray tarr; TriArray tarr;
tarr.arr = internaltriarray; tarr.arr = internaltriarray;
tarr.length = 0; tarr.length = 0;
TriArrayAppend(&tarr,(Tri){(Vector3){0,0,-1000},(Vector3){0,800,-1000},(Vector3){800,800,-1000},WHITE}); TriArrayAppend(&tarr,
(Tri){(Vector3){0, 0, -1000}, (Vector3){0, 800, -1000},
(Vector3){800, 800, -1000}, WHITE});
Tri internaltransformedtriarray[50]; Tri internaltransformedtriarray[50];
TriArray TransformedTris; TriArray TransformedTris;
@ -461,135 +380,149 @@ int main() {
Tri2D internaltri2darray[50]; Tri2D internaltri2darray[50];
Tri2DArray Tri2Darr; Tri2DArray Tri2Darr;
Tri2Darr.length=0; Tri2Darr.length = 0;
Tri2Darr.arr = internaltri2darray; Tri2Darr.arr = internaltri2darray;
//static Zee ZBuff[1920][1080] = {{(Zee){10000,NULL}}}; //FIXME: Stupid static makes the file 32 Megs because pog // static Zee ZBuff[1920][1080] = {{(Zee){10000,NULL}}}; //FIXME: Stupid
Zee * ZBuff = malloc(RENDERHEIGHT*RENDERWIDTH*sizeof(Zee)); // static makes the file 32 Megs because pog
static Color display[1920*1080*4]; Zee *ZBuff = malloc(RENDERHEIGHT * RENDERWIDTH * sizeof(Zee));
memset(display,0,sizeof(display)); static Color display[1920 * 1080 * 4];
memset(display, 0, sizeof(display));
Tri2D funners = (Tri2D){ (Vector2){50,50},(Vector2){500,50},(Vector2){500,500},GREEN}; Tri2D funners = (Tri2D){(Vector2){50, 50}, (Vector2){500, 50},
Tri2D funners2 = (Tri2D){ (Vector2){600,0},(Vector2){600,500},(Vector2){1000,500},RED}; (Vector2){500, 500}, GREEN};
Tri2D funners2 = (Tri2D){(Vector2){600, 0}, (Vector2){600, 500},
(Vector2){1000, 500}, RED};
Tri2D fullscreentritop = (Tri2D){ (Vector2){0,0}, (Vector2){1920,0}, (Vector2){1920,1080}, BLUE}; Tri2D fullscreentritop = (Tri2D){(Vector2){0, 0}, (Vector2){1920, 0},
Tri2D fullscreentribottom = (Tri2D){ (Vector2){0,0}, (Vector2){0,1080}, (Vector2){1920,1080}, RED}; (Vector2){1920, 1080}, BLUE};
Tri2D fullscreentribottom = (Tri2D){(Vector2){0, 0}, (Vector2){0, 1080},
(Vector2){1920, 1080}, RED};
Tri2D blank =
(Tri2D){(Vector2){-10, -10}, (Vector2){-10, -10}, (Vector2){-10, -10}};
Tri2D norm = (Tri2D){(Vector2){500, 50}, (Vector2){0, 0},
(Vector2){250, 500}, GREEN};
Tri2D blank = (Tri2D) { (Vector2){-10,-10},(Vector2){-10,-10}, (Vector2){-10,-10}};
Tri2D norm = (Tri2D){ (Vector2){500,50},(Vector2){0,0},(Vector2){250,500},GREEN};
while (!WindowShouldClose()) { bool run3d = false;
float frametime = GetFrameTime(); while (!WindowShouldClose() && run3d) {
CtrlLocalCam(&camera,frametime); float frametime = GetFrameTime();
LocalCamApplyVelo(&camera,frametime); CtrlLocalCam(&camera, frametime);
//ClearBackground(BLACK); LocalCamApplyVelo(&camera, frametime);
// ClearBackground(BLACK);
/* Vector3 TransVector = TransformWithCam(point,&camera); */ /* Vector3 TransVector = TransformWithCam(point,&camera); */
/* if (TransVector.z < 0) { */ /* if (TransVector.z < 0) { */
/* Vector2 MPos = Conv3Dto2D(TransVector); */ /* Vector2 MPos = Conv3Dto2D(TransVector); */
/* Vector2 FinPos = Conv2DCenteredToScreen(MPos); */ /* Vector2 FinPos = Conv2DCenteredToScreen(MPos); */
/* DrawCircleV(FinPos,100,BLACK); */ /* DrawCircleV(FinPos,100,BLACK); */
/* } */ /* } */
/* EndTextureMode(); */ /* EndTextureMode(); */
TransformedTris.length = 0; TransformedTris.length = 0;
Tri2Darr.length = 0; Tri2Darr.length = 0;
for (int i = 0; i < tarr.length; i++) { for (int i = 0; i < tarr.length; i++) {
TriArrayAppend(&TransformedTris,TriTransformWithCam(&tarr.arr[i],&camera)); TriArrayAppend(&TransformedTris,
} TriTransformWithCam(&tarr.arr[i], &camera));
}
for(int i = 0; i < TransformedTris.length; i++) { for (int i = 0; i < TransformedTris.length; i++) {
Tri2DArrayAppend(&Tri2Darr,ConvertTriToTri2D(&TransformedTris.arr[i])); if ((TransformedTris.arr[i].a.z < 0) &&
} (TransformedTris.arr[i].b.z < 0) &&
(TransformedTris.arr[i].c.z < 0)) {
Tri2DArrayAppend(&Tri2Darr,
ConvertTriToTri2D(&TransformedTris.arr[i]));
}
}
//memset(ZBuff,NULL ,sizeof(Zee) * RENDERHEIGHT*RENDERWIDTH ); /* for (int i = 0; i < RENDERHEIGHT*RENDERWIDTH; i ++) { */
// for (int i = 0; i < RENDERHEIGHT*RENDERWIDTH; i ++) { /* ZBuff[i] = (Zee){10000,NULL}; */
// ZBuff[i] = (Zee){10000,NULL}; /* } */
// } memset(display, 0, sizeof(display));
memset(display,0,sizeof(display)); memset(ZBuff, 0, sizeof(Zee) * 1920 * 1080);
/* for (int y = 0; y < RENDERHEIGHT; y++){ */
/* for (int x = 0; x<RENDERWIDTH; x++){ */
/* ZBuff[x][y].triangle = &blank; */
/* } */
/* } */
for (int i = 0; i < Tri2Darr.length; i++) {
for (int y = 0; y < RENDERHEIGHT; y++) {
for (int x = 0; x < RENDERWIDTH; x++) {
if (IsInTri(Tri2Darr.arr[i], (Vector2){x, y})) {
ZBuff[IndexOfZBuff(x, y)].triangle = &Tri2Darr.arr[i];
}
}
}
}
// FillTopFlatZbuffer(ZBuff, &funners);
// FillBottomFlatZbuffer(ZBuff,&funners2);
/* for (int y = 0; y < RENDERHEIGHT; y++){ */ // FillTopFlatZbuffer(ZBuff, &fullscreentritop);
/* for (int x = 0; x<RENDERWIDTH; x++){ */ // FillBottomFlatZbuffer(ZBuff, &fullscreentribottom);
/* ZBuff[x][y].triangle = &blank; */
/* } */
/* } */
/* for(int i = 0; i < Tri2Darr.length; i++) { */ // DrawTriZuff(ZBuff, &norm);
/* for(int y = 0; y < RENDERHEIGHT; y++){ */ // DrawTriZuff(ZBuff, TriTransformWithCam(&norm, &camera));
/* for(int x = 0; x< RENDERWIDTH; x++) { */
/* if (IsInTri(Tri2Darr.arr[i],(Vector2){x,y})) { */
/* ZBuff[x][y].triangle = &Tri2Darr.arr[i]; */
/* } */
/* } */
/* } */
/* } */
int index = 0;
for (int y = 0; y < RENDERHEIGHT; y++) {
for (int x = 0; x < RENDERWIDTH; x++) {
// int index = (x+y*RENDERWIDTH);
/* Color * c = &ZBuff[x][y].triangle->color;
display[index] = c->r;
display[index+1] = c->g;
display[index+2] = c->b;
display[index+3] = c->a;
*/
if (ZBuff[IndexOfZBuff(x, y)].triangle !=
0) { // memset sets this to 0
// DrawPixel(x,y,ZBuff[x][y].triangle->color);
//FillTopFlatZbuffer(ZBuff, &funners); display[index] = ZBuff[IndexOfZBuff(x, y)].triangle->color;
//FillBottomFlatZbuffer(ZBuff,&funners2); // Zee test = ZBuff[IndexOfZBuff(x,y)];
// display[index] = test.triangle->color;
}
index = index + 1;
}
}
//FillTopFlatZbuffer(ZBuff, &fullscreentritop); BeginTextureMode(uiraylibtexture);
//FillBottomFlatZbuffer(ZBuff, &fullscreentribottom); // gui stuff
EndTextureMode();
DrawTriZuff(ZBuff, &norm); // Copytexture to main display :0
BeginDrawing();
ClearBackground(BLACK);
UpdateTexture(render3dtexture.texture, display);
// Copies render3dtexture to screen
DrawTexturePro(render3dtexture.texture,
(Rectangle){0, 0, render3dtexture.texture.width,
render3dtexture.texture.height},
(Rectangle){0, 0, SCREENWIDTH, SCREENHEIGHT},
(Vector2){0, 0}, 0, WHITE);
int index = 0; // Copies uiraylibtexture to screen (not this is not the texture used
for(int y = 0; y < RENDERHEIGHT; y++) { // for 3d stuff
for(int x = 0; x < RENDERWIDTH; x++) { DrawTexturePro(uiraylibtexture.texture,
//int index = (x+y*RENDERWIDTH); (Rectangle){0, 0, uiraylibtexture.texture.width,
/* Color * c = &ZBuff[x][y].triangle->color; -uiraylibtexture.texture.height},
display[index] = c->r; (Rectangle){0, 0, SCREENWIDTH, SCREENHEIGHT},
display[index+1] = c->g; (Vector2){0, 0}, 0, WHITE);
display[index+2] = c->b;
display[index+3] = c->a;
*/
char fpstext[40];
sprintf(fpstext, "%d", GetFPS());
DrawText(fpstext, 0, 0, 20, WHITE);
if (ZBuff[IndexOfZBuff(x,y)].triangle != NULL) { EndDrawing();
//DrawPixel(x,y,ZBuff[x][y].triangle->color);
display[index] = ZBuff[IndexOfZBuff(x,y)].triangle->color;
// Zee test = ZBuff[IndexOfZBuff(x,y)];
// display[index] = test.triangle->color;
}
index = index+1;
}
}
BeginTextureMode(uiraylibtexture);
//gui stuff
EndTextureMode();
//Copytexture to main display :0
BeginDrawing();
ClearBackground(BLACK);
UpdateTexture(render3dtexture.texture, display);
//Copies render3dtexture to screen
DrawTexturePro(render3dtexture.texture, (Rectangle){0,0,render3dtexture.texture.width, render3dtexture.texture.height}, (Rectangle){0,0,SCREENWIDTH,SCREENHEIGHT},(Vector2){0,0},0,WHITE);
//Copies uiraylibtexture to screen (not this is not the texture used for 3d stuff
DrawTexturePro(uiraylibtexture.texture, (Rectangle){0,0,uiraylibtexture.texture.width,-uiraylibtexture.texture.height},(Rectangle){0,0,SCREENWIDTH,SCREENHEIGHT},(Vector2){0,0},0,WHITE);
char fpstext[40];
sprintf(fpstext,"%d",GetFPS());
DrawText( fpstext ,0,0,20,WHITE);
EndDrawing();
} }
CloseWindow(); CloseWindow();

64
c3dtypes.h Normal file
View file

@ -0,0 +1,64 @@
#include "raylib.h"
#ifndef C3DTYPES_HEADER
#define C3DTYPES_HEADER
struct LocalCam {
Vector3 position;
Vector3 velocity;
Vector3 acceleration;
Vector3 angles;
Vector3 angleVelocity;
Vector3 angleAcceleration;
};
typedef struct LocalCam LocalCam;
struct Tri {
Vector3 a;
Vector3 b;
Vector3 c;
Color color;
};
typedef struct Tri Tri;
struct Tri2D {
Vector2 a;
Vector2 b;
Vector2 c;
Color color;
};
typedef struct Tri2D Tri2D;
struct Zee {
int depth;
Tri2D *triangle;
};
typedef struct Zee Zee;
struct TriArray {
int length;
Tri *arr;
};
typedef struct TriArray TriArray;
struct Tri2DArray {
int length;
Tri2D *arr;
};
typedef struct Tri2DArray Tri2DArray;
struct Object {
char name[100];
Vector3 *VertexArray;
int VertexArrayLength;
TriArray * triangles;
};
typedef struct Object Object;
#endif

46
cube.obj Normal file
View file

@ -0,0 +1,46 @@
# Blender v2.76 (sub 0) OBJ File: ''
# www.blender.org
mtllib cube.mtl
o Cube
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -0.999999
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
vt 1.000000 0.333333
vt 1.000000 0.666667
vt 0.666667 0.666667
vt 0.666667 0.333333
vt 0.666667 0.000000
vt 0.000000 0.333333
vt 0.000000 0.000000
vt 0.333333 0.000000
vt 0.333333 1.000000
vt 0.000000 1.000000
vt 0.000000 0.666667
vt 0.333333 0.333333
vt 0.333333 0.666667
vt 1.000000 0.000000
vn 0.000000 -1.000000 0.000000
vn 0.000000 1.000000 0.000000
vn 1.000000 0.000000 0.000000
vn -0.000000 0.000000 1.000000
vn -1.000000 -0.000000 -0.000000
vn 0.000000 0.000000 -1.000000
usemtl Material
s off
f 2/1/1 3/2/1 4/3/1
f 8/1/2 7/4/2 6/5/2
f 5/6/3 6/7/3 2/8/3
f 6/8/4 7/5/4 3/4/4
f 3/9/5 7/10/5 8/11/5
f 1/12/6 4/13/6 8/11/6
f 1/4/1 2/1/1 4/3/1
f 5/14/2 8/1/2 6/5/2
f 1/12/3 5/6/3 2/8/3
f 2/12/4 6/8/4 3/4/4
f 4/13/5 3/9/5 8/11/5
f 5/6/6 1/12/6 8/11/6

60
reader.c Normal file
View file

@ -0,0 +1,60 @@
#include "c3dtypes.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int TestFunc(int x) {
x = x + 1;
return x;
}
Object ReadObjectFromFile() {
Object out;
out.VertexArray = malloc(10000*sizeof(Vector3));
out.VertexArrayLength = 0;
TriArray tarr;
tarr.arr = malloc(10000*sizeof(Tri));
tarr.length = 0;
out.triangles = &tarr;
strncpy(out.name, "cube.obj", 100);
FILE * f = fopen("cube.obj", "r");
while (true) {
char t[500];
char * fgetres = fgets(t, 500, f);
if (fgetres == NULL) {
break;
}
char objtype[10];
double v1;
double v2;
double v3;
sscanf(t, "%s %lf %lf %lf", objtype, &v1,&v2,&v3);
if ( strcmp(t, "v")) {
out.VertexArray[out.VertexArrayLength].x = v1;
out.VertexArray[out.VertexArrayLength].x = v2;
out.VertexArray[out.VertexArrayLength].x = v3;
out.VertexArrayLength = out.VertexArrayLength + 1;
}
if ( strcmp(t, "f")) {
//TODO: append face to triarry
}
//need to bring in triarray functions into their own file
}
//TODO: use file object and read each line
// add verticies to the vertex list, +1 ing the length value each time
// just fucking parse the obj file its not that hard
}

6
reader.h Normal file
View file

@ -0,0 +1,6 @@
#ifndef READER_HEADER
#define READER_HEADER
int TestFunc(int x);
#endif

46
vecfunc.c Normal file
View file

@ -0,0 +1,46 @@
#include "raylib.h"
#include <stdio.h>
#include <math.h>
Vector3 Vector3Sum(Vector3 v1, Vector3 v2) {
Vector3 retvec;
retvec.x = v1.x + v2.x;
retvec.y = v1.y + v2.y;
retvec.z = v1.z + v2.z;
return retvec;
}
void Vector3Print(Vector3 v) {
printf("VX: %f, VY: %f, FZ: %f\n", v.x, v.y, v.z);
}
Vector3 Vector3Scale(Vector3 v1, float scale) {
Vector3 retvec;
retvec.x = v1.x * scale;
retvec.y = v1.y * scale;
retvec.z = v1.z * scale;
return retvec;
}
Vector3 RotateAboutX(Vector3 v, double radians) {
Vector3 rotatedvector;
rotatedvector.x = 1 * v.x + (0) + (0);
rotatedvector.y = (0) + cos(radians) * v.y + (-sin(radians) * v.z);
rotatedvector.z = 0 + sin(radians) * v.y + cos(radians) * v.z;
return rotatedvector;
}
Vector3 RotateAboutY(Vector3 v, double radians) {
Vector3 rotatedvector;
rotatedvector.x = cos(radians) * v.x + (0) + sin(radians) * v.z;
rotatedvector.y = (0) + 1 * v.y + (0);
rotatedvector.z = -sin(radians) * v.x + 0 + cos(radians) * v.z;
return rotatedvector;
}
Vector3 RotateAboutZ(Vector3 v, double radians) {
Vector3 rotatedvector;
rotatedvector.x = cos(radians) * v.x + (-sin(radians) * v.y) + (0);
rotatedvector.y = sin(radians) * v.x + cos(radians) * v.y + (0);
rotatedvector.z = 0 + 0 + 1 * v.z;
return rotatedvector;
}

15
vecfunc.h Normal file
View file

@ -0,0 +1,15 @@
#ifndef VECFUNC_HEADER
#define VECFUNC_HEADER
Vector3 Vector3Sum(Vector3 v1, Vector3 v2);
void Vector3Print(Vector3 v);
Vector3 Vector3Scale(Vector3 v1, float scale);
Vector3 RotateAboutX(Vector3 V, double radians);
Vector3 RotateAboutY(Vector3 V, double radians);
Vector3 RotateAboutZ(Vector3 V, double radians);
#endif