PDA

View Full Version : Cannot work out why no texture mapping



nick2price
11-30-2008, 11:11 AM
I have a program where this exact code works and texture maps my car. But now i have loaded my car into a skybox, there is no texture at all. I have double checked the code against the working one and copied it all again, but still no result. Texture is in correct folder. If anyone can notice why this might not be working now, would be a great help.
cheers

loading skymap and car



#include "stdafx.h"
#include <windows.h>
#include <stdio.h> //for file access
#include <GL/glut.h>
#include <gl\gl.h>
#include "skybox.h"
#include "glbmp.h"
#include "coursework2.h"
#include "3dsloader.h"
#include "texture.h"

obj_type object;

bool skybox::LoadTexture(unsigned int & aTexture, const char * bitmap_file)
{
glbmp_t bitmap; //object to fill with data from glbmp


if(!glbmp_LoadBitmap(bitmap_file, 0, &amp;bitmap))
{
fprintf(stderr, "Error loading bitmap file: %s\n", bitmap_file);
}

//generate and bind the OpenGL texture
glGenTextures(1, &amp;aTexture);
glBindTexture(GL_TEXTURE_2D, aTexture);

//copy data from bitmap into texture
glTexImage2D(GL_TEXTURE_2D, 0, 3, bitmap.width, bitmap.height,
0, GL_RGB, GL_UNSIGNED_BYTE, bitmap.rgb_data);

//set up texture filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

//free the bitmap
glbmp_FreeBitmap(&amp;bitmap);

return true;
}



void skybox::Init() {

OFFSET = 1/512.0; //set offset value for skybox

LoadTexture(texture[0], "images/1.bmp");
LoadTexture(texture[1], "images/2.bmp");
LoadTexture(texture[2], "images/3.bmp");
LoadTexture(texture[3], "images/4.bmp");
LoadTexture(texture[4], "images/5.bmp");
LoadTexture(texture[5], "images/6.bmp");

}

void skybox::Init2(){

glClearColor(0.0, 0.0, 0.0, 0.0); // This clear the background color to black
glShadeModel(GL_SMOOTH); // Type of shading for the polygons

// Viewport transformation
glViewport(0,0,800,600);

// Projection transformation
glMatrixMode(GL_PROJECTION); // Specifies which matrix stack is the target for matrix operations


glEnable(GL_DEPTH_TEST); // enable the depth test (also called z buffer)
glPolygonMode (GL_FRONT_AND_BACK, GL_FILL); // Polygon rasterization mode (polygon filled)

glEnable(GL_TEXTURE_2D); // This Enable the Texture mapping

Load3DS (&amp;object,"GTR.3ds");

object.id_texture=LoadBitmap("body.bmp");


}

void skybox::draw() {

glPushMatrix();

glColor3f(1.0f,1.0f,1.0f); //Set colour to White
glEnable(GL_TEXTURE_2D); //Enable texture mapping
glDisable (GL_DEPTH_TEST); //Disable depth testing

//before you can use a texture you have to bind it
glBindTexture(GL_TEXTURE_2D, texture[0 + text_offset]);

glBegin (GL_QUADS);

glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET,0.0 + OFFSET); glVertex3d (-10.0,-10.0,10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET,0.0 + OFFSET); glVertex3d (-10.0,-10.0,-10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET,1.0 - OFFSET); glVertex3d (-10.0,10.0,-10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET,1.0 - OFFSET); glVertex3d (-10.0,10.0,10.0);

glEnd ();

glBindTexture(GL_TEXTURE_2D, texture[1 + text_offset]);
glBegin (GL_QUADS);

glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET,0.0 + OFFSET); glVertex3d (-10.0,-10.0,-10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET,0.0 + OFFSET); glVertex3d (10.0,-10.0,-10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET,1.0 - OFFSET); glVertex3d (10.0,10.0,-10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET,1.0 - OFFSET); glVertex3d (-10.0,10.0,-10.0);
glEnd ();

glBindTexture(GL_TEXTURE_2D, texture[2 + text_offset]);
glBegin (GL_QUADS);

glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET ,0.0 + OFFSET); glVertex3d (10.0,-10.0,-10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET ,0.0 + OFFSET); glVertex3d (10.0,-10.0,10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET ,1.0 - OFFSET); glVertex3d (10.0,10.0,10.0);
glNormal3d (0.0,0.0,10.0); glTexCoord2d (0.0 + OFFSET ,1.0 - OFFSET); glVertex3d (10.0,10.0,-10.0);

