Skip to main content
gamedev.net
🔒 Locked

help binding textures to objects

Started by PB Baller27 Sep 3, 2006 at 5:47 PM 4 replies 950+ views
Original Post
PB Baller27
PB Baller27
Hey sup everyone. Im a student in highschool trying to get my head around openGL. I have been going to varius sites for tutorials on binding textures to objects and I cant understand what they are doing. Every site has some different technique on binding the texture. I wanted to learn how to bind textures and apply it to a small(and amature) project below. code:

	

	#include <GL/glut.h>
	#include <cstdio>
	#include <cmath>

	// variable locations

		GLdouble xEye = 0.0f;
		GLdouble yEye = 0.0f;
		GLdouble zEye = 0.0f;
		
		GLfloat angle = 0.0f;
		GLfloat rotateAngle = 0.0f;
		//-----------------\\

		GLfloat bLight = 0.0f;
		GLfloat bWire = 0.0f;
		//-----------------\\

		GLfloat bulletX = 0.0f;
		GLfloat bulletZ = 0.0f;
		bool fire = false;
		//-----------------\\

		GLdouble x = -10.0f;
		GLdouble z = -10.0f;
		//-----------------\\
//------------------------------------------------------------------------------------------------------------------------------------------------------------
	void RenderScene();
	void SetupRC();
	void ChangeScene(GLsizei, GLsizei);
	void timeFunc(int);
	void ProcessKeys(unsigned char, int, int);
	void ProcessMenu(int);
	void DrawConstObject();
	void checkAtrs();

//------------------------------------------------------------------------------------------------------------------------------------------------------------
	int main()
		{

			glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
			glutInitWindowSize(800, 600);

			glutCreateWindow("MY Window");

			glutCreateMenu(ProcessMenu);
			glutAddMenuEntry("Light On/Off",1);
			glutAddMenuEntry("Shaded/Wire" ,2);
			glutAttachMenu(GLUT_RIGHT_BUTTON);

			glutDisplayFunc(RenderScene);
			glutReshapeFunc(ChangeScene);
			glutTimerFunc(33,timeFunc, 1);
			glutKeyboardFunc(ProcessKeys);

			SetupRC();
			glutMainLoop();
			return 0;
		}
//------------------------------------------------------------------------------------------------------------------------------------------------------------
	void ProcessMenu(int value)
		{
			switch(value)
				{
					case 1:
						bLight = !bLight;
						break;
					case 2:
						bWire = !bWire;
						break;
					default:
						break;
				}
			glutPostRedisplay();
		}// end func
//------------------------------------------------------------------------------------------------------------------------------------------------------------
	void RenderScene()
		{
			const GLfloat radius = 2.5f;
			const GLfloat size = 10.0f;
			const GLfloat sphereY = 1.0f;

			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
			glColor3f(0.0f, 1.0f, 0.0f);
			// check attributes
				checkAtrs();

			glPushMatrix();
					 x = 50.0f*sin(angle) + xEye;
					 z = 50.0f*cos(angle) + zEye;
					gluLookAt(xEye, yEye, zEye, x, yEye, z, 0.0, 1.0, 0.0);
				// draw objects
					DrawConstObject();
		
				glLoadIdentity();
			glPopMatrix();
			glutSwapBuffers();
		}
//------------------------------------------------------------------------------------------------------------------------------------------------------------
	void DrawConstObject()
		{		
			GLfloat planeExtent = 150.0f;
			GLfloat step = 5.0f;
			GLfloat planeY = -2.0f;
			GLfloat iLine;

				// draw ground
					glBegin(GL_LINES);
					/*
							glVertex3f(-planeExtent, planeY, planeExtent);
							glVertex3f(planeExtent, planeY, planeExtent);
							glVertex3f(planeExtent, planeY, -planeExtent);
							glVertex3f(-planeExtent, planeY, -planeExtent);					
					*/
							for(iLine = -planeExtent; iLine <= planeExtent; iLine += step)
							{
								glVertex3f(iLine, planeY, planeExtent);
								glVertex3f(iLine, planeY, -planeExtent);

								glVertex3f(planeExtent, planeY, iLine);
								glVertex3f(-planeExtent, planeY, iLine);
							}
					glEnd();
					glPushMatrix();
						glTranslatef(0.0f, 5.0f, 1.0f);
						glRotatef(rotateAngle, 1.0, 0.0, 0.0);
						glRotatef(rotateAngle, 0.0, 1.0, 0.0);
						glutSolidSphere(10, 15, 15);
					glPopMatrix();

					if(fire)
						{
							glPushMatrix();
								glColor3f(1.0, 0.0, 0.0);
								glTranslatef(bulletX + xEye , 0.0, bulletZ + zEye );
								glutSolidSphere(.15, 15, 15);
							glPopMatrix();
						}

		}

