OpenGL textures from PNG files using SDL_image

Hi

I am trying to load textures from png-Files using SDL_image. The problem
is, the output shows only the general color of the png, but not any
patterns on it (e.g. a green background with a black cross on it only
shows the background).

I use this code:

bool LoadGLTextures() {
bool status=false;
SDL_Surface *TextureImage[1];

if ( (TextureImage[0]=IMG_Load("tile1.png")) ) {
	status=true;
	
	glGenTextures(1, &texture[0]);
	glBindTexture(GL_TEXTURE_2D, texture[0]);
	glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->w, 

TextureImage[0]->h, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->pixels);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
if (TextureImage[0]) {
	SDL_FreeSurface(TextureImage[0]);
}
return status;

}

What am I doing wrong?

Please send the code you are using to draw the texture too…

/OlofOn Sun, 20 Feb 2005 14:45:14 +0100, Tobias Langner wrote:

Hi

I am trying to load textures from png-Files using SDL_image. The problem
is, the output shows only the general color of the png, but not any
patterns on it (e.g. a green background with a black cross on it only
shows the background).

I use this code:

bool LoadGLTextures() {
bool status=false;
SDL_Surface *TextureImage[1];

    if ( (TextureImage[0]=IMG_Load("tile1.png")) ) {
            status=true;

            glGenTextures(1, &texture[0]);
            glBindTexture(GL_TEXTURE_2D, texture[0]);
            glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->w,

TextureImage[0]->h, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->pixels);

            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    }
    if (TextureImage[0]) {
            SDL_FreeSurface(TextureImage[0]);
    }
    return status;

}

What am I doing wrong?


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Olof Bjarnason wrote:

Please send the code you are using to draw the texture too…

DrawGLScene() is used to draw the quad with the texture, InitGL is used
to initialize OpenGL. This is a modified nehe-example.

void DrawGLScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glTranslatef(0.0f, 0.0f, -10.0f);

glBindTexture( GL_TEXTURE_2D, texture[0] );
glBegin(GL_QUADS);
	glVertex3f(-1.0f, -1.0f, 0.0f);
	glVertex3f( 1.0f, -1.0f, 0.0f);
	glVertex3f( 1.0f,  1.0f, 0.0f);
	glVertex3f(-1.0f,  1.0f, 0.0f);
glEnd();

SDL_GL_SwapBuffers();

}

void InitGL(int width, int height) {
glViewport(0,0,width, height);
//Todo: load textures
if (!LoadGLTextures()) {
cout<<“Failure to load textures”<<endl;
exit(1);
}

glEnable(GL_TEXTURE_2D); //enable texture mapping
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //Black Background
glClearDepth(1.0); //enables clearing of deapth buffer
glDepthFunc(GL_LESS); //type of depth test
glEnable(GL_DEPTH_TEST); //enables depth testing
glShadeModel(GL_SMOOTH); //enables smooth color shading

glMatrixMode(GL_PROJECTION);
glLoadIdentity(); //Reste projection matrix

gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 0.1f, 100.0f); 

//calculate aspect ratio of the window
glMatrixMode(GL_MODELVIEW);
}> /Olof

On Sun, 20 Feb 2005 14:45:14 +0100, Tobias Langner <@Tobias_Langner> wrote:

Hi

I am trying to load textures from png-Files using SDL_image. The problem
is, the output shows only the general color of the png, but not any
patterns on it (e.g. a green background with a black cross on it only
shows the background).

I use this code:

bool LoadGLTextures() {
bool status=false;
SDL_Surface *TextureImage[1];

   if ( (TextureImage[0]=IMG_Load("tile1.png")) ) {
           status=true;

           glGenTextures(1, &texture[0]);
           glBindTexture(GL_TEXTURE_2D, texture[0]);
           glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->w,

TextureImage[0]->h, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->pixels);

           glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
           glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   }
   if (TextureImage[0]) {
           SDL_FreeSurface(TextureImage[0]);
   }
   return status;

}

What am I doing wrong?


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Hi,

Tobias Langner schrieb:

Hi

I am trying to load textures from png-Files using SDL_image. The problem
is, the output shows only the general color of the png, but not any
patterns on it (e.g. a green background with a black cross on it only
shows the background).

Check that you’re using glTexCoord2f correctly when drawing the texture.
The coordinates for glTexCoord2f should range from 0.0 to 1.0, so if
you’re drawing to a quad, be sure to use the texture coordinates (0,0),
(1,0), (0,1) and (1,1) for each of the corners. If you use the wrong
texture coordinates, a small (green) portion of the image may be
stretched over the whole polygon, making it appear as though the texture
were wholly green.

-Sebastian

Sebastian Beschke wrote:

Hi,

Tobias Langner schrieb:

Hi

I am trying to load textures from png-Files using SDL_image. The
problem is, the output shows only the general color of the png, but
not any patterns on it (e.g. a green background with a black cross on
it only shows the background).

Check that you’re using glTexCoord2f correctly when drawing the texture.
The coordinates for glTexCoord2f should range from 0.0 to 1.0, so if
you’re drawing to a quad, be sure to use the texture coordinates (0,0),
(1,0), (0,1) and (1,1) for each of the corners. If you use the wrong
texture coordinates, a small (green) portion of the image may be
stretched over the whole polygon, making it appear as though the texture
were wholly green.

-Sebastian

that did the trick - sorry for bothering you guys with such trivial
things. I’m still learning.

Thank’s for your help!

Tobias Langner wrote:

glBegin(GL_QUADS);
    glVertex3f(-1.0f, -1.0f, 0.0f);
    glVertex3f( 1.0f, -1.0f, 0.0f);
    glVertex3f( 1.0f,  1.0f, 0.0f);
    glVertex3f(-1.0f,  1.0f, 0.0f);
glEnd();

You aren’t setting any texture co-ordinates with glTexCoord* so each
vertex is getting the same texture co-ordinate. I assume the square is
being covered with one particular pixel from the image. Also, I’d
caution you against using the SDL version of NeHe tutorial 6. It uses
incorrect texture co-ordinates to work around the fact that the texture
has been constructed incorrectly.

Getting a texture from an SDL_Surface is more tricky than it sounds.
Check out these two excellent posts about it:

http://twomix.devolution.com/pipermail/sdl/2002-September/049064.html
http://twomix.devolution.com/pipermail/sdl/2002-September/049078.html

Please note, though, that the code in testgl.c isn’t perfect, because
the texture is constructed upside down. This is because OpenGL images
start at the bottom-left corner and SDL_Surfaces start at the top-left.
testgl.c works around this by using an upside-down orthographic
projection to flip the world upside-down so it looks right. My preferred
method is to turn the SDL_Surface upside-down before using it in
glTexImage2D so the rest of the program can use the normal OpenGL
conventions. I’ve attached my (public domain, C++) code for preparing
SDL_Surfaces for use in OpenGL textures. It’s based on testgl.c. I’m not
100% certain that’s it’s correct, because I’m not sure if I’m allowed to
use memcpy while the surface is locked (I’m not certain if I have to
lock a software surface, either).

It’s used like this:

SDL_Surface *image = IMG_Load(filename);
if (!image) {
throw runtime_error(string("Error loading image ") + filename);
}
image = prepGLTexture(image);
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_RGBA,
GL_UNSIGNED_BYTE, image->pixels);
SDL_FreeSurface(image);–
Jon
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed…
Name: sdlgltex.cpp
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20050220/551fc61c/attachment.txt
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed…
Name: sdlgltex.h
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20050220/551fc61c/attachment.asc