glEnd ();

glBindTexture(GL_TEXTURE_2D, texture[3 + text_offset]);
glBegin (GL_QUADS);

glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET ,0.0 + OFFSET); glVertex3d (10.0,-10.0,10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET ,0.0 + OFFSET); glVertex3d (-10.0,-10.0,10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET ,1.0 - OFFSET); glVertex3d (-10.0,10.0,10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET ,1.0 - OFFSET); glVertex3d (10.0,10.0,10.0);

glEnd ();

glBindTexture(GL_TEXTURE_2D, texture[4 + text_offset]);
glBegin (GL_QUADS);

glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET ,0.0 + OFFSET); glVertex3d (-10.0,-10.0,10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET ,0.0 + OFFSET); glVertex3d (10.0,-10.0,10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET ,1.0 - OFFSET); glVertex3d (10.0,-10.0,-10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET ,1.0 - OFFSET); glVertex3d (-10.0,-10.0,-10.0);

glEnd ();

glBindTexture(GL_TEXTURE_2D, texture[5 + text_offset]);
glBegin (GL_QUADS);

glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET ,0.0 + OFFSET); glVertex3d (-10.0,10.0,-10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET ,0.0 + OFFSET); glVertex3d (10.0,10.0,-10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET ,1.0 - OFFSET); glVertex3d (10.0,10.0,10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET ,1.0 - OFFSET); glVertex3d (-10.0,10.0,10.0);

glEnd ();

//Re-Enable the depth test
glEnable(GL_DEPTH_TEST);
//disable texture mapping
glDisable(GL_TEXTURE_2D);
glPopMatrix();


}

void skybox::draw_car(){

int l_index;

glMatrixMode(GL_MODELVIEW); // Modeling transformation

glBindTexture(GL_TEXTURE_2D, object.id_texture); // set the active texture

glBegin(GL_TRIANGLES);
for (l_index=0;l_index<object.polygons_qty;l_index++)
{
//----------------- FIRST VERTEX -----------------
// Texture coordinates of the first vertex
glTexCoord2f( object.mapcoord[ object.polygon[l_index].a ].u,
object.mapcoord[ object.polygon[l_index].a ].v);
// Coordinates of the first vertex
glVertex3f( object.vertex[ object.polygon[l_index].a ].x,
object.vertex[ object.polygon[l_index].a ].y,
object.vertex[ object.polygon[l_index].a ].z); //Vertex definition

//----------------- SECOND VERTEX -----------------
// Texture coordinates of the second vertex
glTexCoord2f( object.mapcoord[ object.polygon[l_index].b ].u,
object.mapcoord[ object.polygon[l_index].b ].v);
// Coordinates of the second vertex
glVertex3f( object.vertex[ object.polygon[l_index].b ].x,
object.vertex[ object.polygon[l_index].b ].y,
object.vertex[ object.polygon[l_index].b ].z);

//----------------- THIRD VERTEX -----------------
// Texture coordinates of the third vertex
glTexCoord2f( object.mapcoord[ object.polygon[l_index].c ].u,
object.mapcoord[ object.polygon[l_index].c ].v);
// Coordinates of the Third vertex
glVertex3f( object.vertex[ object.polygon[l_index].c ].x,
object.vertex[ object.polygon[l_index].c ].y,
object.vertex[ object.polygon[l_index].c ].z);
}
glEnd();


}




Main class

#include "stdafx.h"
#include <windows.h>
#include <stdio.h> //for file access
#include <gl\gl.h>
#include <GL/glut.h>
#include "skybox.h"
#include "coursework2.h"


int screen_width=800;
int screen_height=600;

skybox aSkyBox;

double rotation_x=0, rotation_x_increment=0.1;
double rotation_y=0, rotation_y_increment=0.05;
double rotation_z=0, rotation_z_increment=0.03;

// Flag for rendering as lines or filled polygons
int filling=1; //0=OFF 1=ON

GLvoid ReSizeGLScene(GLsizei width, GLsizei height) {

if (height<=0) { height=1; }

glViewport(0,0,width,height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW);
glutPostRedisplay ();
}