//------------------------------------------------------------------------------------------------------------------------------------------------------------


//------------------------------------------------------------------------------------------------------------------------------------------------------------
	void checkAtrs()
		{
			if(bLight)
				glEnable(GL_LIGHTING);
			else
				glDisable(GL_LIGHTING);

			if(bWire)
				glPolygonMode(GL_FRONT, GL_FILL);
			else
				glPolygonMode(GL_FRONT, GL_LINE);
		}
//------------------------------------------------------------------------------------------------------------------------------------------------------------
	void SetupRC()
		{
				GLfloat  ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f };
				GLfloat  diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f };
				GLfloat lightPos[] = {  15.0f, 10.0f, 0.0f };

				glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
				glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
				glShadeModel(GL_SMOOTH);
				glEnable(GL_DEPTH_TEST);	// Hidden surface removal
				glFrontFace(GL_CCW);		// Counter clock-wise polygons face out
				glEnable(GL_CULL_FACE);		// Do not calculate inside of jet

				//LIGHTING
				glEnable(GL_LIGHTING);
				glEnable(GL_LIGHT0);

				glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
				glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
				glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
				glEnable(GL_COLOR_MATERIAL);

				glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

				glHint(GL_PERSPECTIVE_CORRECTION_HINT ,GL_NICEST);
				glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);

				//FOG
				GLfloat fogColor[4] = { 0.0f, 0.0f, 0.0f, 0.0f };

				glEnable(GL_FOG);
				glFogfv(GL_FOG_COLOR, fogColor);
				glFogf(GL_FOG_START, 5.0f);
				glFogf(GL_FOG_END, 80.0f);
				glFogf(GL_FOG_DENSITY, 15.0f);
				glFogf(GL_FOG_MODE, GL_LINEAR);
		}
//------------------------------------------------------------------------------------------------------------------------------------------------------------
	void ChangeScene(GLsizei w, GLsizei h)
		{
			    GLfloat fAspect;
				 if(h == 0)
					  h = 1;
				 glViewport(0, 0, w, h);

				 fAspect = (GLfloat)w/(GLfloat)h;

				 glMatrixMode(GL_PROJECTION);
				 glLoadIdentity();

					gluPerspective(60.0f, fAspect, 1.0, 400.0);

				 glMatrixMode(GL_MODELVIEW);
				 glLoadIdentity();
		}// end function
//------------------------------------------------------------------------------------------------------------------------------------------------------------
	void timeFunc(int value)
		{
			rotateAngle += .5f;
		if(rotateAngle >= 359)
			rotateAngle = 0;

		if(fire)
			bulletZ += 1.0f;

		if(bulletZ == 50.0)
			{
				bulletZ = 0.0f;
				fire = false;
			}

			glutPostRedisplay();
			glutTimerFunc(33, timeFunc, 1);
		}
//------------------------------------------------------------------------------------------------------------------------------------------------------------
	void ProcessKeys(unsigned char key, int x, int y)
		{
			switch(key)
				{
					case 27:
						exit(0); 
						break;
					case 'a':
						angle += .1f;
						break;
					case 'd':
						angle -= .1f;
						break;
					case 'w':
						zEye += 1.0f;
						break;
					case 's':
						zEye -= 1.0f;
						break;
					case 'q':
						xEye += 1.0f;
						break;
					case 'e':
						xEye -= 1.0f;
						break;
					case ' ':
						fire = true;
						break;
					default:
						break;
				}	
		}// end function


