Jump to content
  • Advertisement
Sign in to follow this  
starfleetrp

Image Troubles

This topic is 4406 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I seem to be experiencing some very weird problems when I load an image. I am rendering some squares to the screen like: glBegin(GL_QUADS); // Draw Our Quad glTexCoord2f(0.0f, 1.0f); glVertex3f(-10.0f, 10.0f, 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f( 10.0f, 10.0f, 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f( 10.0f, -10.0f, 0.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f(-10.0f, -10.0f, 0.0f); glEnd(); And since I do not have an image bound it appears as a white square. All good. However when I try to load an image 1 of 2 things happens. I of course bind the image first.... 1) When I load the image as a gluBuild2DMipmaps nothing gets rendered to the screen its just the clear color black (i've also tested it with a clear color of red) but the same as before nothing appears to be rendered 2) If i try to load it as a glTexImage2D no images appears just the white squares do, just as if I did not bind a texture. Nothing else in my code has changed except those two lines of code. I have no idea what is wrong since I pretty much used the exact same code as the tutorial for loading a TGA and it worked perfect.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by starfleetrp
2) If i try to load it as a glTexImage2D no images appears just the white squares do, just as if I did not bind a texture.

You can try:
1) Check if the image is power-of-two
2) Post your glTexImage2D call and your source image's data type

Share this post


Link to post
Share on other sites
glTexImage2D (GL_TEXTURE_2D, 0, texture->channels, texture->sizeX, texture->sizeY, 0, textureType, GL_UNSIGNED_BYTE, texture->data);
The image is a TGA and I cant check if its to the power of 2 since I can not make/read them. However it is the same image as from the tutorial.

Share this post


Link to post
Share on other sites
Quote:
Original post by starfleetrp
glTexImage2D (GL_TEXTURE_2D, 0, texture->channels, texture->sizeX, texture->sizeY, 0, textureType, GL_UNSIGNED_BYTE, texture->data);
The image is a TGA and I cant check if its to the power of 2 since I can not make/read them.
You are passing the dimensions right there in your glTexImage2D call. Throw in a break point and check the values.
Quote:
Original post by starfleetrp
However it is the same image as from the tutorial.
There are (too) many tutorials out there. Which one are you talking about?

It would also help to see all of your texture creation code and possibly your whole rendering loop.

Share this post


