943,846 Members | Top Members by Rank

Ad:
Jan 14th, 2009
0

please help with texture mapping on OpenGl.

Expand Post »
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;

}
Last edited by firstPerson; Jan 14th, 2009 at 3:16 am.
Similar Threads
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Jan 14th, 2009
0

Re: please help with texture mapping on OpenGl.

From main, (after calling glutInit and before calling glutMainLoop), call your bitmap load function, put the return value into a global int variable ( e.g. texture1 ), repeat for however many textures you want, ( i.e. texture2, texture3, etc.. ).

When you want to 'switch a texture on' call glBindTexture ( GL_TEXTURE2D, textureX ). You must do this OUTSIDE of a glBegin/glEnd block.

For every vertex, also send a texture co-ordinate. This is done the same way as you send normals, but you will nearly always want to do this once-per-vertex, e.g.:
glBindTexture ( GL_TEXTURE2D, texture1 );
glBegin ( GL_QUADS );
glTexcoord2f ( 0, 0 );
glVertex3f(-1.0f,-1.0f,0.0f);
glTexcoord2f ( 0, 1 );
glVertex3f(1.0f,-1.0f,0.0f);
glTexcoord2f ( 1, 1 );
glVertex3f(1.0f,1.0f,0.0f);
glTexcoord2f ( 1, 0 );
glVertex3f(-1.0f,1.0f,0.0f);
glEnd ( );
This will draw the entire texture on a single polygon. You don't need to call glEnd yet if you want to keep using the same texture for all of the faces.
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
Jan 14th, 2009
0

Re: please help with texture mapping on OpenGl.

thanks matt, but for some reason the image is not sticking to the polygon.

in my texture.h file I have :
extern int num_texture;

extern int Loadbitmap(char* filename);

in my texture.cpp file , I have :

#include<iostream>
#include <stdio.h>
#include <windows.h>
#include <GL/glut.h>
#include "texture.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++;

	file = fopen(filename,"rb");
	if(file == NULL)//if file does not exist return -1
			return -1;
	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, 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 in my main.cpp I have
#include<iostream>
#include<cstdlib>
#include<GL/glut.h>
#include<windows.h>
#include"texture.h"

using namespace std;



void InitGL()
{
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);
	//glEnable(GL_COLOR_MATERIAL);
	//glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
	//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);
	
	
	Loadbitmap("art");

}

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

	
	glEnable(GL_TEXTURE_2D);
	glBindTexture ( GL_TEXTURE_2D, num_texture );

	glBegin(GL_QUADS);
	//Front face
		glColor3f(1.0f,0.0f,0.0f);
		
		glTexCoord2f ( 0, 0 );
			glVertex3f(-1.0f,-1.0f,0.0f);
		glTexCoord2f ( 0, 1 );
			glVertex3f(1.0f,-1.0f,0.0f);
		glTexCoord2f ( 1, 1 );
			glVertex3f(1.0f,1.0f,0.0f);
		glTexCoord2f ( 1, 0 );
			glVertex3f(-1.0f,1.0f,0.0f);

			glDisable(GL_TEXTURE_2D);
	//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;

}


In my texture folder I have a picture saved as art . Its in the attachments. and in this url:
http://www.cc.gatech.edu/cpl/project...ats_resize.bmp


I dont know whats wrong with the program. I mean that i have loaded the bitmap but the texture is not aplying for some reason. Please help.
Attached Images
File Type: bmp art.bmp (44.3 KB, 22 views)
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Jan 17th, 2009
0

Re: please help with texture mapping on OpenGl.

Some things:

- Check the return value of LoadBitmap ( i.e. does the bitmap load atall? ).

- You are loading the file "art" ( without a file extension ). If you want to fopen "art.bmp" file, you have to say so. Also check that the bitmap file is in the application's working directory, or use an absolute path to the file instead.

- You should really call glGenTextures to get a new texture index. All texture indices might work without doing this on some OpenGL implementations, but you shouldn't rely on that being the case.

- You're 'not allowed' to call glDisable within a glBegin/glEnd block.
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
Jan 17th, 2009
0

Re: please help with texture mapping on OpenGl.

Thanks, the problem was 1) the .bmp file was not a power of 2 and
i did not include, .bmp, in art. Thank you very much .
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Game Development Forum Timeline: help with OPenGl
Next Thread in Game Development Forum Timeline: equation of a circle





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC