3 # include <simgear/simgear_config.h>
10 #include <simgear/compiler.h>
14 #include <simgear/screen/extensions.hxx>
15 #include <simgear/screen/RenderTexture.h>
20 void Reshape(int w, int h);
22 GLuint iTextureProgram = 0;
23 GLuint iPassThroughProgram = 0;
25 RenderTexture *rt = NULL;
29 bool bTorusMotion = true;
30 bool bRectMotion = true;
31 bool bShowDepthTexture = false;
33 static const char *g_modeTestStrings[] =
36 "rgba tex2D depthTex2D",
37 "rgba=8 depthTexRECT ctt",
38 "rgba samples=4 tex2D ctt",
39 "rgba=8 tex2D mipmap",
42 "rgba=32f texRECT depthTexRECT",
43 "rgba=16f texRECT depthTexRECT ctt",
44 "r=32f texRECT depth ctt",
46 "r=32f texRECT ctt aux=4"
49 static int g_numModeTestStrings = sizeof(g_modeTestStrings) / sizeof(char*);
50 static int g_currentString = 0;
52 //---------------------------------------------------------------------------
53 // Function : PrintGLerror
55 //---------------------------------------------------------------------------
56 void PrintGLerror( char *msg )
59 const GLubyte *errStr;
61 if ((errCode = glGetError()) != GL_NO_ERROR)
63 errStr = gluErrorString(errCode);
64 fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
68 //---------------------------------------------------------------------------
69 // Function : CreateRenderTexture
71 //---------------------------------------------------------------------------
72 RenderTexture* CreateRenderTexture(const char *initstr)
74 printf("\nCreating with init string: \"%s\"\n", initstr);
76 int texWidth = 256, texHeight = 256;
78 // Test deprecated interface
79 //RenderTexture *rt2 = new RenderTexture(texWidth, texHeight);
80 //if (!rt2->Initialize(true,false,false,false,false,8,8,8,0))
82 RenderTexture *rt2 = new RenderTexture();
84 if (!rt2->Initialize(texWidth, texHeight))
86 fprintf(stderr, "RenderTexture Initialization failed!\n");
89 // for shadow mapping we still have to bind it and set the correct
90 // texture parameters using the SGI_shadow or ARB_shadow extension
91 // setup the rendering context for the RenderTexture
92 if (rt2->BeginCapture())
94 Reshape(texWidth, texHeight);
95 glMatrixMode(GL_MODELVIEW);
97 gluLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
98 glEnable(GL_LIGHTING);
100 glEnable(GL_COLOR_MATERIAL);
101 glEnable(GL_CULL_FACE);
102 glEnable(GL_DEPTH_TEST);
103 glClearColor(0.2, 0.2, 0.2, 1);
107 // enable linear filtering if available
108 if (rt2->IsTexture() || rt2->IsDepthTexture())
110 if (rt2->IsMipmapped())
112 // Enable trilinear filtering so we can see the mipmapping
113 if (rt2->IsTexture())
116 glTexParameteri(rt2->GetTextureTarget(),
117 GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
118 glTexParameteri(rt2->GetTextureTarget(),
119 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
120 glTexParameteri(rt2->GetTextureTarget(),
121 GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
124 if (rt2->IsDepthTexture())
127 glTexParameteri(rt2->GetTextureTarget(),
128 GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
129 glTexParameteri(rt2->GetTextureTarget(),
130 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
131 glTexParameteri(rt2->GetTextureTarget(),
132 GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
135 else if (!(rt2->IsRectangleTexture() || rt2->IsFloatTexture()))
137 if (rt2->IsTexture())
140 glTexParameteri(rt2->GetTextureTarget(),
141 GL_TEXTURE_MIN_FILTER, GL_LINEAR);
142 glTexParameteri(rt2->GetTextureTarget(),
143 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
146 if (rt2->IsDepthTexture())
149 glTexParameteri(rt2->GetTextureTarget(),
150 GL_TEXTURE_MIN_FILTER, GL_LINEAR);
151 glTexParameteri(rt2->GetTextureTarget(),
152 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
157 if (rt2->IsDepthTexture())
160 "\nPress the spacebar to toggle color / depth textures.\n");
161 if (!rt2->IsTexture())
162 bShowDepthTexture = true;
166 if (rt2->IsTexture())
167 bShowDepthTexture = false;
170 PrintGLerror("Create");
174 //---------------------------------------------------------------------------
175 // Function : DestroyRenderTexture
177 //---------------------------------------------------------------------------
178 void DestroyRenderTexture(RenderTexture *rt2)
183 //---------------------------------------------------------------------------
184 // Function : Keyboard
186 //---------------------------------------------------------------------------
187 void Keyboard(unsigned char key, int x, int y)
196 bShowDepthTexture = !bShowDepthTexture;
199 ++g_currentString%=g_numModeTestStrings;
200 DestroyRenderTexture(rt);
201 rt = CreateRenderTexture(g_modeTestStrings[g_currentString]);
204 bTorusMotion = !bTorusMotion;
207 bRectMotion = !bRectMotion;
214 //---------------------------------------------------------------------------
217 //---------------------------------------------------------------------------
220 // make sure we don't try to display nonexistent textures
221 if (!rt->IsDepthTexture())
222 bShowDepthTexture = false;
224 if (bRectMotion) rectAngle += 1;
225 if (bTorusMotion) torusAngle += 1;
229 //---------------------------------------------------------------------------
230 // Function : Reshape
232 //---------------------------------------------------------------------------
233 void Reshape(int w, int h)
237 glViewport(0, 0, w, h);
239 glMatrixMode(GL_PROJECTION);
242 gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 1, 5.0);
245 //---------------------------------------------------------------------------
246 // Function : Display
248 //---------------------------------------------------------------------------
251 if (rt->IsInitialized() && rt->BeginCapture())
253 if (rt->IsDoubleBuffered()) glDrawBuffer(GL_BACK);
254 glMatrixMode(GL_MODELVIEW);
257 glRotatef(torusAngle, 1, 0, 0);
258 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
261 glutSolidTorus(0.25, 1, 32, 64);
264 PrintGLerror("RT Update");
269 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
271 glMatrixMode(GL_MODELVIEW);
273 glRotatef(rectAngle / 10, 0, 1, 0);
275 if(bShowDepthTexture && rt->IsDepthTexture())
277 else if (rt->IsTexture()) {
281 rt->EnableTextureTarget();
283 int maxS = rt->GetMaxS();
284 int maxT = rt->GetMaxT();
287 glTexCoord2f(0, 0); glVertex2f(-1, -1);
288 glTexCoord2f(maxS, 0); glVertex2f( 1, -1);
289 glTexCoord2f(maxS, maxT); glVertex2f( 1, 1);
290 glTexCoord2f(0, maxT); glVertex2f(-1, 1);
293 rt->DisableTextureTarget();
297 PrintGLerror("display");
304 //---------------------------------------------------------------------------
307 //---------------------------------------------------------------------------
308 int main(int argc, char *argv[])
311 glutInit(&argn, argv);
312 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
313 glutInitWindowPosition(50, 50);
314 glutInitWindowSize(512, 512);
315 glutCreateWindow("TestRenderTexture");
317 glutDisplayFunc(_Display);
319 glutReshapeFunc(Reshape);
320 glutKeyboardFunc(Keyboard);
323 glMatrixMode(GL_MODELVIEW);
325 gluLookAt(0, 0, 2, 0, 0, 0, 0, 1, 0);
326 glDisable(GL_LIGHTING);
327 glEnable(GL_COLOR_MATERIAL);
328 glEnable(GL_DEPTH_TEST);
329 glClearColor(0.4, 0.6, 0.8, 1);
332 rt = CreateRenderTexture(g_modeTestStrings[g_currentString]);
334 printf("Press Enter to change RenderTexture parameters.\n"
335 "Press 'r' to toggle the rectangle's motion.\n"
336 "Press 't' to toggle the torus' motion.\n");