Jump to content
  • Advertisement
Sign in to follow this  
Vectormycalculus

OpenGL Loading a Texture File

This topic is 3966 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

how do i load a texture file? if (!m_textureOne->Load("rock.tga")) return false; included in my CGfxOpenGL it does not seem to properly load it from that source. The targa file is below. #include <iostream> #include "CTargaImage.h" CTargaImage::CTargaImage() : m_pImageData(NULL) { } CTargaImage::~CTargaImage() { Release(); } void CTargaImage::SwapRedBlue() { switch (m_colorDepth) { case 32: { unsigned char temp; rgba_t* source = (rgba_t*)m_pImageData; for (int pixel = 0; pixel < (m_width * m_height); ++pixel) { temp = source[pixel].b; source[pixel].b = source[pixel].r; source[pixel].r = temp; } } break; case 24: { unsigned char temp; rgb_t* source = (rgb_t*)m_pImageData; for (int pixel = 0; pixel < (m_width * m_height); ++pixel) { temp = source[pixel].b; source[pixel].b = source[pixel].r; source[pixel].r = temp; } } break; default: // ignore other color depths break; } } bool CTargaImage::Load(const char *filename) { FILE *pFile = fopen(filename, "rb"); if (!pFile) return false; tgaheader_t tgaHeader; // read the TGA header fread(&tgaHeader, 1, sizeof(tgaheader_t), pFile); // see if the image type is one that we support (RGB, RGB RLE, GRAYSCALE, GRAYSCALE RLE) if ( ((tgaHeader.imageTypeCode != TGA_RGB) && (tgaHeader.imageTypeCode != TGA_GRAYSCALE) && (tgaHeader.imageTypeCode != TGA_RGB_RLE) && (tgaHeader.imageTypeCode != TGA_GRAYSCALE_RLE)) || tgaHeader.colorMapType != 0) { fclose(pFile); return false; } // get image width and height m_width = tgaHeader.width; m_height = tgaHeader.height; // colormode -> 3 = BGR, 4 = BGRA int colorMode = tgaHeader.bpp / 8; // we don't handle less than 24 bit if (colorMode < 3) { fclose(pFile); return false; } m_imageSize = m_width * m_height * colorMode; // allocate memory for TGA image data m_pImageData = new unsigned char[m_imageSize]; // skip past the id if there is one if (tgaHeader.idLength > 0) fseek(pFile, SEEK_CUR, tgaHeader.idLength); // read image data if (tgaHeader.imageTypeCode == TGA_RGB || tgaHeader.imageTypeCode == TGA_GRAYSCALE) { fread(m_pImageData, 1, m_imageSize, pFile); } else { // this is an RLE compressed image unsigned char id; unsigned char length; rgba_t color = { 0, 0, 0, 0 }; unsigned int i = 0; while (i < m_imageSize) { id = fgetc(pFile); // see if this is run length data if (id >= 128)// & 0x80) { // find the run length length = (unsigned char)(id - 127); // next 3 (or 4) bytes are the repeated values color.b = (unsigned char)fgetc(pFile); color.g = (unsigned char)fgetc(pFile); color.r = (unsigned char)fgetc(pFile); if (colorMode == 4) color.a = (unsigned char)fgetc(pFile); // save everything in this run while (length > 0) { m_pImageData[i++] = color.b; m_pImageData[i++] = color.g; m_pImageData[i++] = color.r; if (colorMode == 4) m_pImageData[i++] = color.a; --length; } } else { // the number of non RLE pixels length = (unsigned char)(id + 1); while (length > 0) { color.b = (unsigned char)fgetc(pFile); color.g = (unsigned char)fgetc(pFile); color.r = (unsigned char)fgetc(pFile); if (colorMode == 4) color.a = (unsigned char)fgetc(pFile); m_pImageData[i++] = color.b; m_pImageData[i++] = color.g; m_pImageData[i++] = color.r; if (colorMode == 4) m_pImageData[i++] = color.a; --length; } } } } fclose(pFile); switch(tgaHeader.imageTypeCode) { case TGA_RGB: case TGA_RGB_RLE: if (3 == colorMode) { m_imageDataFormat = IMAGE_RGB; m_imageDataType = IMAGE_DATA_UNSIGNED_BYTE; m_colorDepth = 32; } else { m_imageDataFormat = IMAGE_RGBA; m_imageDataType = IMAGE_DATA_UNSIGNED_BYTE; m_colorDepth = 32; } break; case TGA_GRAYSCALE: case TGA_GRAYSCALE_RLE: m_imageDataFormat = IMAGE_LUMINANCE; m_imageDataType = IMAGE_DATA_UNSIGNED_BYTE; m_colorDepth = 8; break; } if ((tgaHeader.imageDesc & TOP_LEFT) == TOP_LEFT) FlipVertical(); // swap the red and blue components in the image data SwapRedBlue(); return (m_pImageData != NULL); } bool CTargaImage::FlipVertical() { if (!m_pImageData) return false; if (m_colorDepth == 32) { rgba_t* tmpBits = new rgba_t[m_width]; if (!tmpBits) return false; int lineWidth = m_width * 4; rgba_t* top = (rgba_t*)m_pImageData; rgba_t* bottom = (rgba_t*)(m_pImageData + lineWidth*(m_height-1)); for (int i = 0; i < (m_height / 2); ++i) { memcpy(tmpBits, top, lineWidth); memcpy(top, bottom, lineWidth); memcpy(bottom, tmpBits, lineWidth); top = (rgba_t*)((unsigned char*)top + lineWidth); bottom = (rgba_t* )((unsigned char*)bottom - lineWidth); } delete [] tmpBits; tmpBits = 0; } else if (m_colorDepth == 32) { rgb_t* tmpBits = new rgb_t[m_width]; if (!tmpBits) return false; int lineWidth = m_width * 3; rgb_t* top = (rgb_t*)m_pImageData; rgb_t* bottom = (rgb_t*)(m_pImageData + lineWidth*(m_height-1)); for (int i = 0; i < (m_height / 2); ++i) { memcpy(tmpBits, top, lineWidth); memcpy(top, bottom, lineWidth); memcpy(bottom, tmpBits, lineWidth); top = (rgb_t*)((unsigned char*)top + lineWidth); bottom = (rgb_t*)((unsigned char*)bottom - lineWidth); } delete [] tmpBits; tmpBits = 0; } return true; } void CTargaImage::Release() { delete [] m_pImageData; m_pImageData = NULL; } bool CTargaImage::ConvertRGBToRGBA(unsigned char alphaValue) { if ((m_colorDepth == 32) && (m_imageDataFormat == IMAGE_RGB)) { rgba_t *newImage = new rgba_t[m_width * m_height]; if (!newImage) return false; rgba_t *dest = newImage; rgb_t *src = (rgb_t*)m_pImageData; for (int x = 0; x < m_height; x++) { for (int y = 0; y < m_width; y++) { dest->r = src->r; dest->g = src->g; dest->b = src->b; dest->a = alphaValue; ++src; ++dest; } } delete [] m_pImageData; m_pImageData = (unsigned char*)newImage; m_colorDepth = 32; m_imageDataType = IMAGE_DATA_UNSIGNED_BYTE; m_imageDataFormat = IMAGE_RGBA; return true; } return false; } bool CTargaImage::ConvertRGBAToRGB() { if ((m_colorDepth == 32) && (m_imageDataFormat == IMAGE_RGBA)) { rgb_t *newImage = new rgb_t[m_width * m_height]; if (!newImage) return false; rgb_t *dest = newImage; rgba_t *src = (rgba_t*)m_pImageData; for (int x = 0; x < m_height; x++) { for (int y = 0; y < m_width; y++) { dest->r = src->r; dest->g = src->g; dest->b = src->b; ++src; ++dest; } } delete [] m_pImageData; m_pImageData = (unsigned char*)newImage; m_colorDepth = 32; m_imageDataType = IMAGE_DATA_UNSIGNED_BYTE; m_imageDataFormat = IMAGE_RGB; return true; } return false; }

