OpenGL show object on the screen - c++

I just started learning OpenGL so I have problem with showing the object on the screen.
Display() contains this object and I want to show this one by glutDisplayFunc(Display); but it's not working, I have only grey screen after compiling. (Yeah, this object is wheel)
#include "stdafx.h"
#include <math.h>
#include <GL/freeglut.h>
GLint Width = 1024, Height = 640;
const int CubeSize = 500;
void Display (void) {
glClearColor (0.7, 0.7, 0.7, 1);
glClear (GL_COLOR_BUFFER_BIT);
glColor3ub (255, 200, 200);
int iTimeElapsed = glutGet(GLUT_ELAPSED_TIME);
float fRevolveScale1 = 0.2f;
float fRevolveScale2 = 0.4f;
long i;
// clear all pixels
glClear(GL_COLOR_BUFFER_BIT);
// push temp state
glPushMatrix();
// translate to center
glTranslatef(0.5f, 0.5f, 0.0);
// rotate around pivot
glRotatef(iTimeElapsed * fRevolveScale1, 0.0, 0.0, 1.0);
// translate to planet location
glTranslatef(0.25f, 0.25f, 0.0);
glRotatef(iTimeElapsed * fRevolveScale2, 0.0, 0.0, 1.0);
glColor3f(0.129412f, 0.129412f, 0.129412f);
glutSolidSphere(0.1f, 16, 16);
// five bolts, step 72 degree (72*5=360 degree)
glColor3f(1.0, 1.0, 1.0);
for (i = 0; i<5; ++i)
{
glPushMatrix();
glRotatef(72.0f*i, 0.0, 0.0, 1.0); // rotate coordinate 72 degree
glTranslatef(0.04f, 0.0, 0.0);// translate on the rotated coordinate
glutSolidSphere(0.01f, 16, 16);
glPopMatrix();
}
glTranslatef(0.0f, 0.0f, 0.0f);// translate on the rotated coordinate
glutSolidSphere(0.01, 16, 16);
// pop temp state
glPopMatrix();
glFlush();
glutPostRedisplay();
glFinish ();
}
void Reshape(GLint w, GLint h) {
Width = w, Height = h;
glViewport (0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity ();
glOrtho (0, w, 0, h, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();
}
void Keyboard(unsigned char key, int x, int y) {
const char ESCAPE = '\033 ';
if (key == ESCAPE) exit (0);
}
void main(int argc, char *argv []) {
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_RGB);
glutInitWindowSize (Width, Height);
glutCreateWindow("E X A M P L E");
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
glutKeyboardFunc(Keyboard);
glutMainLoop ();
}

Related

OpenGL: cylinder misshaped after turning it 90 degree

I and my college project partner are doing this project. We write this in c++ and OpenGL 2.1 with the freeglut.lib We turned the cylinder to lay down on the ground by 90 degrees. Now the cylinder is oddly misshaped. How can we fix the misshape? Is this a perspective thing? We wanted to use the cylinder later as wheels for a car which we should animate driving in a circle track. Right now the wheels on the car are only 2D circles.
void draw_cylinder(GLfloat radius,
GLfloat height,
GLubyte R,
GLubyte G,
GLubyte B)
{
GLfloat x = 0.0;
GLfloat y = 0.0;
GLfloat angle = 0.0;
GLfloat angle_stepsize = 0.1;
/** Draw the tube */
glColor3ub(R - 40, G - 40, B - 40);
glBegin(GL_QUAD_STRIP);
angle = 0.0;
while (angle < 2 * PI) {
x = radius * cos(angle);
y = radius * sin(angle);
glVertex3f(x, y, height);
glVertex3f(x, y, 0.0);
angle = angle + angle_stepsize;
}
glVertex3f(radius, 0.0, height);
glVertex3f(radius, 0.0, 0.0);
glEnd();
/** Draw the circle on top and bottom of cylinder */
glColor3ub(R, G, B);
glBegin(GL_POLYGON);
angle = 0.0;
while (angle < 2 * PI) {
x = radius * cos(angle);
y = radius * sin(angle);
glVertex3f(x, y, height);
angle = angle + angle_stepsize;
}
glVertex3f(radius, 0.0, height);
glEnd();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0, -0.4, -3.0);
//glRotatef(-40, 1.0, 0.0, 0.0);
glRotatef(-200, 1.0, 0.0, 0.0);
draw_cylinder(0.3, 0.5, 255, 160, 100);
glFlush();
}
void reshape(int width, int height)
{
if (width == 0 || height == 0) return;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0, (GLdouble)width / (GLdouble)height,
0.5, 20.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0, 0, width, height);
}
int main(int argc, char** argv)
{
/** Initialize glut */
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutCreateWindow("Create Cylinder");
glClearColor(0.0, 0.0, 0.0, 0.0);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
You need to enable the Depth Test:
glEnable(GL_DEPTH_TEST);
and clear the depth buffer as well
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Example:
float angle = 0.0f;
void display(void)
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0, -0.4, -3.0);
glRotatef(angle, 0.0, 1.0, 0.0);
angle += 0.1f;
glRotatef(-200, 1.0, 0.0, 0.0);
draw_cylinder(0.3, 0.5, 255, 160, 100);
glFlush();
glutPostRedisplay();
}

