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