Share this post


Link to post
Share on other sites
Advertisement
Yes, I think people want to help you when you are

a) vague
b) terse
c) dump code without source blocks or narrowing down the problem

Share this post


Link to post
Share on other sites
Whole swathes of code aren't going to please anyone or allow you to get help! For one thing we (the readers) can't see easily what/where the problem is.

However, I make two suggestions which will certainly put you on the road to solving your trouble.

1. You will receive help but you need to present your request better - please look here http://www.gamedev.net/community/forums/faq.asp#tags to see how to format code.
'cause I'm nice I've written it out for you - it's like this - write each tag on 1 line:
[
source
lang = "cpp"
]

//- now to close
[
/source
]

Result:

//-now to close




2. You are using the GLfx libraries. This suggests to me that you are using the OpenGL books? If this is so have a look through there to compare what you've done with what they've done. You could contact the Author(s)?

Hope that helps.

Share this post


Link to post
Share on other sites
[
source
lang="cpp"
]
//#ifdef _WINDOWS
#include <windows.h>
#endif

#include <gl/gl.h>
#include <gl/glu.h>
#include <math.h>
#include <time.h>
#include "glext.h"
#include "CGfxOpenGL.h"

#include "CTargaImage.h"

CGfxOpenGL::CGfxOpenGL()
{
}

