OpenGL depth test is not working - c++

I am trying to create a model using OpenGL, and I tried to enable depth testing.
I use these commands in my main:
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glEnable(GL_DEPTH_TEST);
And this in my display:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
I even tried adding:
glDepthFunc(GL_LEQUAL);
And it doesn't work. I can still see what I think are depth problems.
Here is a video showing the problem: https://www.youtube.com/watch?v=OafrRH4Mzjc
Note: In that video, the board is build right to left, top to bottom, so the first angle is OK, but any other angle is bad.
What am I missing?
Edit: Minimal example file of reproduction:
#define _CRT_SECURE_NO_WARNINGS
#define SIZE_MOVES 17
#include <stdio.h>
/* Include the GLUT library. This file (glut.h) contains gl.h and glu.h */
#include <GL\glew.h>
#include <GL\freeglut.h>
static int left_click = GLUT_UP;
static int right_click = GLUT_UP;
static int xold;
static int yold;
static float rotate_x = 146;
static float rotate_y = -26;
int width, height;
GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 0, 5, -10, 0 };
GLfloat mat_specular[] = { 0.3, 0.3, 0.3, 1.0 };
GLfloat mat_shininess[] = { 1 };
// colors
const GLfloat colors[2][4] = {
{ 1.0, 1.0, 1.0, 1.0 }, //white
{ 0.0, 0.0, 0.0, 1.0 } //black
};
// rgb
const GLfloat rgb[3][4] = {
{ 1.0, 0.0, 0.0, 1.0 },
{ 0.0, 1.0, 0.0, 1.0 },
{ 0.0, 0.0, 1.0, 1.0 }
};
void resetMaterial() {
GLfloat c[] = { 1, 1, 1, 1 };
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, c);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
}
void drawSquare(int color) {
glPushMatrix(); {
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, colors[color]);
glScalef(1, 0.5, 1);
glutSolidCube(1);
} glPopMatrix();
}
void drawBoard() {
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++) {
glPushMatrix(); {
glTranslatef(i + 0.5, 0, j + 0.5);
drawSquare((i + j) % 2);
} glPopMatrix();
}
}
void drawAxes() {
glBegin(GL_LINES); {
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, rgb[0]);
glVertex3f(-2, 0, 0); glVertex3f(5, 0, 0);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, rgb[1]);
glVertex3f(0, -2, 0); glVertex3f(0, 5, 0);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, rgb[2]);
glVertex3f(0, 0, -2); glVertex3f(0, 0, 5);
} glEnd();
}
void letThereBeLight() {
/*Add ambient light*/
GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
/*Add positioned light*/
GLfloat lightColor1[] = { 0.2f, 0.2f, 0.1f, 1.0f };
GLfloat lightPosition1[] = { -8, 8, 5, 0.0f };
glLightfv(GL_LIGHT0, GL_SPECULAR, lightColor1);
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition1);
/*Add directed light*/
GLfloat lightColor2[] = { 0.3, 0.3, 0.3, 1.0f };
GLfloat lightPosition2[] = { 8, 8, -5, 1.0f };
glLightfv(GL_LIGHT1, GL_AMBIENT, lightColor2);
glLightfv(GL_LIGHT1, GL_POSITION, lightPosition2);
}
void display(void) {
// Clear frame buffer and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Set up viewing transformation, looking down -Z axis
glLoadIdentity();
gluLookAt(0, 5, -15, 0, 0, 3, 0, 1, 0);
letThereBeLight();
resetMaterial();
// Rotate view:
glPushMatrix(); {
glRotatef(rotate_y, 1, 0, 0);
glRotatef(rotate_x, 0, 1, 0);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, colors[0]);
glPushMatrix(); {
glTranslatef(-4, 0, -4); // Move to center
drawBoard();
} glPopMatrix();
drawAxes(); // For debuging
} glPopMatrix();
/* End */
glFlush();
glutSwapBuffers();
}
void mouseFunc(int button, int state, int x, int y) {
if (GLUT_LEFT_BUTTON == button)
left_click = state;
xold = x;
yold = y;
}
void motionFunc(int x, int y) {
if (GLUT_DOWN == left_click) {
rotate_y = rotate_y + (y - yold) / 5.f;
rotate_x = rotate_x + (x - xold) / 5.f;
glutPostRedisplay();
}
xold = x;
yold = y;
}
void reshapeFunc(int new_width, int new_height) {
width = new_width;
height = new_height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(50, width / height, 1, 20);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
int main(int argc, char **argv) {
/* Creation of the window */
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(900, 600);
glEnable(GL_DEPTH_TEST);
glutCreateWindow("Chess");
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_POLYGON_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glShadeModel(GL_SMOOTH);
glDisable(GL_COLOR_MATERIAL);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
/* Declaration of the callbacks */
glutDisplayFunc(&display);
glutReshapeFunc(&reshapeFunc);
glutMouseFunc(&mouseFunc);
glutMotionFunc(&motionFunc);
/* Loop */
glutMainLoop();
/* Never reached */
return 0;
}

gluPerspective(50, width / height, 0, 20);
^ wat
zNear needs to be greater than zero (emphasis mine):
Depth buffer precision is affected by the values specified for zNear
and zFar. The greater the ratio of zFar to zNear is, the less
effective the depth buffer will be at distinguishing between surfaces
that are near each other.
If r = zFar / zNear roughtly log2(r) bits of depth buffer
precision are lost. Because r approaches infinity as zNear approaches 0, zNear must never be set to 0.
EDIT: Given the newly-posted MCVE:
int main(int argc, char **argv) {
/* Creation of the window */
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(900, 600);
glEnable(GL_DEPTH_TEST); // too soon
glutCreateWindow("Chess");
...
}
It's just like datenwolf said: You're glEnable()ing before you have a current GL context (glutCreateWindow() creates the context and makes it current).
Don't call any gl*() functions until after glutCreateWindow().

public void onSurfaceChanged(int w, int h)
{
Matrix.frustumM(projectionMatrix,0,-(9f/18.5f),(9f/18.5f),-1f,1f,1.0f,1000f);
}
I had to change my near clipping form 0.01f to 1.0f.

Related

OpenGL black screen(GL_PERSPECTIVE, GL_MODELVIEW problem)

I'm trying to move the perspective view, so I can see that the sun, earth, and moon is moving around on a screen.
I'm keep searching and trying to fix this but there's nothing on a screen.
I really need help to fix this...
#include <gl/glew.h>
#include <gl/freeglut.h>
#include <GL/GL.h>
#include <GL/GLU.h>
#include <GL/glut.h>
#include <vector>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
using namespace glm;
static int Day = 0, Time = 0;
int windowWidth, windowHeight;
vec3 eye(0, 2.0, 4.0);
vec3 at(0.2, 0, 0);
vec3 up(normalize(cross(vec3(1, 0, 0), at - eye)));
void InitLight() {
// sun_light
GLfloat sun_light_amb[] = { 0.5, 0, 0, 1.0 };
GLfloat sun_light_diffuse[] = { 1, 0.5, 0.5, 1.0 };
GLfloat sun_light_specular[] = { 1, 1, 1, 1.0 };
// moon_light
GLfloat moon_light_amb[] = { 0.5, 0.5, 0, 1.0 };
GLfloat moon_light_diffuse[] = { 1, 1, 0.5, 1.0 };
GLfloat moon_light_specular[] = { 1, 1, 1, 1.0 };
glShadeModel(GL_SMOOTH); // using GL_SMOOTH
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
// sun - GL_LIGHT0
glLightfv(GL_LIGHT0, GL_AMBIENT, sun_light_amb);
glLightfv(GL_LIGHT0, GL_DIFFUSE, sun_light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, sun_light_specular);
// moon - GL_LIGHT1
glLightfv(GL_LIGHT1, GL_AMBIENT, moon_light_amb);
glLightfv(GL_LIGHT1, GL_DIFFUSE, moon_light_diffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR, moon_light_specular);
}
void MyDisplay() {
// material
// sun
GLfloat sun_mat_amb[] = { 0.2, 0 , 0, 1.0 };
GLfloat sun_mat_diffuse[] = { 1, 0.5, 0.5, 1.0 };
GLfloat sun_mat_specular[] = { 0, 0, 0, 1 };
GLfloat sun_mat_emission[] = { 0.3, 0.1, 0.1, 0.0 };
// sun
GLfloat moon_mat_amb[] = { 0.1, 0.1, 0.1, 1.0 };
GLfloat moon_mat_diff[] = { 0.5, 0.5, 0.1, 1.0 };
GLfloat moon_mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat moon_shininess[] = { 100.0 };
GLfloat moon_mat_emission[] = { 0.3, 0.3, 0.1, 0.0 };
// earth
GLfloat earth_mat_amb[] = { 0.1, 0.1, 0.1, 1.0 };
GLfloat earth_mat_diff[] = { 0.1, 0.1, 0.8, 1.0 };
GLfloat earth_mat_specular[] = { 0.5, 0.5, 1.0, 1.0 };
GLfloat earth_shininess[] = { 80.0 };
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(20.f, (GLfloat)windowWidth / (GLfloat)windowHeight, 0.f, 20.f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eye[0], eye[1], eye[2], at[0], at[1], at[2], up[0], up[1], up[2]);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CW);
glCullFace(GL_FRONT);
glColor3f(1.0, 0.3, 0.3); // sun
glMaterialfv(GL_FRONT, GL_AMBIENT, sun_mat_amb);
glMaterialfv(GL_FRONT, GL_DIFFUSE, sun_mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, sun_mat_specular);
glMaterialfv(GL_FRONT, GL_EMISSION, sun_mat_emission);
glutSolidSphere(0.2, 20, 16);
glEnable(GL_LIGHT0); // sun - LIGHT0
glPushMatrix();
glRotatef((GLfloat)Day, 0.0, 1.0, 0.0); // earth
glTranslatef(0.7, 0.0, 0.0);
glRotatef((GLfloat)Time, 0.0, 1.0, 0.0);
glColor3f(0.5, 0.6, 0.7);
glMaterialfv(GL_FRONT, GL_AMBIENT, earth_mat_amb);
glMaterialfv(GL_FRONT, GL_DIFFUSE, earth_mat_diff);
glMaterialfv(GL_FRONT, GL_SPECULAR, earth_mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, earth_shininess);
glutSolidSphere(0.1, 10, 8);
glPushMatrix();
glRotatef((GLfloat)Time, 0.0, 1.0, 0.0); // moon
glTranslatef(0.2, 0.0, 0.0);
glColor3f(0.9, 0.8, 0.2);
glMaterialfv(GL_FRONT, GL_AMBIENT, moon_mat_amb);
glMaterialfv(GL_FRONT, GL_DIFFUSE, moon_mat_diff);
glMaterialfv(GL_FRONT, GL_SPECULAR, moon_mat_specular);
glMaterialfv(GL_FRONT, GL_EMISSION, moon_mat_emission);
glutSolidSphere(0.04, 10, 8);
glEnable(GL_LIGHT1); // moon - LIGHT1
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void MyReshape(int w, int h) {
windowWidth = w;
windowHeight = h;
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
}
void MyTimer(int Value) {
Day = (Day + 2) % 360;
Time = (Time + 1) % 360;
mat3 rot = rotate(mat4(1), radians(1.f), vec3(0, 0, 1)); // rotate 1 degree
eye = rot * eye;
up = rot * up;
glutPostRedisplay();
glutTimerFunc(40, MyTimer, 1);
}
int main(int argc, char** argv) {
//Create window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Test");
//get window size
windowWidth = glutGet(GLUT_WINDOW_WIDTH);
windowHeight = glutGet(GLUT_WINDOW_HEIGHT);
InitLight();
glutDisplayFunc(MyDisplay);
glutReshapeFunc(MyReshape);
glutTimerFunc(40, MyTimer, 1);
glutMainLoop();
return 0;
}
moving sun, earth, moon
using GL_SMOOTH
moving perspective when MyTimer callback (rotate eye and up => 1 degree)
The Perspective Projection matrix defines a Viewing Frustum. The near plane cannot be 0. The near plan must be grater than o and the far plan must be grater than the near plane.
0 < near < far
Change the near plane. e.g:
gluPerspective(20.f, (GLfloat)windowWidth / (GLfloat)windowHeight, 0.f, 20.f);
gluPerspective(20.f, (GLfloat)windowWidth / (GLfloat)windowHeight, 0.1f, 20.f);

How do I set material only for one object in OpenGL?

I have two 3D objects, and I need to set material only on one of them using glMaterial function. How can I accomplish this?
#include <GLUT/glut.h>
#include <cmath>
const int width = 1200, height = 600;
float xValue = 0.0;
float position = 0.0;
float angle = 0.0;
float shininess[] = { 1.0 };
float color[] = { 0.2, 0.2, 0.8 };
void init(void);
void display(void);
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(width, height);
glutInitWindowPosition(85, 75);
glutCreateWindow("3D объекты - освещение");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void init(void) {
glClearColor(0.0, 0.0, 0.0, 1.0);
glEnable(GL_LIGHTING);
float lightAmbient[] = { 0.5, 0.5, 0.5, 0.0 };
float lightDiffuse[] = { 0.5, 0.5, 0.5, 0.0 };
float lightSpecular[] = { 0.5, 0.5, 0.5, 0.0 };
float lightPosition[] = { -10.0, 5.0, 0.0, 1.0 };
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular);
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glOrtho(-10.0, 10.0, -5.0, 5.0, -5.0, 5.0);
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0.0, 0.0, width, height);
xValue += 0.01;
position = 5.0*cos(xValue);
angle = 270.0*sin(xValue);
glLineWidth(3);
glPushMatrix();
glTranslatef(position, 0.0, 0.0);
glRotatef(angle, 0, 1, 0);
glMaterialfv(GL_FRONT, GL_AMBIENT, color);
glMaterialfv(GL_FRONT, GL_DIFFUSE, color);
glMaterialfv(GL_FRONT, GL_SPECULAR, color);
glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
glutWireTorus(1, 2, 35, 35);
glPopMatrix();
glPushMatrix();
glTranslatef(-position, 0.0, 0.0);
glRotatef(angle, 0, 1, 0);
glutSolidTorus(0.5, 3.5, 35, 35);
glPopMatrix();
glutSwapBuffers();
glutPostRedisplay();
}
OpenGL is a state engine. Each state is kept until it is changed again, even beyond frames. For instance you can enable and disable lighting before drawing an object:
glEnable(GL_LIGHTING);
// darw object 1
glDisable(GL_LIGHTING);
// draw object 2
ALike the material settings can be set for each object individually, before drawing the object:
glMaterialfv(...);
// darw object 1
glMaterialfv(...);
// draw object 2

