Generating texture

Hello guys
I'm trying to load a PNG image to use as texture,
but when I compile the sdl window closes.
I'm sure the error is in the function of generating the png texture,
because when i don´t use this function, the sdl window does not close.
So debugging using cout i found that the cout above glTexImage2D function, shows in console, but the cout in below of glTexImage2D does not work.
how can I solve this?
Does not reading this image?

hear is the function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "Texture.h"
#include <iostream>
Texture::Texture() {}

 unsigned int Texture::geraTextureDePNG(const std::string& path)
{

    unsigned int _tex;
    SDL_Surface*  surface = IMG_Load(path.c_str());

    int _imgW = surface->w; //tamanhos.
    int _imgH = surface->h;

    glGenTextures(1, &_tex);  
    glBindTexture(GL_TEXTURE_2D, _tex); 
std::cout<<"texture\n"; // this show in console

    glTexImage2D(GL_TEXTURE_2D, 0, 4, surface->w, surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);

    std::cout<<"texture2";//this cout does not showing in the console

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    SDL_FreeSurface(surface); //destrói a imagem carregada.
    return _tex;
}
Texture::~Texture()
{
    //termina o uso da textura.
    glBindTexture(GL_TEXTURE_2D, 0);

}
void Texture::TexturedCube(unsigned int _tex)
{

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
	glLoadIdentity ( );
	glPushMatrix();
	glTranslatef ( 0.0, 0.0, -5.0 );
	glRotatef ( xrot, 1.0, 0.0, 0.0 );
	glRotatef ( yrot, 0.0, 1.0, 0.0 );
	glRotatef ( zrot, 0.0, 0.0, 1.0 );

	// define qual das texturas usar
	glBindTexture ( GL_TEXTURE_2D, _tex );

	glBegin ( GL_QUADS );
		// Front Face
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
		// Back Face
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
		// Top Face
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
		// Bottom Face
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
		// Right face
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
		// Left Face
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
	glEnd();

	glPopMatrix();
	xrot+=0.3f;
	yrot+=0.2f;
	zrot+=0.4f;
}
glTexImage2D(GL_TEXTURE_2D, 0, 4, surface->w, surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
Strange you are using "4" for the 3rd argument. I guess it ends up being GL_RGBA anyway but it's easier to read if you specify that directly.

I would check that SDL actually sees the image.

1
2
3
4
5
 
SDL_Surface*  surface = IMG_Load(path.c_str());

if(!surface)
    cout << "Useful Info here << endl;  


I guess make sure you have glEnable(GL_TEXTURE_2D); set up somewhere too.
I tried this:

1
2
3
4
if(!surface) {
    printf("IMG_Load: %s\n", IMG_GetError());
    // handle error
}


but it does not enter the if, it means that SDL sees the image.
So what does not work?
I already put the glEnable (GL_TEXTURE_2D);
that was missing.
closed account (10X9216C)
Your code should work. I'm almost sure your surface is null.

Test something like surface->w and surface->h. Check if its not zero. Of course, if they are zero you will see your program blow up again.
¬¬
closed account (N36fSL3A)
An image loaded into an SDL surface is not always RGBA. It's usually something like RGB, or even BGR.

Chances are your image is NULL as myesolar said.
Last edited on
already try this
std::cout<<surface->w;
std::cout<<surface->h;
returns me 256 and 256
closed account (10X9216C)
Yes. As Fredbill said. You cant suppose your image have always 4 components. I'm sure you would like to have images with 3 or 4 components (often useful, but you can have 2 and even 1 component.).

PS: Your texture loader also draw rectangles? Weird....
Ok already solved the problem thanks to my teacher !
Problem was that i was reading an image that was RGB and the code was reading an rgba image.
Thanks for the replies.
You guys rock!
Yes my texture loader draw rectangles
because I didn´t want to create a class and a header only for that
closed account (N36fSL3A)
romulosd wrote:
Ok already solved the problem thanks to my teacher !
Problem was that i was reading an image that was RGB and the code was reading an rgba image.
I wrote:
An image loaded into an SDL surface is not always RGBA. It's usually something like RGB, or even BGR.
Last edited on
Topic archived. No new replies allowed.