Jump to content
  • Advertisement
Sign in to follow this  
Murlok

OpenGL Texture objects texturing help

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

Hello fellow forumers. I've got a question about texturing objects in opengl. I've the following code however i'm only able to texture one of the surface. I tried to solve it however there is 2 undesired outcome that i've gotten so far. The first outcome is that the object will only use the latest texture defined and the other result is that both the texture will be combined together. This is the orginal code that i have and is working so far. Any help and guidiance from the senior board member is much appreciated. #include <gl/glut.h> #include <stdio.h> #include <stdlib.h> #include <windows.h> GLfloat gAngle = 0.0f; //cube rotation angle // material properties GLfloat gAmbientMaterial[] = {0.5f, 0.5f, 0.5f, 1.0f}; GLfloat gDiffuseMaterial[] = {0.3f, 0.5f, 0.8f, 1.0f}; GLfloat gSpecularMaterial[] = {1.0f, 1.0f, 1.0f, 1.0f}; GLint gShininessMaterial = 100; //light properties GLfloat gAmbientLight[] = { 0.5f, 0.5f, 0.5f, 1.0f }; GLfloat gDiffuseLight[] = { 0.5f, 0.5f, 0.5f, 1.0f }; GLfloat gLightPosition[] = { 0.0f, 0.0f, 1.0f, 0.0f }; //variables to store texture information unsigned char* tex_image; //image data GLuint textureID; //texture id GLint imageWidth; //image width info GLint imageHeight; //image height info // This function reads a RGB bitmap file unsigned char* LoadBMPFile(char *filename) { BITMAPINFOHEADER BMPInfoHeader; //bitmap info header BITMAPFILEHEADER BMPFileHeader; //bitmap file header FILE *filePtr; //the file pointer unsigned char *bitmapImage; //bitmap image data unsigned char temp; //temp variable for swapping //RGB values int size, i = 0; //counter // open filename for reading in binary mode if ((filePtr = fopen(filename, "rb")) == NULL) { return NULL; } // read bitmap file header fread(&BMPFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr); // check whether file is really a bitmap file if (BMPFileHeader.bfType != 0x4D42) { fclose(filePtr); // if file is not a bitmap file the close and return NULL return NULL; } // read bitmap information header fread(&BMPInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr); // locate beginning of bitmap data fseek(filePtr, BMPFileHeader.bfOffBits, SEEK_SET); size = BMPInfoHeader.biSizeImage; // store image size imageWidth = BMPInfoHeader.biWidth; // store image width imageHeight = BMPInfoHeader.biHeight; // store image height // allocate memory for image data, check if not allocated properly then close //file and return NULL if((bitmapImage = (unsigned char*)malloc(size)) == NULL) { fclose(filePtr); return NULL; } // read bitmap data fread(bitmapImage, 1, size, filePtr); //colours in bitmap are of the format BGR, hence need to swap B and R to get //RGB for (i = 0; i < size; i += 3) { temp = bitmapImage; bitmapImage = bitmapImage[i+2]; bitmapImage[i+2] = temp; } fclose(filePtr); // close file return bitmapImage; // return pointer to image data } //set up states that are going to be used void init() { glClearColor(0.0, 0.0, 0.0, 1.0); //clears background colour to black glEnable(GL_DEPTH_TEST); //enable depth test //set material properties glMaterialfv(GL_FRONT, GL_AMBIENT, gAmbientMaterial); glMaterialfv(GL_FRONT, GL_DIFFUSE, gDiffuseMaterial); glMaterialfv(GL_FRONT, GL_SPECULAR, gSpecularMaterial); glMateriali(GL_FRONT, GL_SHININESS, gShininessMaterial); //setup light 0 glLightfv(GL_LIGHT0, GL_AMBIENT, gAmbientLight); glLightfv(GL_LIGHT0, GL_DIFFUSE, gDiffuseLight); glLightfv(GL_LIGHT0, GL_POSITION, gLightPosition); //enable lighting and select shading model glEnable(GL_LIGHT0); //enable light0 glEnable(GL_LIGHTING); //enable lighting // load BMP file into image tex_image = LoadBMPFile("check.bmp"); glGenTextures(1, &textureID); // generate texture glBindTexture(GL_TEXTURE_2D, textureID); // bind texture // generate texture mipmaps gluBuild2DMipmaps(GL_TEXTURE_2D, 3, imageWidth, imageHeight, GL_RGB, GL_UNSIGNED_BYTE, tex_image); // setup texture parameters for currently bound texture glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glEnable(GL_TEXTURE_2D); // enable 2D texturing } //called whenever the window is resized (also called once when window is first drawn) void resizeWindow(int width, int height) //window width and height { if(height == 0) //prevent divide by zero height = 1; glViewport(0, 0, width, height); //set the viewport glMatrixMode(GL_PROJECTION); //select projection matrix glLoadIdentity(); //clear current contents to identity matrix gluPerspective(60.0f, //set up perspective projection with 60 degrees field of view (GLfloat) width/(GLfloat) height, //set aspect ratio 1.0f, //set near clipping plane to 1.0 100.0f); //set far clipping plane to 100.0 glMatrixMode(GL_MODELVIEW); //select modelview matrix glLoadIdentity(); //clear current contents to identity matrix gluLookAt(0.0f, 0.0f, 3.0f, //set position 0.0f, 0.0f, 0.0f, //set where eye/camera is looking at 0.0f, 1.0f, 0.0f); //set 'up' vector } //draws a unit cube void drawCube() { glBegin(GL_QUADS); glNormal3f(0.0f, 1.0f, 0.0f); // top face glTexCoord2f(0.0f, 0.0f); glVertex3f(0.5f, 0.5f, 0.5f); glTexCoord2f(2.0f, 0.0f); glVertex3f(0.5f, 0.5f, -0.5f); glTexCoord2f(2.0f, 2.0f); glVertex3f(-0.5f, 0.5f, -0.5f); glTexCoord2f(0.0f, 2.0f); glVertex3f(-0.5f, 0.5f, 0.5f); glNormal3f(0.0f, 0.0f, 1.0f); // front face glTexCoord2f(0.0f, 0.0f); glVertex3f(0.5f, 0.5f, 0.5f); glTexCoord2f(2.0f, 0.0f); glVertex3f(-0.5f, 0.5f, 0.5f); glTexCoord2f(2.0f, 2.0f); glVertex3f(-0.5f, -0.5f, 0.5f); glTexCoord2f(0.0f, 2.0f); glVertex3f(0.5f, -0.5f, 0.5f); glNormal3f(1.0f, 0.0f, 0.0f); // right face glTexCoord2f(0.0f, 0.0f); glVertex3f(0.5f, 0.5f, 0.5f); glTexCoord2f(2.0f, 0.0f); glVertex3f(0.5f, -0.5f, 0.5f); glTexCoord2f(2.0f, 2.0f); glVertex3f(0.5f, -0.5f, -0.5f); glTexCoord2f(0.0f, 2.0f); glVertex3f(0.5f, 0.5f, -0.5f); glNormal3f(-1.0f, 0.0f, 0.0f); // left face glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, 0.5f, -0.5f); glTexCoord2f(2.0f, 0.0f); glVertex3f(-0.5f, 0.5f, 0.5f); glTexCoord2f(2.0f, 2.0f); glVertex3f(-0.5f, -0.5f, 0.5f); glTexCoord2f(0.0f, 2.0f); glVertex3f(-0.5f, -0.5f, -0.5f); glNormal3f(0.0f, -1.0f, 0.0f); // bottom face glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, -0.5f, 0.5f); glTexCoord2f(2.0f, 0.0f); glVertex3f(-0.5f, -0.5f, -0.5f); glTexCoord2f(2.0f, 2.0f); glVertex3f(0.5f, -0.5f, -0.5f); glTexCoord2f(0.0f, 2.0f); glVertex3f(0.5f, -0.5f, 0.5f); glNormal3f(0.0f, 0.0f, -1.0f); // back face glTexCoord2f(0.0f, 0.0f); glVertex3f(0.5f, -0.5f, -0.5f); glTexCoord2f(2.0f, 0.0f); glVertex3f(-0.5f, -0.5f, -0.5f); glTexCoord2f(2.0f, 2.0f); glVertex3f(-0.5f, 0.5f, -0.5f); glTexCoord2f(0.0f, 2.0f); glVertex3f(0.5f, 0.5f, -0.5f); glEnd(); } //draw the scene here void drawScene() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear the colour buffer to the colour previously set in glClearColor glPushMatrix(); glColor3f(0.0, 0.0, 1.0); glRotatef(gAngle, 1.0f, 1.0f, 1.0f); drawCube(); glPopMatrix(); glFlush(); glutSwapBuffers(); //swap front and back buffers } //called when ASCII key pressed void myKeypress(unsigned char key, int x, int y) { switch(key) { //if ESC pressed, quit program case '\e': exit(1); //quit free(tex_image); //free image break; default: break; }; } //called when no event in queue void myIdleFunc() { gAngle += 0.05; //increment rotation angle if(gAngle >= 360) //wrap around if rotation angle greater than 360 { gAngle -= 360; } glutPostRedisplay(); } int main(int argc, char* argv[]) { glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(800, 600); glutInitWindowPosition(50, 50); glutCreateWindow("Texture"); init(); glutDisplayFunc(drawScene); glutReshapeFunc(resizeWindow); glutKeyboardFunc(myKeypress); glutIdleFunc(myIdleFunc); glutMainLoop(); //enter event loop }

