I'm trying to move my code over to modern Opengl, but am having trouble. Right now my code will draw a cube and it will put a texture on, but it will only attach the first texture to all of my face. I am also using SOIL to load my textures into my program. What am I doing wrong?
This is my code:
class Rectangle
{
public:
Rectangle();
Rectangle(float x, float y, float z, float width, float height, float depth, string frontFace, string backFace, string leftFace,
string RightFace, string topFace, string bottomFace);
void Draw();
private:
GLuint m_Textures[6];
string m_TextureNames[6];
GLfloat m_Vertices[72]; // v0,v1,v2,v3 (front)
// normal array
GLfloat m_Normals[72]; // v0,v1,v2,v3 (front)
// color array
GLfloat m_Colours[72]; // v0,v1,v2,v3 (front)
// index array of vertex array for glDrawElements() & glDrawRangeElement()
GLubyte m_Indices[36]; // front
GLfloat m_Texcoords[48];
};
Rectangle::Rectangle(float x, float y, float z, float width, float height, float depth, string topFace, string bottomFace, string frontFace,
string backFace, string leftFace, string rightFace)
{
m_CenterX = x;
m_CenterY = y;
m_CenterZ = z;
m_Width = width;
m_Height = height;
m_Depth = depth;
m_TextureNames[0] = topFace;
m_TextureNames[1] = bottomFace;
m_TextureNames[2] = frontFace;
m_TextureNames[3] = backFace;
m_TextureNames[4] = leftFace;
m_TextureNames[5] = rightFace;
m_ObjectType = Textured;
GLfloat tempVert[] = { // front
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
// top
-1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
1.0, 1.0, -1.0,
-1.0, 1.0, -1.0,
// back
1.0, -1.0, -1.0,
-1.0, -1.0, -1.0,
-1.0, 1.0, -1.0,
1.0, 1.0, -1.0,
// bottom
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, -1.0, 1.0,
-1.0, -1.0, 1.0,
// left
-1.0, -1.0, -1.0,
-1.0, -1.0, 1.0,
-1.0, 1.0, 1.0,
-1.0, 1.0, -1.0,
// right
1.0, -1.0, 1.0,
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
1.0, 1.0, 1.0,
};
// normal array
GLfloat tempNormals[] = { 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, // v0,v1,v2,v3 (front)
1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // v0,v3,v4,v5 (right)
0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, // v0,v5,v6,v1 (top)
-1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, // v1,v6,v7,v2 (left)
0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1, 0, // v7,v4,v3,v2 (bottom)
0, 0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1 }; // v4,v7,v6,v5 (back)
// color array
GLfloat tempColors[] = { 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, // v0,v1,v2,v3 (front)
1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, // v0,v3,v4,v5 (right)
1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, // v0,v5,v6,v1 (top)
1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, // v1,v6,v7,v2 (left)
0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, // v7,v4,v3,v2 (bottom)
0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1 }; // v4,v7,v6,v5 (back)
// index array of vertex array for glDrawElements() & glDrawRangeElement()
GLubyte tempIndices[] = { 0, 1, 2, 2, 3, 0, // front
4, 5, 6, 6, 7, 4, // right
8, 9,10, 10,11, 8, // top
12,13,14, 14,15,12, // left
16,17,18, 18,19,16, // bottom
20,21,22, 22,23,20 }; // back
GLfloat tempTexcoords[2*4*6] = {
// front
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
};
for (int i = 1; i < 6; i++)
memcpy(&tempTexcoords[i*4*2], &tempTexcoords[0], 2*4*sizeof(GLfloat));
copy(tempVert, tempVert + 72, m_Vertices);
copy(tempNormals, tempNormals + 72, m_Normals);
copy(tempColors, tempColors + 72, m_Colours);
copy(tempIndices, tempIndices + 36, m_Indices);
std::copy(tempTexcoords, tempTexcoords + 48, m_Texcoords);
LoadTexture(m_TextureNames);
}
void Rectangle::LoadTexture(string TextureName[6])
{
// Create texture index array.
glGenTextures(6, m_Textures);
for(int i = 0 ; i < 1 ; i++)
{
glBindTexture(GL_TEXTURE_2D, m_Textures[i]);
// Set our texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Set texture filtering
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); // NOTE the GL_NEAREST Here!
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); // NOTE the GL_NEAREST Here!
std::string fileType;
fileType.append(m_TextureNames[i], m_TextureNames[i].size()-3,3);
if(fileType == "jpg")
{
m_Textures[i] = SOIL_load_OGL_texture // load an image file directly as a new OpenGL texture
(
m_TextureNames[i].c_str(),
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
// allocate a texture name
}
else
{
m_Textures[i] = SOIL_load_OGL_texture // load an image file directly as a new OpenGL texture
(
m_TextureNames[i].c_str(),
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
// allocate a texture name
}
}
}
// Function to draw Sphere.
void Rectangle::Draw()
{
// enable and specify pointers to vertex arrays
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glNormalPointer(GL_FLOAT, 0, m_Normals);
glTexCoordPointer(2, GL_FLOAT, 0, m_Texcoords);
glColorPointer(3, GL_FLOAT, 0, m_Colours);
glVertexPointer(3, GL_FLOAT, 0, m_Vertices);
for (int i=0;i<6;i++)
{
glPushMatrix();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_Textures[i]);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, m_Indices);
glPopMatrix();
}
glDisableClientState(GL_VERTEX_ARRAY); // disable vertex arrays
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
}
Rectangle testRect;
// Drawing routine.
void drawScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
testRect.Draw();
glutSwapBuffers();
}
// Initialization routine.
void setup(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
testRect = Rectangle(2, 0.0, 0.0, 1, 2, 1, "grass.bmp","grass.bmp", "grass.bmp", "launch.png", "launch.png", "launch.png");
// Turn on OpenGL texturing.
glEnable(GL_TEXTURE_2D);
glShadeModel (GL_SMOOTH);
}
The posted code has a few issues:
It's loading only one texture:
glGenTextures(6, m_Textures);
for(int i = 0 ; i < 1 ; i++)
{
glBindTexture(GL_TEXTURE_2D, m_Textures[i]);
...
If you want to load 6 textures, like the rest of the code suggests, you'll have to use 6 for the end value of the loop:
for(int i = 0 ; i < 6 ; i++)
It's creating texture ids twice, and sets parameters with the wrong texture bound. At the start of LoadTexture()
, it generates 6 texture ids:
glGenTextures(6, m_Textures);
and then binds them, and makes glTexParameteri()
to set various parameters on them. But then it calls SOIL_load_ogl_texture()
with a flag asking it to create a new id again, and then stores that one away:
m_Textures[i] = SOIL_load_OGL_texture(
m_TextureNames[i].c_str(),
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
To fix this, you can omit the gGenTextures()
call, and move the code to bind the texture and call glTexParameteri()
after the SOIL_load_ogl_texture()
call. Also, this uses a flag to generate mipmaps, but sets the texture filters to not use mipmapping.
In the draw function, it loops over the 6 faces, but then draws the entire cube each time:
for (int i=0;i<6;i++)
{
glPushMatrix();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_Textures[i]);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, m_Indices);
glPopMatrix();
}
The second argument to glDrawElements()
specifies the number of vertices to be rendered. With the value 36, all vertices will be used (6 sides of the cube, with 2 triangles each, with 3 vertices each.
Also, the glPushMatrix()
/glPopMatrix()
serves absolutely no purpose. The draw loop should look something like this:
glActiveTexture(GL_TEXTURE0);
for (int i=0;i<6;i++)
{
glBindTexture(GL_TEXTURE_2D, m_Textures[i]);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, m_Indices + i * 6);
}
I don't see the depth test being enabled anywhere. Add this to setup()
:
glEnable(GL_DEPTH_TEST);