Making the square bigger when it moves

#include <stdio.h> // this library is for standard input and output
#include "glut.h" // this library is for glut the OpenGL Utility Toolkit
#include <math.h>
float squareX = 0.0f;
float squareY = 200.0f;
static int flag = 1;
void drawShape(void) {
float width = 58.0f;
float height = 40.0f;
glTranslatef(squareX, squareY, 0);
// test
// glScalef(0.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(0, 0);
glVertex2f(width, 0);
glVertex2f(width, height);
glVertex2f(0, height);
glVertex2f(0, 0);
glEnd();
}
void initRendering() {
glEnable(GL_DEPTH_TEST);
}
// called when the window is resized
void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, (float)w, 0.0f, (float)h, -1.0f, 1.0f);
}
int state = 1;
void update(int value) {
if (state == 1) { // 1 : move right
squareX += 1.0f;
if (squareX > 400.0) {
state = 0;
}
}
glutPostRedisplay();
glutTimerFunc(25, update, 0);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
drawShape();
glutSwapBuffers();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutCreateWindow("Moving Square");
initRendering();
glutDisplayFunc(display);
glutReshapeFunc(handleResize);
glutTimerFunc(25, update, 0);
glutMainLoop();
return(0);
}
I want to make the square get bigger while it is moving to the right. See the second GIF below. I know that I need glScalef to make the square bigger but I don't know how to make it bigger while it is moving.
Code preview:
I need it to do something similar to this (sorry about the quality, I created the GIF myself):
Use glScale to scale the rectangle dependent on the X position (squareX):
float rectScale = 1.0f + (squareX / 400.0f);
glScalef(rectScale, rectScale, 1.0f);
Note squareX is in range [0.0, 400.0], so 1.0f + (squareX / 400.0f) is in range [1.0, 2.0].
First the scaling has to be applied to the rectangle. This means it has to be the last operation, which is applied to the model view matrix, before the rectangle is drawn. the final function drawShape may look like this:
void drawShape(void) {
float width = 58.0f;
float height = 40.0f;
glTranslatef(squareX, squareY, 0);
float rectScale = 1.0f + (squareX / 400.0f);
glScalef(rectScale, rectScale, 1.0f);
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(0, 0);
glVertex2f(width, 0);
glVertex2f(width, height);
glVertex2f(0, height);
glVertex2f(0, 0);
glEnd();
}
Preview:

OpenGL blank screen?

