SOIL Loading image but doesn't get rendered

#1

Hey, I’m relativley new to OpenGL and having a problem: I’m loading a texture and don’t get any error, and the Soil last result says that it loaded my file as an OpenGL texture, but it doesn’t get displayed… Here’s my renderer for the texture

    void Sprite::Render() {
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, texture.getID());
	glPushMatrix();
	glLoadIdentity();

	// translate
	glTranslatef(xLoc, yLoc, 0);

	// Actually Render the Sprite
	glColor4f(1, 1, 1, 1); 

	glBegin(GL_QUADS);
	{
		glTexCoord2f(0, 0);		glVertex2i(-texture.getWidth() / 2, -texture.getHeight() / 2);
		glTexCoord2f(1, 0);		glVertex2i(texture.getWidth() / 2, -texture.getHeight() / 2);
		glTexCoord2f(1, 1);		glVertex2i(texture.getWidth() / 2, texture.getHeight() / 2);
		glTexCoord2f(0, 1);		glVertex2i(-texture.getWidth() / 2, texture.getHeight() / 2);
	};
	glEnd();

	glPopMatrix();

	glBindTexture(GL_TEXTURE_2D, 0);
    }

If anything else is needed, I’ll provide it

#2

I experienced a similar problem where a rectangle would render, but it would be all black. My problem was that I forgot to specify the texture parameters after loading the images via SOIL.

textureHandleArray = new GLuint[1];

int imageWidth, imageHeight;
unsigned char* image = SOIL_load_image("res/my_picture.png", &imageWidth, &imageHeight, 0, SOIL_LOAD_AUTO);

glGenTextures(1, &textureHandleArray[0]);
glBindTexture(GL_TEXTURE_2D, textureHandleArray[0]);
//Texture Parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageWidth, imageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);

SOIL_free_image_data(image);

When you mean loaded your file as an OpenGL texture, are you using
(SOIL_load_OGL_texture) and if so, could you also show the code that loads the image file?
Also, make sure your path to your image is correct.

#3

Thanks, here is the way I load the image:
#include “Texture.h”

Texture::Texture() {
	id = -1;
}

Texture::Texture(int _id) {
	id = _id;
	if (!GetTextureParams())
	{
		cout << "Error loading image with ID: " << id << "; (ERROR 2)" << endl;
	}
}

Texture::Texture(string path) {
	id = SOIL_load_OGL_texture(path.c_str(), SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y);
	if (!GetTextureParams()) {
		cout << "ERROR: An error occured whilst loading the image: " << "; (ERROR 2)" << path << endl;
		cout << "ERROR DETAILS: " << SOIL_last_result() << " (LAST SOIL RESULT);";
	}
}

// Getters
int Texture::getID()     { return id;     }
int Texture::getWidth()  { return width;  }
int Texture::getHeight() { return height; }

bool Texture::GetTextureParams() {
	if (id > 0) {
		int mipLevel = 0;
		glBindTexture(GL_TEXTURE_2D, id);
		glGetTexLevelParameteriv(GL_TEXTURE_2D, mipLevel, GL_TEXTURE_WIDTH, &width);
		glGetTexLevelParameteriv(GL_TEXTURE_2D, mipLevel, GL_TEXTURE_HEIGHT, &height);
		return true;
	}

	return false;
}