CGfxOpenGL::~CGfxOpenGL()
{
}

bool CGfxOpenGL::Init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);

// enable 2D texturing
glEnable(GL_TEXTURE_2D);

m_textureOne = new CTargaImage;

// load texture image data
if (!m_textureOne->Load("rock.tga"))
return false;

// retrieve "unused" texture object
glGenTextures(1, &m_textureObjectOne);

// bind the texture object
glBindTexture(GL_TEXTURE_2D, m_textureObjectOne);

// minimum required to set the min and mag texture filters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

// now that the texture object is bound, specify a texture for it
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_textureOne->GetWidth(), m_textureOne->GetHeight(),
0, GL_RGB, GL_UNSIGNED_BYTE, m_textureOne->GetImage());

// create the second texture object
glGenTextures(1, &m_textureObjectTwo);

glBindTexture(GL_TEXTURE_2D, m_textureObjectTwo);

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

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_textureOne->GetWidth(), m_textureOne->GetHeight(),
0, GL_RGBA, GL_UNSIGNED_BYTE, m_textureOne->GetImage());

// initialize movement variables
m_zPos = -5.0f;
m_zMoveNegative = true;

return true;
}

bool CGfxOpenGL::Shutdown()
{
glDeleteTextures(1, &m_textureObjectOne);
glDeleteTextures(1, &m_textureObjectTwo);

m_textureOne->Release();
delete m_textureOne;

return true;
}

void CGfxOpenGL::SetupProjection(int width, int height)
{
if (height == 0) // don't want a divide by zero
{
height = 1;
}

glViewport(0, 0, width, height); // reset the viewport to new dimensions
glMatrixMode(GL_PROJECTION); // set projection matrix current matrix
glLoadIdentity(); // reset projection matrix

// calculate perspective
gluPerspective(54.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.0f);

glMatrixMode(GL_MODELVIEW); // set modelview matrix
glLoadIdentity(); // reset modelview matrix

m_windowWidth = width;
m_windowHeight = height;
}

void CGfxOpenGL::Prepare(float dt)
{
// change polygon z position
if (m_zMoveNegative)
m_zPos -= 5.0f*dt;
else
m_zPos += 5.0f*dt;

if (m_zPos > -5.0f)
{
m_zPos = -5.0f;
m_zMoveNegative = true;
}
if (m_zPos < -40.0f)
{
m_zPos = -40.0f;
m_zMoveNegative = false;
}
}

void CGfxOpenGL::DrawPlane()
{
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(2.0, 0.0); glVertex3f(2.0, -2.0, -2.0);
glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -2.0, -2.0);
glTexCoord2f(2.0, 2.0); glVertex3f(2.0, -2.0, 2.0);
glTexCoord2f(0.0, 2.0); glVertex3f(-2.0, -2.0, 2.0);
glEnd();
}

void CGfxOpenGL::Render()
{
// clear screen and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// load the identity matrix (clear to default position and orientation)
glLoadIdentity();

// draw the left polygon
glPushMatrix();
// translate the world coordinate system along the z axis
glTranslatef(-3.0, 0.0, m_zPos);
glRotatef(90.0, 1.0, 0.0, 0.0);

// bind the texture
glBindTexture(GL_TEXTURE_2D, m_textureObjectOne);

// draw the plane at the world origin
DrawPlane();
glPopMatrix();

// do it all again for the right polygon
glPushMatrix();
glTranslatef(3.0, 0.0, m_zPos);
glRotatef(90.0, 1.0, 0.0, 0.0);
glBindTexture(GL_TEXTURE_2D, m_textureObjectTwo);
DrawPlane();
glPopMatrix();
}

[
/source
]

Share this post


Link to post
Share on other sites
Don't put them on seperate lines.


//#ifdef _WINDOWS
#include <windows.h>
#endif

#include <gl/gl.h>
#include <gl/glu.h>
#include <math.h>
#include <time.h>
#include "glext.h"
#include "CGfxOpenGL.h"

