2
THIS WORK, INCLUDING THE SOURCE CODE, DOCUMENTATION
3
AND RELATED MEDIA AND DATA, IS PLACED INTO THE PUBLIC DOMAIN.
5
THE ORIGINAL AUTHOR IS KYLE FOLEY.
7
THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY
8
OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF
9
MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE,
10
ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE
11
RESULTING FROM THE USE, MODIFICATION, OR
12
REDISTRIBUTION OF THIS SOFTWARE.
25
#include "SDL/SDL_opengl.h"
32
PFNGLGENVERTEXARRAYSPROC glGenVertexArrays_ = NULL;
33
PFNGLBINDVERTEXARRAYPROC glBindVertexArray_ = NULL;
34
PFNGLDELETEVERTEXARRAYSPROC glDeleteVertexArrays_ = NULL;
37
int width = 640, height = 480;
38
unsigned char *data = (unsigned char*)malloc(width*height*4);
39
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
41
for (int x = 0; x < width*height*4; x++) {
42
if (x % 4 != 3) sum += x * data[x];
50
int main(int argc, char *argv[])
53
if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
54
printf("Unable to initialize SDL: %s\n", SDL_GetError());
58
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
59
screen = SDL_SetVideoMode( 640, 480, 24, SDL_OPENGL );
61
printf("Unable to set video mode: %s\n", SDL_GetError());
65
glClearColor( 0, 0, 0, 0 );
66
glClear( GL_COLOR_BUFFER_BIT );
68
glGenVertexArrays_ = (PFNGLGENVERTEXARRAYSPROC) SDL_GL_GetProcAddress("glGenVertexArrays");
69
assert(glGenVertexArrays_);
70
glBindVertexArray_ = (PFNGLBINDVERTEXARRAYPROC) SDL_GL_GetProcAddress("glBindVertexArray");
71
assert(glBindVertexArray_);
72
glDeleteVertexArrays_ = (PFNGLDELETEVERTEXARRAYSPROC) SDL_GL_GetProcAddress("glDeleteVertexArrays");
73
assert(glDeleteVertexArrays_);
77
glGenVertexArrays_(1, &vao);
78
glBindVertexArray_(vao);
83
glGenTextures( 1, &texture );
84
glBindTexture( GL_TEXTURE_2D, texture );
85
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
86
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
87
GLubyte textureData[16*16*4];
88
for (int x = 0; x < 16; x++) {
89
for (int y = 0; y < 16; y++) {
90
*((int*)&textureData[(x*16 + y) * 4]) = x*16 + ((y*16) << 8);
93
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0,
94
GL_RGBA, GL_UNSIGNED_BYTE, textureData );
96
// Create a second texture
99
glGenTextures( 1, &texture2 );
100
glBindTexture( GL_TEXTURE_2D, texture2 );
101
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
102
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
103
GLubyte texture2Data[] = { 0xff, 0, 0, 0xff,
106
0x80, 0x90, 0x70, 0 };
107
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0,
108
GL_RGBA, GL_UNSIGNED_BYTE, texture2Data );
116
glMatrixMode(GL_PROJECTION);
118
// original: glFrustum(-0.6435469817188064, 0.6435469817188064 ,-0.48266022190470925, 0.48266022190470925 ,0.5400000214576721, 2048);
119
//glFrustum(-0.6435469817188064, 0.1435469817188064 ,-0.48266022190470925, 0.88266022190470925 ,0.5400000214576721, 2048);
120
GLfloat pm[] = { 1.372136116027832, 0, 0, 0, 0, 0.7910231351852417, 0, 0, -0.6352481842041016, 0.29297152161598206, -1.0005275011062622, -1, 0, 0, -1.080284833908081, 0 };
123
glMatrixMode(GL_MODELVIEW);
124
GLfloat matrixData[] = { -1, 0, 0, 0,
128
glLoadMatrixf(matrixData);
129
//glTranslated(-512,-512,-527); // XXX this should be uncommented, but if it is then nothing is shown
131
// glEnable(GL_CULL_FACE);
132
// glEnable(GL_DEPTH_TEST);
134
//glClear(GL_DEPTH_BUFFER_BIT);
136
// glEnableClientState(GL_NORMAL_ARRAY);
137
// glEnableClientState(GL_COLOR_ARRAY);
138
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
140
glActiveTexture(GL_TEXTURE0);
142
glEnableClientState(GL_VERTEX_ARRAY);
144
GLuint arrayBuffer, elementBuffer;
145
glGenBuffers(1, &arrayBuffer);
146
glGenBuffers(1, &elementBuffer);
148
GLubyte arrayData[] = {
150
[0, 0, 0, 67] ==> 128 float
151
[0, 0, 128, 67] ==> 256 float
152
[0, 0, 0, 68] ==> 512 float
153
[0, 0, 128, 68] ==> 1024 float
155
[vertex x ] [vertex y ] [vertex z ] [nr] [texture u ] [texture v ] [lm u ] [lm v ] [color r,g,b,a ] */
156
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, // 0
157
0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, // 1
158
0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 2
159
0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 3
160
0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, // 4
161
0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, // 5
162
0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 6
163
0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 7
164
0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 8
165
0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 9
166
0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 128, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 10
167
0, 0, 0, 0, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 11
168
0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 12
169
0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 13
170
0, 0, 128, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 128, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 14
171
0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 128, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 15
173
0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
174
0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
175
0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
176
0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
177
0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
178
0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
179
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
180
0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
181
0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
182
0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
183
0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
184
0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
185
0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
186
0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
187
0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
188
0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
189
0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
190
0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
191
0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
192
0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
193
0, 0, 0, 0, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
194
0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
195
0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
196
0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
197
0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
198
0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
199
0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
200
0, 0, 128, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128
202
assert(sizeof(arrayData) == 1408);
203
glBindBuffer(GL_ARRAY_BUFFER, arrayBuffer);
204
glBufferData(GL_ARRAY_BUFFER, sizeof(arrayData), arrayData, GL_STATIC_DRAW);
205
glBindBuffer(GL_ARRAY_BUFFER, 0);
207
GLushort elementData[] = { 1, 2, 0, 2, 3, 0, 5, 6, 4, 6, 7, 4, 9, 10, 8, 10, 11, 8, 13, 14, 12, 14, 15, 12 };
208
assert(sizeof(elementData) == 48);
209
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer);
210
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elementData), elementData, GL_STATIC_DRAW);
211
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
213
glBindBuffer(GL_ARRAY_BUFFER, arrayBuffer);
214
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer);
216
// sauer vertex data is apparently 0-12: V3F, 12: N1B, 16-24: T2F, 24-28: T2S, 28-32: C4B
217
glVertexPointer(3, GL_FLOAT, 32, (void*)0); // all these apply to the ARRAY_BUFFER that is bound
218
glTexCoordPointer(2, GL_FLOAT, 32, (void*)16);
219
// glClientActiveTexture(GL_TEXTURE1); // XXX seems to be ignored in native build
220
// glTexCoordPointer(2, GL_SHORT, 32, (void*)24);
221
// glClientActiveTexture(GL_TEXTURE0); // likely not needed, it is a cleanup
222
// glNormalPointer(GL_BYTE, 32, (void*)12);
223
// glColorPointer(4, GL_UNSIGNED_BYTE, 32, (void*)28);
225
glBindTexture(GL_TEXTURE_2D, texture); // diffuse?
226
glActiveTexture(GL_TEXTURE0);
227
glActiveTexture(GL_TEXTURE1);
228
glBindTexture(GL_TEXTURE_2D, texture2); // lightmap?
229
glActiveTexture(GL_TEXTURE0);
233
const char *vertexShader = "uniform mat4 u_modelView;\n"
234
"uniform mat4 u_projection;\n"
235
"varying vec4 v_texCoord0;\n"
237
"{\n" // (gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex)
238
// (u_projection * u_modelView * a_position)
239
" gl_Position = (u_projection * u_modelView * gl_Vertex) + vec4(200, 0, 0, 0);\n"
240
" v_texCoord0.xy = gl_MultiTexCoord0.xy/20.0;\n" // added /100 here
242
const char *fragmentShader = "uniform sampler2D diffusemap;\n"
243
"varying vec4 v_texCoord0;\n"
246
" vec4 diffuse = texture2D(diffusemap, v_texCoord0.xy);\n"
247
" gl_FragColor = diffuse;\n"
250
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
251
glShaderSource(vs, 1, &vertexShader, NULL);
253
glGetShaderiv(vs, GL_COMPILE_STATUS, &ok);
255
printf("Shader compilation error with vertex\n");
257
glGetShaderiv (vs, GL_INFO_LOG_LENGTH, &infoLen);
260
char* infoLog = (char *)malloc(sizeof(char) * infoLen+1);
261
glGetShaderInfoLog(vs, infoLen, NULL, infoLog);
262
printf("Error compiling shader:\n%s\n", infoLog);
266
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
267
glShaderSource(fs, 1, &fragmentShader, NULL);
269
glGetShaderiv(fs, GL_COMPILE_STATUS, &ok);
271
printf("Shader compilation error with fragment\n");
273
glGetShaderiv (vs, GL_INFO_LOG_LENGTH, &infoLen);
276
char* infoLog = (char *)malloc(sizeof(char) * infoLen+1);
277
glGetShaderInfoLog(vs, infoLen, NULL, infoLog);
278
printf("Error compiling shader:\n%s\n", infoLog);
282
GLuint program = glCreateProgram();
284
glAttachShader(program, vs);
285
glAttachShader(program, fs);
286
glLinkProgram(program);
287
glGetProgramiv(program, GL_LINK_STATUS, &ok);
290
glUseProgram(program);
292
//GLint lightmapLocation = glGetUniformLocation(program, "lightmap");
293
//assert(lightmapLocation >= 0);
294
//glUniform1i(lightmapLocation, 1); // sampler2D? Is it the texture unit?
296
GLint diffusemapLocation = glGetUniformLocation(program, "diffusemap");
297
assert(diffusemapLocation >= 0);
298
glUniform1i(diffusemapLocation, 0);
300
//GLint texgenscrollLocation = glGetUniformLocation(program, "texgenscroll");
301
//assert(texgenscrollLocation >= 0);
303
//GLint colorparamsLocation = glGetUniformLocation(program, "colorparams");
304
//assert(colorparamsLocation >= 0);
306
//GLfloat texgenscrollData[] = { 0, 0, 0, 0 };
307
//glUniform4fv(texgenscrollLocation, 1, texgenscrollData);
309
//GLfloat colorparamsData[] = { 2, 2, 2, 1 };
310
//glUniform4fv(colorparamsLocation, 1, colorparamsData);
314
glGetFloatv(GL_MODELVIEW_MATRIX, data);
315
printf("Modelview: ");
316
for (int i = 0; i < 16; i++) printf("%.3f, ", data[i]);
318
//memset(data, 0, 16*4);
319
GLint modelViewLocation = glGetUniformLocation(program, "u_modelView");
320
assert(modelViewLocation >= 0);
321
glUniformMatrix4fv(modelViewLocation, 1, GL_FALSE, data);
325
glGetFloatv(GL_PROJECTION_MATRIX, data);
326
printf("Projection: ");
327
for (int i = 0; i < 16; i++) printf("%.3f, ", data[i]);
329
//memset(data, 0, 16*4);
330
GLint projectionLocation = glGetUniformLocation(program, "u_projection");
331
assert(projectionLocation >= 0);
332
glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, data);
336
glBindAttribLocation(program, 0, "a_position");
337
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 32, (void*)0);
338
glEnableVertexAttribArray(0);
340
glBindAttribLocation(program, 1, "v_texCoord0");
341
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 32, (void*)16);
342
glEnableVertexAttribArray(1);
345
// stop recording in the VAO
347
glBindVertexArray_(0);
349
// unbind all the stuff the VAO would save for us, so this is a valid test
351
glBindBuffer(GL_ARRAY_BUFFER, 0);
352
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
354
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
355
glDisableClientState(GL_VERTEX_ARRAY);
359
glBindVertexArray_(vao);
361
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)12);
362
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*) 0);
363
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)24);
364
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)36);
368
SDL_GL_SwapBuffers();