PDA

View Full Version : lighting problem

jonn82
02-02-2004, 05:28 AM
Here's the code I have written...

#include <windows.h> // - Windows API and system calls
#include <stdio.h> // - Just for some ASCII messages
#include <gl/gl.h> // - The OpenGL API
#include <gl/glaux.h> // - A windows library extension API
#include <gl/glut.h> // - An interface and windows
#include <math.h> // management library
#include "visuals.h" // Header file for our OpenGL functions

#define PI 3.14159265

void Circle() //circle drawing function
{
GLint i;
GLfloat sine,cosine;
glBegin(GL_POLYGON);
for(i=1; i<100; i++)
{
cosine=cos(i*2*PI/100.0); //calculation of the circle's size via sin and cos values
sine=sin(i*2*PI/100.0);
glVertex2f(cosine,sine);
}
glEnd();
}

void Render()
{

glClear(GL_COLOR_BUFFER_BIT ); // Clean up the colour of the window
// and the depth buffer

/* glColor3f(1.0, 0.9, 0.0); // Set drawing colour
glRectf(-10.0, -10.0, 15.0, 25.0); //down left corner 2 first parameters + right up corner 2 last ones
glColor4f(0.2, 0.5, 1.0, 0.5); //color 4f supports RGB+alpha blending
glRectf(-7.0, -10.0, 25.0, 15.0); // Draw a built-in primitive */

/*glBegin(GL_LINE_STRIP);
glVertex2f(10,10);
glVertex2f(-20,10);
glVertex2f(15,-30);
glVertex2f(10,10); */
//glRotatef(30,1,0,0);
//glRotatef(-45,1,0,0);

glPushMatrix();
glTranslatef(0,-30,0);
glRotatef(80,1,0,0);
glColor3f(0.5, 0.5, 1.0);
gluCylinder(p, 40.0, 40.0, 7.0, 1000, 100);
glColor3f(1.0, 1.0, 1.0);
glScalef(40,40,40);
Circle();
glPopMatrix();

glPushMatrix();
glTranslatef(0,-30,0);
glRotatef(-80,1,0,0);
glColor3f(0.5, 0.5, 1.0);
gluCylinder(p1, 10.0, 10.0, 40.0, 1000, 100);
glPopMatrix();

glPushMatrix();
glTranslatef(30,-30,0);
glRotatef(-80,1,0,0);
glColor3f(1.0, 0.0, 0.0);
gluCylinder(p2, 2.0, 2.0, 40.0, 1000, 100);
glPopMatrix();

glPushMatrix();
glTranslatef(-30,-30,0);
glRotatef(-80,1,0,0);
glColor3f(1.0, 0.0, 0.0);
gluCylinder(p3, 2.0, 2.0, 40.0, 1000, 100);
glPopMatrix();

glPushMatrix();
glTranslatef(15,-34,20);
glRotatef(-80,1,0,0);
glColor3f(1.0, 0.0, 0.0);
gluCylinder(p4, 2.0, 2.0, 40.0, 1000, 100);
glPopMatrix();

glPushMatrix();
glTranslatef(-15,-26,20);
glRotatef(-80,1,0,0);
glColor3f(1.0, 0.0, 0.0);
gluCylinder(p5, 2.0, 2.0, 40.0, 1000, 100);
glPopMatrix();

glPushMatrix();
glTranslatef(0,10,0);
glRotatef(80,1,0,0);
glColor3f(1.0, 1.0, 1.0);
gluCylinder(p6, 40.0, 40.0, 3.0, 1000, 100);
glColor3f(1.0, 1.0, 1.0);
glScalef(40,40,40);
Circle();
glPopMatrix();

glPushMatrix();
glTranslatef(0,11.5,0);
glColor3f(1.0, 1.0, 1.0);
glRotatef(-80,1,0,0);
glutSolidCone(35.0, 17.0, 1000, 100);
glPopMatrix();

//glMatrixMode(GL_MODELVIEW);
//(x,y,z) pixels move
// glRotatef(30,0,0,1); //(degrees,x,y,z) axonas peristrofhs

// static int th=0; //idio apotelesma me glPostRedisplay
//glRotatef(th,0,0,1);
//th++;

//glScalef(2,1,1); //multiply (x,y,z) allagh klimakas
glEnd();
glutSwapBuffers(); // All drawing commands applied to the
// hidden buffer, so now, bring forward
// the hidden buffer and hide the visible one
//glutPostRedisplay();

}

//-----------------------------------------------------------

void Resize(int w, int h)
{
// define the visible area of the window ( in pixels )
if (h==0) h=1;
glViewport(0,0,w,h);

// Setup viewing volume

glMatrixMode(GL_PROJECTION);

// L R B T N F
// glOrtho (-50.0, 50.0, -50.0, 50.0,100.0,-100.0);

if(w<=h) //if :: diathrei analogies diastaseon
{
glOrtho(-50,50,-50*h/w,50*h/w,100,-100);
}
else
{
glOrtho(-50*h/w,50*h/w,-50,50,100,-100);
}

}

//-----------------------------------------------------------

void Setup() // DON'T TOUCH IT
{
glFrontFace(GL_CW);

// polygon rendering mode and material properties
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);

//Set up light source
GLfloat ambientLight[] = { 0.2, 0.2, 0.2, 1.0 };
GLfloat diffuseLight[] = { 0.8, 0.8, 0.8, 1.0 };
GLfloat lightPos[] = { -50.0, 20.0, 150.0, 1.0 };

// glEnable( GL_NORMALIZE );
glEnable( GL_LIGHTING );

glLightfv( GL_LIGHT0, GL_AMBIENT, ambientLight );
glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuseLight );
glLightfv( GL_LIGHT0, GL_POSITION,lightPos );

glEnable( GL_LIGHT0);
glEnable( GL_BLEND) ; //enabling alpha blending
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

// Black background
glClearColor(0.0f,0.0f,0.0f,1.0f);

}

Of course there is a main function and my header file visuals.h which are not needed here.
What I want to ask is why although for these two objects ( a cylinder and a solid cone ):

glPushMatrix();
glTranslatef(0,10,0);
glRotatef(80,1,0,0);
glColor3f(1.0, 1.0, 1.0);
gluCylinder(p6, 40.0, 40.0, 3.0, 1000, 100);
glColor3f(1.0, 1.0, 1.0);
glScalef(40,40,40);
Circle();
glPopMatrix();

glPushMatrix();
glTranslatef(0,11.5,0);
glColor3f(1.0, 1.0, 1.0);
glRotatef(-80,1,0,0);
glutSolidCone(35.0, 17.0, 1000, 100);
glPopMatrix();

I use the same colour setting I get a picture where these two have different colours.What should I do?

Relic
02-02-2004, 07:19 AM
You have a glScale(40, 40, 40) in your code.
Normals are transformed by the inverse transpose modelview matrix.
You need to enable GL_NORMALIZE when you have non-identity scaling together with lighting.