Skip to main content
GameDev.net gamedev.net
🔒 Locked

Texture appearing black

Started by MrPickle Oct 19, 2008 at 11:30 AM 5 replies 9.3k views
Original Post
MrPickle
MrPickle
I am using SDL to load my texture but it is appearing as a black quad and I don't know why:
//Texture.h

#pragma once
#include <SDL/SDL_image.h>
#include <GL/glut.h>
#include <string>
#pragma comment(lib, "SDL_image.lib")

class Texture {
	public:
		unsigned int Width, Height, Image;
		Texture(std::string Filename);
};
//Texture.cpp
#include "Texture.h"

Texture::Texture(std::string Filename) {
	Width = Height = Image = 0;
	SDL_Surface* Surface;
	SDL_Surface* Blank;
	Surface = IMG_Load(Filename.c_str());
	if(!Surface)
		throw std::runtime_error("Unable to load \"" + Filename + "\": " + IMG_GetError());

	Blank = SDL_CreateRGBSurface(SDL_SWSURFACE, Surface->w, Surface->h, 32,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
    0x000000FF,
    0x0000FF00,
    0x00FF0000,
    0xFF000000
#else
    0xFF000000,
    0x00FF0000,
    0x0000FF00,
    0x000000FF
#endif
	);

	SDL_Rect Area;
	Area.x = Area.y = 0;
	Area.w = Surface->w;
	Area.h = Surface->h;
	SDL_BlitSurface(Surface, &Area, Blank, &Area);

	unsigned int numColours = Blank->format->BytesPerPixel;
	GLenum Format;
	if(numColours == 4) {
		if(Blank->format->Rmask == 0x000000FF)
			Format = GL_RGBA;
		else
			Format = GL_BGRA_EXT;
	} else if(numColours == 3) {
		if(Blank->format->Rmask == 0x000000FF)
			Format = GL_RGB;
		else
			Format = GL_BGR_EXT;
	} else
		throw std::runtime_error("Image isn't truecolour");

	glGenTextures(1, &Image);
	glBindTexture(GL_TEXTURE_2D, Image);
	glTexImage2D(GL_TEXTURE_2D, 0, numColours, Blank->w, Blank->h, 0, Format, GL_UNSIGNED_BYTE, Blank->pixels);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_DECAL);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_DECAL);

	Width = Blank->w;
	Height = Blank->h;
	SDL_FreeSurface(Surface);
	SDL_FreeSurface(Blank);
}
I am drawing it like so:

	Texture Image("C:/Users/James/Desktop/Obj/test.bmp");
//...
		glEnable(GL_TEXTURE_2D);
		glBindTexture(GL_TEXTURE_2D, Image.Image);
		glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f); glVertex2f(544.0f, 0.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex2f(544.0f, (float)Image.Height);
		glTexCoord2f(1.0f, 1.0f); glVertex2f(544.0f + (float)Image.Width, (float)Image.Height);
		glTexCoord2f(1.0f, 0.0f); glVertex2f(544.0f + (float)Image.Width, 0.0f);
		glEnd();
Here to learn :)
Graz
Graz
GL_DECAL is not a valid setting for the GL_TEXTURE_MIN_FILTER or GL_TEXTURE_MAG_FILTER parameters. They should be set to GL_NEAREST or GL_LINEAR. GL_TEXTURE_MIN_FILTER could also be set to one of the mipmap settings.
swiftcoder
swiftcoder
Quote:
Original post by Graz
GL_DECAL is not a valid setting for the GL_TEXTURE_MIN_FILTER or GL_TEXTURE_MAG_FILTER parameters. They should be set to GL_NEAREST or GL_LINEAR. GL_TEXTURE_MIN_FILTER could also be set to one of the mipmap settings.
This is a great example of why you should check the OpenGL error state at many points in your program. I have a setup like this:
void CheckGLError() {	GLuint err = glGetError();	if (err != GL_NO_ERROR)		std::cerr << std::hex << err << std::endl;}#ifndef NDEBUG#	define CHECK_GL_ERROR CheckGLError();#else#	define CHECK_GL_ERROR#endif

Then just place CHECK_GL_ERROR after every OpenGL operation. This will let you check everything in a bedug build, and the checks will magically disappear in release builds.

In your case the error should be 0x501, or GL_INVALID_VALUE.
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
MrPickle
MrPickle
Quote:
Original post by Graz
GL_DECAL is not a valid setting for the GL_TEXTURE_MIN_FILTER or GL_TEXTURE_MAG_FILTER parameters. They should be set to GL_NEAREST or GL_LINEAR. GL_TEXTURE_MIN_FILTER could also be set to one of the mipmap settings.


It was GL_LINEAR and I changed I was just changing it to things in the off chance that it would work.

It still doesn't work with GL_LINEAR or GL_NEAREST
Here to learn :)
Graz
Graz
Randomly changing things in a program is not a generally accepted method of debugging.

If your texture environment mode is set to GL_MODULATE (the default) and the color of the primitive is black (or your lighting is broken), then the poly will render in black. Try setting the environment mode to GL_REPLACE (glTexEnvi(GL_TEXTURE_2D, GL_TEXURE_ENV_MODE, GL_REPLACE)).
MrPickle
MrPickle
I am getting "Undeclared identifier GL_TEXTURE_ENV_MODE" I have glut.h included.
Here to learn :)
V-man
V-man
That would have to be
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
which sets the texture unit state.
http://www.opengl.org/wiki/index.php/Common_Mistakes#Creating_a_Texture_.232.2C_glTexEnvi

GL_TEXTURE_ENV_MODE is defined in gl.h since this is a OpenGL token.

Also, if you want to know what tokens each GL function takes, have a look at the spec
http://www.opengl.org/documentation/specs/

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.