]> git.mxchange.org Git - simgear.git/blob - simgear/screen/TestRenderTexture.cpp
Geoff McLane:
[simgear.git] / simgear / screen / TestRenderTexture.cpp
1
2 #ifdef HAVE_CONFIG_H
3 #  include <simgear/simgear_config.h>
4 #endif
5
6 #ifdef HAVE_WINDOWS_H
7 #  include <windows.h>
8 #endif
9
10 #include <simgear/compiler.h>
11
12 #include <osg/GL>
13
14 #ifdef __APPLE__
15 #  include <GLUT/glut.h>
16 #else
17 #  include <GL/glut.h>
18 #endif
19
20 #include <simgear/debug/logstream.hxx>
21 #include <simgear/screen/extensions.hxx>
22 #include <simgear/screen/RenderTexture.h>
23
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #define dbg_printf
29 //#define dbg_printf printf
30
31 void Reshape(int w, int h);
32
33 GLuint      iTextureProgram     = 0;
34 GLuint      iPassThroughProgram = 0;
35
36 RenderTexture *rt = NULL;
37
38 float       rectAngle         = 0;
39 float       torusAngle        = 0;
40 bool        bTorusMotion      = true;
41 bool        bRectMotion       = true;
42 bool        bShowDepthTexture = false;
43
44 static const char *g_modeTestStrings[] = 
45 {
46     "rgb tex2D",
47     "rgba tex2D depthTex2D",
48     "rgba=8 depthTexRECT ctt",
49     "rgba samples=4 tex2D ctt",
50     "rgba=8 tex2D mipmap",
51     "rgb=5,6,5 tex2D",
52     "rgba=16f texRECT",
53     "rgba=32f texRECT depthTexRECT",
54     "rgba=16f texRECT depthTexRECT ctt",
55     "r=32f texRECT depth ctt",
56     "rgb double tex2D",
57     "r=32f texRECT ctt aux=4"
58 };
59
60 static int g_numModeTestStrings = sizeof(g_modeTestStrings) / sizeof(char*);
61 static int g_currentString      = 0;
62
63 //---------------------------------------------------------------------------
64 // Function             : PrintGLerror
65 // Description      : 
66 //---------------------------------------------------------------------------
67 void PrintGLerror( char *msg )
68 {
69     GLenum errCode;
70     const GLubyte *errStr;
71     
72     if ((errCode = glGetError()) != GL_NO_ERROR) 
73     {
74         errStr = gluErrorString(errCode);
75         fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
76     }
77 }
78
79 //---------------------------------------------------------------------------
80 // Function             : CreateRenderTexture
81 // Description      : 
82 //---------------------------------------------------------------------------
83 RenderTexture* CreateRenderTexture(const char *initstr)
84 {
85     printf("\nCreating with init string: \"%s\"\n", initstr);
86
87     int texWidth = 256, texHeight = 256;
88
89     // Test deprecated interface
90     //RenderTexture *rt2 = new RenderTexture(texWidth, texHeight);
91     //if (!rt2->Initialize(true,false,false,false,false,8,8,8,0))
92
93     RenderTexture *rt2 = new RenderTexture(); 
94     rt2->Reset(initstr);
95     if (!rt2->Initialize(texWidth, texHeight))
96     {
97         fprintf(stderr, "RenderTexture Initialization failed!\n");
98     }
99     else
100     {
101         printf("RenderTexture Initialization done.\n");
102     }
103
104     // for shadow mapping we still have to bind it and set the correct 
105     // texture parameters using the SGI_shadow or ARB_shadow extension
106     // setup the rendering context for the RenderTexture
107     if (rt2->BeginCapture())
108     {
109         Reshape(texWidth, texHeight);
110         glMatrixMode(GL_MODELVIEW);
111         glLoadIdentity();
112         gluLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
113         glEnable(GL_LIGHTING);
114         glEnable(GL_LIGHT0);
115         glEnable(GL_COLOR_MATERIAL);
116         glEnable(GL_CULL_FACE);
117         glEnable(GL_DEPTH_TEST); 
118         glClearColor(0.2, 0.2, 0.2, 1);
119         rt2->EndCapture();
120     }
121
122     // enable linear filtering if available
123     if (rt2->IsTexture() || rt2->IsDepthTexture())
124     {
125         if (rt2->IsMipmapped())
126         {
127             // Enable trilinear filtering so we can see the mipmapping
128             if (rt2->IsTexture())
129             {
130                 rt2->Bind();
131                 glTexParameteri(rt2->GetTextureTarget(),
132                                 GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
133                 glTexParameteri(rt2->GetTextureTarget(),
134                                 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
135                 glTexParameteri(rt2->GetTextureTarget(),
136                                 GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
137             }
138             
139             if (rt2->IsDepthTexture())
140             {
141                 rt2->BindDepth();
142                 glTexParameteri(rt2->GetTextureTarget(),
143                                 GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
144                 glTexParameteri(rt2->GetTextureTarget(),
145                                 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
146                 glTexParameteri(rt2->GetTextureTarget(),
147                                 GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
148             }
149         }   
150         else if (!(rt2->IsRectangleTexture() || rt2->IsFloatTexture()))
151         {
152             if (rt2->IsTexture())
153             {
154                 rt2->Bind();
155                 glTexParameteri(rt2->GetTextureTarget(),
156                                 GL_TEXTURE_MIN_FILTER, GL_LINEAR);
157                 glTexParameteri(rt2->GetTextureTarget(),
158                                 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
159             }
160             
161             if (rt2->IsDepthTexture())
162             {
163                 rt2->BindDepth();
164                 glTexParameteri(rt2->GetTextureTarget(),
165                                 GL_TEXTURE_MIN_FILTER, GL_LINEAR);
166                 glTexParameteri(rt2->GetTextureTarget(),
167                                 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
168             }
169         }
170     }
171
172     if (rt2->IsDepthTexture())
173     {
174         fprintf(stderr, 
175             "\nPress the spacebar to toggle color / depth textures.\n");
176         if (!rt2->IsTexture())
177             bShowDepthTexture = true;
178     }
179     else 
180     {
181         if (rt2->IsTexture())
182             bShowDepthTexture = false;
183     }
184
185     PrintGLerror("Create");
186     return rt2;
187 }
188
189 //---------------------------------------------------------------------------
190 // Function             : DestroyRenderTexture
191 // Description      : 
192 //---------------------------------------------------------------------------
193 void DestroyRenderTexture(RenderTexture *rt2)
194 {
195     delete rt2;
196 }
197
198 //---------------------------------------------------------------------------
199 // Function             : Keyboard
200 // Description      : 
201 //---------------------------------------------------------------------------
202 void Keyboard(unsigned char key, int x, int y)
203 {
204     switch(key)
205     {
206     case 27: 
207     case 'q':
208         exit(0);
209         break;
210     case ' ':
211         bShowDepthTexture = !bShowDepthTexture;
212         break;
213     case 13:
214         ++g_currentString %= g_numModeTestStrings;
215         dbg_printf("Changed to #%d = [%s]\n", g_currentString, g_modeTestStrings[g_currentString]);
216         DestroyRenderTexture(rt);
217         rt = CreateRenderTexture(g_modeTestStrings[g_currentString]);
218         break;
219     case 't':
220         bTorusMotion = !bTorusMotion;
221         break;
222     case 'r':
223         bRectMotion = !bRectMotion;
224         break;
225     default:
226         return;
227     }
228 }
229
230 //---------------------------------------------------------------------------
231 // Function             : Idle
232 // Description      : 
233 //---------------------------------------------------------------------------
234 void Idle()
235 {
236     // make sure we don't try to display nonexistent textures
237     if (!rt->IsDepthTexture())
238         bShowDepthTexture = false; 
239     
240     if (bRectMotion) rectAngle += 1;
241     if (bTorusMotion) torusAngle += 1;
242     glutPostRedisplay();
243 }
244
245 //---------------------------------------------------------------------------
246 // Function             : Reshape
247 // Description      : 
248 //---------------------------------------------------------------------------
249 void Reshape(int w, int h)
250 {
251     if (h == 0) h = 1;
252     
253     glViewport(0, 0, w, h);
254     
255     glMatrixMode(GL_PROJECTION);
256     glLoadIdentity();
257     
258     gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 1, 5.0);
259 }
260
261 //---------------------------------------------------------------------------
262 // Function             : Display
263 // Description      : 
264 //---------------------------------------------------------------------------
265 void _Display()
266 {
267     if (rt->IsInitialized() && rt->BeginCapture())
268     {
269       if (rt->IsDoubleBuffered()) glDrawBuffer(GL_BACK);
270         glMatrixMode(GL_MODELVIEW);
271         glPushMatrix();
272         
273         glRotatef(torusAngle, 1, 0, 0);
274         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
275         glColor3f(1,1,0);
276         
277         glutSolidTorus(0.25, 1, 32, 64);
278         
279         glPopMatrix();
280         PrintGLerror("RT Update");
281
282     rt->EndCapture();
283     }    
284
285     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
286     glColor3f(1, 1, 1);
287     glMatrixMode(GL_MODELVIEW);
288     glPushMatrix();
289     glRotatef(rectAngle / 10, 0, 1, 0);
290         
291     if(bShowDepthTexture && rt->IsDepthTexture())
292         rt->BindDepth();
293     else if (rt->IsTexture()) {
294         rt->Bind();
295     }
296
297     rt->EnableTextureTarget();
298
299     int maxS = rt->GetMaxS();
300     int maxT = rt->GetMaxT();  
301     
302     glBegin(GL_QUADS);
303     glTexCoord2f(0,       0); glVertex2f(-1, -1);
304     glTexCoord2f(maxS,    0); glVertex2f( 1, -1);
305     glTexCoord2f(maxS, maxT); glVertex2f( 1,  1);
306     glTexCoord2f(0,    maxT); glVertex2f(-1,  1);
307     glEnd();
308     
309     rt->DisableTextureTarget();
310           
311     glPopMatrix();
312     
313     PrintGLerror("display");
314     glutSwapBuffers();
315 }
316
317
318
319
320 //---------------------------------------------------------------------------
321 // Function             : main
322 // Description      : 
323 //---------------------------------------------------------------------------
324 int main(int argc, char *argv[])
325 {
326     int argn = argc;
327     glutInit(&argn, argv);
328     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
329     glutInitWindowPosition(50, 50);
330     glutInitWindowSize(512, 512);
331     glutCreateWindow("TestRenderTexture");  
332     
333     glutDisplayFunc(_Display);
334     glutIdleFunc(Idle);
335     glutReshapeFunc(Reshape);
336     glutKeyboardFunc(Keyboard);
337     
338     Reshape(512, 512);
339     glMatrixMode(GL_MODELVIEW);
340     glLoadIdentity();
341     gluLookAt(0, 0, 2, 0, 0, 0, 0, 1, 0);
342     glDisable(GL_LIGHTING);
343     glEnable(GL_COLOR_MATERIAL);
344     glEnable(GL_DEPTH_TEST); 
345     glClearColor(0.4, 0.6, 0.8, 1);
346     
347
348     rt = CreateRenderTexture(g_modeTestStrings[g_currentString]);
349
350     if (rt->IsInitialized() && rt->BeginCapture()) {
351       rt->EndCapture();
352       dbg_printf("Torus should also be shown.\n");
353     } else {
354       dbg_printf("No Torus init = %s\n", (rt->IsInitialized() ? "ok" : "NOT INITIALISED"));
355     }
356     
357     printf("Press Enter to change RenderTexture parameters.\n"
358            "Press 'r' to toggle the rectangle's motion.\n"
359            "Press 't' to toggle the torus' motion.\n");
360
361     
362     glutMainLoop();
363     return 0;
364 }