PDA

View Full Version : Problems with Shading/Light



int0xicated
04-16-2009, 01:38 PM
Hi,

The following simple c++ code puts some light to the scene, but my sphere is shaded incorrect (see links below) and I don't know why. I tried nearly all but it still doesn't work. I'm using VC++ 2008 express on Windows XP.

Here are my problems visualized :) :

For example if I zoom (increasing z value ---> gltranslate(0,0,z) ) I get the following effect (see link --> the screenshot shows the sphere with an other material but it has the same effect):

http://img25.imageshack.us/my.php?image=spherezgroeer50.gif
--> when I zoom further it finally looks like that:
http://img8.imageshack.us/my.php?image=spherezgroeer50bild2.gif


When I rotate the object it looks like this:

http://img242.imageshack.us/my.php?image=spherezgroeer50glanzpun.gif

The strange thing is that when I reach the point where the farplane ends (z values lower than -20 --> that means -21,-22, ... until -infinity) the sphere is shaded correctly
The farplane is defined here: --> gluPerspective(45.0, (double)window_width / (double)window_height, 1.0, 20);

I hope you can help me - would be very glad! I've posted this thread already on the glfw user forum, but I was directed to this forum.

CODE:


#include <cstdlib>
#include <windows.h>
#include <gl\GL.h> // Header Datei für die OpenGL32 Library
#include <gl\glu.h> // Header Datei für die GLu32 Library
#include <gl\glfw.h>

void Init();
void Shut_Down(int return_code);
void Main_Loop();
void Draw();
void DemoLight(void);
void draw_Planet();

float rotate_y = 0,
rotate_z = 0,
z = -8;
const float rotations_per_tick = 0.2;

int main()
{
Init();
Main_Loop();
Shut_Down(0);
}

void Init()
{
const int window_width = 800,
window_height = 600;

if (glfwInit() != GL_TRUE)
Shut_Down(1);

float aspect_ratio = ((float)window_height) / window_width;

// 800 x 600, 16 bit color, no depth, alpha or stencil buffers, windowed
if (glfwOpenWindow(window_width, window_height, 5, 6, 5,
0, 0, 0, GLFW_WINDOW) != GL_TRUE)
Shut_Down(1);
glfwSetWindowTitle("The GLFW Window");
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING); //Enable lighting
glEnable(GL_LIGHT0); //Enable light #0
glEnable(GL_LIGHT1); //Enable light #1
glEnable(GL_NORMALIZE);

// set the projection matrix to a normal frustum with a max depth of 50
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)window_width / (double)window_height, 1.0, 20);
//glFrustum(.5, -.5, -.5 * aspect_ratio, .5 * aspect_ratio, 1, 50);


}

void Shut_Down(int return_code)
{
glfwTerminate();
exit(return_code);
}

void Main_Loop()
{
// the time of the previous frame
double old_time = glfwGetTime();
// this just loops as long as the program runs
while(1)
{
// calculate time elapsed, and the amount by which stuff rotates
double current_time = glfwGetTime(),
delta_rotate = (current_time - old_time) * rotations_per_tick * 360;
old_time = current_time;
// escape to quit, arrow keys to rotate view
if (glfwGetKey(GLFW_KEY_ESC) == GLFW_PRESS)
break;
if (glfwGetKey(GLFW_KEY_LEFT) == GLFW_PRESS)
rotate_y += delta_rotate;
if (glfwGetKey(GLFW_KEY_RIGHT) == GLFW_PRESS)
rotate_y -= delta_rotate;
if(glfwGetKey(GLFW_KEY_DOWN) == GLFW_PRESS)
z -=0.1;
if(glfwGetKey(GLFW_KEY_UP) == GLFW_PRESS)
z +=0.1;

// clear the buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f}; //Color (0.2, 0.2, 0.2)

glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);

//Add positioned light
GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 1.0f}; //Color (0.5, 0.5, 0.5)
GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f}; //Positioned at (4, 0, 8)
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);

//Add directed light
GLfloat lightColor1[] = {0.5f, 0.2f, 0.2f, 1.0f}; //Color (0.5, 0.2, 0.2)
//Coming from the direction (-1, 0.5, 0.5)
GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f};
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);
glColor3f(1.0f, 1.0f, 0.0f);
// draw the figure
Draw();
// swap back and front buffers
glfwSwapBuffers();
}
}


void Draw()
{

glTranslatef(0, 0, z);
// apply the current rotation
glRotatef(rotate_y, 0, 1, 0);
glRotatef(rotate_z, 0, 0, 1);

draw_Planet();

}

void draw_Planet(){

glPushMatrix();
glTranslatef(0, 0, 0);
glColor3f(.5, .8, .2);
gluSphere(gluNewQuadric(),1, 20, 20);
glPopMatrix();

}

ephtracy
04-16-2009, 07:52 PM
i think your zfar( 20.0 ) set with gluPerspective is too small,
especially when your sphere has a radius( or diameter ) of 20,
the circle appears in the center is the hole clipped by the far plane.

try 100.0 or more which could ensure your sphere is between the near and far clip plane no matter how you move or rotate it.

int0xicated
04-17-2009, 03:28 AM
thanks for the answer ;), but that doesn't matter what farplane I take I still get this strange material errors - if someone could take a look at it I would very very glad, because I'm near getting mad :( ...

ephtracy
04-17-2009, 11:08 AM
allright, i still think it's a clipping problem.

when you just translate it along the z axis, the hole appears in the center of the sphere,
while when you rotate it, it appears on the left ( may caused by perspective ).

or wait, you said when the z value is lower than -20
( that means you move the camera toward the direction of positive z, just away from the sphere ),
it is shaded correctly.

that is saying maybe your eye is too close to the sphere that the near plane is clipping the sphere( notice that your initial eye position is at origin, toward the negative z direction ), and as a result the hole appears.

int0xicated
04-17-2009, 11:46 AM
hi,

thanks for the answer :) ...


or wait, you said when the z value is lower than -20
( that means you move the camera toward the direction of positive z, just away from the sphere ),
it is shaded correctly.

hmm I haven't defined any camera (this means I see the object from the origin) and when z vlaue is lower than the farplane (z = -21, -22 ...) does this mean my object isn't moved but only the camera along the positive z axis, or is the object translated to position i.e 0,0,-22 (x,y,z) and the camera is still at 0,0,0?

Sry, I'm still a bit confused - what would you suggest to solve this problem?

Here's a further screenshot - this effect appears also while rotating:

http://www.informatik-forum.at/attachment.php?attachmentid=14497&d=1239804784

and this one shows how it looks like, when z value lower -20, that means z = -21,-22, ... (this one is shaded correctly)

http://www.informatik-forum.at/attachment.php?attachmentid=14491&d=1239790424