#include "CTargaImage.h"

CGfxOpenGL::CGfxOpenGL()
{
}

CGfxOpenGL::~CGfxOpenGL()
{
}

bool CGfxOpenGL::Init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);

// enable 2D texturing
glEnable(GL_TEXTURE_2D);

m_textureOne = new CTargaImage;

// load texture image data
if (!m_textureOne->Load("rock.tga"))
return false;

// retrieve "unused" texture object
glGenTextures(1, &m_textureObjectOne);

// bind the texture object
glBindTexture(GL_TEXTURE_2D, m_textureObjectOne);

// minimum required to set the min and mag texture filters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

// now that the texture object is bound, specify a texture for it
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_textureOne->GetWidth(), m_textureOne->GetHeight(),
0, GL_RGB, GL_UNSIGNED_BYTE, m_textureOne->GetImage());

// create the second texture object
glGenTextures(1, &m_textureObjectTwo);

glBindTexture(GL_TEXTURE_2D, m_textureObjectTwo);

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

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_textureOne->GetWidth(), m_textureOne->GetHeight(),
0, GL_RGBA, GL_UNSIGNED_BYTE, m_textureOne->GetImage());

// initialize movement variables
m_zPos = -5.0f;
m_zMoveNegative = true;

return true;
}

bool CGfxOpenGL::Shutdown()
{
glDeleteTextures(1, &m_textureObjectOne);
glDeleteTextures(1, &m_textureObjectTwo);

m_textureOne->Release();
delete m_textureOne;

return true;
}

void CGfxOpenGL::SetupProjection(int width, int height)
{
if (height == 0) // don't want a divide by zero
{
height = 1;
}

glViewport(0, 0, width, height); // reset the viewport to new dimensions
glMatrixMode(GL_PROJECTION); // set projection matrix current matrix
glLoadIdentity(); // reset projection matrix

// calculate perspective
gluPerspective(54.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.0f);

glMatrixMode(GL_MODELVIEW); // set modelview matrix
glLoadIdentity(); // reset modelview matrix

m_windowWidth = width;
m_windowHeight = height;
}

void CGfxOpenGL::Prepare(float dt)
{
// change polygon z position
if (m_zMoveNegative)
m_zPos -= 5.0f*dt;
else
m_zPos += 5.0f*dt;

if (m_zPos > -5.0f)
{
m_zPos = -5.0f;
m_zMoveNegative = true;
}
if (m_zPos < -40.0f)
{
m_zPos = -40.0f;
m_zMoveNegative = false;
}
}

void CGfxOpenGL::DrawPlane()
{
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(2.0, 0.0); glVertex3f(2.0, -2.0, -2.0);
glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -2.0, -2.0);
glTexCoord2f(2.0, 2.0); glVertex3f(2.0, -2.0, 2.0);
glTexCoord2f(0.0, 2.0); glVertex3f(-2.0, -2.0, 2.0);
glEnd();
}

void CGfxOpenGL::Render()
{
// clear screen and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// load the identity matrix (clear to default position and orientation)
glLoadIdentity();

// draw the left polygon
glPushMatrix();
// translate the world coordinate system along the z axis
glTranslatef(-3.0, 0.0, m_zPos);
glRotatef(90.0, 1.0, 0.0, 0.0);

// bind the texture
glBindTexture(GL_TEXTURE_2D, m_textureObjectOne);

// draw the plane at the world origin
DrawPlane();
glPopMatrix();

// do it all again for the right polygon
glPushMatrix();
glTranslatef(3.0, 0.0, m_zPos);
glRotatef(90.0, 1.0, 0.0, 0.0);
glBindTexture(GL_TEXTURE_2D, m_textureObjectTwo);
DrawPlane();
glPopMatrix();
}



Share this post


Link to post
Share on other sites
1. What do you mean by "texture isn't working". Black polygon displayed? Rainbow on polygon?

2. Is your texture 2^n size? (1x1, 2x2, 4x4, 8x8, 16x16, 32x32, 64x64, 128x128 etc.)

Share this post


Link to post
Share on other sites
i figured it out.

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_textureOne->GetWidth(), m_textureOne->GetHeight(),
0, GL_RGBA, GL_UNSIGNED_BYTE, m_textureOne->GetImage());

this code was with alpha, and i removed it so that it read RGB insted of RGBA
and now it works. horrah!

and thanks for posting some tips, it lead me to that fix.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!