3 # include <simgear/simgear_config.h>
10 #include <simgear/compiler.h>
15 # include <GLUT/glut.h>
20 #include <simgear/debug/logstream.hxx>
21 #include <simgear/screen/extensions.hxx>
22 #include <simgear/screen/RenderTexture.h>
28 // DEBUG - add a lot of noise
34 const char * get_attr_name( int val, int * pdef );
35 #define dbg_printf printf
37 #if defined (__GNUC__)
38 #define dbg_printf(format,args...) ((void)0)
39 #else // defined (__GNUC__)
41 #endif // defined (__GNUC__)
42 #endif // defined (_DEBUG)
45 void Reshape(int w, int h);
47 GLuint iTextureProgram = 0;
48 GLuint iPassThroughProgram = 0;
50 RenderTexture *rt = NULL;
54 bool bTorusMotion = true;
55 bool bRectMotion = true;
56 bool bShowDepthTexture = false;
58 static const char *g_modeTestStrings[] =
61 "rgba tex2D depthTex2D",
62 "rgba=8 depthTexRECT ctt",
63 "rgba samples=4 tex2D ctt",
64 "rgba=8 tex2D mipmap",
67 "rgba=32f texRECT depthTexRECT",
68 "rgba=16f texRECT depthTexRECT ctt",
69 "r=32f texRECT depth ctt",
71 "r=32f texRECT ctt aux=4"
74 static int g_numModeTestStrings = sizeof(g_modeTestStrings) / sizeof(char*);
75 static int g_currentString = 0;
77 //---------------------------------------------------------------------------
78 // Function : PrintGLerror
80 //---------------------------------------------------------------------------
81 void PrintGLerror( const char *msg )
84 const GLubyte *errStr;
86 if ((errCode = glGetError()) != GL_NO_ERROR)
88 errStr = gluErrorString(errCode);
89 fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
93 //---------------------------------------------------------------------------
94 // Function : CreateRenderTexture
96 //---------------------------------------------------------------------------
97 RenderTexture* CreateRenderTexture(const char *initstr)
99 printf("\nCreating with init string: \"%s\"\n", initstr);
101 int texWidth = 256, texHeight = 256;
103 // Test deprecated interface
104 //RenderTexture *rt2 = new RenderTexture(texWidth, texHeight);
105 //if (!rt2->Initialize(true,false,false,false,false,8,8,8,0))
107 RenderTexture *rt2 = new RenderTexture();
109 if (!rt2->Initialize(texWidth, texHeight))
111 fprintf(stderr, "RenderTexture Initialization failed!\n");
115 printf("RenderTexture Initialization done.\n");
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())
123 Reshape(texWidth, texHeight);
124 glMatrixMode(GL_MODELVIEW);
126 gluLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
127 glEnable(GL_LIGHTING);
129 glEnable(GL_COLOR_MATERIAL);
130 glEnable(GL_CULL_FACE);
131 glEnable(GL_DEPTH_TEST);
132 glClearColor(0.2, 0.2, 0.2, 1);
136 // enable linear filtering if available
137 if (rt2->IsTexture() || rt2->IsDepthTexture())
139 if (rt2->IsMipmapped())
141 // Enable trilinear filtering so we can see the mipmapping
142 if (rt2->IsTexture())
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);
153 if (rt2->IsDepthTexture())
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);
164 else if (!(rt2->IsRectangleTexture() || rt2->IsFloatTexture()))
166 if (rt2->IsTexture())
169 glTexParameteri(rt2->GetTextureTarget(),
170 GL_TEXTURE_MIN_FILTER, GL_LINEAR);
171 glTexParameteri(rt2->GetTextureTarget(),
172 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
175 if (rt2->IsDepthTexture())
178 glTexParameteri(rt2->GetTextureTarget(),
179 GL_TEXTURE_MIN_FILTER, GL_LINEAR);
180 glTexParameteri(rt2->GetTextureTarget(),
181 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
186 if (rt2->IsDepthTexture())
189 "\nPress the spacebar to toggle color / depth textures.\n");
190 if (!rt2->IsTexture())
191 bShowDepthTexture = true;
195 if (rt2->IsTexture())
196 bShowDepthTexture = false;
199 PrintGLerror("Create");
203 //---------------------------------------------------------------------------
204 // Function : DestroyRenderTexture
206 //---------------------------------------------------------------------------
207 void DestroyRenderTexture(RenderTexture *rt2)
212 //---------------------------------------------------------------------------
213 // Function : Keyboard
215 //---------------------------------------------------------------------------
216 void Keyboard(unsigned char key, int x, int y)
225 bShowDepthTexture = !bShowDepthTexture;
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]);
234 bTorusMotion = !bTorusMotion;
237 bRectMotion = !bRectMotion;
244 //---------------------------------------------------------------------------
247 //---------------------------------------------------------------------------
250 // make sure we don't try to display nonexistent textures
251 if (!rt->IsDepthTexture())
252 bShowDepthTexture = false;
254 if (bRectMotion) rectAngle += 1;
255 if (bTorusMotion) torusAngle += 1;
259 //---------------------------------------------------------------------------
260 // Function : Reshape
262 //---------------------------------------------------------------------------
263 void Reshape(int w, int h)
267 glViewport(0, 0, w, h);
269 glMatrixMode(GL_PROJECTION);
272 gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 1, 5.0);
275 //---------------------------------------------------------------------------
276 // Function : Display
278 //---------------------------------------------------------------------------
281 if (rt->IsInitialized() && rt->BeginCapture())
283 if (rt->IsDoubleBuffered()) glDrawBuffer(GL_BACK);
284 glMatrixMode(GL_MODELVIEW);
287 glRotatef(torusAngle, 1, 0, 0);
288 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
291 glutSolidTorus(0.25, 1, 32, 64);
294 PrintGLerror("RT Update");
299 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
301 glMatrixMode(GL_MODELVIEW);
303 glRotatef(rectAngle / 10, 0, 1, 0);
305 if(bShowDepthTexture && rt->IsDepthTexture())
307 else if (rt->IsTexture()) {
311 rt->EnableTextureTarget();
313 int maxS = rt->GetMaxS();
314 int maxT = rt->GetMaxT();
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);
323 rt->DisableTextureTarget();
327 PrintGLerror("display");
334 //---------------------------------------------------------------------------
337 //---------------------------------------------------------------------------
338 int main(int argc, char *argv[])
341 glutInit(&argn, argv);
342 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
343 glutInitWindowPosition(50, 50);
344 glutInitWindowSize(512, 512);
345 glutCreateWindow("TestRenderTexture");
347 glutDisplayFunc(_Display);
349 glutReshapeFunc(Reshape);
350 glutKeyboardFunc(Keyboard);
353 glMatrixMode(GL_MODELVIEW);
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);
362 rt = CreateRenderTexture(g_modeTestStrings[g_currentString]);
364 if (rt->IsInitialized() && rt->BeginCapture()) {
366 dbg_printf("Torus should also be shown.\n");
368 dbg_printf("No Torus init = %s\n", (rt->IsInitialized() ? "ok" : "NOT INITIALISED"));
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");