3 # include <simgear/simgear_config.h>
10 #include <simgear/compiler.h>
15 # include <GLUT/glut.h>
20 #include <simgear/screen/extensions.hxx>
21 #include <simgear/screen/RenderTexture.h>
27 void Reshape(int w, int h);
29 GLuint iTextureProgram = 0;
30 GLuint iPassThroughProgram = 0;
32 RenderTexture *rt = NULL;
36 bool bTorusMotion = true;
37 bool bRectMotion = true;
38 bool bShowDepthTexture = false;
40 static const char *g_modeTestStrings[] =
43 "rgba tex2D depthTex2D",
44 "rgba=8 depthTexRECT ctt",
45 "rgba samples=4 tex2D ctt",
46 "rgba=8 tex2D mipmap",
49 "rgba=32f texRECT depthTexRECT",
50 "rgba=16f texRECT depthTexRECT ctt",
51 "r=32f texRECT depth ctt",
53 "r=32f texRECT ctt aux=4"
56 static int g_numModeTestStrings = sizeof(g_modeTestStrings) / sizeof(char*);
57 static int g_currentString = 0;
59 //---------------------------------------------------------------------------
60 // Function : PrintGLerror
62 //---------------------------------------------------------------------------
63 void PrintGLerror( char *msg )
66 const GLubyte *errStr;
68 if ((errCode = glGetError()) != GL_NO_ERROR)
70 errStr = gluErrorString(errCode);
71 fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
75 //---------------------------------------------------------------------------
76 // Function : CreateRenderTexture
78 //---------------------------------------------------------------------------
79 RenderTexture* CreateRenderTexture(const char *initstr)
81 printf("\nCreating with init string: \"%s\"\n", initstr);
83 int texWidth = 256, texHeight = 256;
85 // Test deprecated interface
86 //RenderTexture *rt2 = new RenderTexture(texWidth, texHeight);
87 //if (!rt2->Initialize(true,false,false,false,false,8,8,8,0))
89 RenderTexture *rt2 = new RenderTexture();
91 if (!rt2->Initialize(texWidth, texHeight))
93 fprintf(stderr, "RenderTexture Initialization failed!\n");
96 // for shadow mapping we still have to bind it and set the correct
97 // texture parameters using the SGI_shadow or ARB_shadow extension
98 // setup the rendering context for the RenderTexture
99 if (rt2->BeginCapture())
101 Reshape(texWidth, texHeight);
102 glMatrixMode(GL_MODELVIEW);
104 gluLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
105 glEnable(GL_LIGHTING);
107 glEnable(GL_COLOR_MATERIAL);
108 glEnable(GL_CULL_FACE);
109 glEnable(GL_DEPTH_TEST);
110 glClearColor(0.2, 0.2, 0.2, 1);
114 // enable linear filtering if available
115 if (rt2->IsTexture() || rt2->IsDepthTexture())
117 if (rt2->IsMipmapped())
119 // Enable trilinear filtering so we can see the mipmapping
120 if (rt2->IsTexture())
123 glTexParameteri(rt2->GetTextureTarget(),
124 GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
125 glTexParameteri(rt2->GetTextureTarget(),
126 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
127 glTexParameteri(rt2->GetTextureTarget(),
128 GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
131 if (rt2->IsDepthTexture())
134 glTexParameteri(rt2->GetTextureTarget(),
135 GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
136 glTexParameteri(rt2->GetTextureTarget(),
137 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
138 glTexParameteri(rt2->GetTextureTarget(),
139 GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
142 else if (!(rt2->IsRectangleTexture() || rt2->IsFloatTexture()))
144 if (rt2->IsTexture())
147 glTexParameteri(rt2->GetTextureTarget(),
148 GL_TEXTURE_MIN_FILTER, GL_LINEAR);
149 glTexParameteri(rt2->GetTextureTarget(),
150 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
153 if (rt2->IsDepthTexture())
156 glTexParameteri(rt2->GetTextureTarget(),
157 GL_TEXTURE_MIN_FILTER, GL_LINEAR);
158 glTexParameteri(rt2->GetTextureTarget(),
159 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
164 if (rt2->IsDepthTexture())
167 "\nPress the spacebar to toggle color / depth textures.\n");
168 if (!rt2->IsTexture())
169 bShowDepthTexture = true;
173 if (rt2->IsTexture())
174 bShowDepthTexture = false;
177 PrintGLerror("Create");
181 //---------------------------------------------------------------------------
182 // Function : DestroyRenderTexture
184 //---------------------------------------------------------------------------
185 void DestroyRenderTexture(RenderTexture *rt2)
190 //---------------------------------------------------------------------------
191 // Function : Keyboard
193 //---------------------------------------------------------------------------
194 void Keyboard(unsigned char key, int x, int y)
203 bShowDepthTexture = !bShowDepthTexture;
206 ++g_currentString%=g_numModeTestStrings;
207 DestroyRenderTexture(rt);
208 rt = CreateRenderTexture(g_modeTestStrings[g_currentString]);
211 bTorusMotion = !bTorusMotion;
214 bRectMotion = !bRectMotion;
221 //---------------------------------------------------------------------------
224 //---------------------------------------------------------------------------
227 // make sure we don't try to display nonexistent textures
228 if (!rt->IsDepthTexture())
229 bShowDepthTexture = false;
231 if (bRectMotion) rectAngle += 1;
232 if (bTorusMotion) torusAngle += 1;
236 //---------------------------------------------------------------------------
237 // Function : Reshape
239 //---------------------------------------------------------------------------
240 void Reshape(int w, int h)
244 glViewport(0, 0, w, h);
246 glMatrixMode(GL_PROJECTION);
249 gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 1, 5.0);
252 //---------------------------------------------------------------------------
253 // Function : Display
255 //---------------------------------------------------------------------------
258 if (rt->IsInitialized() && rt->BeginCapture())
260 if (rt->IsDoubleBuffered()) glDrawBuffer(GL_BACK);
261 glMatrixMode(GL_MODELVIEW);
264 glRotatef(torusAngle, 1, 0, 0);
265 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
268 glutSolidTorus(0.25, 1, 32, 64);
271 PrintGLerror("RT Update");
276 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
278 glMatrixMode(GL_MODELVIEW);
280 glRotatef(rectAngle / 10, 0, 1, 0);
282 if(bShowDepthTexture && rt->IsDepthTexture())
284 else if (rt->IsTexture()) {
288 rt->EnableTextureTarget();
290 int maxS = rt->GetMaxS();
291 int maxT = rt->GetMaxT();
294 glTexCoord2f(0, 0); glVertex2f(-1, -1);
295 glTexCoord2f(maxS, 0); glVertex2f( 1, -1);
296 glTexCoord2f(maxS, maxT); glVertex2f( 1, 1);
297 glTexCoord2f(0, maxT); glVertex2f(-1, 1);
300 rt->DisableTextureTarget();
304 PrintGLerror("display");
311 //---------------------------------------------------------------------------
314 //---------------------------------------------------------------------------
315 int main(int argc, char *argv[])
318 glutInit(&argn, argv);
319 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
320 glutInitWindowPosition(50, 50);
321 glutInitWindowSize(512, 512);
322 glutCreateWindow("TestRenderTexture");
324 glutDisplayFunc(_Display);
326 glutReshapeFunc(Reshape);
327 glutKeyboardFunc(Keyboard);
330 glMatrixMode(GL_MODELVIEW);
332 gluLookAt(0, 0, 2, 0, 0, 0, 0, 1, 0);
333 glDisable(GL_LIGHTING);
334 glEnable(GL_COLOR_MATERIAL);
335 glEnable(GL_DEPTH_TEST);
336 glClearColor(0.4, 0.6, 0.8, 1);
339 rt = CreateRenderTexture(g_modeTestStrings[g_currentString]);
341 printf("Press Enter to change RenderTexture parameters.\n"
342 "Press 'r' to toggle the rectangle's motion.\n"
343 "Press 't' to toggle the torus' motion.\n");