2 #include <simgear/compiler.h>
6 #include <simgear/screen/RenderTexture.h>
7 #include <simgear/screen/extensions.hxx>
12 void Reshape(int w, int h);
14 GLuint iTextureProgram = 0;
15 GLuint iPassThroughProgram = 0;
17 RenderTexture *rt = NULL;
21 bool bTorusMotion = true;
22 bool bRectMotion = true;
23 bool bShowDepthTexture = false;
25 static const char *g_modeTestStrings[] =
28 "rgba tex2D depthTex2D",
29 "rgba=8 depthTexRECT ctt",
30 "rgba samples=4 tex2D ctt",
31 "rgba=8 tex2D mipmap",
34 "rgba=32f texRECT depthTexRECT",
35 "rgba=16f texRECT depthTexRECT ctt",
36 "r=32f texRECT depth ctt",
38 "r=32f texRECT ctt aux=4"
41 static int g_numModeTestStrings = sizeof(g_modeTestStrings) / sizeof(char*);
42 static int g_currentString = 0;
44 //---------------------------------------------------------------------------
45 // Function : PrintGLerror
47 //---------------------------------------------------------------------------
48 void PrintGLerror( char *msg )
51 const GLubyte *errStr;
53 if ((errCode = glGetError()) != GL_NO_ERROR)
55 errStr = gluErrorString(errCode);
56 fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
60 //---------------------------------------------------------------------------
61 // Function : CreateRenderTexture
63 //---------------------------------------------------------------------------
64 RenderTexture* CreateRenderTexture(const char *initstr)
66 printf("\nCreating with init string: \"%s\"\n", initstr);
68 int texWidth = 256, texHeight = 256;
70 // Test deprecated interface
71 //RenderTexture *rt2 = new RenderTexture(texWidth, texHeight);
72 //if (!rt2->Initialize(true,false,false,false,false,8,8,8,0))
74 RenderTexture *rt2 = new RenderTexture();
76 if (!rt2->Initialize(texWidth, texHeight))
78 fprintf(stderr, "RenderTexture Initialization failed!\n");
81 // for shadow mapping we still have to bind it and set the correct
82 // texture parameters using the SGI_shadow or ARB_shadow extension
83 // setup the rendering context for the RenderTexture
84 if (rt2->BeginCapture())
86 Reshape(texWidth, texHeight);
87 glMatrixMode(GL_MODELVIEW);
89 gluLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
90 glEnable(GL_LIGHTING);
92 glEnable(GL_COLOR_MATERIAL);
93 glEnable(GL_CULL_FACE);
94 glEnable(GL_DEPTH_TEST);
95 glClearColor(0.2, 0.2, 0.2, 1);
99 // enable linear filtering if available
100 if (rt2->IsTexture() || rt2->IsDepthTexture())
102 if (rt2->IsMipmapped())
104 // Enable trilinear filtering so we can see the mipmapping
105 if (rt2->IsTexture())
108 glTexParameteri(rt2->GetTextureTarget(),
109 GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
110 glTexParameteri(rt2->GetTextureTarget(),
111 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
112 glTexParameteri(rt2->GetTextureTarget(),
113 GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
116 if (rt2->IsDepthTexture())
119 glTexParameteri(rt2->GetTextureTarget(),
120 GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
121 glTexParameteri(rt2->GetTextureTarget(),
122 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
123 glTexParameteri(rt2->GetTextureTarget(),
124 GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
127 else if (!(rt2->IsRectangleTexture() || rt2->IsFloatTexture()))
129 if (rt2->IsTexture())
132 glTexParameteri(rt2->GetTextureTarget(),
133 GL_TEXTURE_MIN_FILTER, GL_LINEAR);
134 glTexParameteri(rt2->GetTextureTarget(),
135 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
138 if (rt2->IsDepthTexture())
141 glTexParameteri(rt2->GetTextureTarget(),
142 GL_TEXTURE_MIN_FILTER, GL_LINEAR);
143 glTexParameteri(rt2->GetTextureTarget(),
144 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
149 if (rt2->IsDepthTexture())
152 "\nPress the spacebar to toggle color / depth textures.\n");
153 if (!rt2->IsTexture())
154 bShowDepthTexture = true;
158 if (rt2->IsTexture())
159 bShowDepthTexture = false;
162 PrintGLerror("Create");
166 //---------------------------------------------------------------------------
167 // Function : DestroyRenderTexture
169 //---------------------------------------------------------------------------
170 void DestroyRenderTexture(RenderTexture *rt2)
175 //---------------------------------------------------------------------------
176 // Function : Keyboard
178 //---------------------------------------------------------------------------
179 void Keyboard(unsigned char key, int x, int y)
188 bShowDepthTexture = !bShowDepthTexture;
191 ++g_currentString%=g_numModeTestStrings;
192 DestroyRenderTexture(rt);
193 rt = CreateRenderTexture(g_modeTestStrings[g_currentString]);
196 bTorusMotion = !bTorusMotion;
199 bRectMotion = !bRectMotion;
206 //---------------------------------------------------------------------------
209 //---------------------------------------------------------------------------
212 // make sure we don't try to display nonexistent textures
213 if (!rt->IsDepthTexture())
214 bShowDepthTexture = false;
216 if (bRectMotion) rectAngle += 1;
217 if (bTorusMotion) torusAngle += 1;
221 //---------------------------------------------------------------------------
222 // Function : Reshape
224 //---------------------------------------------------------------------------
225 void Reshape(int w, int h)
229 glViewport(0, 0, w, h);
231 glMatrixMode(GL_PROJECTION);
234 gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 1, 5.0);
237 //---------------------------------------------------------------------------
238 // Function : Display
240 //---------------------------------------------------------------------------
243 if (rt->IsInitialized() && rt->BeginCapture())
245 if (rt->IsDoubleBuffered()) glDrawBuffer(GL_BACK);
246 glMatrixMode(GL_MODELVIEW);
249 glRotatef(torusAngle, 1, 0, 0);
250 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
253 glutSolidTorus(0.25, 1, 32, 64);
256 PrintGLerror("RT Update");
261 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
263 glMatrixMode(GL_MODELVIEW);
265 glRotatef(rectAngle / 10, 0, 1, 0);
267 if(bShowDepthTexture && rt->IsDepthTexture())
269 else if (rt->IsTexture()) {
273 rt->EnableTextureTarget();
275 int maxS = rt->GetMaxS();
276 int maxT = rt->GetMaxT();
279 glTexCoord2f(0, 0); glVertex2f(-1, -1);
280 glTexCoord2f(maxS, 0); glVertex2f( 1, -1);
281 glTexCoord2f(maxS, maxT); glVertex2f( 1, 1);
282 glTexCoord2f(0, maxT); glVertex2f(-1, 1);
285 rt->DisableTextureTarget();
289 PrintGLerror("display");
296 //---------------------------------------------------------------------------
299 //---------------------------------------------------------------------------
302 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
303 glutInitWindowPosition(50, 50);
304 glutInitWindowSize(512, 512);
305 glutCreateWindow("TestRenderTexture");
307 glutDisplayFunc(_Display);
309 glutReshapeFunc(Reshape);
310 glutKeyboardFunc(Keyboard);
313 glMatrixMode(GL_MODELVIEW);
315 gluLookAt(0, 0, 2, 0, 0, 0, 0, 1, 0);
316 glDisable(GL_LIGHTING);
317 glEnable(GL_COLOR_MATERIAL);
318 glEnable(GL_DEPTH_TEST);
319 glClearColor(0.4, 0.6, 0.8, 1);
322 rt = CreateRenderTexture(g_modeTestStrings[g_currentString]);
324 printf("Press Enter to change RenderTexture parameters.\n"
325 "Press 'r' to toggle the rectangle's motion.\n"
326 "Press 't' to toggle the torus' motion.\n");