Well at work where I try to make this project I have ATI RADEON HD 5800. But i’ll post the entire code maybe I made a mistake somewhere, also i must mention that I am using a bmp texture that is loaded through another cpp and header file.
Here is the entire code:
// light2.cpp : Defines the entry point for the console application.
//
#include “stdafx.h”
// lighting.cpp : Defines the entry point for the console application.
//
#include “stdafx.h”
#include <iostream>
#include <stdlib.h>
#include “imageloader.h”
#ifdef APPLE
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include “imageloader.h”
using namespace std;
bool lightingEnabled = false; //Lighting enabled or disabled
bool textureEnabled = true;
int _change=1;
float _coord=1.0;
float _angle=30.0f;
float _xLight2=-6.0f;
float _uvcoordF1=0.0f;
float _uvcoordF2=5.0f;
float _uvcoordF3=10.0f;
//Makes the image into texture and returns the id of the texture
GLuint loadTexture(Image* image) {
GLuint textureId;
glGenTextures(1, &textureId); //Make room for our texture
glBindTexture(GL_TEXTURE_2D, textureId); //Tell OpenGl which texture to edit
//Map the image to the texture
glTexImage2D (GL_TEXTURE_2D, //Always GL_TEXTURE_2D
0, //0 for now
GL_RGB, //Format OpenGL uses for image
image->width, image->height, //Width and height
0, //The border of the image
GL_RGB, //GL_RGB because pixels are stored in RGB format
GL_UNSIGNED_BYTE, //GL_UNSIGNED_BYTE, because pixels are score as unsigned numbers
image->pixels); //The actual pixel data
return textureId; //Returns the id of the texture
}
GLuint textureId;
void handleKeyPress (unsigned char key, int x, int y)
{
switch (key)
{
case ‘l’: //‘l’: Toggle lighting
lightingEnabled = !lightingEnabled;
if (!lightingEnabled)
glDisable(GL_LIGHTING);
else
glEnable(GL_LIGHTING);
break;
case ‘t’:
textureEnabled = !textureEnabled;
if (!textureEnabled)
/glBindTexture(0, _textureId)/glDeleteTextures(1, &textureId);
else
GLuint loadTexture(Image* image);
break;
case 27:
exit(0);
}
}
void initRendering()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
glEnable(GL_NORMALIZE);
glShadeModel(GL_SMOOTH);
glDisable(GL_LIGHTING);
Image* image = loadBMP("vtr.bmp");
textureId = loadTexture(image);
delete image;
}
void handleResize (int w, int h)
{
glViewport (0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective (45.0, (double) w/ (double) h, 1.0, 200.0);
}
void update (int value)
{
_angle +=2.0f;
if (_angle >360)
{
_angle -=360;
}
if (_change !=3)
++_change;
else
_change=1;
if (_xLight2 !=6)
++_xLight2;
else
_xLight2=-6;
_uvcoordF1 +=1.0f/10;
if (_uvcoordF1>2.5)
{
_uvcoordF1 -=2;
}
_uvcoordF2 +=1.0f/60;
if (_uvcoordF2>7.5)
{
_uvcoordF2 -=2;
}
_uvcoordF3 +=10/100;
if ( _uvcoordF3>30.5)
{
_uvcoordF3 -=20;
}
glutPostRedisplay();
glutTimerFunc(25, update, 0);
}
/*void update2 (int value)
{
_coord +=1.0f;
if (_coord > 6)
{
_coord -=6;
}
glutPostRedisplay();
glutTimerFunc(250, update2, 0);
}*/
void drawScene()
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 1.0, -6.0);
//Add ambient light
GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
//Add positioned light
GLfloat lightColor0[] = {0.5, 0.5, 0.5, 1.0};
GLfloat lightPos0[] = {4.0, 0.0, 8.0, 1.0};
glLightfv (GL_LIGHT0, GL_DIFFUSE, lightColor0);
glLightfv (GL_LIGHT0, GL_POSITION, lightPos0);
//Add directed light
GLfloat lightColor1[] = {0.5, 0.2, 0.2, 1.0};
//Coming from the direction
GLfloat lightPos1[] = {-1.0, 0.5, 0.5, 0.0};
glLightfv (GL_LIGHT1, GL_DIFFUSE, lightColor1);
glLightfv (GL_LIGHT1, GL_POSITION, lightPos1);
GLfloat lightColor2[] = {0.0, 1.0, 0.0, 1.0};
GLfloat lightPos2[]= {_xLight2, 0.0, 0.0, 1.0};
glLightfv (GL_LIGHT2, GL_DIFFUSE, lightColor2);
glLightfv (GL_LIGHT2, GL_POSITION, lightPos2);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glRotatef(_angle, 0.0, 1.0, 0.0);
glColor3f (1.0, 0.2f, 0.2f);
glBegin(GL_QUADS);
//Front
glNormal3f(0.0, 1.0, 0.0);
glTexCoord2f(0.0, 0.0);
glVertex3f(-2.5, -2.5, 2.5);
glTexCoord2f(1.0, 0.0);
glVertex3f(2.5, -2.5, 2.5);
glTexCoord2f(1.0, 1.0);
glVertex3f(2.5, -2.5, -2.5);
glTexCoord2f(0.0, 1.0);
glVertex3f(-2.5, -2.5, -2.5);
glEnd();
//Back
glEnable(GL_TEXTURE_2D);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor3f(1.0, 1.0, 1.0);
glTranslatef(_coord, 0.0, 0.0);
glBegin(GL_TRIANGLES);
glNormal3f(1.0, 0.0, -1.0);
glTexCoord2f(_uvcoordF1, 0.0);
glVertex3f(-2.5, -2.5, -2.5);
glTexCoord2f(_uvcoordF2, 5.0);
glVertex3f(0.0, 2.5, -2.5);
glTexCoord2f(_uvcoordF3, 0.0);
glVertex3f(2.5, -2.5, -2.5);
glEnd();
glPopMatrix();
//Left
glDisable(GL_TEXTURE_2D);
glColor3f(1.0, 0.7, 0.3);
glBegin(GL_QUADS);
glNormal3f(1.0, 0.0, 0.0);
glVertex3f(-2.5, -2.0, 2.5);
glVertex3f(-2.5, -2.5, -2.5);
glVertex3f(-2.5, 2.5, -2.5);
glVertex3f(-2.5, 2.5, 2.5);
glEnd();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (400, 400);
glutCreateWindow ("Lighting -videotutorialsrock.com");
initRendering();
glutKeyboardFunc (handleKeyPress);
glutDisplayFunc(drawScene);
glutReshapeFunc (handleResize);
glutTimerFunc (25, update, 0);
/*glutTimerFunc(100, update2, 0);*/
glutMainLoop();
return 0;
}