hey,
I am just learning textures in OpenGl, from a certain tutorial site :
http://www.spacesimulator.net/tut3_texturemapping.html
the site had a good tutorial for texture, but after loading the .bmp picture, it goes to create its own function for vertex3f and such ( I think). In any matter, I have not learned that way of style, not yet anyway, but it seems that now I am stuck on how to apply the texture to polygons.
Can you help me?
here is the code where I loaded , made room and associated the
bmp file with openGl, all i need help now is to lather the texture
onto the a polygon, say a box for instance, please help me finish the rest.
this is my loadimage.cpp file
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<GL/glut.h>
#include<windows.h>
using namespace std;
int num_texture = -1;
int LoadBitmap(char* filename)
{
unsigned char *l_texture;
int i,j=0;
File* file;
BITMAPFILEHEADER fileheader;
BITMAPINFOHEADER infoheader;
RGBTRIPLE rgb;
num_texture++;
if(file = (fopen(filename,"rb") == NULL)//if file does not exist return -1
return 0;
fread(&fileheader,sizeof(fileheader),1,file);
fseek(file,sizeof(fileheader),SEEK_SET);
fread(&infoheader,sizeof(infoheader),1,file);
l_texture = (byte* ) malloc(infoheader.biWidth * infoheader.biHeight * 4);
memset(l_texture,0, malloc(infoheader.biWidth * infoheader.biHeight * 4);
for (i=0; i < infoheader.biWidth * infoheader.biHeight; i++)
{
fread(&rgb, sizeof(rgb), 1, file);
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(file);
glBindTexture(GL_TEXTURE_2D,num_texture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexImage2D(GL_TEXTURE_2D, 0, 4, infoheader.biWidth, infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, infoheader.biWidth, infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);
free(l_texture);
return num_texture;
}
and here is the code for 3d box for which I want to lather the texture with, ( main.cpp)
#include<iostream>
#include<cstdlib>
#include<GL/glut.h>
using namespace std;
void InitGL()
{
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
//glEnable(GL_COLOR_MATERIAL);
//glClearColor(0.0f,0.0f,0.0f,1.0f);
//glClear(1.0f);
//glEnable(GL_LIGHTING);
//glEnable(GL_LIGHT0);
//glEnable(GL_LIGHT1);
//glEnable(GL_NORMALIZE);
glEnable(GL_SMOOTH);
glClearColor(0.0f,0.0f,0.0f,0.0f);
}
void EnableLight()
{
//NOT SET UP YET...
}
void handleResize(int width, int height)
{
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(double)width / (double)height,1.0f,200.0f);
}
void keyboard(unsigned char key,int x, int y)
{
switch(key)
{
case 27: exit(0);
case 'l': EnableLight(); // not set up yet
break;
}
}
GLfloat Yrot = 0.0f;
void disp()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0.0f,0.0f,0.0f,1.0f);
glTranslatef(0.0f,0.0f,-5.0f);
glRotatef(Yrot,0.0f,1.0f,0.0f);
glBegin(GL_QUADS);
//Front face
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f,-1.0f,0.0f);
glVertex3f(1.0f,-1.0f,0.0f);
glVertex3f(1.0f,1.0f,0.0f);
glVertex3f(-1.0f,1.0f,0.0f);
//Right face.
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(1.0f,-1.0f,0.0f);
glVertex3f(1.0f,-1.0f,-1.0f);
glVertex3f(1.0f,1.0f,-1.0f);
glVertex3f(1.0f,1.0f,0.0f);
//Back face.
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(1.0f,-1.0f,-1.0f);
glVertex3f(1.0f,1.0f,-1.0f);
glVertex3f(-1.0f,1.0f,-1.0f);
//Left face.
glColor3f(1.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,0.0f);
glVertex3f(-1.0f,1.0f,0.0f);
glVertex3f(-1.0f,1.0f,-1.0f);
glEnd();
glutPostRedisplay();
glutSwapBuffers();
}
void update_rot(int val)
{
Yrot += 3.5;
if(Yrot > 360)
Yrot = 0;
glutTimerFunc(50,update_rot,0);
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(550,550);
glutInitWindowPosition(300,100);
glutCreateWindow("Texture Practice!!!!!");
InitGL();
glutDisplayFunc(disp);
glutKeyboardFunc(keyboard);
glutReshapeFunc(handleResize);
glutTimerFunc(50,update_rot,0);
glutMainLoop();
return 0;
}