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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
|
//////////////////////////////////////////////////////////////////////////////////////////
// Main.cpp
// Render to texture
// Downloaded from: www.paulsprojects.net
// Created: 10th September 2002
//
// Copyright (c) 2006, Paul Baker
// Distributed under the New BSD Licence. (See accompanying file License.txt or copy at
// http://www.paulsprojects.net/NewBSDLicense.txt)
//////////////////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <GL/glew.h>
#include <GL/wglew.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include "log.h"
#include "window.h"
#include "fps_counter.h"
#include "timer.h"
#include "maths/maths.h"
#include "pbuffer.h"
#include "interactor.h"
#include "image.h"
#include "main.h"
//errorLog MUST be kept - it is used by other files
LOG errorLog;
WINDOW window;
FPS_COUNTER fpsCounter;
TIMER timer;
COLOR windowBackgroundColor(0.0f, 0.0f, 1.0f, 0.0f);
COLOR pbufferBackgroundColor(0.0f, 0.0f, 0.0f, 0.0f);
INTERACTOR camera;
int pbufferSize=512;
PBUFFER pbuffer;
GLuint pbufferTexture; //texture object which is used when pbuffer texture is displayed
//anisotropy
int currentAnisotropy=1.0f;
int maxAnisotropy=1.0f;
//Using mipmap filtering?
bool useMipmapFilter=false;
//Draw the textured teapot into the pbuffer?
bool drawTextured=false;
GLuint decalTexture; //texture ID
//Set up variables
bool DemoInit()
{
if(!window.Init("Render To Texture", 640, 480, 32, 24, 8, WINDOWED_SCREEN))
return 0; //quit if not created
glewInit();
camera.Init(VECTOR3D(0.0f, 0.0f, -2.5f), 2.0f, 100.0f);
//Set up extensions
if( !WGLEW_ARB_extensions_string)
return false;
//Set up wgl extensions
if( !WGLEW_ARB_pbuffer || !WGLEW_ARB_pixel_format ||
!WGLEW_ARB_render_texture)
return false;
//Init the pbuffer
int pbufferExtraIAttribs[]={WGL_BIND_TO_TEXTURE_RGBA_ARB, true,
0};
int pbufferFlags[]={WGL_TEXTURE_FORMAT_ARB, WGL_TEXTURE_RGBA_ARB,
WGL_TEXTURE_TARGET_ARB, WGL_TEXTURE_2D_ARB,
//request mipmap space if mipmaps are to be used
GLEW_SGIS_generate_mipmap ? WGL_MIPMAP_TEXTURE_ARB : 0,
GLEW_SGIS_generate_mipmap ? true : 0,
0};
if(!pbuffer.Init(pbufferSize, pbufferSize, 32, 24, 8, 1, pbufferExtraIAttribs, pbufferFlags))
return false;
//Create the texture object to relate to the pbuffer
glGenTextures(1, &pbufferTexture);
glBindTexture(GL_TEXTURE_2D, pbufferTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//Use generated mipmaps if supported
if(GLEW_SGIS_generate_mipmap)
{
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, true);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glHint(GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
useMipmapFilter=true;
}
//Use maximum anisotropy if supported
if(GLEW_EXT_texture_filter_anisotropic)
{
glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAnisotropy);
currentAnisotropy=maxAnisotropy;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, currentAnisotropy);
}
//Load the decal texture
//Note: This MUST be done when the pbuffer is the current context
pbuffer.MakeCurrent();
IMAGE decalImage;
decalImage.Load("decal.bmp");
glGenTextures(1, &decalTexture);
glBindTexture(GL_TEXTURE_2D, decalTexture);
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, decalImage.width, decalImage.height,
0, decalImage.format, GL_UNSIGNED_BYTE, decalImage.data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//reset timer for start
timer.Reset();
return true;
}
//Set up openGL
bool GLInit()
{
//Set up for pbuffer
pbuffer.MakeCurrent();
//set viewport
glViewport(0, 0, pbufferSize, pbufferSize);
//set up projection matrix
glMatrixMode(GL_PROJECTION); //select projection matrix
glLoadIdentity(); //reset
gluPerspective(45.0f, 1.0f, 1.0f, 100.0f);
//load identity modelview
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//other states
//shading
glShadeModel(GL_SMOOTH);
glClearColor( pbufferBackgroundColor.r,
pbufferBackgroundColor.g,
pbufferBackgroundColor.b,
pbufferBackgroundColor.a);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
//depth
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
//hints
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
//Set up for window
window.MakeCurrent();
//set viewport
int height;
if (window.height==0)
height=1;
else
height=window.height;
glViewport(0, 0, window.width, height); //reset viewport
//set up projection matrix
glMatrixMode(GL_PROJECTION); //select projection matrix
glLoadIdentity(); //reset
gluPerspective(45.0f, (GLfloat)window.width/(GLfloat)height, 1.0f, 100.0f);
//load identity modelview
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//other states
//shading
glShadeModel(GL_SMOOTH);
glClearColor( windowBackgroundColor.r,
windowBackgroundColor.g,
windowBackgroundColor.b,
windowBackgroundColor.a);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
//depth
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
//hints
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_TEXTURE_2D);
return true;
}
//Perform per frame updates
void UpdateFrame()
{
window.Update();
camera.Update();
//Change anisotropy level
if( window.isKeyPressed(VK_UP) && GLEW_EXT_texture_filter_anisotropic &&
currentAnisotropy<maxAnisotropy)
{
window.MakeCurrent();
currentAnisotropy*=2;
glBindTexture(GL_TEXTURE_2D, pbufferTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, currentAnisotropy);
window.SetKeyReleased(VK_UP);
}
if( window.isKeyPressed(VK_DOWN) && GLEW_EXT_texture_filter_anisotropic &&
currentAnisotropy>1)
{
window.MakeCurrent();
currentAnisotropy/=2;
glBindTexture(GL_TEXTURE_2D, pbufferTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, currentAnisotropy);
window.SetKeyReleased(VK_DOWN);
}
//toggle mipmaps
if( window.isKeyPressed('M') && useMipmapFilter==false && GLEW_SGIS_generate_mipmap)
{
window.MakeCurrent();
glBindTexture(GL_TEXTURE_2D, pbufferTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, true);
useMipmapFilter=true;
}
if( window.isKeyPressed('L') && useMipmapFilter==true && GLEW_SGIS_generate_mipmap)
{
window.MakeCurrent();
glBindTexture(GL_TEXTURE_2D, pbufferTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, false);
useMipmapFilter=false;
}
//Pause/unpause
if(window.isKeyPressed('P'))
timer.Pause();
if(window.isKeyPressed('U'))
timer.Unpause();
//Swap between scenes in the pbuffer
if(window.isKeyPressed('1') && drawTextured)
{
//Draw wire tori
drawTextured=false;
}
if(window.isKeyPressed('2') && !drawTextured)
{
//draw textured sphere
drawTextured=true;
}
}
//draw a frame
void RenderFrame()
{
//Draw to pbuffer
pbuffer.MakeCurrent();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); //reset modelview matrix
gluLookAt( 0.0f, 0.0f, 4.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
//Draw scene
if(drawTextured)
{
glBindTexture(GL_TEXTURE_2D, decalTexture);
glEnable(GL_TEXTURE_2D);
glPushMatrix();
glRotatef(timer.GetTime()/20, 0.0f, 1.0f, 0.0f);
glutSolidTeapot(0.8f);
glPopMatrix();
glDisable(GL_TEXTURE_2D);
}
else
{
glPushMatrix();
glRotatef(timer.GetTime()/20, 0.0f, 1.0f, 0.0f);
glRotatef(55.0f, 1.0f, 0.0f, 0.0f);
glutWireTorus(0.3f, 1.0f, 12, 24);
glPopMatrix();
glPushMatrix();
glRotatef(timer.GetTime()/20, 0.0f, 1.0f, 0.0f);
glRotatef(-55.0f, 1.0f, 0.0f, 0.0f);
glutWireTorus(0.3f, 1.0f, 12, 24);
glPopMatrix();
}
//Draw to window
window.MakeCurrent();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
camera.SetupViewMatrix();
glLoadMatrixf(camera.viewMatrix);
glBindTexture(GL_TEXTURE_2D, pbufferTexture);
//use the pbuffer as the texture
wglBindTexImageARB(pbuffer.hBuffer, WGL_FRONT_LEFT_ARB);
//Draw simple rectangle
glBegin(GL_TRIANGLE_STRIP);
{
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 0.0f);
}
glEnd();
//release the pbuffer for further rendering
wglReleaseTexImageARB(pbuffer.hBuffer, WGL_FRONT_LEFT_ARB);
fpsCounter.Update(); //update frames per second counter
glColor4f(1.0f, 1.0f, 0.0f, 1.0f);
window.StartTextMode();
window.Print(0, 28, "FPS: %.2f", fpsCounter.GetFps()); //print the fps
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
window.Print(0, 48, "%dx Anisotropy", currentAnisotropy);
glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
window.Print(0, 68, "%s", useMipmapFilter ? "LINEAR_MIPMAP_LINEAR filtering" :
"LINEAR filtering");
window.EndTextMode();
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
if(window.isKeyPressed(VK_F1))
{
window.SaveScreenshot();
window.SetKeyReleased(VK_F1);
}
window.SwapBuffers(); //swap buffers
//check for any opengl errors
window.CheckGLError();
//quit if necessary
if(window.isKeyPressed(VK_ESCAPE))
PostQuitMessage(0);
}
void DemoShutdown()
{
pbuffer.Shutdown();
window.Shutdown(); //Shutdown window
}
//ENTRY POINT FOR APPLICATION
//CALL WINDOW CREATION ROUTINE, DEAL WITH MESSAGES, WATCH FOR INTERACTION
int WINAPI WinMain( HINSTANCE hInstance, //instance
HINSTANCE hPrevInstance, //Previous Instance
LPSTR lpCmdLine, //command line parameters
int nCmdShow) //Window show state
{
//Initiation
errorLog.Init("Error Log.txt");
//init variables etc, then GL
if(!DemoInit())
{
errorLog.OutputError("Demo Initiation failed");
return 0;
}
else
errorLog.OutputSuccess("Demo Initiation Successful");
if(!GLInit())
{
errorLog.OutputError("OpenGL Initiation failed");
return 0;
}
else
errorLog.OutputSuccess("OpenGL Initiation Successful");
//Main Loop
for(;;)
{
if(!(window.HandleMessages())) break;//handle windows messages, quit if returns false
UpdateFrame();
RenderFrame();
}
DemoShutdown();
errorLog.OutputSuccess("Exiting...");
return (window.msg.wParam); //Exit The Program
}
|