• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.

Archived

This topic is now archived and is closed to further replies.

Blackstream

Having a newbie light problem

5 posts in this topic

I don't think I'm fully understanding how light in OpenGL works... Anyways, I'm trying to get a colored light shining on a different colored object. My light colors are set with GLfloat ambientLight[] = { 0.0, 0.3f, 0.0f, 1.0f }; GLfloat diffuseLight[] = { 0.0f, 0.7f, 0.0f, 1.0f }; and my object color is set via color tracking with glColor3f(1.0, 0.0, 0.0); However, in my scene, I only get a dark red cube, not a red cube with a green light on it. Can somebody please tell me what I'm doing wrong? Here is my code
#include 
#include 
#include "3dMath.h"

GLfloat xrot = 0.0f;
GLfloat yrot = 0.0f;

//Define my cube triangles, 6 sides to a cube, so 12 triangles in all (with 3 vertices each)
float cube[12][3][3] = {
	{{-50, 50, -50},  {50, 50, -50},  {-50, -50, -50}}, //Triangle 1
	{{50, 50, -50},   {50, -50, -50}, {-50, -50, -50}}, //Triangle 2
	{{50, 50, -50},   {50, 50, 50},   {50, -50, -50}},  //Triangle 3
	{{50, 50, 50},    {50, -50, 50},  {50, -50, -50}},  //Triangle 4
	{{50, 50, 50},    {-50, -50, 50}, {50, -50, 50}},   //Triangle 5
	{{50, 50, 50},    {-50, 50, 50},  {-50, -50, 50}},  //Triangle 6
	{{-50, -50, 50},  {-50, 50, 50},  {-50, 50, -50}},  //Triangle 7
	{{-50, -50, 50},  {-50, 50, -50}, {-50, -50, -50}}, //Triangle 8
	{{50, 50, 50},    {50, 50, -50},  {-50, 50, -50}},  //Triangle 9
	{{50, 50, 50},    {-50, 50, -50}, {-50, 50, 50}},   //Triangle 10
	{{50, -50, 50},   {-50, -50, 50}, {-50, -50, -50}}, //Triangle 11
	{{50, -50, -50},  {50, -50, 50},  {-50, -50, -50}}};//Triangle 12

float cubeNormals[12][3]; //Contains one normal for each triangle in the cube.

void ChangeSize(GLsizei w, GLsizei h) {
	if(h == 0)
		h = 1;
	
	glViewport(0, 0, w, h);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluPerspective(60, (GLfloat)w/(GLfloat)h, 1, 400);
	
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void RenderScene() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glPushMatrix();
	
	glTranslatef(0.0f, 0.0f, -10.0f);

	glRotatef(xrot, 1.0f, 0.0f, 0.0f);
	glRotatef(yrot, 0.0f, 1.0f, 0.0f);

	//Draw the cube

	/*glBegin(GL_TRIANGLES);
		int i = 0;
		for(i = 0; i<12; i++) {
			glNormal3f(cubeNormals[0], cubeNormals[i][1], cubeNormals[i][2]);
			glVertex3f(cube[i][0][0], cube[i][0][1], cube[i][0][2]);
			glVertex3f(cube[i][1][0], cube[i][1][1], cube[i][1][2]);
			glVertex3f(cube[i][2][0], cube[i][2][1], cube[i][2][2]);
		}
	glEnd();*/

	glColor3f(1.0, 0.0, 0.0);

	glutSolidCube(5.0);

	glPopMatrix();

	glutSwapBuffers();
}

void Keyboard(int key, int x, int y) {
	if(key == GLUT_KEY_UP) xrot -= 5;
	if(key == GLUT_KEY_DOWN) xrot += 5;
	if(key == GLUT_KEY_RIGHT) yrot += 5;
	if(key == GLUT_KEY_LEFT) yrot -= 5;

	glutPostRedisplay();
}

void SetupRC() {
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

	//Setup a few flags
	glEnable(GL_CULL_FACE);
	glEnable(GL_DEPTH_TEST);
	
	//Calculate all the normals in the cube
	for(int i = 0; i < 12; i++) {
		calcNormal(cube[i], cubeNormals[i]);
		ReduceToUnit(cubeNormals[i]);
	}

	//Setup polygons and lights
	glShadeModel(GL_SMOOTH);

	/// Light values and coordinates
	GLfloat  ambientLight[] = { 0.0, 0.3f, 0.0f, 1.0f };
	GLfloat  diffuseLight[] = { 0.0f, 0.7f, 0.0f, 1.0f };
	GLfloat  lightPos[] = {0.f, 50.0f, 0.0f, 1.0f};

	// Enable lighting
	glEnable(GL_LIGHTING);

	// Setup and enable light 0
	glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
	glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
	glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
	glEnable(GL_LIGHT0);

	// Enable color tracking
	glEnable(GL_COLOR_MATERIAL);
	
	// Set Material properties to follow glColor values
	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

}