Link to post
Share on other sites
Here is my texture creation/usage class its kinda hacked together for now :(

#include "StdAfx.h"
#include ".\textureresource.h"
#include "Prerefequites.h"
#include "Log.h"

TextureResource::TextureResource(void) : textureBound(false),
resourceID(0),
Resources()
{
mType = RESOURCE_TEXTURE;
}
//------------------------------------------------------------------------------------
TextureResource::~TextureResource(void)
{
_clear();
}
//------------------------------------------------------------------------------------
bool TextureResource::load(const std::string& name)
{
std::string extension;
Texture *texture = NULL;

if (textureBound)
{
// This resource is already in use
Log::GetSingleton().addRecord("Texture resource already has a texture <b>" + mName + "</b> bound to it. You were trying to bind: <b>" + name + "</b>", LOG_ERROR);

return false;
}


if ( name.length() <= 4 )
{
// File name is too short or was not entered
// Needs to be atleast 5 characters in length "a.tga"
Log::GetSingleton().addRecord("Texture resource can not load a file that has a name less that 5 characters. You were trying to load: <b>" + name + "</b>", LOG_ERROR);
return false;
}


// Get the extension of the file (last 3 characters)
extension = name.substr(name.length() - 3);

//Load the image
if (extension == "tga")
{
texture = loadTGA(name);
}
else
{
// Invalid file extension
Log::GetSingleton().addRecord("Could not load texture: Unknown extention. You were trying to load: <b>" + name + "</b>", LOG_ERROR);
return false;
}


if(texture == NULL)
{
Log::GetSingleton().addRecord("Could not load texture. You were trying to load: <b>" + name + "</b>", LOG_ERROR);
return false;
}


// Add texture to video memory
glGenTextures(1, &resourceID);
glBindTexture(GL_TEXTURE_2D, resourceID);
int textureType = GL_RGB;
if(texture->channels == 4) textureType = GL_RGBA;
//GLenum errCode = gluBuild2DMipmaps(GL_TEXTURE_2D, texture->channels, texture->sizeX, texture->sizeY, textureType, GL_UNSIGNED_BYTE, texture->data);
glTexImage2D (GL_TEXTURE_2D, 0, texture->channels, texture->sizeX, texture->sizeY, 0, textureType, GL_UNSIGNED_BYTE, texture->data);
/*if (errCode != 0){
//Log::GetSingletonPtr()->addRecord(gluErrorString(errCode), LOG_ERROR);
Log::GetSingletonPtr()->addRecord("gluBuild2DMipmaps error - TextureResource.cpp", LOG_ERROR);
//MessageBox(NULL,"glBuild2dMipmaps error","",MB_OK);
}*/

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

if (texture)
{
if (texture->data)
{
free(texture->data);
}
free(texture);
}

Log::GetSingleton().addRecord("Texture loaded. <b>" + name + "</b>", LOG_SUCCESS);

textureBound = true;
mName = name;

return true;
}
//------------------------------------------------------------------------------------
Texture* TextureResource::loadTGA(const std::string& name)
{
// From APRON tutorial - replace at later date if need arises
Texture *texture = NULL;
FILE *pFile = NULL;
unsigned short width = 0;
unsigned short height = 0;
byte length = 0;
byte imgType = 0;
byte bits = 0;
int channels = 0;
int stride = 0;
int i = 0;


if((pFile = fopen(name.c_str(), "rb")) == NULL)
{
//MessageBox(NULL, "Error loading tga file.", "ERROR", MB_OK);
Log::GetSingleton().addRecord("Error loading TGA. File not found: <b>" + name + "</b>", LOG_ERROR);
return NULL;
}

texture = (Texture*)malloc(sizeof(Texture));

fread(&length, sizeof(byte), 1, pFile);

fseek(pFile,1,SEEK_CUR);

fread(&imgType, sizeof(byte), 1, pFile);

fseek(pFile, 9, SEEK_CUR);

fread(&width, sizeof(WORD), 1, pFile);
fread(&height, sizeof(WORD), 1, pFile);
fread(&bits, sizeof(byte), 1, pFile);

fseek(pFile, length + 1, SEEK_CUR);

if(imgType != TGA_RLE)
{
// Check for 24 or 32 Bit
if(bits == 24 || bits == 32)
{

channels = bits / 8;
stride = channels * width;
texture->data = new unsigned char[stride * height];

for(int y = 0; y < height; y++)
{
unsigned char *pLine = &(texture->data[stride * y]);

fread(pLine, stride, 1, pFile);

for(i = 0; i < stride; i += channels)
{
int temp = pLine;
pLine = pLine[i + 2];
pLine[i + 2] = temp;
}
}
}

// Check for 16 Bit
else if(bits == 16)
{
unsigned short pixels = 0;
int r=0, g=0, b=0;

channels = 3;
stride = channels * width;
texture->data = new unsigned char[stride * height];

for(int i = 0; i < width*height; i++)
{
fread(&pixels, sizeof(unsigned short), 1, pFile);

b = (pixels & 0x1f) << 3;
g = ((pixels >> 5) & 0x1f) << 3;
r = ((pixels >> 10) & 0x1f) << 3;

texture->data[i * 3 + 0] = r;
texture->data[i * 3 + 1] = g;
texture->data[i * 3 + 2] = b;
}
}

else
return NULL;
}

else
{

byte rleID = 0;
int colorsRead = 0;
channels = bits / 8;
stride = channels * width;

texture->data = new unsigned char[stride * height];
byte *pColors = new byte [channels];

while(i < width*height)
{

fread(&rleID, sizeof(byte), 1, pFile);


if(rleID < 128)
{
rleID++;

while(rleID)
{
fread(pColors, sizeof(byte) * channels, 1, pFile);

texture->data[colorsRead + 0] = pColors[2];
texture->data[colorsRead + 1] = pColors[1];
texture->data[colorsRead + 2] = pColors[0];

if(bits == 32) texture->data[colorsRead + 3] = pColors[3];

i++;
rleID--;
colorsRead += channels;
}
}

else
{
rleID -= 127;

fread(pColors, sizeof(byte) * channels, 1, pFile);

while(rleID)
{
texture->data[colorsRead + 0] = pColors[2];
texture->data[colorsRead + 1] = pColors[1];
texture->data[colorsRead + 2] = pColors[0];

if(bits == 32) texture->data[colorsRead + 3] = pColors[3];

i++;
rleID--;
colorsRead += channels;
}
}
}
delete[] pColors;
}

fclose(pFile);


texture->channels = channels;
texture->sizeX = width;
texture->sizeY = height;

return texture;
}
//------------------------------------------------------------------------------------
void TextureResource::_clear()
{
if (textureBound)
{
// Remove this texture from video memory
glDeleteTextures(1, &resourceID);

Log::GetSingleton().addRecord("Unloaed texture <b>" + mName + "</b>", LOG_SUCCESS);
}
textureBound = false;
}
//------------------------------------------------------------------------------------
void TextureResource::render()
{
// Change to our texture so it can be rendered
glBindTexture(GL_TEXTURE_2D, resourceID); // ( CHANGE )
if(resourceID == 0)
Log::GetSingleton().addRecord("Tried to call TextureResource::render() when not resourceID was defined.", LOG_ERROR);
}



I beleive that it was based off of the apron tutorials (I forget since I looked at a few but im almost 90% sure)

Now my rendering loop is pretty much

(*test_tga)->render(); // Binds the texture
for (each node) // Calls node::render or something like that for testing which does the below
{
glLoadIdentity();
glTranslatef(mDerivedPosition.x, mDerivedPosition.y,-500);// mDerivedPosition.z);
glRotatef(angle, axis.x, axis.y, axis.z);
ResourceManager::GetSingleton().getResource("images\\test.tga")->render();
glBegin(GL_QUADS); // Draw Our Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-10.0f, 10.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 10.0f, 10.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 10.0f, -10.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-10.0f, -10.0f, 0.0f);
glEnd();
}


Share this post


Link to post
Share on other sites
Okay, I noticed a couple things that might be problematic.
Quote:
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);
The magnification filter can't accept any mipmapping filter types, so use either GL_NEAREST or GL_LINEAR for that. The minification filter should only use a mipmapping filter type if you are using mipmaps (creating the entire mipmap chain). So if you use gluBuild2DMipmaps (which creates the mipmap chain for you) you should use one of the GL_*_MIPMAP_* filter types but if you use glTexImage2D to create just the 0th level of the mipmap chain (ie: no mipmapping), you have to use GL_NEAREST or GL_LINEAR.

The other thing is I don't see you enabling texturing anywhere (although I may have overlooked it). Make sure that it is enabled when you want to render with texturing and disabled when you want to render without texturing.

Share this post


Link to post
Share on other sites
Ok I am now experiencing a different problem. Now nothing appears on the screen, not even the white squares. glEnable(GL_TEXTURE_2D); is in my initGl() section.

The only thing that I have changed is:
Quote:

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);

to
Quote:

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!