Results 1 to 3 of 3

Thread: Wrapping a OpenGL Texture from a SDL_Surface does not work

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2007
    Posts
    2

    Wrapping a OpenGL Texture from a SDL_Surface does not work

    Hi,

    i tried to wrap a opengl texture from a SDL_Surface that i loaded from a png-file. First i tried on my own but that didn't work. Now i use a function i found on the web. Here my complete code:
    Code :
    #include <cstdlib>
    #include <iostream>
    using namespace std;
     
    #include <SDL.h>
    #include <SDL_image.h>
    #include <GL/gl.h>
    #include <GL/glu.h>
     
    uint32_t PowerOfTwo(uint32_t size) {
    	int value = 1;
    	while(value < size) {
    		value <<= 1;
    	}
    	return value;
    }
     
    GLuint LoadTexture(const char *filename, GLfloat* texture_coords) {
    	GLuint texture;
    	int w, h;
    	SDL_Surface *image;
    	SDL_Surface* surface = IMG_Load(filename);
    	SDL_Rect area;
    	Uint32 saved_flags;
    	Uint8  saved_alpha;
     
    	/* Use the surface width and height expanded to powers of 2 */
    	w = PowerOfTwo(surface->w);
    	h = PowerOfTwo(surface->h);
    	texture_coords[0] = 0.0f;         /* Min X */
    	texture_coords[1] = 0.0f;         /* Min Y */
    	texture_coords[2] = (GLfloat)surface->w / w;  /* Max X */
    	texture_coords[3] = (GLfloat)surface->h / h;  /* Max Y */
     
    	image = SDL_CreateRGBSurface(
    		SDL_SWSURFACE,
    		w, h,
    		32,
    	#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
    		0x000000FF,
    		0x0000FF00,
    		0x00FF0000,
    		0xFF000000);
    	#else
    		0xFF000000,
    		0x00FF0000,
    		0x0000FF00,
    		0x000000FF);
    	#endif
     
    	if ( image == NULL ) {
    	   return 0;
    	}
     
    	/* Save the alpha blending attributes */
    	saved_flags = surface->flags&amp;(SDL_SRCALPHA|SDL_RLEACCELOK);
    	saved_alpha = surface->format->alpha;
    	if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
    	   SDL_SetAlpha(surface, 0, 0);
    	}
     
    	/* Copy the surface into the GL texture image */
    	area.x = 0;
    	area.y = 0;
    	area.w = surface->w;
    	area.h = surface->h;
    	SDL_BlitSurface(surface, &amp;area, image, &amp;area);
     
    	/* Restore the alpha blending attributes */
    	if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
    	   SDL_SetAlpha(image, saved_flags, saved_alpha);
    	}
     
    	/* Create an OpenGL texture for the image */
    	glGenTextures(1, &amp;texture);
    	glBindTexture(GL_TEXTURE_2D, texture);
     
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);/**/
     
    	/*glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    	gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, image->w, image->h, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);/**/
     
    	SDL_FreeSurface(image); /* No longer needed */
     
    	return texture;
    }
     
     
    int main()
    {
    	Uint32 Width = 640;
    	Uint32 Height = 480;
    	SDL_Init(SDL_INIT_VIDEO);
    	SDL_SetVideoMode(Width, Height, 0, SDL_OPENGL);
     
    	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 );
     
    	glShadeModel(GL_SMOOTH);
    	glClearColor(1, 1, 1, 0);
    	glClearDepth(1.0f);
     
    	glEnable(GL_TEXTURE_2D);
    	glEnable(GL_DEPTH_TEST);
     
    	glDepthFunc(GL_LEQUAL);
     
     
    	glEnable(GL_BLEND);
    	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    	glViewport(0, 0, Width, Height);
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
     
    	gluOrtho2D(0, Width, Height, 0);
     
    	GLfloat text_png_coords[4];
    	GLuint text_png = LoadTexture("test.png", text_png_coords);
    	if(text_png == 0)
    	{
    		cout << "Could not load texture!" << endl;
    		exit(0);
    	}
     
     
    	bool quit = false;
    	while (!quit)
    	{
    		SDL_Event event;
    		while(SDL_PollEvent(&amp;event))
    		{
    			if(event.type == SDL_QUIT)
    				quit = true;
    			if(event.type == SDL_KEYDOWN)
    				if(event.key.keysym.sym == SDLK_ESCAPE)
    					quit = true;
     
    		}
     
    		glMatrixMode(GL_MODELVIEW);
     
    		//glLoadIdentity();
     
    		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
    		glLoadIdentity();
     
    		glBegin(GL_LINES);
    			glColor3f(1.0f, 0.0f, 0.0f);
    			glVertex2i(0, Height/2);
    			glVertex2i(Width, Height/2);
    			glVertex2i(Width/2, 0);
    			glVertex2i(Width/2, Height);
    		glEnd();
     
    		glBindTexture(GL_TEXTURE_2D, text_png);
    		glBegin(GL_QUADS);
    			glColor3f(1.0f, 1.0f, 1.0f);
     
    			glTexCoord2f(0.0f, 0.0f);
    			glVertex2i(100, 100);
     
    			glTexCoord2f(text_png_coords[2], 0.0f);
    			glVertex2i(150, 100);
     
    			glTexCoord2f(text_png_coords[2], text_png_coords[3]);
    			glVertex2i(150, 150);
     
    			glTexCoord2f(0.0f, text_png_coords[3]);
    			glVertex2i(100, 150);
    		glEnd();
     
    		glFinish();
     
    		SDL_GL_SwapBuffers();
    		SDL_Delay(10);
    	}
     
    	glDeleteTextures(1, &amp;text_png);
    	SDL_Quit();
     
    	exit(0);
    }
    It draws a red cross on my screen and then shows the texture i loaded. My problem now is that the texture is shown without issues. But from the point i call glTexImage2D at the lines i paint arent shown anymore they just disappear! And i have no idea why. If i comment the call of glTexImage2D out of source it works the lines are drawn (the texture is missing, logicaly). Can you help me? I allready posted in many other forum but never got any proper help.

    Greetings, Prophet05

    EDIT: Here the png-file i loaded: http://prophet.underground-irc.net/test.png

  2. #2
    Senior Member Regular Contributor
    Join Date
    Jul 2005
    Location
    Berlin, Germany
    Posts
    188

    Re: Wrapping a OpenGL Texture from a SDL_Surface does not work

    You never disable texturing, so after loading the texture, not only is your quad textured, but so are your lines. Commenting out the glTexImage2D call helps because without the call the texture isn't initialized and texturing is disabled automatically.

    Add glEnable(GL_TEXTURE_2D) and glDisable(GL_TEXTURE_2D) around your quad draw code to fix the problem.
    355/113 -- Not the famous irrational number PI, but an incredible simulation!

  3. #3
    Junior Member Newbie
    Join Date
    Jun 2007
    Posts
    2

    Re: Wrapping a OpenGL Texture from a SDL_Surface does not work

    Thank you! That solved my problem.

Similar Threads

  1. Replies: 2
    Last Post: 11-30-2014, 10:04 PM
  2. Texture Wrapping
    By Manel Goucha in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 11-23-2011, 02:02 AM
  3. Video OpenGL texture with libvlc (on SDL_Surface)
    By Paul Iomom in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 05-07-2010, 08:07 AM
  4. Equivalent in OpenGL of DirectX texture wrapping ?
    By Zeross in forum OpenGL: Advanced Coding
    Replies: 6
    Last Post: 07-30-2004, 03:50 AM
  5. Texture Wrapping
    By Andrea Doimo in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 04-18-2001, 08:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean