Problem with rendering to texture

Started by
1 comment, last by jocasa 15 years, 3 months ago
Hi, I'm trying to draw a simple triangle to a texture but it doesn't work. The triangle is seen, also the texture but it is empty, looks like nothing has been drawn to it. Maybe I'm doing something wrong with the texture coordinates, but i don't know. Can anyone help me? Here's the code. SDL is used to manage windows details. I'm programing in Linux. #include <SDL/SDL.h> #include <GL/gl.h> #include <GL/glu.h> #include <stdio.h> #include <stdlib.h> #include <math.h> GLuint textura; #define lado 1024 GLuint EmptyTexture() // Create An Empty Texture { GLuint txtnumber; // Texture ID unsigned int* data; // Stored Data data = (unsigned int*)new GLuint[((lado * lado)* 4 * sizeof(unsigned int))]; glGenTextures(1, &txtnumber); // Create 1 Texture glBindTexture(GL_TEXTURE_2D, txtnumber); // Bind The Texture glTexImage2D(GL_TEXTURE_2D, 0, 4, lado, lado, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); delete [] data; return txtnumber; // Return The Texture ID } static void draw_screen( void ) { glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity( ); glTranslatef(0.0,0.0,-1.0); glBegin(GL_TRIANGLES); glColor3f(1.0,1.0,0.0); glVertex2f(0.0,0.0); glColor3f(1.0,0.0,1.0); glVertex2f(0.5,0.0); glColor3f(0.0,1.0,1.0); glVertex2f(0.5,0.5); glEnd(); glBindTexture(GL_TEXTURE_2D, textura); glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, lado, lado, 0); glRotatef(25.0,0.,0.0,1.0); glBegin(GL_QUADS); glTexCoord2f(0.0,0.0); glVertex2f(-0.5,-0.5); glTexCoord2f(0.0,1.0); glVertex2f(-0.5,0.5); glTexCoord2f(1.0,1.0); glVertex2f(0.5,0.5); glTexCoord2f(1.0,0.0); glVertex2f(0.5,-0.5); glEnd(); SDL_GL_SwapBuffers( ); } static void setup_opengl( int width, int height ) { float ratio = (float) width / (float) height; glClearColor( 0, 0, 0, 0 ); glViewport( 0, 0, width, height ); glMatrixMode( GL_PROJECTION ); glLoadIdentity( ); gluPerspective( 60.0, ratio, 0.0, 1024.0 ); glEnable(GL_BLEND); glBlendFunc(GL_ONE,GL_ONE); glLineWidth(0.1); textura = EmptyTexture(); } static void handle_key_down( SDL_keysym* keysym ) { switch( keysym->sym ) { case SDLK_ESCAPE: SDL_Quit(); exit(0); break; case SDLK_SPACE: break; case SDLK_RETURN: break; case SDLK_RSHIFT: break; case SDLK_q: break; case SDLK_a: break; case SDLK_w: break; case SDLK_s: break; case SDLK_z: break; case SDLK_x: break; case SDLK_c: break; case SDLK_v: break; default: break; } } static void process_events( void ) { SDL_Event event; while( SDL_PollEvent( &event ) ) { switch( event.type ) { case SDL_KEYDOWN: handle_key_down( &event.key.keysym ); break; case SDL_QUIT: SDL_Quit(); exit(0); break; } } } int main( int argc, char* argv[] ) { const SDL_VideoInfo* info = NULL; int width = 0; int height = 0; int bpp = 0; int flags = 0; if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { fprintf( stderr, "Video initialization failed: %s\n", SDL_GetError( ) ); SDL_Quit(); exit(1); } info = SDL_GetVideoInfo( ); if( !info ) { fprintf( stderr, "Video query failed: %s\n", SDL_GetError( ) ); SDL_Quit(); exit(1); } width = 1270; height = 960; bpp = info->vfmt->BitsPerPixel; SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 ); SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 ); SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 ); SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 ); SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); flags = SDL_OPENGL /*| SDL_FULLSCREEN*/; if( SDL_SetVideoMode( lado, lado, bpp, flags ) == 0 ) { fprintf( stderr, "Video mode set failed: %s\n", SDL_GetError( ) ); SDL_Quit(); exit(1); } setup_opengl(lado, lado); while( 1 ) { process_events( ); draw_screen( ); } return 0; }
Advertisement
Where is the call to glEnable(GL_TEXTURE_2D) and glDisable(GL_TEXTURE_2D)?
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Yes V-man! Thanks for the advise. I've corrected the code and now it does work. See you!

This topic is closed to new replies.

Advertisement