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>
29 //#define dbg_printf printf
31 void Reshape(int w, int h);
33 GLuint iTextureProgram = 0;
34 GLuint iPassThroughProgram = 0;
36 RenderTexture *rt = NULL;
40 bool bTorusMotion = true;
41 bool bRectMotion = true;
42 bool bShowDepthTexture = false;
44 static const char *g_modeTestStrings[] =
47 "rgba tex2D depthTex2D",
48 "rgba=8 depthTexRECT ctt",
49 "rgba samples=4 tex2D ctt",
50 "rgba=8 tex2D mipmap",
53 "rgba=32f texRECT depthTexRECT",
54 "rgba=16f texRECT depthTexRECT ctt",
55 "r=32f texRECT depth ctt",
57 "r=32f texRECT ctt aux=4"
60 static int g_numModeTestStrings = sizeof(g_modeTestStrings) / sizeof(char*);
61 static int g_currentString = 0;
63 //---------------------------------------------------------------------------
64 // Function : PrintGLerror
66 //---------------------------------------------------------------------------
67 void PrintGLerror( char *msg )
70 const GLubyte *errStr;
72 if ((errCode = glGetError()) != GL_NO_ERROR)
74 errStr = gluErrorString(errCode);
75 fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
79 //---------------------------------------------------------------------------
80 // Function : CreateRenderTexture
82 //---------------------------------------------------------------------------
83 RenderTexture* CreateRenderTexture(const char *initstr)
85 printf("\nCreating with init string: \"%s\"\n", initstr);
87 int texWidth = 256, texHeight = 256;
89 // Test deprecated interface
90 //RenderTexture *rt2 = new RenderTexture(texWidth, texHeight);
91 //if (!rt2->Initialize(true,false,false,false,false,8,8,8,0))
93 RenderTexture *rt2 = new RenderTexture();
95 if (!rt2->Initialize(texWidth, texHeight))
97 fprintf(stderr, "RenderTexture Initialization failed!\n");
101 printf("RenderTexture Initialization done.\n");
104 // for shadow mapping we still have to bind it and set the correct
105 // texture parameters using the SGI_shadow or ARB_shadow extension
106 // setup the rendering context for the RenderTexture
107 if (rt2->BeginCapture())
109 Reshape(texWidth, texHeight);
110 glMatrixMode(GL_MODELVIEW);
112 gluLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
113 glEnable(GL_LIGHTING);
115 glEnable(GL_COLOR_MATERIAL);
116 glEnable(GL_CULL_FACE);
117 glEnable(GL_DEPTH_TEST);
118 glClearColor(0.2, 0.2, 0.2, 1);
122 // enable linear filtering if available
123 if (rt2->IsTexture() || rt2->IsDepthTexture())
125 if (rt2->IsMipmapped())
127 // Enable trilinear filtering so we can see the mipmapping
128 if (rt2->IsTexture())
131 glTexParameteri(rt2->GetTextureTarget(),
132 GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
133 glTexParameteri(rt2->GetTextureTarget(),
134 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
135 glTexParameteri(rt2->GetTextureTarget(),
136 GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
139 if (rt2->IsDepthTexture())
142 glTexParameteri(rt2->GetTextureTarget(),
143 GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
144 glTexParameteri(rt2->GetTextureTarget(),
145 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
146 glTexParameteri(rt2->GetTextureTarget(),
147 GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
150 else if (!(rt2->IsRectangleTexture() || rt2->IsFloatTexture()))
152 if (rt2->IsTexture())
155 glTexParameteri(rt2->GetTextureTarget(),
156 GL_TEXTURE_MIN_FILTER, GL_LINEAR);
157 glTexParameteri(rt2->GetTextureTarget(),
158 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
161 if (rt2->IsDepthTexture())
164 glTexParameteri(rt2->GetTextureTarget(),
165 GL_TEXTURE_MIN_FILTER, GL_LINEAR);
166 glTexParameteri(rt2->GetTextureTarget(),
167 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
172 if (rt2->IsDepthTexture())
175 "\nPress the spacebar to toggle color / depth textures.\n");
176 if (!rt2->IsTexture())
177 bShowDepthTexture = true;
181 if (rt2->IsTexture())
182 bShowDepthTexture = false;
185 PrintGLerror("Create");
189 //---------------------------------------------------------------------------
190 // Function : DestroyRenderTexture
192 //---------------------------------------------------------------------------
193 void DestroyRenderTexture(RenderTexture *rt2)
198 //---------------------------------------------------------------------------
199 // Function : Keyboard
201 //---------------------------------------------------------------------------
202 void Keyboard(unsigned char key, int x, int y)
211 bShowDepthTexture = !bShowDepthTexture;
214 ++g_currentString %= g_numModeTestStrings;
215 dbg_printf("Changed to #%d = [%s]\n", g_currentString, g_modeTestStrings[g_currentString]);
216 DestroyRenderTexture(rt);
217 rt = CreateRenderTexture(g_modeTestStrings[g_currentString]);
220 bTorusMotion = !bTorusMotion;
223 bRectMotion = !bRectMotion;
230 //---------------------------------------------------------------------------
233 //---------------------------------------------------------------------------
236 // make sure we don't try to display nonexistent textures
237 if (!rt->IsDepthTexture())
238 bShowDepthTexture = false;
240 if (bRectMotion) rectAngle += 1;
241 if (bTorusMotion) torusAngle += 1;
245 //---------------------------------------------------------------------------
246 // Function : Reshape
248 //---------------------------------------------------------------------------
249 void Reshape(int w, int h)
253 glViewport(0, 0, w, h);
255 glMatrixMode(GL_PROJECTION);
258 gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 1, 5.0);
261 //---------------------------------------------------------------------------
262 // Function : Display
264 //---------------------------------------------------------------------------
267 if (rt->IsInitialized() && rt->BeginCapture())
269 if (rt->IsDoubleBuffered()) glDrawBuffer(GL_BACK);
270 glMatrixMode(GL_MODELVIEW);
273 glRotatef(torusAngle, 1, 0, 0);
274 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
277 glutSolidTorus(0.25, 1, 32, 64);
280 PrintGLerror("RT Update");
285 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
287 glMatrixMode(GL_MODELVIEW);
289 glRotatef(rectAngle / 10, 0, 1, 0);
291 if(bShowDepthTexture && rt->IsDepthTexture())
293 else if (rt->IsTexture()) {
297 rt->EnableTextureTarget();
299 int maxS = rt->GetMaxS();
300 int maxT = rt->GetMaxT();
303 glTexCoord2f(0, 0); glVertex2f(-1, -1);
304 glTexCoord2f(maxS, 0); glVertex2f( 1, -1);
305 glTexCoord2f(maxS, maxT); glVertex2f( 1, 1);
306 glTexCoord2f(0, maxT); glVertex2f(-1, 1);
309 rt->DisableTextureTarget();
313 PrintGLerror("display");
320 //---------------------------------------------------------------------------
323 //---------------------------------------------------------------------------
324 int main(int argc, char *argv[])
327 glutInit(&argn, argv);
328 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
329 glutInitWindowPosition(50, 50);
330 glutInitWindowSize(512, 512);
331 glutCreateWindow("TestRenderTexture");
333 glutDisplayFunc(_Display);
335 glutReshapeFunc(Reshape);
336 glutKeyboardFunc(Keyboard);
339 glMatrixMode(GL_MODELVIEW);
341 gluLookAt(0, 0, 2, 0, 0, 0, 0, 1, 0);
342 glDisable(GL_LIGHTING);
343 glEnable(GL_COLOR_MATERIAL);
344 glEnable(GL_DEPTH_TEST);
345 glClearColor(0.4, 0.6, 0.8, 1);
348 rt = CreateRenderTexture(g_modeTestStrings[g_currentString]);
350 if (rt->IsInitialized() && rt->BeginCapture()) {
352 dbg_printf("Torus should also be shown.\n");
354 dbg_printf("No Torus init = %s\n", (rt->IsInitialized() ? "ok" : "NOT INITIALISED"));
357 printf("Press Enter to change RenderTexture parameters.\n"
358 "Press 'r' to toggle the rectangle's motion.\n"
359 "Press 't' to toggle the torus' motion.\n");