Jump to content
  • Advertisement
Sign in to follow this  
jocasa

OpenGL Problem with rendering to texture

This topic is 3453 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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; }

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!