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"
33
int width = 640, height = 480;
34
unsigned char *data = (unsigned char*)malloc(width*height*4);
35
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
37
for (int x = 0; x < width*height*4; x++) {
38
if (x % 4 != 3) sum += x * data[x];
46
int main(int argc, char *argv[])
49
if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
50
printf("Unable to initialize SDL: %s\n", SDL_GetError());
54
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
55
screen = SDL_SetVideoMode( 640, 480, 24, SDL_OPENGL );
57
printf("Unable to set video mode: %s\n", SDL_GetError());
61
glClearColor( 0, 0, 0, 0 );
62
glClear( GL_COLOR_BUFFER_BIT );
67
glGenTextures( 1, &texture );
68
glBindTexture( GL_TEXTURE_2D, texture );
69
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
70
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
71
GLubyte textureData[16*16*4];
72
for (int x = 0; x < 16; x++) {
73
for (int y = 0; y < 16; y++) {
74
*((int*)&textureData[(x*16 + y) * 4]) = x*16 + ((y*16) << 8);
77
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0,
78
GL_RGBA, GL_UNSIGNED_BYTE, textureData );
80
// Create a second texture
83
glGenTextures( 1, &texture2 );
84
glBindTexture( GL_TEXTURE_2D, texture2 );
85
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
86
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
87
GLubyte texture2Data[] = { 0xff, 0, 0, 0xff,
90
0x80, 0x90, 0x70, 0 };
91
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0,
92
GL_RGBA, GL_UNSIGNED_BYTE, texture2Data );
100
glMatrixMode(GL_PROJECTION);
102
// original: glFrustum(-0.6435469817188064, 0.6435469817188064 ,-0.48266022190470925, 0.48266022190470925 ,0.5400000214576721, 2048);
103
glFrustum(-0.6435469817188064, 0.1435469817188064 ,-0.48266022190470925, 0.88266022190470925 ,0.5400000214576721, 2048);
104
glRotatef(-30, 1, 1, 1);
105
//GLfloat pm[] = { 1.372136116027832, 0, 0, 0, 0, 0.7910231351852417, 0, 0, -0.6352481842041016, 0.29297152161598206, -1.0005275011062622, -1, 0, 0, -1.080284833908081, 0 };
108
glMatrixMode(GL_MODELVIEW);
109
GLfloat matrixData[] = { -1, 0, 0, 0,
113
glLoadMatrixf(matrixData);
114
//glTranslated(-512,-512,-527); // XXX this should be uncommented, but if it is then nothing is shown
116
glEnable(GL_CULL_FACE);
117
glEnable(GL_DEPTH_TEST);
119
glClear(GL_DEPTH_BUFFER_BIT);
121
glEnableClientState(GL_NORMAL_ARRAY);
122
glEnableClientState(GL_COLOR_ARRAY);
123
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
125
glActiveTexture(GL_TEXTURE0);
127
glEnableClientState(GL_VERTEX_ARRAY);
129
GLuint arrayBuffer, elementBuffer;
130
glGenBuffers(1, &arrayBuffer);
131
glGenBuffers(1, &elementBuffer);
133
GLubyte arrayData[] = {
135
[0, 0, 0, 67] ==> 128 float
136
[0, 0, 128, 67] ==> 256 float
137
[0, 0, 0, 68] ==> 512 float
138
[0, 0, 128, 68] ==> 1024 float
140
[vertex x ] [vertex y ] [vertex z ] [nr] [texture u ] [texture v ] [lm u ] [lm v ] [color r,g,b,a ] */
141
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, 255, 128, 255, 128, // 0
142
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, 0, 255, 128, // 1
143
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, 64, 255, 128, // 2
144
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, 255, 255, // 3
145
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, 196, 128, 255, 128, // 4
146
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, 0, 255, 128, // 5
147
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, 255, 128, // 6
148
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, 64, 255, 0, // 7
149
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, 64, 128, 255, 128, // 8
150
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, 0, 255, 128, // 9
151
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, 255, 128, // 10
152
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, 255, 0, // 11
153
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, 100, 128, 255, 128, // 12
154
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, 0, 255, 128, // 13
155
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, 255, 128, // 14
156
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, 255, 0, // 15
158
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,
159
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,
160
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,
161
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,
162
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,
163
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,
164
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,
165
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,
166
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,
167
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,
168
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,
169
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,
170
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,
171
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,
172
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,
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, 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,
175
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,
176
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,
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, 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,
179
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,
180
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,
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, 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,
183
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,
184
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,
185
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
187
assert(sizeof(arrayData) == 1408);
188
glBindBuffer(GL_ARRAY_BUFFER, arrayBuffer);
189
glBufferData(GL_ARRAY_BUFFER, sizeof(arrayData), arrayData, GL_STATIC_DRAW);
190
glBindBuffer(GL_ARRAY_BUFFER, 0);
192
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 };
193
assert(sizeof(elementData) == 48);
194
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer);
195
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elementData), elementData, GL_STATIC_DRAW);
196
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
198
glBindBuffer(GL_ARRAY_BUFFER, arrayBuffer);
199
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer);
201
// sauer vertex data is apparently 0-12: V3F, 12: N1B, 16-24: T2F, 24-28: T2S, 28-32: C4B
202
glVertexPointer(3, GL_FLOAT, 32, (void*)0); // all these apply to the ARRAY_BUFFER that is bound
203
glTexCoordPointer(2, GL_FLOAT, 32, (void*)16);
204
glClientActiveTexture(GL_TEXTURE1); // XXX seems to be ignored in native build
205
glTexCoordPointer(2, GL_SHORT, 32, (void*)24);
206
glClientActiveTexture(GL_TEXTURE0); // likely not needed, it is a cleanup
207
glNormalPointer(GL_BYTE, 32, (void*)12);
208
glColorPointer(4, GL_UNSIGNED_BYTE, 32, (void*)28);
210
glBindTexture(GL_TEXTURE_2D, texture); // diffuse?
211
glActiveTexture(GL_TEXTURE0);
212
glActiveTexture(GL_TEXTURE1);
213
glBindTexture(GL_TEXTURE_2D, texture2); // lightmap?
214
glActiveTexture(GL_TEXTURE0);
218
const char *vertexShader = "uniform vec4 texgenscroll;\n"
221
" gl_Position = ftransform();\n"
222
" gl_TexCoord[0].xy = gl_MultiTexCoord0.xy/100.0 + texgenscroll.xy;\n" // added /100 here
223
" gl_TexCoord[1] = gl_Color;\n"
225
const char *fragmentShader = "uniform vec4 colorparams;\n"
226
"uniform sampler2D diffusemap, lightmap;\n"
229
" vec4 diffuse = texture2D(diffusemap, gl_TexCoord[0].xy);\n"
230
" vec4 lm = texture2D(lightmap, gl_TexCoord[1].xy);\n"
231
" diffuse *= colorparams;\n"
232
" gl_FragColor = (diffuse * lm * 0.2) + gl_TexCoord[1];\n"
235
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
236
glShaderSource(vs, 1, &vertexShader, NULL);
238
glGetShaderiv(vs, GL_COMPILE_STATUS, &ok);
241
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
242
glShaderSource(fs, 1, &fragmentShader, NULL);
244
glGetShaderiv(fs, GL_COMPILE_STATUS, &ok);
247
GLuint program = glCreateProgram();
249
glAttachShader(program, vs);
250
glAttachShader(program, fs);
251
glLinkProgram(program);
252
glGetProgramiv(program, GL_LINK_STATUS, &ok);
255
glUseProgram(program);
257
GLint lightmapLocation = glGetUniformLocation(program, "lightmap");
258
assert(lightmapLocation >= 0);
259
glUniform1i(lightmapLocation, 1); // sampler2D? Is it the texture unit?
261
GLint diffusemapLocation = glGetUniformLocation(program, "diffusemap");
262
assert(diffusemapLocation >= 0);
263
glUniform1i(diffusemapLocation, 0);
265
GLint texgenscrollLocation = glGetUniformLocation(program, "texgenscroll");
266
assert(texgenscrollLocation >= 0);
268
GLint colorparamsLocation = glGetUniformLocation(program, "colorparams");
269
assert(colorparamsLocation >= 0);
271
GLfloat texgenscrollData[] = { 0, 0, 0, 0 };
272
glUniform4fv(texgenscrollLocation, 1, texgenscrollData);
274
GLfloat colorparamsData[] = { 2, 2, 2, 1 };
275
glUniform4fv(colorparamsLocation, 1, colorparamsData);
277
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)12);
278
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*) 0);
279
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)24);
280
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)36);
284
SDL_GL_SwapBuffers();