Share this post


Link to post
Share on other sites
Advertisement
Well, I only can see one texture loaded, so I assume that's not code that produces the error. Please post your current code using the [source] tags.

However, I couldn't see you binding textures in your draw code, i.e. if you load two textures only the last one will be used. If you want to use different textures, you have to bind the right one before drawing the objects that should use it.

Example:


void draw()
{
... //glClear() etc.

glBindTexture(textureId[index]); //textureId is an array of all loaded texture ids, index is the index of the texture you want to use
drawCube();

... //matrix setup

glBindTexture(textureId[index2]);
drawCude();

... //swap
}



Share this post


Link to post
Share on other sites
Hi this is the code that i have so far.The following code "merges" the 2 textures that is have together.


#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <gl/glut.h> //include glut library (automatically includes gl and glu libraries)

GLfloat gAngle = 0.0f; //cube rotation angle

// material properties
GLfloat gAmbientMaterial[] = {0.5f, 0.5f, 0.5f, 1.0f};
GLfloat gDiffuseMaterial[] = {0.3f, 0.5f, 0.8f, 1.0f};
GLfloat gSpecularMaterial[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLint gShininessMaterial = 100;

//light properties
GLfloat gAmbientLight[] = { 0.5f, 0.5f, 0.5f, 1.0f }; //ambient light
GLfloat gDiffuseLight[] = { 0.5f, 0.5f, 0.5f, 1.0f }; //diffuse light
GLfloat gLightPosition[] = { 0.0f, 0.0f, 1.0f, 0.0f }; //light position

//variables to store texture information
unsigned char* tex_image[2]; //image data
GLuint textureID[2]; //texture id
GLint imageWidth; //image width info
GLint imageHeight; //image height info

// This function reads a RGB bitmap file
unsigned char* LoadBMPFile(char *filename)
{
BITMAPINFOHEADER BMPInfoHeader; //bitmap info header
BITMAPFILEHEADER BMPFileHeader; //bitmap file header
FILE *filePtr; //the file pointer
unsigned char *bitmapImage; //bitmap image data
unsigned char temp; //temp variable for swapping RGB values
int size, i = 0; //counter

// open filename for reading in binary mode
if ((filePtr = fopen(filename, "rb")) == NULL)
{
return NULL;
}

// read bitmap file header
fread(&BMPFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);

// check whether file is really a bitmap file
if (BMPFileHeader.bfType != 0x4D42)
{
fclose(filePtr); // if file is not a bitmap file the close and return NULL
return NULL;
}

// read bitmap information header
fread(&BMPInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);

// locate beginning of bitmap data
fseek(filePtr, BMPFileHeader.bfOffBits, SEEK_SET);

size = BMPInfoHeader.biSizeImage; // store image size

imageWidth = BMPInfoHeader.biWidth; // store image width
imageHeight = BMPInfoHeader.biHeight; // store image height

// allocate memory for image data, check if not allocated properly then close file and return NULL
if((bitmapImage = (unsigned char*)malloc(size)) == NULL)
{
fclose(filePtr);
return NULL;
}

// read bitmap data
fread(bitmapImage, 1, size, filePtr);

// colours in bitmap are of the format BGR, hence need to swap B and R to get RGB
for (i = 0; i < size; i += 3)
{
temp = bitmapImage;
bitmapImage = bitmapImage[i+2];
bitmapImage[i+2] = temp;
}

fclose(filePtr); // close file
return bitmapImage; // return pointer to image data
}

//set up states that are going to be used
void init()
{
glClearColor(0.0, 0.0, 0.0, 1.0); //clears background colour to black

glEnable(GL_DEPTH_TEST); //enable depth test

//set material properties
glMaterialfv(GL_FRONT, GL_AMBIENT, gAmbientMaterial); //set material ambient component
glMaterialfv(GL_FRONT, GL_DIFFUSE, gDiffuseMaterial); //set material diffuse component
glMaterialfv(GL_FRONT, GL_SPECULAR, gSpecularMaterial); //set material specular component
glMateriali(GL_FRONT, GL_SHININESS, gShininessMaterial); //set material shininess

//setup light 0
glLightfv(GL_LIGHT0, GL_AMBIENT, gAmbientLight); //set light0's ambient component
glLightfv(GL_LIGHT0, GL_DIFFUSE, gDiffuseLight); //set light0's diffuse component
glLightfv(GL_LIGHT0, GL_POSITION, gLightPosition); //set light0's position

//enable lighting and select shading model
glEnable(GL_LIGHT0); //enable light0
glEnable(GL_LIGHTING); //enable lighting

// load BMP file into image
tex_image[0] = LoadBMPFile("wall.bmp");
tex_image[1] = LoadBMPFile("check.bmp");

for (int i=0; i < 2; i++)
{
glGenTextures(2, &textureID); // generate texture
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, imageWidth, imageHeight, GL_RGB, GL_UNSIGNED_BYTE, tex_image);
}
// setup texture parameters for currently bound texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_TEXTURE_2D); // enable 2D texturing
}