int InitGL(GLvoid) {

glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH); //Try GL_FLAT to see the difference
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); //Background colour
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST); //Enables depth testing
glDepthFunc(GL_LEQUAL); //Specifies how Depth testing is done
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); //Affects quality of colour and texture interpolation
glFrontFace (GL_CCW); //Specifies that the front of a face is defined
//in a Counter Clockwise manner
glCullFace(GL_BACK); //Specifies that the back of faces are not shown
//so OpenGL does not need to draw things twice
glEnable(GL_CULL_FACE); //Enables culling of faces


aSkyBox.Init(); //Initalize the Skybox
aSkyBox.Init2();

return TRUE;
}


void keyboard_s (int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_UP:
rotation_x_increment = rotation_x_increment +0.005;
glutPostRedisplay();
break;
case GLUT_KEY_DOWN:
rotation_x_increment = rotation_x_increment -0.005;
glutPostRedisplay();
break;
case GLUT_KEY_LEFT:
rotation_y_increment = rotation_y_increment +0.005;
glutPostRedisplay();
break;
case GLUT_KEY_RIGHT:
rotation_y_increment = rotation_y_increment -0.005;
glutPostRedisplay();
break;
}
}

void display(void) {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0,0.0,-10);

rotation_x = rotation_x + rotation_x_increment;
rotation_y = rotation_y + rotation_y_increment;
rotation_z = rotation_z + rotation_z_increment;

if (rotation_x > 359) rotation_x = 0;
if (rotation_y > 359) rotation_y = 0;
if (rotation_z > 359) rotation_z = 0;

glRotatef(rotation_x,1.0,0.0,0.0); // Rotations of the object (the model matrix is multiplied by the rotation matrices)
glRotatef(rotation_y,0.0,1.0,0.0);
glRotatef(rotation_z,0.0,0.0,1.0);

aSkyBox.draw();

glPushMatrix();
glScalef(0.1,0.1,0.1);
aSkyBox.draw_car();
glPopMatrix();
glFlush();
glutSwapBuffers();
}


int main(int argc, char **argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(screen_width,screen_height);
glutInitWindowPosition(0,0);
glutCreateWindow("Coursework 2");

glutReshapeFunc(ReSizeGLScene);
glutDisplayFunc(display);
glutSpecialFunc (keyboard_s);
InitGL();
glutMainLoop();

return(0);
}

trinitrotoluene
11-30-2008, 11:22 AM
In your skybox::draw() you call glEnable(GL_TEXTURE_2D) then after you call glDisable(GL_TEXTURE_2D). Do you call glEnable(GL_TEXTURE_2D) inside your skybox::draw_car() function to have texture enabled for your car? Inside your skybox::draw(), you call glBindTexture(GL_TEXTURE_2D, texture[0 + text_offset]);,glBindTexture(GL_TEXTURE_2D, texture[1 + text_offset]); ... glBindTexture(GL_TEXTURE_2D, texture[5 + text_offset]);. If text_offset == 0 this should be ok. If it is undefined, problem can occur.

nick2price
11-30-2008, 11:38 AM
I changed my skybox::draw_car function too

void skybox::draw_car(){

int l_index;
glPushMatrix();
glMatrixMode(GL_MODELVIEW); // Modeling transformation
glEnable(GL_TEXTURE_2D); //Enable texture mapping
glDisable (GL_DEPTH_TEST); //Disable depth testing

glBindTexture(GL_TEXTURE_2D, object.id_texture); // set the active texture

glBegin(GL_TRIANGLES);
for (l_index=0;l_index<object.polygons_qty;l_index++)
{
//----------------- FIRST VERTEX -----------------
// Texture coordinates of the first vertex
glTexCoord2f( object.mapcoord[ object.polygon[l_index].a ].u,
object.mapcoord[ object.polygon[l_index].a ].v);
// Coordinates of the first vertex
glVertex3f( object.vertex[ object.polygon[l_index].a ].x,
object.vertex[ object.polygon[l_index].a ].y,
object.vertex[ object.polygon[l_index].a ].z); //Vertex definition

//----------------- SECOND VERTEX -----------------
// Texture coordinates of the second vertex
glTexCoord2f( object.mapcoord[ object.polygon[l_index].b ].u,
object.mapcoord[ object.polygon[l_index].b ].v);
// Coordinates of the second vertex
glVertex3f( object.vertex[ object.polygon[l_index].b ].x,
object.vertex[ object.polygon[l_index].b ].y,
object.vertex[ object.polygon[l_index].b ].z);

//----------------- THIRD VERTEX -----------------
// Texture coordinates of the third vertex
glTexCoord2f( object.mapcoord[ object.polygon[l_index].c ].u,
object.mapcoord[ object.polygon[l_index].c ].v);
// Coordinates of the Third vertex
glVertex3f( object.vertex[ object.polygon[l_index].c ].x,
object.vertex[ object.polygon[l_index].c ].y,
object.vertex[ object.polygon[l_index].c ].z);
}
glEnd();
//Re-Enable the depth test
glEnable(GL_DEPTH_TEST);
//disable texture mapping
glDisable(GL_TEXTURE_2D);
glPopMatrix();
}