I want to see the movement of the Sphere on the screen(OpenGL)

I want to see the movement of the Sphere on the screen.
but, If you run the code, you will not be able to update the screen.
How do I change the code to show a change in the location of the phrase I created?
void display() {
glLoadIdentity();
gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
GLfloat diffuse1[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat ambient1[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat specular1[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat position1[] = { 0, 0, 0, 1.0 };
GLfloat diffuse2[] = { 0.5, 0.5, 0.5, 1 };
GLfloat ambient2[] = { 0.1, 0.1, 0.1, 1 };
GLfloat specular2[] = { 0.5, 0.5, 0.5, 1 };
GLfloat emission2[] = { 0, 0, 0, 1 };
GLfloat shine = 300;
glLightfv(GL_LIGHT0, GL_POSITION, position);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient1);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse1);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular1);
glPushMatrix();
glMaterialfv(GL_FRONT, GL_AMBIENT, ambient2);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse2);
glMaterialfv(GL_FRONT, GL_SPECULAR, specular2);
glMaterialfv(GL_FRONT, GL_EMISSION, emission2);
glMaterialf(GL_FRONT, GL_SHININESS, shine);
glRotatef(mer_revolution, 0, 0, 1);
glTranslatef(0, 0.8, 0);
glRotatef(mer_rotate, 0, 0, 1);
glutSolidSphere(1, 300, 300);
glPopMatrix();
glFlush();
}
void main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800,600);
glutCreateWindow("");
glClearColor(0, 0, 0, 0);
glColor3f(1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glMatrixMode(GL_MODELVIEW);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(move);
glutMainLoop();
}
Move 'gL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT' at the top of the display.
The screen must be initialized when performing a function. Only then will the screen continue to change.