So i was hoping that someone here could teach me in a quick step by step what is the most modern and efficient way to take an image and bind it to an object by using my project above.
swordfish
swordfish
OpenGL texturing seems complicated at first, but it is really simple.

1) Generate texture handle, aka texture 'name'. This is a unique integer ID that you will use to reference the texture.

GLuint texture;
glGenTextures(1, &texture); // generate 1 texture name

2) 'Bind' the texture for the initial texture setup. You will be binding again when you need to actually use the texture on some geometry.

glBindTexture(GL_TEXTURE_2D, texture);

3) Setup texture attributes. This isn't necessary in the most basic of texturing, so don't worry about it too much until you get a grip on what all of this does.

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); // tells OpenGL to 'modulate' the texture pixels with whatever color & lighting you'll be using

4) Setup texture paramters. Again, this isn't all that important - yet!

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

*) Keep in mind, that steps 3 & 4 could very well take place after step 5 if you want it to

5) Load image data. Remember, we are operating on the currently binded texture (step 2). Keep in mind that the 3, GL_RGB, and GL_UNSIGNED_BYTE input paramters to gluBuild2DMipMaps all rely on exactly what type of image you are loading. You may need to change these to suit your texture image.

int width, height;
BYTE* data;
// ...Load image data here...
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, data );

6) Use texture

// Enable texturing
glEnable(GL_TEXTURE_2D);

// Bind the texture you want to use
glBindTexture(GL_TEXTURE_2D, texture);

// Define geometry
// For each vertex of the geometry, define texture coordinate (UV)
glBegin( GL_QUADS );
glTexCoord2d(0.0,0.0); glVertex2d(0.0,0.0);
glTexCoord2d(1.0,0.0); glVertex2d(1.0,0.0);
glTexCoord2d(1.0,1.0); glVertex2d(1.0,1.0);
glTexCoord2d(0.0,1.0); glVertex2d(0.0,1.0);
glEnd();

7) Clean up when done

glDeleteTextures(1, &texture);
http://blog.protonovus.com/
PB Baller27
PB Baller27
perfect. thats exactly what i needed. Alot of the other tutorials added in little stuff which made it more complicated for me. I just needed the basic texturing commands.

However one question, what would you change 3, GL_RGB, and GL_UNSIGNED_BYTE input paramters TO if I had an different image type and WHAT image type are those parameters set at.

Also i dont see where you specify what image your loading in. Like how do you import the image you want into the cpp file.
Sirisian
Sirisian
Look into SDL_Image, it's a good way to load images. I used .png myself, but you may not need an alpha channel.

http://www.libsdl.org/projects/SDL_image/

though you don't have to use SDL_image to do it. If you look online you can probably find a tutorial for loading an image with just OpenGL.

Try NeHe.

[Edited by - Sirisian on September 3, 2006 9:30:43 PM]
PB Baller27
PB Baller27
um.. the whole idea was to not use a tutorial. Thats where I have been getting most of my problems. What I dont understand is how BYTE *date and
GLuint texture; are connected. Then I need to know how to actually import a file.
Sirisian
Sirisian
I don't wanna sound odd, but your going to have to learn how to interpret tutorials and use them in your coding.

However if you want to load a .bmp with no alpha channel. It's extremely simple.

http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=06

Click that link go to the second blue box and copy the function called:

AUX_RGBImageRec *LoadBMP(char *Filename)

it's seperated and has explanations between the function so just go to the function's end brace.

Also grab the loadTexture function. This function uses the loadBMP function.

you'll notice you need to declare

GLuint texture[1];

That will hold a texture surface. (GLuint is just an unsigned int). You don't have to use an array, the function just shows how an array of textures can be created. In reality you'd do it differently, using std::vector or something (if you know about dynamic arrays already if not don't worry about it yet, you can learn them later).

to use the fopen you need:

#include

if you don't understand, read the stuff in the blue box it describes everything fine.

Maybe someone else knows a better way, I've never used a .bmp image.

You can replace the texture array with a different variable name if you want.

Okay I think that's as much time as I'm willing to spend to help. All I did to get that nehe page was
http://www.google.com/search?hl=en&q=OpenGL+loading+an+texture&btnG=Google+Search

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.