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