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