View Full Version : texturing a car model

12-04-2008, 03:26 PM
So i have code which textures a car, but as u all problably know by now, when i try it in my skybox, for some reason it wont texture. I just wanted to ask a question. This is my texture class

#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <GL/glut.h>
#include "texture.h"

/************************************************** ********
************************************************** *******/

int num_texture=-1; //Counter to keep track of the last loaded texture

/************************************************** ********
* FUNCTION LoadBitmap(char *)
* This function loads a bitmap file and return the OpenGL reference ID to use that texture
************************************************** *******/

int LoadBitmap(char *filename)
int i, j=0; //Index variables
FILE *l_file; //File pointer
unsigned char *l_texture; //The pointer to the memory zone in which i will load the texture

// windows.h gives me these types to work with the Bitmap files

num_texture++; // The counter of the current texture is increased

if( (l_file = fopen(filename, "rb"))==NULL) return (-1); // Open the file for reading

fread(&amp;fileheader, sizeof(fileheader), 1, l_file); // Read the fileheader

fseek(l_file, sizeof(fileheader), SEEK_SET); // Jump the fileheader
fread(&amp;infoheader, sizeof(infoheader), 1, l_file); // and read the infoheader

// Now i need to allocate the memory for my image (width * height * color deep)
l_texture = (byte *) malloc(infoheader.biWidth * infoheader.biHeight * 4);
// And fill it with zeros
memset(l_texture, 0, infoheader.biWidth * infoheader.biHeight * 4);

// At this point i can read every pixel of the image
for (i=0; i < infoheader.biWidth*infoheader.biHeight; i++)
// load an RGB value from the file
fread(&amp;rgb, sizeof(rgb), 1, l_file);

// And store it
l_texture[j+0] = rgb.rgbtRed; // Red component
l_texture[j+1] = rgb.rgbtGreen; // Green component
l_texture[j+2] = rgb.rgbtBlue; // Blue component
l_texture[j+3] = 255; // Alpha value
j += 4; // Go to the next position

fclose(l_file); // Closes the file stream

glBindTexture(GL_TEXTURE_2D, num_texture); // Bind the ID texture specified by the 2nd parameter

// The next commands sets the texture parameters
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // If the u,v coordinates overflow the range 0,1 the image is repeated
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // The magnification function ("linear" produces better results)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); //The minifying function

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); // i don't combine the color with the original surface color, use only the texture map.

// Finally define the 2d texture
glTexImage2D(GL_TEXTURE_2D, 0, 4, infoheader.biWidth, infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);

// And create 2d mipmaps for the minifying function
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, infoheader.biWidth, infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);

free(l_texture); // Free the memory i used to load the texture

return (num_texture); // Returns the current texture OpenGL ID

When i come to texture my car in another class, i load the model and get the bitmap id.

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

// 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");



And then when i texture it, i do this I(code from tutorial)

void skybox::draw_car(){

int l_index;

glMatrixMode(GL_MODELVIEW); // Modeling transformation

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

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);


Now what i would like to try and get an answer to is whether the skybox::draw_car() works in conjunction with the texture class, or would it be possible for me to try different code to load in the texture and use the exact same draw_car() method?

12-04-2008, 05:38 PM
Couple of hints:

1. Are you sure your texture is ever being loaded? Try drawing it on a quad or something.

2. You init your num_texture global var to 0 for the first loaded texture, but 0 is a special value for the default texture (I never tried it, but it may be reseting your state).

3. You never enable texturing inside your drawing code, maybe it is being disabled somewhere else?

12-05-2008, 12:04 PM
kool, somehow texturing works now, i swear i tried ur point 3 before. The top of my car is facing towards me on my screen, and i am trying to sort this out. So say you are looking at the computer screen, you would be looking at the top of my car. So i am trying to use glRotatef to get it facing the correct way. Cant work it out, i supposed i want to rotate the car backwards 90 degrees, but how would this be done in glrotate?
Also, i need to get my program to run like a tv advert, not movements through controls. What is this process called so i can research it? Is it just frame animation?