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();
}