Original Post
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: 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.
#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