Skybox Texturing Problem Please Help :(

Started by
4 comments, last by i_luv_cplusplus 15 years, 3 months ago
Hey guys, I have recently gotten into openGL programming for University, I am currently working on a project, not quite sure what I am going to develop yet but I have gotten to a good stage so far, I have a skybox which moves relevant to the camera giving the appearance of never being able to reach it compared the the floor tecturing, I have one problem though, I can texture my floor perfectly but when I texture my skybox with the images I want to use, it will only use the first image I bind, then plaster it all over my skybox, at first I thought it might just be the fact that I am not overriding the texture bind with a new image but if that was the case then my floor texture would be everywhere instead of my skybox one. I will paste the 2 main class files with the methods I have created. In my Texture header I have the public GLuint "test" variable here is my Texture.cpp #include "stdafx.h" #include "Texture.h" #include <stdio.h> #include <stdlib.h> Texture::Texture() { } Texture::Texture(const char* path) { test = LoadTexture(path, 1024, 1024); } //load the RAW file GLuint Texture::LoadTexture(const char * filename, int width, int height) { GLuint texture; unsigned char * data; FILE * file; //The following code will read in our RAW file file = fopen( filename, "rb" ); if ( file == NULL ) return 0; data = (unsigned char *)malloc( width * height * 3 ); fread( data, width * height * 3, 1, file ); fclose( file ); glGenTextures( 1, &texture ); glBindTexture( GL_TEXTURE_2D, texture ); glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); free( data ); return texture; } void Texture::FreeTexture( GLuint texture ) { glDeleteTextures( 1, &texture ); } void Texture::bind() { glBindTexture( GL_TEXTURE_2D, test ); } void Texture::unbind() { glBindTexture( GL_TEXTURE_2D, 0 ); } and here is where I am using the Texture class in my WorldEnvironment class #include "stdafx.h" #include "WorldEnvironment.h" #include "Texture.h" Texture* texture = new Texture(); WorldEnvironment::WorldEnvironment() { floorTexture = new Texture("Bottom.raw"); skyBoxTop = new Texture("Top.raw"); skyBoxBottom = new Texture("Bottom.raw"); skyBoxFront = new Texture("Front.raw"); skyBoxBack = new Texture("Back.raw"); skyBoxLeft = new Texture("Left.raw"); skyBoxRight = new Texture("Right.raw"); } void WorldEnvironment::floor() { glEnable(GL_TEXTURE_2D); floorTexture->bind(); glBegin(GL_QUADS); glTexCoord2d(0.0,0.0); glVertex3f(-50.5, -10.5, 100.0); bottom left glTexCoord2d(5.0,0.0); glVertex3f(-50.5, -10.5, -100.5); top left glTexCoord2d(5.0,5.0); glVertex3f(50.5, -10.5, -100.5); top right glTexCoord2d(0.0,5.0); glVertex3f(50.5, -10.5, 100.0); bottom right floorTexture->unbind(); glEnd(); glDisable(GL_TEXTURE_2D); } void WorldEnvironment::skybox() { glEnable(GL_TEXTURE_2D); skyBoxTop->bind(); glScalef( 50.0, 50.0, 50.0 ); glBegin(GL_QUADS); //begin the four sided shape glTexCoord2d(0.0,0.0); glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top) glTexCoord2d(1.0,0.0); glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top) glTexCoord2d(1.0,1.0); glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top) glTexCoord2d(0.0,1.0); glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top) skyBoxTop->unbind(); glDisable(GL_TEXTURE_2D); skyBoxTop->unbind(); glEnable(GL_TEXTURE_2D); skyBoxBottom->bind(); glTexCoord2d(0.0,0.0); glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom) glTexCoord2d(1.0,0.0); glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom) glTexCoord2d(1.0,1.0); glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom) glTexCoord2d(0.0,1.0); glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom) skyBoxBottom->unbind(); glDisable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D); skyBoxFront->bind(); glTexCoord2d(0.0,0.0); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front) glTexCoord2d(1.0,0.0); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front) glTexCoord2d(1.0,1.0); glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front) glTexCoord2d(0.0,1.0); glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front) skyBoxFront->unbind(); glDisable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D); skyBoxBack->bind(); glTexCoord2d(0.0,0.0); glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Back) glTexCoord2d(1.0,0.0); glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Back) glTexCoord2d(1.0,1.0); glVertex3f(-1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Back) glTexCoord2d(0.0,1.0); glVertex3f( 1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Back) skyBoxBack->unbind(); glDisable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D); skyBoxLeft->bind(); glTexCoord2d(0.0,0.0); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left) glTexCoord2d(1.0,0.0); glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left) glTexCoord2d(1.0,1.0); glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left) glTexCoord2d(0.0,1.0); glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left) skyBoxLeft->unbind(); glDisable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D); skyBoxRight->bind(); glTexCoord2d(0.0,0.0); glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right) glTexCoord2d(1.0,0.0); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right) glTexCoord2d(1.0,1.0); glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right) glTexCoord2d(0.0,1.0); glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right) skyBoxRight->unbind(); glEnd(); //end the shape we are currently working on glDisable(GL_TEXTURE_2D); } The skybox variables in the constructor are public Texture pointers in the header file, I simply just dont understand why each quad is not differently textured..... im completely stumped.... If I change the first quads skyBox texture to another one... say skyBoxBottom->bind() then it will load all the quads as the bottom texture... im so confused...
Advertisement
Hmm, I programmed my texture loader quite a bit differently but I see what your trying to do.

Ive had problems like this before but usually its because I was loading only 1 texture slot. Im not too sure, but have you tried manually binding the textures.

Eg.
int Test1 = LoadTexture("Tex1.RAW", 1024,1024);
glBindTexture(GL_TEXTURE_2D, Test1);

Try commenting out Unbind, see if that changes anything.

My loader works a bit differently Ill just explain it to see if it helps you out at all. I just define a Model, Bind a texture to it, and use it.

Eg.
ModelCreate Object1;
Object1.Texture = LoadTex("TestTex.tga");
Object1.Model = LoadModel("TestModel.Model");

glBindTexture(GL_TEXTURE_2D, Object1.Texture);
Object1.Draw();
I suggest that you spend more time reading the official OpenGL documentation. Between glBegin/glEnd calls you can only use glColor/glNormal/glVertex and glTexCoord calls. So if you want to render each face with different texture, you need to do:

bind texture
glbegin(gl_quads);
glvertex;
glvertex;
glvertex;
glvertex;
glend();
bind texture
glbegin...

et cetera et cetera
OpenGL fanboy.
Hey! Thanks for your responce,

I have tried your example of

Eg.
GLuint Test1 = LoadTexture("Tex1.RAW", 1024,1024);
glBindTexture(GL_TEXTURE_2D, Test1);

I did this originally defore I drew each side of the box using a different texture each time, the only problem with doing it this way is it loads a new texture into memory each time I redraw in the main loop causing the game to just increase in memory usage, until I cant us it anymore, which is why I developed an object oriented approach to it.

If I split each side of the box into methods and then use the pointers it seems to work ok, but it causes problems with scaling as I always have to modify each method, it is also a tacky dirty hack..... I just dont see why I cannot use pointers unless I am missing something obvious.
i_luv_cplusplus

Cheers.... never realised that, been running through tutorials and havent seen that mentioned once....

Wont be making that mistake again in a hurry....

It worked....thanks :)

Appreciated....
From http://www.opengl.org/sdk/docs/man/xhtml/glBegin.xml:

Quote:Only a subset of GL commands can be used between glBegin and glEnd. The commands are glVertex, glColor, glSecondaryColor, glIndex, glNormal, glFogCoord, glTexCoord, glMultiTexCoord, glVertexAttrib, glEvalCoord, glEvalPoint, glArrayElement, glMaterial, and glEdgeFlag. Also, it is acceptable to use glCallList or glCallLists to execute display lists that include only the preceding commands. If any other GL command is executed between glBegin and glEnd, the error flag is set and the command is ignored.
OpenGL fanboy.

This topic is closed to new replies.

Advertisement