Texture Mapping

Started by
5 comments, last by Neutrinohunter 16 years, 2 months ago
hey im new to the forums and i would greatly appreciate some help on this! i was successful on the magnification and minification for the 1D square movements. although the "rock.tga" texture does not show; the code still works. So i thougth the problem might be in the loading of the rock.tga at first my code was: if (!m_textureOne->Load("rock.tga")) return false; it produces no error, but i am puzzled as to why my 2 squares are without texture i thought changing the code to image.Load("rock.tga"); glGenTextures(1, &m_rockTexture); glBindTexture(GL_TEXTURE_2D, m_rockTexture); gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.GetWidth(), image.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, image.GetImage()); image.Release(); would work, but i still see no texture! im not having any errors, but i would like to achieve the point to my current chapter before i move on. yes i have read the NeHe lessons and i really just dont understand how to get my code to correspond with the one givin on that site
Advertisement
Did you set up the glBindTexture function up correctly?


Small pieces of code what some parts should have.

INIT OpenGL function
	glEnable(GL_TEXTURE_2D); //Enable textures

Render function
			glBindTexture(GL_TEXTURE_2D, imageId); //Needed first for your model		    glBegin(GL_TRIANGLES);          glTexCoord2f( 1.0,1.0); glVertex3f( 2.2,2.2,2.2);                //More vertexes etc...                    glEnd();	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); /* We will use linear			 interpolation for magnification filter */			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);


Helped?
Check out my open source code projects/libraries! My Homepage You may learn something.
You should check out GLintercept. It can really help for problems like these.

Otherwise your stabbing in the dark somewhat [wink]
nothing has helped. its not loading properly..maybe. i have the texture file include in C:\myprojectfiles but i guess its not the right place. <sigh>

heres the entire code:


#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();
}

Post the log from GLintercept, a screenshot, and then try gldrawpixels().
Quote:glTexCoord2f(2.0, 0.0)
Can you do that?

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

Quote:Original post by Geometrian
Quote:glTexCoord2f(2.0, 0.0)
Can you do that?


Yes, you can but it depends on your values for wrapping. Such as:

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);


What is the Value of this?

if (!m_textureOne->Load("rock.tga"))return false;


And are you sure that the Targa file is not 32bit (i.e has an alpha channel)?

Jamie

This topic is closed to new replies.

Advertisement