PDA

View Full Version : open gl lighting



clipsymaze
11-14-2012, 03:48 AM
903


Hi guys

i'm completely baffled, i'm trying to set lighting on the ground of the maze but every location i set, whether it be different, the light always ends up in the same location every time.


This is my floorvertex code:


#pragma once


#include "GXBase.h"


typedef struct {
float u; // texture u
float v; // texture v
float nx; // normal x
float ny; // normal y
float nz; // normal z
float x; // position x
float y; // position y
float z; // position z
}
FloorVertex;


static const FloorVertex Floor_Data[108] = {


//room 1




{
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, -7.5f, 0.0f, -32.5f /* 0 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, -7.5f, 0.0f, -17.5f /* 1 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 7.5f, 0.0f, -17.5f /* 1 */
},






{
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 7.5f, 0.0f, -17.5f /* 0 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 7.5f, 0.0f, -32.5f /* 1 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, -7.5f, 0.0f, -32.5f /* 1 */
},




//room 1c


{
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, -2.5f, 0.0f, -17.5f /* 0 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, -2.5f, 0.0f, -7.5f /* 1 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 2.5f, 0.0f, -7.5f /* 1 */
},






{
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 2.5f, 0.0f, -7.5f /* 0 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 2.5f, 0.0f, -17.5f /* 1 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, -2.5f, 0.0f, -17.5f /* 1 */
},


//room 2


{
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, -32.5f, 0.0f, -7.5f /* 0 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, -17.5f, 0.0f, -7.5f /* 1 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, -17.5f, 0.0f, 7.5f /* 1 */
},






{
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, -17.5f, 0.0f, 7.5f /* 0 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, -32.5f, 0.0f, 7.5f /* 1 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, -32.5f, 0.0f, -7.5f /* 1 */
},


//room 2c


{
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, -17.5f, 0.0f, -2.5f /* 0 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, -7.5f, 0.0f, -2.5f /* 1 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, -7.5f, 0.0f, 2.5f /* 1 */
},






{
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, -7.5f, 0.0f, 2.5f /* 0 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, -17.5f, 0.0f, 2.5f /* 1 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, -17.5f, 0.0f, -2.5f /* 1 */
},




//room 3

{

0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 32.5f, 0.0f, 7.5f /* 0 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 17.5f, 0.0f, 7.5f /* 1 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 17.5f, 0.0f, -7.5f /* 1 */
},






{
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 17.5f, 0.0f, -7.5f /* 0 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 32.5f, 0.0f, -7.5f /* 1 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 32.5f, 0.0f, 7.5f /* 1 */
},




//room 3c


{
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 17.5f, 0.0f, 2.5f /* 0 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 7.5f, 0.0f, 2.5f /* 1 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 7.5f, 0.0f, -2.5f /* 1 */
},






{
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 7.5f, 0.0f, -2.5f /* 0 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 17.5f, 0.0f, -2.5f /* 1 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 17.5f, 0.0f, 2.5f /* 1 */
},




//room 4


{
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 7.5f, 0.0f, 32.5f /* 0 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 7.5f, 0.0f, 17.5f /* 1 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, -7.5f, 0.0f, 17.5f /* 1 */
},






{
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, -7.5f, 0.0f, 17.5f /* 0 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, -7.5f, 0.0f, 32.5f /* 1 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 7.5f, 0.0f, 32.5f /* 1 */
},




//room 4c


{
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 2.5f, 0.0f, 17.5f /* 0 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 2.5f, 0.0f, 7.5f /* 1 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, -2.5f, 0.0f, 7.5f /* 1 */
},






{
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, -2.5f, 0.0f, 7.5f /* 0 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, -2.5f, 0.0f, 17.5f /* 1 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 2.5f, 0.0f, 17.5f /* 1 */
},




//room 5


{
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 7.5f, 0.0f, 7.5f /* 2 */
},


{
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, -7.5f, 0.0f, 7.5f /* 0 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, -7.5f, 0.0f, -7.5f /* 1 */
},


{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, -7.5f, 0.0f, -7.5f /* 1 */
},


{
1.0f, 1.0f, 0.0f, 1.0f,
0.0f, 7.5f, 0.0f, -7.5f /* 3 */
},


{
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 7.5f, 0.0f, 7.5f /* 2 */
},






};[/QUOTE]

and this is my main code:

[QUOTE]
#include "GXBaseCube.h"
#include "../../Common/GameFactory.h"
#include "GXBase.h"
#include "MazeVertices.h"
#include "GroundVertices.h"
#include "FloorVertices.h"
#include "RoofVertices.h"
#include <cmath>


using namespace gxbase;


GXBaseCube::GXBaseCube(void)
{
_texture0 = GameFactory::LoadTexture("08356Hill.bmp");
_texture1 = GameFactory::LoadTexture("08356Ground.bmp");
_texture2 = GameFactory::LoadTexture("08356Sky.bmp");
_texture3 = GameFactory::LoadTexture("Cool64x64.bmp");
_texture4 = GameFactory::LoadTexture("08356Floor.bmp");
_texture5 = GameFactory::LoadTexture("08356Maze.bmp");
}