Still no texture though. The text offset for the skymapping seems to work ok. Could it be anything to do with the way i display it in my main class?

trinitrotoluene
11-30-2008, 11:50 AM
If texture mapping work for the sky map, try to replace object.id_texture=LoadBitmap("body.bmp"); with LoadTexture(object.id_texture, "body.bmp");

nick2price
11-30-2008, 12:05 PM
that wouldnt work because the first parameter needs to be an int array. The way i have it at the moment works in the following program where i only load in a car.




#include "stdafx.h"
#include <windows.h>
#include <GL/glut.h>
#include "coursework2.h"
#include "texture.h"
#include "3dsloader.h"


int screen_width=640;
int screen_height=480;



// Absolute rotation values (0-359 degrees) and rotation increments for each frame
double rotation_x=0, rotation_x_increment=0.1;
double rotation_y=0, rotation_y_increment=0.05;
double rotation_z=0, rotation_z_increment=0.03;

// Flag for rendering as lines or filled polygons
int filling=1; //0=OFF 1=ON


obj_type object;



/************************************************** ********
*
* init()
*
* Used to initialize OpenGL and to setup the world
*
************************************************** *******/

void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0); // This clear the background color to black
glShadeModel(GL_SMOOTH); // Type of shading for the polygons

// Viewport transformation
glViewport(0,0,screen_width,screen_height);

// Projection transformation
glMatrixMode(GL_PROJECTION); // Specifies which matrix stack is the target for matrix operations
glLoadIdentity(); // initialize the projection matrix as identity
gluPerspective(45.0f,(GLfloat)screen_width/(GLfloat)screen_height,10.0f,10000.0f); // define the "viewing volume"

glEnable(GL_DEPTH_TEST); // enable the depth test (also called z buffer)
glPolygonMode (GL_FRONT_AND_BACK, GL_FILL); // Polygon rasterization mode (polygon filled)

glEnable(GL_TEXTURE_2D); // This Enable the Texture mapping

Load3DS (&amp;object,"GTR.3ds");

object.id_texture=LoadBitmap("body.bmp"); // The Function LoadBitmap() return the current texture ID


}



/************************************************** ********
*
* resize(int,int)
*
* This routine must be called everytime i resize our window.
*
************************************************** *******/

void resize (int width, int height)
{
screen_width=width; // obtain the new screen width values and store it
screen_height=height; // Height value

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear both the color and the depth buffer so to draw the next frame
glViewport(0,0,screen_width,screen_height); // Viewport transformation

glMatrixMode(GL_PROJECTION); // Projection transformation
glLoadIdentity(); // initialize the projection matrix as identity
gluPerspective(45.0f,(GLfloat)screen_width/(GLfloat)screen_height,10.0f,10000.0f);

glutPostRedisplay (); // This command redraws the scene
}




/************************************************** ********
*
* display()
*
* This is the main rendering subroutine, called each frame
*
************************************************** *******/

void display(void)
{
int l_index;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); // Modeling transformation
glLoadIdentity(); // Initialize the model matrix as identity

glTranslatef(0.0,0.0,-300); // move the object forward (the model matrix is multiplied by the translation matrix)

rotation_x = rotation_x + rotation_x_increment;
rotation_y = rotation_y + rotation_y_increment;
rotation_z = rotation_z + rotation_z_increment;

if (rotation_x > 359) rotation_x = 0;
if (rotation_y > 359) rotation_y = 0;
if (rotation_z > 359) rotation_z = 0;