I am learning OpenGL from book, I did exactly what is in the book, but when I run it (Eclipse C++ IDE) I get just blank screen.
Book is "OpenGL guide for programmers". I think the error is in Reshape(), but code is from book.
#include <iostream>
#include <GL/glew.h>
#include <GL/glut.h>
typedef const char* String;
String TITLE = "OpenGL";
int HEIGHT = 480;
int WIDTH = HEIGHT / 9 * 12;
void Update(int time){
glutPostRedisplay();
glutTimerFunc( 10, Update, 1);
}
void Init(){
glClearColor(1.0, 1.0, 1.0, 0.0);
glShadeModel(GL_FLAT);
}
void Render(){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 0.0);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glScalef(1.0, 2.0, 1.0); // Modeling transformation
glutWireCube(1.0);
glutSwapBuffers();
}
void Reshape(int w, int h){
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow(TITLE);
glewExperimental = true;
GLenum err = glewInit();
if(err!=GLEW_OK){
exit(1);
}
Init();
Update(1);
glutDisplayFunc(Render);
glutReshapeFunc(Reshape);
glutMainLoop();
return 0;
}
Yes reshape function is definitely wrong
use that
void Reshape(GLsizei w,GLsizei h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (h == 0)
h = 1;
GLfloat aspectRatio;
aspectRatio = static_cast<GLfloat>(w) / static_cast<GLfloat>(h);
gluPerspective(45.0,aspectRatio,1.0,4000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
instead

OpenGL C++ Depth buffer not working

I've been testing with OpenGL for a while and have not been able to get the depth buffer to work, despite using GLUT_DEPTH as a parameter in glutInitDisplayMode and doing glClear(GL_DEPTH_BUFFER_BIT) at the start of the display function. I don't know what else I'm missing.
Below is a minimum working example and Figure 1 and 2. Comment out the parameters under Figure 1 and 2 when you want to view the other one.
Figure 1 (above blue):
Figure 2 (below red):
Example:
#include <vector>
#include <gl\glut.h>
typedef std::vector<float> floatvec;
// Figure 1 (above blue)
float posX = 8.00f;
float posY = 7.54f;
float posZ = -0.89f;
float angleX = 300.50f;
float angleY = 45.33f;
// Figure 2 (below red)
float posX = 4.12f;
float posY = -4.87f;
float posZ = -3.84f;
float angleX = 343.25f;
float angleY = -45.00f;
int screenW = 720;
int screenH = 540;
float fMin = 0.5;
float fMax = 100.0;
float alpha = 60.0;
// Draws a rectangle in 3D
void DrawQuad(floatvec c) {
glBegin(GL_QUADS);
glVertex3f(-c[0], -c[1], -c[2]);
glVertex3f(-c[3], -c[4], -c[5]);
glVertex3f(-c[6], -c[7], -c[8]);
glVertex3f(-c[9], -c[10], -c[11]);
glEnd();
}
void Display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
glRotatef(angleY, 1.0f, 0.0f, 0.0f);
glRotatef(angleX, 0.0f, 1.0f, 0.0f);
glRotatef(180.0, 0.0f, 0.0f, 1.0f);
glTranslatef(posX, posY, posZ);
// Draws the tiles
glColor3f(1.0, 0.0, 0.0); // Red
DrawQuad({ 0, 0, 0,
5, 0, 0,
5, 0, 5,
0, 0, 5 });
glColor3f(0.0, 0.0, 1.0); // Blue
DrawQuad({ 0, 3, 0,
5, 3, 0,
5, 3, 5,
0, 3, 5 });
glPopMatrix();
glutSwapBuffers();
}
void Reshape(int width, int height) {
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(alpha, (GLfloat)width / (GLfloat)height, fMin, fMax);
glMatrixMode(GL_MODELVIEW);
}
int main(int iArgc, char** cppArgv) {
glutInit(&iArgc, cppArgv);
glEnable(GL_DEPTH_TEST);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(screenW, screenH);
glutCreateWindow("Example");
glutDisplayFunc(Display);
glutIdleFunc(Display);
glutReshapeFunc(Reshape);
glutMainLoop();
return 0;
}
You just need to change where you call glEnable(GL_DEPTH_TEST);
Put it after glutCreateWindow and it will work fine.
Something like this:
int main(int iArgc, char** cppArgv) {
glutInit(&iArgc, cppArgv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(screenW, screenH);
glutCreateWindow("Example");
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(Display);
glutIdleFunc(Display);
glutReshapeFunc(Reshape);
glutMainLoop();
return 0;
}
Just tested here and it worked.
I am a little bit rusty in OpenGL, but, putting the glEnable line in the Display function will make the trick.
#include <vector>
#include <GL/glut.h>
typedef std::vector<float> floatvec;
//Figure 1 (above blue)
//float posX = 8.00f;
//float posY = 7.54f;
//float posZ = -0.89f;
//float angleX = 300.50f;
//float angleY = 45.33f;
// Figure 2 (below red)
float posX = 4.12f;
float posY = -4.87f;
float posZ = -3.84f;
float angleX = 343.25f;
float angleY = -45.00f;
int screenW = 720;
int screenH = 540;
float fMin = 0.5;
float fMax = 100.0;
float alpha = 60.0;
// Draws a rectangle in 3D
void DrawQuad(floatvec c) {
glBegin(GL_QUADS);
glVertex3f(-c[0], -c[1], -c[2]);
glVertex3f(-c[3], -c[4], -c[5]);
glVertex3f(-c[6], -c[7], -c[8]);
glVertex3f(-c[9], -c[10], -c[11]);
glEnd();
}
void Display() {
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
glRotatef(angleY, 1.0f, 0.0f, 0.0f);
glRotatef(angleX, 0.0f, 1.0f, 0.0f);
glRotatef(180.0, 0.0f, 0.0f, 1.0f);
glTranslatef(posX, posY, posZ);
// Draws the tiles
glColor3f(1.0, 0.0, 0.0); // Red
DrawQuad({ 0, 0, 0,
5, 0, 0,
5, 0, 5,
0, 0, 5 });
glColor3f(0.0, 0.0, 1.0); // Blue
DrawQuad({ 0, 3, 0,
5, 3, 0,
5, 3, 5,
0, 3, 5 });
glPopMatrix();
glutSwapBuffers();
}
void Reshape(int width, int height) {
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(alpha, (GLfloat)width / (GLfloat)height, fMin, fMax);
glMatrixMode(GL_MODELVIEW);
}
int main(int iArgc, char** cppArgv) {
glutInit(&iArgc, cppArgv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(screenW, screenH);
glutCreateWindow("Example");
glutDisplayFunc(Display);
glutIdleFunc(Display);
glutReshapeFunc(Reshape);
glutMainLoop();
return 0;
}

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.