void main(void) {
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutCreateWindow("Primitives");
	glutDisplayFunc(RenderScene);
	glutReshapeFunc(ChangeSize);
	glutSpecialFunc(Keyboard);

	SetupRC();

	glutMainLoop();
}
 
Edited by - Blackstream on July 9, 2001 8:06:38 PM
0

Share this post


Link to post
Share on other sites
Well, I''m still trying to figure out lighting and now something else weird is happening.

I changed my object to a torus (easier to see lighting effects), got rid of the color tracking thing and replaced part of my initialization code with this


/// Light values and coordinates
GLfloat ambientLight[] = { 0.0f, 0.0f, 1.0f, 1.0f };
GLfloat diffuseLight[] = { 0.0f, 0.0f, 1.0f, 1.0f };
GLfloat ambient[] = {1.0, 0.0, 0.0, 0.0};
GLfloat diffuse[] = {1.0, 0.0, 0.0, 1.0};
GLfloat specular[] = {1.0, 1.0, 1.0, 0.0};
GLfloat lightPos[] = {0.f, 20.0f, 0.0f, 1.0f};

// Enable lighting
glEnable(GL_LIGHTING);

// Setup and enable light 0
glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
glEnable(GL_LIGHT0);

glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
glMateriali(GL_FRONT, GL_SHININESS, 128);


But my diffuse color seems weird. If I have it set to something like 1.0, 1.0, 1.0, 1.0, my torus looks like a normal, red, lighted torus. But if I have it something like 0.0, 1.0, 0.0, 1.0, it looks like a solid dark red torus (no variation of color), with some specular highlights.... instead of a red torus with a green light shining on it. I''m very confused.
0

Share this post


Link to post
Share on other sites
I''m wondering, could this actually be a hardware problem? I tried using the Material and Lighting studio from the OpenGL SuperBible, and it doesn''t seem to be able to do colored lights very well, if at all...

Hmm, I just messed with it some more... When I messed with the colors some more, it seemed that the objects material colors merely affected what colors of light affected it and how much. Like the redder the object, the more affected the color you see is by red light. And if the object has 0 blue and 0 green, then those color factors of the light don''t affect it at all.

Is that how light is supposed to work? I think what I have here is not bad code, but a misunderstanding of how light works in OpenGl.
0

Share this post


Link to post
Share on other sites
>>ike the redder the object, the more affected the color you see is by red light. And if the object has 0 blue and 0 green, then those color factors of the light don''t affect it at all.
Is that how light is supposed to work<<

i had a v quick read and Yes this is the way lighting works. exactlly the same as in real life go into a dark room and shine a red light on a blue piece of paper the paper should appear black.

http://www.frii.com/~martz/oglfaq/

http://members.xoom.com/myBollux
0

Share this post


Link to post
Share on other sites
Ahh thanks for confirming my suspision! It truely was an odd experience. In my mind I saw this red torus being lit up green like.

Actually, now that you speak of it, it makes a lot more sense. How can I see a green object with a red light, if there was never any green in the light to begin with?

Thanks for your help!

-Blackstream

Will you, won''t you, will you, won''t you, won''t you take my virus?

-The Mad Hacker

Blackstream''s Webpage
0

Share this post


Link to post
Share on other sites
Well if your getting a dark reddish looking cube then its working just fine. If you knew the math OpenGL uses to calculate the color values for each vertex from the lighting info you would understand why. Its doing like in real life, the color of the light is getting scaled by the color of the cube. It just takes the material color which is the color of your cube and the light color and multiplies them together.

You could go get a red cube of some sort like a block and shine a green light on it and you will see what im talking about here. I would suggest reading up on OpenGL''s lighting model. The lighting section in the opengl superbible is pretty good.

This page will show you the math behind the scenes of what opengl does to calculate the vertex color. http://reality.sgi.com/blythe/sig99/advanced99/notes/node410.html

-SirKnight
0

Share this post


Link to post
Share on other sites