00001
00002 #ifdef HAVE_CONFIG_H
00003 # include <simgear/simgear_config.h>
00004 #endif
00005
00006 #ifdef HAVE_WINDOWS_H
00007 # include <windows.h>
00008 #endif
00009
00010 #include <simgear/compiler.h>
00011
00012 #include <osg/GL>
00013
00014 #ifdef __APPLE__
00015 # include <GLUT/glut.h>
00016 #else
00017 # include <GL/glut.h>
00018 #endif
00019
00020 #include <simgear/debug/logstream.hxx>
00021 #include <simgear/screen/extensions.hxx>
00022 #include <simgear/screen/RenderTexture.h>
00023
00024 #include <assert.h>
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027
00028
00029
00030
00031
00032
00033 #if defined (_DEBUG)
00034 const char * get_attr_name( int val, int * pdef );
00035 #define dbg_printf printf
00036 #else
00037 #if defined (__GNUC__)
00038 #define dbg_printf(format,args...) ((void)0)
00039 #else // defined (__GNUC__)
00040 #define dbg_printf
00041 #endif // defined (__GNUC__)
00042 #endif // defined (_DEBUG)
00043
00044
00045 void Reshape(int w, int h);
00046
00047 GLuint iTextureProgram = 0;
00048 GLuint iPassThroughProgram = 0;
00049
00050 RenderTexture *rt = NULL;
00051
00052 float rectAngle = 0;
00053 float torusAngle = 0;
00054 bool bTorusMotion = true;
00055 bool bRectMotion = true;
00056 bool bShowDepthTexture = false;
00057
00058 static const char *g_modeTestStrings[] =
00059 {
00060 "rgb tex2D",
00061 "rgba tex2D depthTex2D",
00062 "rgba=8 depthTexRECT ctt",
00063 "rgba samples=4 tex2D ctt",
00064 "rgba=8 tex2D mipmap",
00065 "rgb=5,6,5 tex2D",
00066 "rgba=16f texRECT",
00067 "rgba=32f texRECT depthTexRECT",
00068 "rgba=16f texRECT depthTexRECT ctt",
00069 "r=32f texRECT depth ctt",
00070 "rgb double tex2D",
00071 "r=32f texRECT ctt aux=4"
00072 };
00073
00074 static int g_numModeTestStrings = sizeof(g_modeTestStrings) / sizeof(char*);
00075 static int g_currentString = 0;
00076
00077
00078
00079
00080
00081 void PrintGLerror( const char *msg )
00082 {
00083 GLenum errCode;
00084 const GLubyte *errStr;
00085
00086 if ((errCode = glGetError()) != GL_NO_ERROR)
00087 {
00088 errStr = gluErrorString(errCode);
00089 fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
00090 }
00091 }
00092
00093
00094
00095
00096
00097 RenderTexture* CreateRenderTexture(const char *initstr)
00098 {
00099 printf("\nCreating with init string: \"%s\"\n", initstr);
00100
00101 int texWidth = 256, texHeight = 256;
00102
00103
00104
00105
00106
00107 RenderTexture *rt2 = new RenderTexture();
00108 rt2->Reset(initstr);
00109 if (!rt2->Initialize(texWidth, texHeight))
00110 {
00111 fprintf(stderr, "RenderTexture Initialization failed!\n");
00112 }
00113 else
00114 {
00115 printf("RenderTexture Initialization done.\n");
00116 }
00117
00118
00119
00120
00121 if (rt2->BeginCapture())
00122 {
00123 Reshape(texWidth, texHeight);
00124 glMatrixMode(GL_MODELVIEW);
00125 glLoadIdentity();
00126 gluLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
00127 glEnable(GL_LIGHTING);
00128 glEnable(GL_LIGHT0);
00129 glEnable(GL_COLOR_MATERIAL);
00130 glEnable(GL_CULL_FACE);
00131 glEnable(GL_DEPTH_TEST);
00132 glClearColor(0.2, 0.2, 0.2, 1);
00133 rt2->EndCapture();
00134 }
00135
00136
00137 if (rt2->IsTexture() || rt2->IsDepthTexture())
00138 {
00139 if (rt2->IsMipmapped())
00140 {
00141
00142 if (rt2->IsTexture())
00143 {
00144 rt2->Bind();
00145 glTexParameteri(rt2->GetTextureTarget(),
00146 GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
00147 glTexParameteri(rt2->GetTextureTarget(),
00148 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00149 glTexParameteri(rt2->GetTextureTarget(),
00150 GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
00151 }
00152
00153 if (rt2->IsDepthTexture())
00154 {
00155 rt2->BindDepth();
00156 glTexParameteri(rt2->GetTextureTarget(),
00157 GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
00158 glTexParameteri(rt2->GetTextureTarget(),
00159 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00160 glTexParameteri(rt2->GetTextureTarget(),
00161 GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
00162 }
00163 }
00164 else if (!(rt2->IsRectangleTexture() || rt2->IsFloatTexture()))
00165 {
00166 if (rt2->IsTexture())
00167 {
00168 rt2->Bind();
00169 glTexParameteri(rt2->GetTextureTarget(),
00170 GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00171 glTexParameteri(rt2->GetTextureTarget(),
00172 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00173 }
00174
00175 if (rt2->IsDepthTexture())
00176 {
00177 rt2->BindDepth();
00178 glTexParameteri(rt2->GetTextureTarget(),
00179 GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00180 glTexParameteri(rt2->GetTextureTarget(),
00181 GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00182 }
00183 }
00184 }
00185
00186 if (rt2->IsDepthTexture())
00187 {
00188 fprintf(stderr,
00189 "\nPress the spacebar to toggle color / depth textures.\n");
00190 if (!rt2->IsTexture())
00191 bShowDepthTexture = true;
00192 }
00193 else
00194 {
00195 if (rt2->IsTexture())
00196 bShowDepthTexture = false;
00197 }
00198
00199 PrintGLerror("Create");
00200 return rt2;
00201 }
00202
00203
00204
00205
00206
00207 void DestroyRenderTexture(RenderTexture *rt2)
00208 {
00209 delete rt2;
00210 }
00211
00212
00213
00214
00215
00216 void Keyboard(unsigned char key, int x, int y)
00217 {
00218 switch(key)
00219 {
00220 case 27:
00221 case 'q':
00222 exit(0);
00223 break;
00224 case ' ':
00225 bShowDepthTexture = !bShowDepthTexture;
00226 break;
00227 case 13:
00228 ++g_currentString %= g_numModeTestStrings;
00229 dbg_printf("Changed to #%d = [%s]\n", g_currentString, g_modeTestStrings[g_currentString]);
00230 DestroyRenderTexture(rt);
00231 rt = CreateRenderTexture(g_modeTestStrings[g_currentString]);
00232 break;
00233 case 't':
00234 bTorusMotion = !bTorusMotion;
00235 break;
00236 case 'r':
00237 bRectMotion = !bRectMotion;
00238 break;
00239 default:
00240 return;
00241 }
00242 }
00243
00244
00245
00246
00247
00248 void Idle()
00249 {
00250
00251 if (!rt->IsDepthTexture())
00252 bShowDepthTexture = false;
00253
00254 if (bRectMotion) rectAngle += 1;
00255 if (bTorusMotion) torusAngle += 1;
00256 glutPostRedisplay();
00257 }
00258
00259
00260
00261
00262
00263 void Reshape(int w, int h)
00264 {
00265 if (h == 0) h = 1;
00266
00267 glViewport(0, 0, w, h);
00268
00269 glMatrixMode(GL_PROJECTION);
00270 glLoadIdentity();
00271
00272 gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 1, 5.0);
00273 }
00274
00275
00276
00277
00278
00279 void _Display()
00280 {
00281 if (rt->IsInitialized() && rt->BeginCapture())
00282 {
00283 if (rt->IsDoubleBuffered()) glDrawBuffer(GL_BACK);
00284 glMatrixMode(GL_MODELVIEW);
00285 glPushMatrix();
00286
00287 glRotatef(torusAngle, 1, 0, 0);
00288 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00289 glColor3f(1,1,0);
00290
00291 glutSolidTorus(0.25, 1, 32, 64);
00292
00293 glPopMatrix();
00294 PrintGLerror("RT Update");
00295
00296 rt->EndCapture();
00297 }
00298
00299 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00300 glColor3f(1, 1, 1);
00301 glMatrixMode(GL_MODELVIEW);
00302 glPushMatrix();
00303 glRotatef(rectAngle / 10, 0, 1, 0);
00304
00305 if(bShowDepthTexture && rt->IsDepthTexture())
00306 rt->BindDepth();
00307 else if (rt->IsTexture()) {
00308 rt->Bind();
00309 }
00310
00311 rt->EnableTextureTarget();
00312
00313 int maxS = rt->GetMaxS();
00314 int maxT = rt->GetMaxT();
00315
00316 glBegin(GL_QUADS);
00317 glTexCoord2f(0, 0); glVertex2f(-1, -1);
00318 glTexCoord2f(maxS, 0); glVertex2f( 1, -1);
00319 glTexCoord2f(maxS, maxT); glVertex2f( 1, 1);
00320 glTexCoord2f(0, maxT); glVertex2f(-1, 1);
00321 glEnd();
00322
00323 rt->DisableTextureTarget();
00324
00325 glPopMatrix();
00326
00327 PrintGLerror("display");
00328 glutSwapBuffers();
00329 }
00330
00331
00332
00333
00334
00335
00336
00337
00338 int main(int argc, char *argv[])
00339 {
00340 int argn = argc;
00341 glutInit(&argn, argv);
00342 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
00343 glutInitWindowPosition(50, 50);
00344 glutInitWindowSize(512, 512);
00345 glutCreateWindow("TestRenderTexture");
00346
00347 glutDisplayFunc(_Display);
00348 glutIdleFunc(Idle);
00349 glutReshapeFunc(Reshape);
00350 glutKeyboardFunc(Keyboard);
00351
00352 Reshape(512, 512);
00353 glMatrixMode(GL_MODELVIEW);
00354 glLoadIdentity();
00355 gluLookAt(0, 0, 2, 0, 0, 0, 0, 1, 0);
00356 glDisable(GL_LIGHTING);
00357 glEnable(GL_COLOR_MATERIAL);
00358 glEnable(GL_DEPTH_TEST);
00359 glClearColor(0.4, 0.6, 0.8, 1);
00360
00361
00362 rt = CreateRenderTexture(g_modeTestStrings[g_currentString]);
00363
00364 if (rt->IsInitialized() && rt->BeginCapture()) {
00365 rt->EndCapture();
00366 dbg_printf("Torus should also be shown.\n");
00367 } else {
00368 dbg_printf("No Torus init = %s\n", (rt->IsInitialized() ? "ok" : "NOT INITIALISED"));
00369 }
00370
00371 printf("Press Enter to change RenderTexture parameters.\n"
00372 "Press 'r' to toggle the rectangle's motion.\n"
00373 "Press 't' to toggle the torus' motion.\n");
00374
00375
00376 glutMainLoop();
00377 return 0;
00378 }