Making spotlight stationary

I'm trying to implement a pure stationary spotlight into my simple OpenGL scene, which doesn't move with the camera. It should keep highlighting the center of the scene (coords of 0,0,0), where I put the teapot.
Here is a simplified code:
#include <windows.h>
#define GLUT_DISABLE_ATEXIT_HACK
#include <gl/glut.h>
#include <gl/glu.h>
#include <gl/gl.h>
#include <math.h>
#include <iostream>
const int windowWidth = 640;
const int windowHeight = 480;
float alpha=0.0, alphaDelta=0.0, ratio, beta=0.0, betaDelta=0.0;
float x=0.0, y=1.75, z=5.0;
float lx=0.0, ly=0.0, lz=-1.0;
int moveDelta = 0;
float lastMouseX=windowWidth*0.5, lastMouseY=windowHeight*0.5;
void reshape(int w, int h){
//...
}
// handles angle changes
void orientMe(float horizontalAngle, float verticalAngle) {
lx = sin(horizontalAngle);
if(beta > -1.5 && beta < 1.5)
ly = sin(verticalAngle);
lz = -cos(horizontalAngle);
glLoadIdentity();
gluLookAt(x, y, z,
x + lx, y + ly, z + lz,
0.0, 1.0, 0.0);
}
// handles x,y,z coords changes
void flatMovement(int i) {
x = x + i*(lx)*0.1;
z = z + i*(lz)*0.1;
glLoadIdentity();
gluLookAt(x, y, z,
x + lx, y + ly, z + lz,
0.0, 1.0, 0.0);
}
void display() {
// orient observer
if(moveDelta)
flatMovement(moveDelta);
if(alphaDelta || betaDelta) {
alpha += alphaDelta;
alphaDelta = 0;
beta += betaDelta;
betaDelta = 0;
orientMe(alpha, beta);
}
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0,1,0);
glBegin(GL_QUADS);
glNormal3d(0,1,0);
glVertex3f(-100.0, 0.0, -100.0);
glVertex3f(-100.0, 0.0, 100.0);
glVertex3f(100.0, 0.0, 100.0);
glVertex3f(100.0, 0.0, -100.0);
glEnd();
glColor3f(1,0,0);
glutSolidTeapot(1);
glutSwapBuffers();
}
void pressSpecialKey(int key, int x, int y) {
//...
}
void releaseSpecialKey(int key, int x, int y) {
//...
}
static void idle(void)
{
glutPostRedisplay();
}
void mouseMove(int x, int y) {
//...
}
void setupLights() {
GLfloat spotDirection[] = {0.0f, -1.0f, 0.0f, 1.0f};
GLfloat spotPosition[] = {0.0f, 4.0f, 0.0f, 1.0f};
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 40);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spotDirection);
glLightfv(GL_LIGHT0, GL_POSITION, spotPosition);
glEnable(GL_LIGHT0);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(windowWidth,windowHeight);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Spotlight");
glutSpecialFunc(pressSpecialKey);
glutSpecialUpFunc(releaseSpecialKey);
glutPassiveMotionFunc(mouseMove);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_LIGHTING);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
setupLights();
glutMainLoop();
return EXIT_SUCCESS;
}
Unfortunately, the spotlight seems to follow camera changes and I have no idea what's wrong with the code above.
EDIT
Thanks to what #genpfault posted, I've alreadysorted things out.
The answer was to leave only the glEnable(GL_LIGHT0) in the main() function, and move the rest of the code responsible for lighting to the very end of the display() function (right before calling glutSwapBuffers().
So the final, simplified code looks as follows:
#include <windows.h>
#define GLUT_DISABLE_ATEXIT_HACK
#include <gl/glut.h>
#include <gl/glu.h>
#include <gl/gl.h>
#include <math.h>
#include <iostream>
const int windowWidth = 640;
const int windowHeight = 480;
float alpha=0.0, alphaDelta=0.0, ratio, beta=0.0, betaDelta=0.0;
float x=0.0, y=1.75, z=5.0;
float lx=0.0, ly=0.0, lz=-1.0;
int moveDelta = 0;
float lastMouseX=windowWidth*0.5, lastMouseY=windowHeight*0.5;
void reshape(int w, int h){
//...
}
// handles angle changes
void orientMe(float horizontalAngle, float verticalAngle) {
//...
}
// handles x,y,z coords changes
void flatMovement(int i) {
//...
}
void setupLights() {
//THE CONTENTS SLIGHTLY CHANGED HERE
GLfloat spotPosition[] = {2.0f, 4.0f, 0.0f, 1.0f};
GLfloat spotDirection[] = {0.0f, -1.0f, 0.0f, 1.0f};
glLightfv(GL_LIGHT0, GL_POSITION, spotPos
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 40);ition);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spotDirection);
}
void display() {
// orient observer
if(moveDelta)
flatMovement(moveDelta);
if(alphaDelta || betaDelta) {
alpha += alphaDelta;
alphaDelta = 0;
beta += betaDelta;
betaDelta = 0;
orientMe(alpha, beta);
}
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0,1,0);
glBegin(GL_QUADS);
glNormal3d(0,1,0);
glVertex3f(-100.0, 0.0, -100.0);
glVertex3f(-100.0, 0.0, 100.0);
glVertex3f(100.0, 0.0, 100.0);
glVertex3f(100.0, 0.0, -100.0);
glEnd();
glColor3f(1,0,0);
glutSolidTeapot(1);
setupLights(); // PUT THE LIGHTING SETUP AT THE END OF DISPLAY()
glutSwapBuffers();
}
void pressSpecialKey(int key, int x, int y) {
//...
}
void releaseSpecialKey(int key, int x, int y) {
//...
}
static void idle(void)
{
glutPostRedisplay();
}
void mouseMove(int x, int y) {
//...
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(windowWidth,windowHeight);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Spotlight");
glutSpecialFunc(pressSpecialKey);
glutSpecialUpFunc(releaseSpecialKey);
glutPassiveMotionFunc(mouseMove);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glEnable(GL_LIGHTING);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0); // IN THE MAIN, LEAVE ONLY THE ENABLING CALL
glutMainLoop();
return EXIT_SUCCESS;
}
Set your light position after you set your camera transform:
void display()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// orient observer
if(moveDelta)
flatMovement(moveDelta);
if(alphaDelta || betaDelta)
{
alpha += alphaDelta;
alphaDelta = 0;
beta += betaDelta;
betaDelta = 0;
orientMe(alpha, beta);
}
setupLights();
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0,1,0);
glBegin(GL_QUADS);
glNormal3d(0,1,0);
glVertex3f(-100.0, 0.0, -100.0);
glVertex3f(-100.0, 0.0, 100.0);
glVertex3f(100.0, 0.0, 100.0);
glVertex3f(100.0, 0.0, -100.0);
glEnd();
glColor3f(1,0,0);
glutSolidTeapot(1);
glutSwapBuffers();
}
See here. Your existing code follows the "Moving the Light Source Together with Your Viewpoint" call sequence.

