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