glRotatef(rotation_x,1.0,0.0,0.0); // Rotations of the object (the model matrix is multiplied by the rotation matrices)
glRotatef(rotation_y,0.0,1.0,0.0);
glRotatef(rotation_z,0.0,0.0,1.0);

glBindTexture(GL_TEXTURE_2D, object.id_texture); // set the active texture

glBegin(GL_TRIANGLES);
for (l_index=0;l_index<object.polygons_qty;l_index++)
{
//----------------- FIRST VERTEX -----------------
// Texture coordinates of the first vertex
glTexCoord2f( object.mapcoord[ object.polygon[l_index].a ].u,
object.mapcoord[ object.polygon[l_index].a ].v);
// Coordinates of the first vertex
glVertex3f( object.vertex[ object.polygon[l_index].a ].x,
object.vertex[ object.polygon[l_index].a ].y,
object.vertex[ object.polygon[l_index].a ].z); //Vertex definition

//----------------- SECOND VERTEX -----------------
// Texture coordinates of the second vertex
glTexCoord2f( object.mapcoord[ object.polygon[l_index].b ].u,
object.mapcoord[ object.polygon[l_index].b ].v);
// Coordinates of the second vertex
glVertex3f( object.vertex[ object.polygon[l_index].b ].x,
object.vertex[ object.polygon[l_index].b ].y,
object.vertex[ object.polygon[l_index].b ].z);

//----------------- THIRD VERTEX -----------------
// Texture coordinates of the third vertex
glTexCoord2f( object.mapcoord[ object.polygon[l_index].c ].u,
object.mapcoord[ object.polygon[l_index].c ].v);
// Coordinates of the Third vertex
glVertex3f( object.vertex[ object.polygon[l_index].c ].x,
object.vertex[ object.polygon[l_index].c ].y,
object.vertex[ object.polygon[l_index].c ].z);
}
glEnd();

glFlush(); // This force the execution of OpenGL commands
glutSwapBuffers();
}



/************************************************** ********
*
* The main routine
*
************************************************** *******/

int main(int argc, char **argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(screen_width,screen_height);
glutInitWindowPosition(0,0);
glutCreateWindow("Coursework 2");
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc (resize);
init();
glutMainLoop();

return(0);
}

I just cant spot anything i have done differently

trinitrotoluene
11-30-2008, 01:19 PM
that wouldnt work because the first parameter needs to be an int array.

False. the signature of the function bool skybox::LoadTexture(unsigned int & aTexture, const char * bitmap_file) tell that you pass an integer by reference not an array. When you call LoadTexture(texture[0], "images/1.bmp"); for your sky box,texture[0] is a scalar, texture is an array. This is basic C/C++ programming. Try it.

nick2price
11-30-2008, 01:23 PM
but if change it from what it is too
LoadTexture(object.id_texture, "body.bmp");

I get back
1>f:\graphics, c++\skybox\skybox\skybox.cpp(77) : error C2664: 'skybox::LoadTexture' : cannot convert parameter 1 from 'int' to 'unsigned int &'

trinitrotoluene
11-30-2008, 01:40 PM
What is the type of the field object.id_texture?

nick2price
11-30-2008, 01:44 PM
an int?

trinitrotoluene
11-30-2008, 01:49 PM
Can you modify it to an unsigned int. I only hope now that you will not have another error like this: cannot convert from a unsigned int to an unsigned int &. Can you provide the function signature of LoadBitmap("") to compare.

nick2price
11-30-2008, 02:00 PM
Sorry, i come from a strong java background, so i have never used things like unsigned ints before, have no idea what they are, will go and check. The signature for LoadBitmap is simply this
int LoadBitmap(char *filename)
{

As i say, i have a program with just the car model, and it is textured ok. There must be some sort of problem from taking out the code snipplets of the working program and putting them in my skybox program. I am not too sure though.

trinitrotoluene
11-30-2008, 02:11 PM
No problem, the only reason is that I want you to use LoadTexture in place of LoadBitmap is that we know that LoadTexture work correctly and I don't know what is inside LoadBitmap. In theory, the field object.texture_id should be unsigned int because when you pass it to glGenTextures(...), glBindTexture(...) these need a GLuint which is a typedef of unsigned int. The first thing to do here is to verify if the pointer that hold texture data is correct. Then go step by step until the texture is applied to the car.