Why is this quad not rendering? Or if it is why can't I see it?

I am using Visual C++ and GLUT. Am I missing something such a normals? this is the exact code I am using. I tried to insert glNormal3f(0, 0, 1); before the vertex statements but this did not change anything. The glutSolidSphere renders just as I expect it to but not the quad.
#include "stdafx.h"
#include <stdlib.h>
#include <GL/glut.h>
float spin = 0.0f;
float zDir = 0;
GLfloat diffuseIntensity[] = {.75, .75, .75, 1};
GLfloat specularHue[] = {0, 0, .5f, 1};
GLfloat shininess[] = {5};
void moveCamera();
void checkKeys(int, int, int);
void setUpLighting();
void changeSize(int w, int h) {
if (h == 0)
h = 1;
float ratio = w * 1.0 / h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(45,ratio,1,1000);
glMatrixMode(GL_MODELVIEW);
}
void renderScene(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
moveCamera();
glTranslatef(0, 0, zDir-5);
glEnable(GL_COLOR_MATERIAL);
glShadeModel(GL_FLAT);
glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
glMaterialfv(GL_FRONT, GL_SPECULAR, specularHue);
glPushMatrix();
glRotatef(90, 1, 0, 0);
glRotatef(spin, 0, 0, 1);
glutSolidSphere(.5f, 24, 24);
glPopMatrix();
spin += .01f;
if(spin > 360) spin -= 360;
glBegin(GL_QUADS);
glVertex3f(-10, 0, -10);
glVertex3f(-10, 0, 10);
glVertex3f(10, 0, 10);
glVertex3f(10, 0, -10);
glEnd();
glutSwapBuffers();
glutPostRedisplay();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(800,600);
glutCreateWindow("Lighthouse3D - GLUT Tutorial");
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
glutSpecialFunc(checkKeys);
glEnable(GL_DEPTH_TEST);
setUpLighting();
glutMainLoop();
return 1;
}
void moveCamera(){
glTranslatef(0, 0, zDir);
}
void checkKeys(int key, int x, int y){
switch(key){
case GLUT_KEY_UP:
zDir += .1f;
glutPostRedisplay();
break;
case GLUT_KEY_DOWN:
zDir += -.1f;
glutPostRedisplay();
break;
case GLUT_KEY_END:
exit(0);
}
}
void setUpLighting(){
GLfloat position0[] = {3, 1, 0, 1};
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, position0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseIntensity);
}
Give this a shot:
#include <GL/glut.h>
float zDir = 12;
void checkKeys(int key, int x, int y)
{
switch(key)
{
case GLUT_KEY_UP:
zDir += -.5f;
glutPostRedisplay();
break;
case GLUT_KEY_DOWN:
zDir += .5f;
glutPostRedisplay();
break;
case GLUT_KEY_END:
exit(0);
}
}
float spin = 0;
void renderScene(void)
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
gluPerspective(45,w/h,0.1,100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -zDir);
// set up light
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
GLfloat diffuseIntensity[] = {.75, .75, .75, 1};
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseIntensity);
// draw sphere at light position
glDisable( GL_LIGHTING );
glPushMatrix();
// spin light position around the y axis
glRotatef( -spin, 0, 1, 0 );
GLfloat position0[] = {3,3,3, 1};
glLightfv(GL_LIGHT0, GL_POSITION, position0);
glTranslatef( position0[0], position0[1], position0[2] );
glColor3ub(255,255,255);
glutSolidSphere(0.1,8,8);
glPopMatrix();
glEnable( GL_LIGHTING );
// draw sphere
glEnable(GL_COLOR_MATERIAL);
glShadeModel(GL_FLAT);
GLfloat specularHue[] = {0, 0, .5f, 1};
GLfloat shininess[] = {5};
glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
glMaterialfv(GL_FRONT, GL_SPECULAR, specularHue);
spin += .01f;
if(spin > 360) spin -= 360;
glPushMatrix();
glRotatef(spin, 0, 1, 0);
glutSolidSphere(2, 24, 24);
glPopMatrix();
// draw quad
glColor3ub(255,0,0);
glPushMatrix();
glScalef( 3, 3, 3 );
glBegin(GL_QUADS);
glNormal3f( 0, 0, 1 );
glVertex2f( -1, -1 );
glVertex2f( 1, -1 );
glVertex2f( 1, 1 );
glVertex2f( -1, 1 );
glEnd();
glPopMatrix();
glutSwapBuffers();
glutPostRedisplay();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(800,600);
glutCreateWindow("Lighthouse3D - GLUT Tutorial");
glutDisplayFunc(renderScene);
glutSpecialFunc(checkKeys);
glutMainLoop();
return 1;
}
The quad is rendered, but you don't see it because it is drawn very much like the surface of a table and your eyes are at the same level of the table. Because of this, the effect is like that of which nothing was drawn at all. That or you'll see a line.
Your rotation above, being wrapped in a PushMatrix/PopMatrix only works with the solid sphere.