-
Notifications
You must be signed in to change notification settings - Fork 0
/
SDL_cubeDemo.c
204 lines (168 loc) · 7.98 KB
/
SDL_cubeDemo.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//I will do literally anything but use a makefile i guess
//clang SDL_cubeDemo.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.1f
#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*/
//cube1
vertexBuffer* vb0 = createVertexBuffer(108);
memcpy(vb0->inputVertices, correctedCube, sizeof(float) * 108);
normalBuffer* nb0 = generateNormals(vb0);
colorBuffer* cb0 = createColorBuffer(108);
for(int i = 0; i < cb0->length; i++) cb0->inputColors[i] = 127;
mesh* mesh0 = meshify(vb0, cb0, nb0);
//cube2
vertexBuffer* vb1 = createVertexBuffer(108);
memcpy(vb1->inputVertices, correctedCube, sizeof(float) * 108);
matrix4x4 translation1;
createTranslationMatrix(-1.5, 0, 0, translation1);
vertexBufferByMatrix(vb1, translation1);
normalBuffer* nb1 = generateNormals(vb1);
colorBuffer* cb1 = createColorBuffer(108);
for(int i = 0; i < cb1->length; i++) cb1->inputColors[i] = 127;
mesh* mesh1 = meshify(vb1, cb1, nb1);
//cube3
vertexBuffer* vb2 = createVertexBuffer(108);
memcpy(vb2->inputVertices, correctedCube, sizeof(float) * 108);
createTranslationMatrix(1.5, 0, 0, translation1);
vertexBufferByMatrix(vb2, translation1);
normalBuffer* nb2 = generateNormals(vb2);
colorBuffer* cb2 = createColorBuffer(108);
for(int i = 0; i < cb2->length; i++) cb2->inputColors[i] = 127;
mesh* mesh2 = meshify(vb2, cb2, nb2);
scene* sc = createScene(3);
sc->meshes[0] = mesh0;
sc->meshes[1] = mesh1;
sc->meshes[2] = mesh2;
//lighting vector
vec3 light; light.x = 0.0f; light.y = 0.0f; light.z = -1.0f;
//movement variables
float mx = 0.0f;
float my = 0.0f;
float mz = 5.0f;
float angleX = 0.0f;
float angleY = 0.0f;
float angleZ = 0.0f;
//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;}
//linear operations
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;
//im guessing that using the index buffer will be better architecture;
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;
//not ideal that this has to be a vec4
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.2){
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);
//vecByMatrix4x4(&temp, perspectiveProjectionMatrix);
perspectiveProjection(&temp, perspectiveProjectionMatrix);
perspectiveDivide(&temp);
//expandDepthRange(&temp, -1.0f, 2.0f);
NDCToScreenSpace(&temp, 1.0, 100.0, 700, 1000);
//create the temporary VBO
vb->vertices[i] = temp.x;
vb->vertices[i + 1] = temp.y;
vb->vertices[i + 2] = temp.w * 20;
printf("%f : %f\n", temp.x, temp.z);
//printf("%i, %d, %d\n", cb->inputColors[i], cb->inputColors[i + 1], cb->inputColors[i + 2]);
}
rasterize(rc, vb, cb);
}
SDL_UpdateTexture(texture, NULL, rc->frameBuffer, rc->width * 3);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
cleanRenderContext(rc);
//SDL_Delay(30);
}
deleteRenderContext(rc);
//dont forget to free the vertex buffers
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}