-
Notifications
You must be signed in to change notification settings - Fork 0
/
SDL_lowPolyDemo.c
179 lines (150 loc) · 7.07 KB
/
SDL_lowPolyDemo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//I will do literally anything but use a makefile i guess
//clang SDl_lowPolyDemo.c -o main -I/opt/homebrew/cellar/sdl2/2.28.4/include/sdl2 -L/opt/homebrew/cellar/sdl2/2.28.4/lib -lSDL2
#include <SDL.h>
#include <string.h>
#include "GraphicsEngine/raster/rasterizer.c"
#include "GraphicsEngine/3Dmath/operations.c"
#include "GraphicsEngine/engineTypes.h"
#include "GraphicsEngine/graphicsEngineFunctions.c"
#include "commonCoords.c"
#include "GraphicsEngine/OBJParser/parser.c"
#define TRANSLATION_SPEED 0.5f
#define ANGLE_INCREMENT 1.0f
int main(){
renderContext* rc = createRenderContext(1000, 700);
/*START OF SDL BOILERPLATE*/
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window;
window = SDL_CreateWindow("Display Image",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
rc->width, rc->height, 0);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED| SDL_RENDERER_PRESENTVSYNC);
int format;
format = SDL_PIXELFORMAT_RGB24;
SDL_Texture* texture = SDL_CreateTexture(renderer,
format, SDL_TEXTUREACCESS_STREAMING,
rc->width, rc->height);
if(!texture){printf("%s", SDL_GetError()); return 0;}
SDL_UpdateTexture(texture, NULL, rc->frameBuffer, rc->width * 3);
/*END OF SDL BOILERPLATE*/
object* obj = parseNoMTL("forestPondFixed.obj");
vertexBuffer* vb0 = createVertexBuffer(obj->faceCount * 9);
memcpy(vb0->inputVertices, obj->faces, sizeof(float) * obj->faceCount * 9);
printf("%d", obj->faceCount);
matrix4x4 prescale, center;
createScalingMatrix(2, prescale);
vertexBufferByMatrix(vb0, prescale);
normalBuffer* nb0 = generateNormals(vb0);
colorBuffer* cb0 = createColorBuffer(obj->faceCount * 9);
for(int i = 0; i < cb0->length; i++) cb0->inputColors[i] = 127;
mesh* mesh0 = meshify(vb0, cb0, nb0);
calculateCentroid(mesh0);
createTranslationMatrix(mesh0->centroid->x,mesh0->centroid->y, mesh0->centroid->z, center);
vertexBufferByMatrix(vb0, center);
delete(obj);
//lighting vector
vec3 light; light.x = 0; light.y = -1; light.z = 0;
//scene
scene* sc = createScene(1);
sc->meshes[0] = mesh0;
//movement variables
float mx = 0;
float my = 0;
float mz = 0;
float angleX = 180.0;
float angleY = 0.0;
float angleZ = 0.0;
//update matrices
matrix4x4 rotationMatrixX, rotationMatrixY, rotationMatrixZ, translationMatrix, scalingMatrix, perspectiveProjectionMatrix, screenSpaceMatrix;
matrix4x4 rodMatrix;
createRotationMatrix(0.0, 0.0, 0.0, 0.0, rodMatrix);
createPerspectiveProjectionMatrix(45.0, 1.0, 10.0, 1000.0/700.0, perspectiveProjectionMatrix);
createScalingMatrix(0.5f, scalingMatrix);
createTranslationMatrix(mx, my, mz, translationMatrix);
createRotationMatrixX(angleX, rotationMatrixX);
createRotationMatrixY(angleY, rotationMatrixY);
createRotationMatrixZ(angleZ, rotationMatrixZ);
createNDCToScreenSpaceMatrix(1000, 700, screenSpaceMatrix);
int quit = 0;
SDL_Event e;
while(!quit){
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
quit = 1;
}
}
//input gathering
const Uint8* keystate = SDL_GetKeyboardState(NULL);
if(keystate[SDL_SCANCODE_S]){my += TRANSLATION_SPEED;}
if(keystate[SDL_SCANCODE_A]){mx -= TRANSLATION_SPEED;}
if(keystate[SDL_SCANCODE_W]){my -= TRANSLATION_SPEED;}
if(keystate[SDL_SCANCODE_D]){mx += TRANSLATION_SPEED;}
if(keystate[SDL_SCANCODE_LSHIFT]){ mz -= TRANSLATION_SPEED;}
if(keystate[SDL_SCANCODE_SPACE]){ mz += TRANSLATION_SPEED;}
if(keystate[SDL_SCANCODE_E]){angleX += ANGLE_INCREMENT;}
if(keystate[SDL_SCANCODE_F]){angleX -= ANGLE_INCREMENT;}
if(keystate[SDL_SCANCODE_R]){angleY += ANGLE_INCREMENT;}
if(keystate[SDL_SCANCODE_G]){angleY -= ANGLE_INCREMENT;}
if(keystate[SDL_SCANCODE_T]){angleZ += ANGLE_INCREMENT;}
if(keystate[SDL_SCANCODE_H]){angleZ -= ANGLE_INCREMENT;}
for(int j = 0; j < sc->length; j++){
vertexBuffer* vb = sc->meshes[j]->vb;
colorBuffer* cb = sc->meshes[j]->cb;
normalBuffer* nb = sc->meshes[j]->nb;
for(int i = 0; i < vb->length; i += 3){
vec4 temp;
temp.x = vb->inputVertices[i];
temp.y = vb->inputVertices[i + 1];
temp.z = vb->inputVertices[i + 2];
temp.w = 1.0f;
vec3 normTemp;
normTemp.x = nb->normals[i];
normTemp.y = nb->normals[i + 1];
normTemp.z = nb->normals[i + 2];
vec4 normTempH = homogenizeVector(normTemp);
createRotationMatrixX(angleX, rotationMatrixX);
createRotationMatrixY(angleY, rotationMatrixY);
createRotationMatrixZ(angleZ, rotationMatrixZ);
vecByMatrix4x4(&temp, rotationMatrixX);
vecByMatrix4x4(&temp, rotationMatrixY);
vecByMatrix4x4(&temp, rotationMatrixZ);
vecByMatrix4x4(&normTempH, rotationMatrixX);
vecByMatrix4x4(&normTempH, rotationMatrixY);
vecByMatrix4x4(&normTempH, rotationMatrixZ);
createTranslationMatrix(mx, my, mz, translationMatrix);
vecByMatrix4x4(&temp, translationMatrix);
normTemp = dehomogenizeVector(normTempH);
normalizeVector(&normTemp);
float lightScalar = dotProduct(normTemp, light);
lightScalar += 1;
if(dotProduct(normTemp, *sc->cameraVector) < -0.5){
vb->indexBuffer[i/3] = 0;
}else vb->indexBuffer[i/3] = 1;
cb->colors[i] = RGBClamp(cb->inputColors[i] * lightScalar);
cb->colors[i + 1] = RGBClamp(cb->inputColors[i + 1] * lightScalar);
cb->colors[i + 2] = RGBClamp(cb->inputColors[i + 2] * lightScalar);
perspectiveProjection(&temp, perspectiveProjectionMatrix);
perspectiveDivide(&temp);
NDCToScreenSpace(&temp, 1.0, 100.0, 700, 1000);
//create the temporary VBO
vb->vertices[i] = temp.x;
vb->vertices[i + 1] = temp.y;
//we are now w-buffering, maybe a more thorough implementation would be good
vb->vertices[i + 2] = temp.w * 25;
// printf("%f\n", temp.w);
}
rasterize(rc, vb, cb);
}
SDL_UpdateTexture(texture, NULL, rc->frameBuffer, rc->width * 3);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
cleanRenderContext(rc);
}
deleteRenderContext(rc);
//dont forget to free the vertex buffers
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}