-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsGLHelper.h
More file actions
285 lines (228 loc) · 5 KB
/
sGLHelper.h
File metadata and controls
285 lines (228 loc) · 5 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
struct color_t
{
float r;
float g;
float b;
float a;
color_t(float rr, float gg, float bb)
{
r = rr;
g = gg;
b = bb;
a = 1.0f;
}
color_t()
{
color_t(0.0f, 0.0f, 0.0f);
}
};
struct in_out_vertex_t
{
float x;
float y;
float z;
float w;
float u;
float v;
color_t color;
in_out_vertex_t(float xx, float yy, float zz, float uu, float vv, float rr, float gg, float bb)
{
x = xx;
y = yy;
z = zz;
w = 1.0f;
u = uu;
v = vv;
color.r = rr;
color.g = gg;
color.b = bb;
}
in_out_vertex_t()
{
in_out_vertex_t(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
}
};
struct in_out_pixel_t
{
int x;
int y;
float z;
float u;
float v;
float w;
color_t color;
};
/* --------------------------- You can safely ignore everything below this line --------------------------*/
#include <SDL.h>
// Just some forward declarations
void sGLInit(int windowWidth, int windowHeight);
void sGLSetPixel(int x, int y, Uint32 color);
void sGLDrawElements(int count);
void sGLGenBuffer(int * outBufferIndex);
void sGLBindBuffer(int bufferID);
void sGLBufferData(int size, in_out_vertex_t * vertexData);
void sGLClearDepth(float zz);
void sGLEnableDepthTest();
void sGLEnableAlphaTest();
void sGLDisableAlphaTest();
void sGLUseVertexShader(in_out_vertex_t(*inVS)(in_out_vertex_t));
void sGLUsePixelShader(in_out_pixel_t(*inPS)(in_out_pixel_t));
void sGLUniform1f(float * location, float value);
void sGLSwapBuffers();
void sGLClear();
void sGLExit();
bool isRunning();
struct point2_t
{
int x;
int y;
float dotProduct(point2_t otherVector)
{
return (float)(x * otherVector.x + y * otherVector.y);
}
point2_t(int xx, int yy)
{
x = xx;
y = yy;
}
};
// More Globals, WOHOOO!
SDL_Window * g_SDLWindow;
SDL_Renderer * g_SDLRenderer;
SDL_Texture * g_SDLTexture;
Uint32 * g_SDLBackBuffer;
int g_SDLWidth;
int g_SDLHeight;
void SDLStart(int windowWidth, int windowHeight)
{
SDL_Init(SDL_INIT_VIDEO);
g_SDLWidth = windowWidth;
g_SDLHeight = windowHeight;
g_SDLWindow = SDL_CreateWindow("stupidGL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, windowWidth, windowHeight, 0);
g_SDLRenderer = SDL_CreateRenderer(g_SDLWindow, -1, 0);
g_SDLTexture = SDL_CreateTexture(g_SDLRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, windowWidth, windowHeight);
g_SDLBackBuffer = new Uint32[windowWidth * windowHeight];
}
void SDLSwapBuffers(color_t * backbuffer)
{
for (int i = 0; i < g_SDLHeight; ++i)
{
for (int j = 0; j < g_SDLWidth; ++j)
{
// Convert color_t to Uint32
Uint32 iColor = ((uint8_t)(255.0f * backbuffer[i * g_SDLWidth + j].b) << 16) |
((uint8_t)(255.0f * backbuffer[i * g_SDLWidth + j].g) << 8) |
((uint8_t)(255.0f * backbuffer[i * g_SDLWidth + j].r)) & 0xffffff;
g_SDLBackBuffer[i * g_SDLWidth + j] = iColor;
}
}
SDL_UpdateTexture(g_SDLTexture, NULL, g_SDLBackBuffer, g_SDLWidth * sizeof(Uint32));
SDL_RenderClear(g_SDLRenderer);
SDL_RenderCopy(g_SDLRenderer, g_SDLTexture, NULL, NULL);
SDL_RenderPresent(g_SDLRenderer);
}
void SDLEnd()
{
delete[] g_SDLBackBuffer;
SDL_DestroyTexture(g_SDLTexture);
SDL_DestroyRenderer(g_SDLRenderer);
SDL_DestroyWindow(g_SDLWindow);
SDL_Quit();
}
bool isRunning()
{
SDL_Event sEvent;
SDL_PollEvent(&sEvent);
switch (sEvent.type)
{
case SDL_QUIT:
return false;
break;
}
return true;
}
void calculateBarycentric(int pointX, int pointY, in_out_pixel_t * verts, float * outU, float * outV, float * outW)
{
// Taken from http://gamedev.stackexchange.com/questions/23743/whats-the-most-efficient-way-to-find-barycentric-coordinates
point2_t v0 = point2_t(verts[1].x - verts[0].x, verts[1].y - verts[0].y);
point2_t v1 = point2_t(verts[2].x - verts[0].x, verts[2].y - verts[0].y);
point2_t v2 = point2_t(pointX - verts[0].x, pointY - verts[0].y);
float d00 = v0.dotProduct(v0);
float d01 = v0.dotProduct(v1);
float d11 = v1.dotProduct(v1);
float d20 = v2.dotProduct(v0);
float d21 = v2.dotProduct(v1);
float invDenom = 1.0f / (d00 * d11 - d01 * d01);
(*outV) = (d11 * d20 - d01 * d21) * invDenom;
(*outW) = (d00 * d21 - d01 * d20) * invDenom;
(*outU) = 1.0f - (*outV) - (*outW);
}
void sortAscendingY(int * xx, int * yy)
{
int tX, tY;
if (yy[1] > yy[0])
{
if (yy[1] > yy[2])
{
tX = xx[0];
tY = yy[0];
xx[0] = xx[1];
yy[0] = yy[1];
if (tY > yy[2])
{
xx[1] = tX;
yy[1] = tY;
}
else
{
xx[1] = xx[2];
yy[1] = yy[2];
xx[2] = tX;
yy[2] = tY;
}
}
else
{
tX = xx[0];
tY = yy[0];
xx[0] = xx[2];
yy[0] = yy[2];
xx[2] = tX;
yy[2] = tY;
}
}
else
{
if (yy[0] > yy[2])
{
if (yy[1] < yy[2])
{
tX = xx[1];
tY = yy[1];
xx[1] = xx[2];
yy[1] = yy[2];
xx[2] = tX;
yy[2] = tY;
}
}
else
{
tX = xx[0];
tY = yy[0];
xx[0] = xx[2];
yy[0] = yy[2];
xx[2] = xx[1];
yy[2] = yy[1];
xx[1] = tX;
yy[1] = tY;
}
}
// Accidently sorted it the wrong way around...
// Too lazy to fix.. So here is the dirty fix:
tX = xx[0];
tY = yy[0];
xx[0] = xx[2];
yy[0] = yy[2];
xx[2] = tX;
yy[2] = tY;
}