//called whenever the window is resized (also called once when window is first drawn)
void resizeWindow(int width, int height) //window width and height
{
if(height == 0) //prevent divide by zero
height = 1;

glViewport(0, 0, width, height); //set the viewport

glMatrixMode(GL_PROJECTION); //select projection matrix
glLoadIdentity(); //clear current contents to identity matrix
gluPerspective(60.0f, //set up perspective projection with 60 degrees field of view
(GLfloat) width/(GLfloat) height, //set aspect ratio
1.0f, //set near clipping plane to 1.0
100.0f); //set far clipping plane to 100.0

glMatrixMode(GL_MODELVIEW); //select modelview matrix
glLoadIdentity(); //clear current contents to identity matrix

gluLookAt(0.0f, 0.0f, 3.0f, //set position
0.0f, 0.0f, 0.0f, //set where eye/camera is looking at
0.0f, 1.0f, 0.0f); //set 'up' vector
}

//draws a unit cube
void drawCube()
{

glBegin(GL_QUADS);

glBindTexture(GL_TEXTURE_2D, textureID[0]);
glNormal3f(0.0f, 1.0f, 0.0f); // top face
glTexCoord2f(0.0f, 0.0f); glVertex3f(0.5f, 0.5f, 0.5f);
glTexCoord2f(2.0f, 0.0f); glVertex3f(0.5f, 0.5f, -0.5f);
glTexCoord2f(2.0f, 2.0f); glVertex3f(-0.5f, 0.5f, -0.5f);
glTexCoord2f(0.0f, 2.0f); glVertex3f(-0.5f, 0.5f, 0.5f);

glBindTexture(GL_TEXTURE_2D, textureID[0]);
glNormal3f(0.0f, 0.0f, 1.0f); // front face
glTexCoord2f(0.0f, 0.0f); glVertex3f(0.5f, 0.5f, 0.5f);
glTexCoord2f(2.0f, 0.0f); glVertex3f(-0.5f, 0.5f, 0.5f);
glTexCoord2f(2.0f, 2.0f); glVertex3f(-0.5f, -0.5f, 0.5f);
glTexCoord2f(0.0f, 2.0f); glVertex3f(0.5f, -0.5f, 0.5f);

glBindTexture(GL_TEXTURE_2D, textureID[0]);
glNormal3f(1.0f, 0.0f, 0.0f); // right face
glTexCoord2f(0.0f, 0.0f); glVertex3f(0.5f, 0.5f, 0.5f);
glTexCoord2f(2.0f, 0.0f); glVertex3f(0.5f, -0.5f, 0.5f);
glTexCoord2f(2.0f, 2.0f); glVertex3f(0.5f, -0.5f, -0.5f);
glTexCoord2f(0.0f, 2.0f); glVertex3f(0.5f, 0.5f, -0.5f);

glBindTexture(GL_TEXTURE_2D, textureID[1]);
glNormal3f(-1.0f, 0.0f, 0.0f); // left face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, 0.5f, -0.5f);
glTexCoord2f(2.0f, 0.0f); glVertex3f(-0.5f, 0.5f, 0.5f);
glTexCoord2f(2.0f, 2.0f); glVertex3f(-0.5f, -0.5f, 0.5f);
glTexCoord2f(0.0f, 2.0f); glVertex3f(-0.5f, -0.5f, -0.5f);

glBindTexture(GL_TEXTURE_2D, textureID[1]);
glNormal3f(0.0f, -1.0f, 0.0f); // bottom face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, -0.5f, 0.5f);
glTexCoord2f(2.0f, 0.0f); glVertex3f(-0.5f, -0.5f, -0.5f);
glTexCoord2f(2.0f, 2.0f); glVertex3f(0.5f, -0.5f, -0.5f);
glTexCoord2f(0.0f, 2.0f); glVertex3f(0.5f, -0.5f, 0.5f);

glNormal3f(0.0f, 0.0f, -1.0f); // back face
glTexCoord2f(0.0f, 0.0f); glVertex3f(0.5f, -0.5f, -0.5f);
glTexCoord2f(2.0f, 0.0f); glVertex3f(-0.5f, -0.5f, -0.5f);
glTexCoord2f(2.0f, 2.0f); glVertex3f(-0.5f, 0.5f, -0.5f);
glTexCoord2f(0.0f, 2.0f); glVertex3f(0.5f, 0.5f, -0.5f);
glEnd();
}