GXBaseCube::~GXBaseCube(void)
{
}


void GXBaseCube::Draw(void)
{
glPushMatrix();
glTranslatef(_x, _y, _z);


glBindTexture(GL_TEXTURE_2D, _texture0);


static float angle = 0.0f;
angle += 0.0f;
glRotatef(angle, 1.0f, 0.5f, 0.2f);









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







//GLfloat lightpos[] = { 40.000000f, -40.000000f, -40.000000f, -40.000000f, 40.000000f, 40.000000f };
GLfloat lightpos[] =
{

0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 32.5f, 0.0f, 7.5f, /* 0 */




1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 17.5f, 0.0f, 7.5f, /* 1 */




1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 17.5f, 0.0f, -7.5f, /* 1 */








0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 17.5f, 0.0f, -7.5f, /* 0 */




1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 32.5f, 0.0f, -7.5f, /* 1 */




1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 32.5f, 0.0f, 7.5f /* 1 */



};
//GLfloat lightpos[] =
//{
//1.0f, 0.0f, 0.0f, 1.0f,
// 0.0f, -17.5f, 0.0f, -7.5f /* 1 */
//};
////GLfloat lightpos[] =
// {
/*1.0f, 0.0f, 0.0f, 1.0f,
0.0f, -17.5f, 0.0f, 7.5f*/ /* 1 */
// };


glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
glDisable(GL_LIGHTING);




//glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );



glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(CubeVertex), &Cube_Data[0].x);

glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, sizeof(CubeVertex), &Cube_Data[0].nx);


glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, sizeof(CubeVertex), &Cube_Data[0].u);









glDrawArrays(GL_TRIANGLES, 0, 36);//glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
//glPopMatrix();




//glPushMatrix();
glTranslatef(_x, _y, _z);


glBindTexture(GL_TEXTURE_2D, _texture1);


angle += 0.0f;
glRotatef(angle, 1.0f, 0.5f, 0.2f);




glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(GroundVertex), &Ground_Data[0].x);



glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, sizeof(GroundVertex), &Ground_Data[0].nx);


glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, sizeof(GroundVertex), &Ground_Data[0].u);


glDrawArrays(GL_TRIANGLES, 0, 36);
//glPopMatrix();


glTranslatef(_x, _y, _z);


glBindTexture(GL_TEXTURE_2D, _texture4);




//static float angle = 0.0f;
angle += 0.0f;
glRotatef(angle, 1.0f, 0.5f, 0.2f);



glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(FloorVertex), &Floor_Data[0].x);



glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, sizeof(FloorVertex), &Floor_Data[0].nx);


glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, sizeof(FloorVertex), &Floor_Data[0].u);


glColor3f(1.0f,1.0f,1.0f);
glDrawArrays(GL_TRIANGLES, 0, 108);




//glPushMatrix();
glTranslatef(_x, _y, _z);


glBindTexture(GL_TEXTURE_2D, _texture2);


angle += 0.0f;
glRotatef(angle, 1.0f, 0.5f, 0.2f);


glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(RoofVertex), &Roof_Data[0].x);


glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, sizeof(RoofVertex), &Roof_Data[0].nx);


glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, sizeof(RoofVertex), &Roof_Data[0].u);


glDrawArrays(GL_TRIANGLES, 0, 36);


glTranslatef(_x, _y, _z);


glBindTexture(GL_TEXTURE_2D, _texture5);


angle += 0.0f;
glRotatef(angle, 1.0f, 0.5f, 0.2f);


glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(MazeVertex), &Maze_Data[0].x);


glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, sizeof(MazeVertex), &Maze_Data[0].nx);


glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, sizeof(MazeVertex), &Maze_Data[0].u);


glDrawArrays(GL_TRIANGLES, 0, 244);








glPopMatrix();
}


void GXBaseCube::Update(void)
{
}


I will appreciate any help guys!!

Thank you.

Dark Photon
11-14-2012, 04:31 AM
... or ... blocks do a lot better on source code than
.... Fixed it for you.

For these old-style fixed-function lights, the MODELVIEW transform active when you set the light source position is what positions that location into the scene:

glLightfv(GL_LIGHT0, GL_POSITION, lightpos);

clipsymaze
11-14-2012, 04:46 AM
Thank you for fixing editing, about the lighting issue, i hate to ask too much, but could you show exactly what you mean?...im fairly new to open gl..

Thank you..

Dark Photon
11-14-2012, 05:51 PM
See these:

* http://www.cse.msu.edu/~cse872/tutorial3.html
* http://www.glprogramming.com/red/chapter05.html
* http://www.opengl.org/archives/resources/faq/technical/lights.htm

Search for "position". They all explain it.