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