//draw the scene here
void drawScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear the colour buffer to the colour previously set in glClearColor

glPushMatrix();
glColor3f(0.0, 0.0, 1.0);
glRotatef(gAngle, 1.0f, 1.0f, 1.0f);
drawCube();
glPopMatrix();

glFlush();
glutSwapBuffers(); //swap front and back buffers
}

//called when ASCII key pressed
void myKeypress(unsigned char key, int x, int y)
{
switch(key)
{
//if ESC pressed, quit program
case '\e': exit(1); //quit
free(tex_image); //free image
break;
default: break;
};
}

//called when no event in queue
void myIdleFunc()
{
gAngle += 0.1; //increment rotation angle

if(gAngle >= 360) //wrap around if rotation angle greater than 360
{
gAngle -= 360;
}

glutPostRedisplay(); //refresh display
}

int main(int argc, char* argv[])
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); //requests properties for the window (sets up the rendering context)
glutInitWindowSize(800, 600); //set window size in pixels
glutInitWindowPosition(50, 50); //set window position from top-left corner of display
glutCreateWindow("Texture"); //set window title
init(); //call function to setup states
glutDisplayFunc(drawScene); //register display callback function
glutReshapeFunc(resizeWindow); //register resize callback function
glutKeyboardFunc(myKeypress); //register non-ASCII keypress callback function
glutIdleFunc(myIdleFunc); //idle callback function

glutMainLoop(); //enter event loop
}





[Edited by - Murlok on April 21, 2010 12:25:27 AM]

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!