3 # include <simgear/simgear_config.h>
10 #include <simgear/compiler.h>
14 #include <simgear/screen/extensions.hxx>
15 #include <simgear/screen/RenderTexture.h>
21 void Reshape(int w, int h);
23 GLuint iTextureProgram = 0;
24 GLuint iPassThroughProgram = 0;
26 RenderTexture *rt = NULL;
30 bool bTorusMotion = true;
31 bool bRectMotion = true;
32 bool bShowDepthTexture = false;
34 static const char *g_modeTestStrings[] =
37 "rgba tex2D depthTex2D",
38 "rgba=8 depthTexRECT ctt",
39 "rgba samples=4 tex2D ctt",
40 "rgba=8 tex2D mipmap",
43 "rgba=32f texRECT depthTexRECT",
44 "rgba=16f texRECT depthTexRECT ctt",
45 "r=32f texRECT depth ctt",
47 "r=32f texRECT ctt aux=4"
50 static int g_numModeTestStrings = sizeof(g_modeTestStrings) / sizeof(char*);
51 static int g_currentString = 0;
53 //---------------------------------------------------------------------------
54 // Function : PrintGLerror
56 //---------------------------------------------------------------------------
57 void PrintGLerror( char *msg )
60 const GLubyte *errStr;
62 if ((errCode = glGetError()) != GL_NO_ERROR)
64 errStr = gluErrorString(errCode);
65 fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
69 //---------------------------------------------------------------------------
70 // Function : CreateRenderTexture
72 //---------------------------------------------------------------------------
73 RenderTexture* CreateRenderTexture(const char *initstr)
75 printf("\nCreating with init string: \"%s\"\n", initstr);
77 int texWidth = 256, texHeight = 256;
79 // Test deprecated interface
80 //RenderTexture *rt2 = new RenderTexture(texWidth, texHeight);
81 //if (!rt2->Initialize(true,false,false,false,false,8,8,8,0))
83 RenderTexture *rt2 = new RenderTexture();
85 if (!rt2->Initialize(texWidth, texHeight))
87 fprintf(stderr, "RenderTexture Initialization failed!\n");
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())
95 Reshape(texWidth, texHeight);
96 glMatrixMode(GL_MODELVIEW);
98 gluLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
99 glEnable(GL_LIGHTING);
101 glEnable(GL_COLOR_MATERIAL);
102 glEnable(GL_CULL_FACE);
103 glEnable(GL_DEPTH_TEST);
104 glClearColor(0.2, 0.2, 0.2, 1);
108 // enable linear filtering if available
109 if (rt2->IsTexture() || rt2->IsDepthTexture())
111 if (rt2->IsMipmapped())
113 // Enable trilinear filtering so we can see the mipmapping
114 if (rt2->IsTexture())
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);
125 if (rt2->IsDepthTexture())
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);
136 else if (!(rt2->IsRectangleTexture() || rt2->IsFloatTexture()))
138 if (rt2->IsTexture())
141 glTexParameteri(rt2->GetTextureTarget(),
142 GL_TEXTURE_MIN_FILTER, GL_LINEAR);
143 glTexParameteri(rt2->GetTextureTarget(),
144 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
147 if (rt2->IsDepthTexture())
150 glTexParameteri(rt2->GetTextureTarget(),
151 GL_TEXTURE_MIN_FILTER, GL_LINEAR);
152 glTexParameteri(rt2->GetTextureTarget(),
153 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
158 if (rt2->IsDepthTexture())
161 "\nPress the spacebar to toggle color / depth textures.\n");
162 if (!rt2->IsTexture())
163 bShowDepthTexture = true;
167 if (rt2->IsTexture())
168 bShowDepthTexture = false;
171 PrintGLerror("Create");
175 //---------------------------------------------------------------------------
176 // Function : DestroyRenderTexture
178 //---------------------------------------------------------------------------
179 void DestroyRenderTexture(RenderTexture *rt2)
184 //---------------------------------------------------------------------------
185 // Function : Keyboard
187 //---------------------------------------------------------------------------
188 void Keyboard(unsigned char key, int x, int y)
197 bShowDepthTexture = !bShowDepthTexture;
200 ++g_currentString%=g_numModeTestStrings;
201 DestroyRenderTexture(rt);
202 rt = CreateRenderTexture(g_modeTestStrings[g_currentString]);
205 bTorusMotion = !bTorusMotion;
208 bRectMotion = !bRectMotion;
215 //---------------------------------------------------------------------------
218 //---------------------------------------------------------------------------
221 // make sure we don't try to display nonexistent textures
222 if (!rt->IsDepthTexture())
223 bShowDepthTexture = false;
225 if (bRectMotion) rectAngle += 1;
226 if (bTorusMotion) torusAngle += 1;
230 //---------------------------------------------------------------------------
231 // Function : Reshape
233 //---------------------------------------------------------------------------
234 void Reshape(int w, int h)
238 glViewport(0, 0, w, h);
240 glMatrixMode(GL_PROJECTION);
243 gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 1, 5.0);
246 //---------------------------------------------------------------------------
247 // Function : Display
249 //---------------------------------------------------------------------------
252 if (rt->IsInitialized() && rt->BeginCapture())
254 if (rt->IsDoubleBuffered()) glDrawBuffer(GL_BACK);
255 glMatrixMode(GL_MODELVIEW);
258 glRotatef(torusAngle, 1, 0, 0);
259 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
262 glutSolidTorus(0.25, 1, 32, 64);
265 PrintGLerror("RT Update");
270 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
272 glMatrixMode(GL_MODELVIEW);
274 glRotatef(rectAngle / 10, 0, 1, 0);
276 if(bShowDepthTexture && rt->IsDepthTexture())
278 else if (rt->IsTexture()) {
282 rt->EnableTextureTarget();
284 int maxS = rt->GetMaxS();
285 int maxT = rt->GetMaxT();
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);
294 rt->DisableTextureTarget();
298 PrintGLerror("display");
305 //---------------------------------------------------------------------------
308 //---------------------------------------------------------------------------
309 int main(int argc, char *argv[])
312 glutInit(&argn, argv);
313 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
314 glutInitWindowPosition(50, 50);
315 glutInitWindowSize(512, 512);
316 glutCreateWindow("TestRenderTexture");
318 glutDisplayFunc(_Display);
320 glutReshapeFunc(Reshape);
321 glutKeyboardFunc(Keyboard);
324 glMatrixMode(GL_MODELVIEW);
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);
333 rt = CreateRenderTexture(g_modeTestStrings[g_currentString]);
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");