Switch to full style
C++ openGL code examples
Post a reply

Add light to texture

Sun Apr 01, 2012 3:42 pm

In this example we add light to a textured cube, we also calculate the normal for each cube surface. You will notice that the shading is flat and this because we define only one normal vertex for the surface. To achieve smooth shading you must define normal for all polygon vertices using openGL.
cpp code
#include <stdlib.h>
#include <GL/glut.h>
#include<math.h>
#include<windows.h>
#include "RgbImage.h"
GLfloat xRotated, yRotated, zRotated;
GLuint texture[3]; // Storage For One Texture ( NEW )
GLfloat qaGreen[] = {0.0, 1.0, 0.0, 1.0}; //Green Color
GLfloat qaWhiteAM[] = {1.0, 1.0, 1.0, 1.0}; //White Ambient Material Color
GLfloat qaWhiteDF[] = {1.0, 1.0, 1.0, 1.0}; //White Diffuse Material Color
GLfloat qaWhite[] = {1.0, 1.0, 1.0, 1.0}; //White Color
GLfloat NormalX=0;
GLfloat NormalY=0;
GLfloat NormalZ=0;

// Set lighting intensity and color
GLfloat qaAmbientLight[] = {0.2, 0.2, 0.2, 1.0};// Whilte Ambient Light
GLfloat qaDiffuseLight[] = {1, 0.9, 1, 1.0}; // Whilte Diffuse Light
GLfloat qaSpecularLight[] = {1.0, 1.0, 1.0, 1.0}; // White Specular Light

// Light source position
GLfloat qaLightPosition[] = {3, 0, 0, 0};
/*
* Read a texture map from a BMP bitmap file.
*/
void CalcNormal(GLfloat point1_x,GLfloat point1_y,GLfloat point1_z,GLfloat point2_x,GLfloat


point2_y,GLfloat point2_z,GLfloat point3_x,GLfloat point3_y,GLfloat point3_z)
{
// Calculate vectors
GLfloat value1_x = point2_x - point1_x;
GLfloat value1_y = point2_y - point1_y;
GLfloat value1_z = point2_z - point1_z;

GLfloat value2_x = point3_x - point1_x;
GLfloat value2_y = point3_y - point1_y;
GLfloat value2_z = point3_z - point1_z;

// Get cross product of vectors
NormalX = (value1_y * value2_z) - (value2_y * value1_z);
NormalY = (value1_z * value2_x) - (value2_z * value1_x);
NormalZ = (value1_x * value2_y) - (value2_x * value1_y);

// Normalise final vector
float vLen = sqrt( (NormalX * NormalX) + (NormalY * NormalY) + (NormalZ * NormalZ) );

NormalX = (float)NormalX/vLen;
NormalY = (float)NormalY/vLen;
NormalZ = (float)NormalZ/vLen;


}
void initLighting()
{

// Enable lighting
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

// Set lighting intensity and color
glLightfv(GL_LIGHT0, GL_AMBIENT, qaAmbientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, qaDiffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, qaSpecularLight);

// Set the light position
glLightfv(GL_LIGHT0, GL_POSITION, qaLightPosition);

}
void loadTextureFromFile(char *filename,int index)
{
glClearColor (0.0, 0.0, 0.0, 0.0);

glEnable(GL_DEPTH_TEST);

RgbImage theTexMap( filename );

glGenTextures(1, &texture[index]); // Create The Texture
glBindTexture(GL_TEXTURE_2D, texture[index]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);


// Typical Texture Generation Using Data From The Bitmap

glTexImage2D(GL_TEXTURE_2D, 0, 3, theTexMap.GetNumCols(), theTexMap.GetNumRows(), 1, GL_RGB, GL_UNSIGNED_BYTE, theTexMap.ImageData() );

}

/*
* Draw the texture in the OpenGL graphics window
*/
void drawScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);

glLoadIdentity();
glTranslatef(0.0,0.0,-5);

glLightfv(GL_LIGHT0, GL_POSITION, qaLightPosition);


glPushMatrix();

glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, qaWhiteAM);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, qaWhiteDF);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, qaWhite);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 20);

glRotatef(yRotated, 0, 1, 0);
glRotatef(zRotated, 0, 0, 1);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
// Front Face

CalcNormal(-1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f,-1.0f, 1.0f, 1.0f);
glNormal3f(NormalX,NormalY,NormalZ);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(2.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(2.0f, 2.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 2.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
// Back Face
CalcNormal(-1.0f, -1.0f, -1.0f,-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f);
glNormal3f(NormalX,NormalY,NormalZ);

glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glEnd();


glBindTexture(GL_TEXTURE_2D, texture[1]);
glBegin(GL_QUADS);
// Top Face
//glBindTexture(GL_TEXTURE_2D, texture[1]);
CalcNormal( -1.0f, 1.0f, -1.0f,-1.0f, 1.0f, 1.0f,1.0f, 1.0f, -1.0f);
glNormal3f(NormalX,NormalY,NormalZ);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
// Bottom Face
CalcNormal(-1.0f, -1.0f, -1.0f,1.0f, -1.0f, -1.0f,-1.0f, -1.0f, 1.0f);
glNormal3f(NormalX,NormalY,NormalZ);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face
CalcNormal( 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f,1.0f, -1.0f, 1.0f);
glNormal3f(NormalX,NormalY,NormalZ);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glEnd();



glBindTexture(GL_TEXTURE_2D, texture[2]);
glBegin(GL_QUADS);
// Left Face
glBindTexture(GL_TEXTURE_2D, texture[2]);
CalcNormal(-1.0f, -1.0f, -1.0f,-1.0f, -1.0f, 1.0f,-1.0f, 1.0f, -1.0f);
glNormal3f(NormalX,NormalY,NormalZ);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd();
glPopMatrix();


glFlush();
glDisable(GL_TEXTURE_2D);

}

void resizeWindow(int x, int y)
{
if (y == 0 || x == 0) return; //Nothing is visible then, so return
//Set a new projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//Angle of view:40 degrees
//Near clipping plane distance: 0.5
//Far clipping plane distance: 20.0

gluPerspective(40.0,(GLdouble)x/(GLdouble)y,0.5,20.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0,0,x,y); //Use the whole window for rendering
}

void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
default:
break;
}
}

char* filename[] = {"./salt_on_spoon.bmp","./win030.bmp","./win044.bmp"};

void idleFunc(void)
{

yRotated += 0.01;
zRotated += 0.01;
drawScene();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(240, 240);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
for ( int i=0;i<3;i++)
{
loadTextureFromFile(filename[i],i);
}
initLighting();
glutDisplayFunc(drawScene);
glutReshapeFunc(resizeWindow);
glutKeyboardFunc(keyboard);
glutIdleFunc(idleFunc);
glutMainLoop();
return 0;
}


You will find missing files for reading the texture image in the previous example
Code:
http://www.codemiles.com/c-opengl-examples/using-many-textures-t9272.html



Attachments
LightTexture.png
LightTexture.png (46.85 KiB) Viewed 9331 times

Post a reply
  Related Posts  to : Add light to texture
 add texture to cube3D     -  
 how to attach a texture to polygon     -  
 Text Texture in java     -  
 Add texture to teapot object using     -  
 Add attenuation to light     -  
 Simple Light     -  
 Add spot light to object     -  
 Read an image bitmap (.bmp) file and draw it as texture     -  
 rotate sphere in a circle with light     -  
 J2ME Image Capturing with Flash Light     -  

Topic Tags

C++ Graphics