Opengl Lightning from object - opengl

I want to draw some light from an object (sphere in my case, its a pole with a handle and sphere on the top).
The light should draw on the other objects too but from the only sphere as I mentioned.
Currently, I am only getting ambient light only.
I have set the L key to show lightning but it only shows ambient light(darkens the scene) when I press the key.
here is the code
/// Lights
float ambientLight[4] = { 0.35f, 0.35f, 0.35f, 1.0f }; // colour
float ambientLightOff[4] = { 0.0f, 0.0f, 0.0f, 1.0f }; // colour
float sunLightPos[4] = { 6.f, 15.5f, 5.0f, 0.0f }; // distant above
float sunLightDif[4] = { 0.8f,0.8f,0.8f,0.8f };
float sunLightSpec[4] = { 0.8f,0.8f,0.8f,0.8f }; // colour
float sunLightAmb[4] = { 0.1f,0.1f,0.1f,0.0f }; // colour
// materials
float wooddif1[] = { 0.647059f, 0.164706f, 0.164706f , 1.0f };
float woodamb1[] = { 0.41f, 0.164706f, 0.164706f, 1.0f };
float woodspe1[] = { 0.547059f, 0.364706f, 0.264706f, 1.0f };
float woodshiny1 = 60;
bool showNormals = TRUE;
bool lighting = false;
bool texture = false;
bool ambientlighting = true;
bool sunlighting = false;
bool buildinglighting = false;
static GLfloat lightIntensity = 0.5;
bool whereLights = true;
void drawpole()
{
glColor3f(0.5f, 0.35f, 0.05f);
glTranslatef(0, 1, 0);
drawCylinder3();
glColor3f(0.5f, 0.35f, 0.05f);
glTranslatef(0, 1, 0);
drawCylinder3();
glPushMatrix();
//glTranslatef(light2Pos[0],0, light2Pos[2]);
//glTranslatef(25, 0, 13);
glColor3f(0.3, 0.4, 0.1);
if (lighting) { setMaterial(4); }
drawSphere();
glPopMatrix();
}
void setLights()
{
if (lighting)
{
glEnable(GL_LIGHTING);
glEnable(GL_NORMALIZE);
//setShademodel();
}
else glDisable(GL_LIGHTING);
if (sunlighting)
{
}
else glDisable(GL_LIGHT0);
//if (buildinglighting)
if (ambientlighting)
{
}
else
{
}
}
void drawLightPlaces()
{
//draw the lights at position
glDisable(GL_LIGHTING);
if (lighting) glEnable(GL_LIGHTING);
}
Edit (related to comment).
void drawpole()
{
glColor3f(0.5f, 0.35f, 0.05f);
glTranslatef(0, 1, 0);
drawCylinder3();
glColor3f(0.5f, 0.35f, 0.05f);
glTranslatef(0, 1, 0);
drawCylinder3();
glPushMatrix();
glColor3f(0.5f, 0.35f, 0.05f);
if (buildinglighting) {
glDisable(GL_LIGHTING);
glColor3f(1.0, 1.0, 1.0);
setMaterial(4);
}
drawSphere();
glPopMatrix();
}
void setLights()
{
if (lighting)
{
glEnable(GL_LIGHTING);
}
else glDisable(GL_LIGHTING);
if (sunlighting)
{
sunlighting;
}
else glDisable(GL_LIGHT0);
if (buildinglighting)
{
// glLightfv(GL_LIGHT0, GL_POSITION, light_position);
//glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
//glEnable(GL_DEPTH_TEST);
}
if (ambientlighting)
{
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_NORMALIZE);
setShademodel();
//glDisable(GL_LIGHTING);
}
else
{
}
}
float wooddif1[] = { 0.647059f, 0.164706f, 0.164706f , 1.0f };
float woodamb1[] = { 0.41f, 0.164706f, 0.164706f, 1.0f };
float woodspe1[] = { 0.547059f, 0.364706f, 0.264706f, 1.0f };
float woodshiny1 = 60;
void setMaterial(int m) // 1=plane 2= 3=ground 4=wood 5=roof 6=wood2 7=
{
if (m == 1)
{
glMaterialfv(GL_FRONT, GL_AMBIENT, woodamb1);
glMaterialfv(GL_FRONT, GL_DIFFUSE, wooddif1);
glMaterialfv(GL_FRONT, GL_SPECULAR, woodspe1);
glMaterialf(GL_FRONT, GL_SHININESS, woodshiny1);
}
if (m == 2) // 2
{
}
}

A wild guess: the glEnable(GL_LIGHT0) is missing in your code (at least in the posted part).
Just in case, the glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); might also be useful to be sure that back-facing triangles are lit.
After that, the GL_SHININESS and GL_SPECULAR parameters should be tweaked to get this kind of highlights.

Related

How can I create a PopUp menu to adjust the light setting in OpenGL

Can I create a PopUp menu to change or modify my light property in OpenGL using SDL?
I am trying to use glutCreateMenu();:
void mydisplay()
{
//glClear(GL_COLOR_BUFFER_BIT);
GLfloat tp_ambient[] = { 0.05,0.05,0.05,1 };
GLfloat tp_diffuse[] = { 0.7,0.3,1,1 };
GLfloat tp_specular[] = { 0.6,0.6,0.6,1 };
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, tp_ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, tp_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, tp_specular);
glPushMatrix();
glTranslatef(0.0f, 0.0f, -0.6f);
GLUquadricObj *quadric = gluNewQuadric();
gluSphere(quadric, 0.1, 16, 16);
gluDeleteQuadric(quadric);
glPopMatrix();
/*
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -0.1f);*/
glutCreateMenu(ProcessMenu);
glutAddMenuEntry("position change", 0);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void ProcessMenu(int value) {
if (value == 0) {
GLfloat ambient[] = { 0.1,0.1,0.1,1 };
}
glutPostRedisplay();
}
But I get this error:
Error C2065 'ProcessMenu': undeclared identifier
What should I set up?
Am I using the correct way to build a PopUp menu?
If it is useful to view all my coding, please have a look on my previous, solved question.

How to use the ambient color of the material instead of the global ambient color of lights? (OpenGL 1)

I'm writting the lighting effect of my programming in OpenGL 1.1. The light and normals stuffs works. I have only a wrong color
I would like to use the ambient color of the used material but not the global ambient light.
When I use this code, I have a dark grey(64, 64, 64) for light-hidden face but never black (0, 0, 0).
This is the pseudo code :
void Init()
{
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
SetAmbientLight(64, 64, 64); // global value
material.ambientColor = VECTOR3F(0,0,0); // black color, other are default values of OpenGL
}
void SetAmbientLight(U8 r, U8 g, U8 b)
{
// Ambient Light is a global value
const GLfloat inv = 1.0f / 255.0f;
const GLfloat ambientColor[] = {r * inv, g * inv, b * inv, 1.0f};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
}
void Render()
{
UseMaterial(material);
RenderTexturedObject();
UseMaterial(NULL);
UseLights();
}
void UseMaterial(MATERIAL *materialdata)
{
if (materialdata)
{
const VECTOR3F& ambiantColor = materialdata->ambiantColor;
const VECTOR3F& diffuseColor = materialdata->diffuseColor;
const VECTOR3F& specularColor = materialdata->specularColor;
const float& shiness = materialdata->shiness;
const VECTOR3F& emissiveColor = materialdata->emissiveColor;
const GLfloat amb[4] = {ambiantColor.x, ambiantColor.y, ambiantColor.z, 1.0f};
const GLfloat diff[4] = {diffuseColor.x, diffuseColor.y, diffuseColor.z, 1.0f};
const GLfloat spec[4] = {specularColor.x, specularColor.y, specularColor.z, 1.0f};
const GLfloat emi[4] = {emissiveColor.x, emissiveColor.y, emissiveColor.z, 1.0f};
glEnable(GL_COLOR_MATERIAL);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, amb);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diff);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shiness);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, emi);
}
else
{
glDisable(GL_COLOR_MATERIAL);
}
}
void RenderTexturedObject()
{
FillVertexBuffer(mesh, t);
FillTexCoordBuffer(mesh);
FillColorBuffer(mesh, 255, 255, 255);
glColor3ub(255, 255, 255);
// Use normals only when lighting is used
if (IsLightingEnabled() && s_nbLights > 0)
{
FillNormalBuffer(mesh, t);
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, 12, gNormals);
}
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, gVertices);
glTexCoordPointer(2, GL_FLOAT, 0, gTexCoords);
glColorPointer(3, GL_UNSIGNED_BYTE, 0, gColors);
glDrawElements(GL_TRIANGLES, 3 * mesh->nbtriangles, GL_UNSIGNED_SHORT, gIndexes);
glDisable (GL_TEXTURE_2D);
if (IsLightingEnabled() && s_nbLights > 0)
{
glDisableClientState(GL_NORMAL_ARRAY);
}
}
void UseLights()
{
for (int lightIdx = GL_LIGHT0; lightIdx <= GL_LIGHT7; lightIdx++)
{
const int idx = lightIdx - GL_LIGHT0;
const LIGHT_DATA& lightdata = s_lightsData[idx];
const VECTOR3F& ambiantColor = lightdata.ambiantColor;
const VECTOR3F& diffuseColor = lightdata.diffuseColor;
const VECTOR3F& specularColor = lightdata.specularColor;
const VECTOR3F& direction = lightdata.direction;
const GLfloat amb[4] = {ambiantColor.x, ambiantColor.y, ambiantColor.z, 1.0f};
const GLfloat diff[4] = {diffuseColor.x, diffuseColor.y, diffuseColor.z, 1.0f};
const GLfloat spec[4] = {specularColor.x, specularColor.y, specularColor.z, 1.0f};
const GLfloat dir[4] = {-direction.x, -direction.y, -direction.z, 0.0f}; // always use w=0.0 for a direction
glLightfv(lightIdx, GL_AMBIENT, amb);
glLightfv(lightIdx, GL_DIFFUSE, diff);
glLightfv(lightIdx, GL_SPECULAR, spec);
glLightfv(lightIdx, GL_POSITION, dir);
glLightf(lightIdx, GL_SPOT_EXPONENT, 0.0f);
glLightf(lightIdx, GL_SPOT_CUTOFF, 180.0f);
}
}
The solution is here : https://www.sjbaker.org/steve/omniv/opengl_lighting.html
2 things to do :
I must to remove glEnable(GL_COLOR_MATERIAL);
I must also to set the global ambient with (1.0f,1.0f,1.0f,1.0f)

OpenGL depth test is not working

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.

`xxxx' : looks like a function definition, but there is no parameter list; skipping apparent body

I have the code below which is a bowling game. This code was working good in Eclipse but I Then moved this to microsoft visual studio express 2012 for windows desktop, it gives the following error.
Error 1 error C2470: 'cone1' : looks like a function definition, but there is no parameter list; skipping apparent body c:\users\rocckky\documents\visual studio 2012\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 26 1 ConsoleApplication1
Note: - I am not pasting all errors, it gives the same error where type cones are defined.
#include "stdafx.h"
#include "cstdlib"
#include "GL/glut.h"
#include "GL/glu.h"
#include "stdlib.h"
#include "vector"
using namespace std;
int refreshMillis = 30; // Refresh period in milliseconds
int windowWidth = 640; // Windowed mode's width
int windowHeight = 480; // Windowed mode's height
int windowPosX = 50; // Windowed mode's top-left corner x
int windowPosY = 50; // Windowed mode's top-left corner y
bool fullScreenMode = false; // Full-screen or windowed mode?
GLfloat ballSpeed = 0.150f; // Ball's speed in y directions
GLfloat speedLine;
GLfloat ballMaxSpeed = 0.550f, ballMinSpeed = 0.150f;
bool moveBallUp = false, moveBallDown = false, isCollision = false, resetCall =
false, moveRight = false, moveLeft = false, ballInRight = false,
ballInMiddle = true, ballInLeft = false;
GLfloat vp_x = 2.0f, vp_y = 10.0f, vp_z = 25.0f, vt_x = 2.0f, vt_y = 20.0f,
vt_z = 0.0f, vu_x = 0.0f, vu_y = 0.0f, vu_z = 1.0f;
//eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz
/* the cones in the center */
vector<GLfloat> cone1 { 0.0f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0f, 0.0f };
vector<GLfloat> cone2 { 1.6f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0 };
vector<GLfloat> cone3 { -1.6f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0 };
/* the cones in the left */
vector<GLfloat> cone4 { -4.9f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0 };
vector<GLfloat> cone5 { -6.7f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0 };
vector<GLfloat> cone6 { -8.3f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0 };
/* the right cones */
vector<GLfloat> cone7 { 4.9f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0 };
vector<GLfloat> cone8 { 6.6f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0 };
vector<GLfloat> cone9 { 8.3f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0 };
vector<GLfloat> ball {/* X */0.0f, /* Y */0.0f, /* Z */-3.0f, /*sphere*/0.85f,
50.0, 50.0 };
//
void resetGame() {
resetCall = true;
cone1 = {0.0f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0f, 0.0f};;
cone2= {1.6f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0};
cone3= {-1.6f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0};
/* the cones in the left */
cone4 = {-4.9f,0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0};
cone5 = {-6.7f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0};
cone6 = {-8.3f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0};
/* the right cones */
cone7= {4.9f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0};
cone8 = {6.6f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0};
cone9= {8.3f, 0.0f, -21.0f, /*rotated*/60.0f, -1.5f, 0.0, 0.0};
ball = {/* X */0.0f, /* Y */0.0f, /* Z */-3.0f, /*sphere*/0.85f, 50.0,
50.0};
}
float ar;
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
/* Called back when the timer expired */
void Timer(int value) {
if (moveBallUp) {
// ball[1] += ballSpeed;
ball[2] -= ballSpeed;
if (ballInRight)
ball[0] += 0.08;
if (ballInLeft)
ball[0] -= 0.08;
}
if (moveRight) {
if (ball[0] >= 4.0) {
moveRight = false;
ballInMiddle = false;
ballInLeft = false;
ballInRight = true;
}
ball[0] += 0.156;
if (ball[0] >= -0.02 && ball[0] <= 0.02) {
moveRight = false;
ballInLeft = false;
ballInRight = false;
ballInMiddle = true;
}
}
if (moveLeft) {
if (ball[0] <= -4.0) {
moveLeft = false;
ballInRight = false;
ballInMiddle = false;
ballInLeft = true;
}
ball[0] -= 0.156;
if (ball[0] >= -0.02 && ball[0] <= 0.02) {
moveLeft = false;
ballInLeft = true;
}
}
/* If ball reaches to Z coordinates of the all cones */
if (ball[2] <= cone1[2]) {
/* Now check ball's x axis that which set of cones has it hit */
/* check for the middle set of cones */
if (ball[0] >= cone3[0] && ball[0] <= cone2[0]) {
if (!isCollision /* isCollision ! = true */) {
cone1[0] -= 0.5;
cone1[4] -= 10.0;
cone1[5] += 10.0;
cone1[2] += -0.3;
cone2[0] += 0.5;
cone2[4] -= 10.0;
cone2[5] -= 10.0;
cone2[2] += -0.4;
cone3[0] += 0.5;
cone3[4] -= 10.0;
cone3[5] -= 10.0;
cone3[2] += -0.4;
}
isCollision = true;
moveBallUp = false; // stop moving the ball
}
/* check if ball is in the range of x axis of right set of cones */
if (ball[0] >= cone6[0] && ball[0] <= cone4[0]) {
if (!isCollision /* isCollision ! = true */) {
cone4[0] -= 0.5;
cone4[4] -= 10.0;
cone4[5] += 10.0;
cone4[2] += -0.3;
cone5[0] += 0.5;
cone5[4] -= 10.0;
cone5[5] -= 10.0;
cone5[2] += -0.4;
cone6[0] += 0.5;
cone6[4] -= 10.0;
cone6[5] -= 10.0;
cone6[2] += -0.4;
}
isCollision = true;
moveBallUp = false; // stop moving the ball
}
if (ball[0] >= cone7[0] && ball[0] <= cone9[0]) {
if (!isCollision /* isCollision ! = true */) {
cone7[0] -= 0.5;
cone7[4] -= 10.0;
cone7[5] += 10.0;
cone7[2] += -0.3;
cone8[0] += 0.5;
cone8[4] -= 10.0;
cone8[5] -= 10.0;
cone8[2] += -0.4;
cone9[0] += 0.5;
cone9[4] -= 10.0;
cone9[5] -= 10.0;
cone9[2] += -0.4;
}
isCollision = true;
moveBallUp = false; // stop moving the ball
}
}
if (resetCall) {
if (ball[2] >= -3.0f) {
resetCall = false;
isCollision = false;
}
else {
if (ballInRight)
ball[0] -= 0.08;
if (ballInLeft)
ball[0] += 0.08;
ball[2] -= ballSpeed;
}
}
glutPostRedisplay(); // Post a paint request to activate display()
glutTimerFunc(refreshMillis, Timer, 0); // subsequent timer call at milliseconds
}
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 27: // ESC key
exit(0);
break;
case 'r':
resetGame();
break;
case 'i':
vp_x += 0.5;
break;
case 'I':
vp_x -= 0.5;
break;
/*GLfloat vp_x = 3.0f, vp_y = 10.0f, vp_z = 10.0f, vt_x = 0.0f, vt_y = 0.0f,
vt_z = 0.0f, vu_x = 0.0f, vu_y = 0.0f, vu_z = -1.0f;*/
case 'o':
vp_y += 0.5;
break;
case 'O':
vp_y -= 0.5;
break;
case 'p':
vp_z += 0.5;
break;
case 'P':
vp_z -= 0.5;
break;
case 'j':
vt_x += 0.5;
break;
case 'J':
vt_x -= 0.5;
break;
case 'k':
vt_y += 0.5;
break;
case 'K':
vt_y -= 0.5;
break;
case 'l':
vt_z += 0.5;
break;
case 'L':
vt_z -= 0.5;
break;
case 'b':
vu_x += 0.5;
break;
case 'B':
vu_x -= 0.5;
break;
case 'n':
vu_y += 0.5;
break;
case 'N':
vu_y -= 0.5;
break;
case 'm':
vt_z += 0.5;
break;
case 'M':
vu_z -= 0.5;
break;
}
glutPostRedisplay();
}
void specialKeys(int key, int x, int y) {
switch (key) {
case GLUT_KEY_F1: // F1: Toggle between full-screen and windowed mode
fullScreenMode = !fullScreenMode; // Toggle state
if (fullScreenMode) { // Full-screen mode
windowPosX = glutGet(GLUT_WINDOW_X ); // Save parameters for restoring later
windowPosY = glutGet(GLUT_WINDOW_Y );
windowWidth = glutGet(GLUT_WINDOW_WIDTH );
windowHeight = glutGet(GLUT_WINDOW_HEIGHT );
glutFullScreen(); // Switch into full screen
} else { // Windowed mode
glutReshapeWindow(windowWidth, windowHeight); // Switch into windowed mode
glutPositionWindow(windowPosX, windowPosX); // Position top-left corner
}
break;
case GLUT_KEY_UP:
if (!isCollision)
moveBallUp = true;
break;
case GLUT_KEY_PAGE_UP:
if (ballSpeed >= ballMaxSpeed)
break;
ballSpeed *= 1.1f;
break;
case GLUT_KEY_PAGE_DOWN:
if (ballSpeed <= ballMinSpeed)
break;
ballSpeed *= 0.95f;
break;
case GLUT_KEY_RIGHT:
if (ball[0] >= 4.0)
break;
moveRight = true;
break;
case GLUT_KEY_LEFT:
if (ball[0] <= -4.0)
break;
moveLeft = true;
break;
}
}
static void display(void) {
int const width = glutGet(GLUT_WINDOW_WIDTH );
int const height = glutGet(GLUT_WINDOW_HEIGHT );
const float ar = (float) width / (float) height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-ar, ar, -ar, ar, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(vp_x, vp_y, vp_z, vt_x, vt_y, vt_z, vu_x, vu_y, vu_z);
glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
/* Center */
glColor3d(1, 1, 0);
glPushMatrix();
glTranslated(cone1[0], cone1[1], cone1[2]);
glRotated(cone1[3], cone1[4], cone1[5], cone1[6]);
glutSolidCone(0.8, 2, 50, 50);
glPopMatrix();
glColor3d(1, 0, 1);
glPushMatrix();
glTranslated(cone2[0], cone2[1], cone2[2]);
glRotated(cone2[3], cone2[4], cone2[5], cone2[6]);
glutSolidCone(0.8, 2, 50, 50);
glPopMatrix();
glColor3d(0, 0, 1);
glPushMatrix();
glTranslated(cone3[0], cone3[1], cone3[2]);
glRotated(cone3[3], cone3[4], cone3[5], cone3[6]);
glutSolidCone(0.8, 2, 50, 50);
glPopMatrix();
/************************************************************/
/* Left cones */
glColor3d(1, 1.5, 0.6);
glPushMatrix();
glTranslated(cone4[0], cone4[1], cone4[2]);
glRotated(cone4[3], cone4[4], cone4[5], cone4[6]);
glutSolidCone(0.8, 2, 50, 50);
glPopMatrix();
glColor3d(1, 1.5, 0.6);
glPushMatrix();
glTranslated(cone5[0], cone5[1], cone5[2]);
glRotated(cone5[3], cone5[4], cone5[5], cone5[6]);
glutSolidCone(0.8, 2, 50, 50);
glPopMatrix();
glColor3d(1, 1.5, 0.6);
glPushMatrix();
glTranslated(cone6[0], cone6[1], cone6[2]);
glRotated(cone6[3], cone6[4], cone6[5], cone6[6]);
glutSolidCone(0.8, 2, 50, 50);
glPopMatrix();
/*******************************************************/
/* Right cones */
glColor3d(1, 1.5, 0.6);
glPushMatrix();
glTranslated(cone7[0], cone7[1], cone7[2]);
glRotated(cone7[3], cone7[4], cone7[5], cone7[6]);
glutSolidCone(0.8, 2, 50, 50);
glPopMatrix();
glColor3d(1, 1.5, 0.6);
glPushMatrix();
glTranslated(cone8[0], cone8[1], cone8[2]);
glRotated(cone8[3], cone8[4], cone8[5], cone8[6]);
glutSolidCone(0.8, 2, 50, 50);
glPopMatrix();
glColor3d(1, 1.5, 0.6);
glPushMatrix();
glTranslated(cone9[0], cone9[1], cone9[2]);
glRotated(cone9[3], cone9[4], cone9[5], cone9[6]);
glutSolidCone(0.8, 2, 50, 50);
glPopMatrix();
/****************************/
/* THE BALLING BALL */
glColor3d(1, 0, 0);
glPushMatrix();
glTranslated(ball[0], ball[1], ball[2]);
glutSolidSphere(ball[3], ball[4], ball[5]);
glPopMatrix();
/******* Floor ********/
//glPushMatrix();
glColor3d(0.4, 1, 0.20);
glBegin(GL_QUADS);
glVertex3f(21.0, -1.0, -25.0);
glVertex3f(-21.0, -1.0, -25.0);
glVertex3f(-20.0, -1.0, -4.0);
glVertex3f(20.0, -1.0, -4.0);
glEnd();
/** wall at the back **/
glColor3d(0, 1, 1);
glBegin(GL_QUADS);
glVertex3f(21.0, 10.0, -25.0);
glVertex3f(-21.0, 10.0, -25.0);
glVertex3f(-21.0, -1.0, -25.0);
glVertex3f(21.0, -1.0, -25.0);
glEnd();
/* // speed line
glColor3d(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glVertex3f(7.0, 7.0, -24.9999);
glVertex3f(7.5, 7.0, -24.9999);
glVertex3f(6.0, -4.0, -4.9999);
glVertex3f(6.0, -4.0, -4.9999);
glEnd();
Lines between three
glColor3d(0.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glVertex3f(-7.5, 7.0, -24.9999);
glVertex3f(-7.0, 7.0, -24.9999);
glVertex3f(-2.0, -4.0, -4.9999);
glVertex3f(-1.5, -4.0, -4.9999);
glEnd();
Speed line
glLineWidth(8.0f);
glColor3d(1.0f, 0.0f, 0.0f);
glBegin(GL_LINE_STRIP);
%age from the range of speed by the current speed
speedLine = (100 / (ballMaxSpeed - ballMinSpeed)) * ballSpeed;
speedLine = (3.0 / 100) * speedLine;
glVertex3f(6.0, speedLine, -5.0);
glVertex3f(6.0, 0.0, -4.999);
glEnd();
*/
//glPopMatrix();
glutSwapBuffers();
}
/* Program entry point */
int main(int argc, char *argv[]) {
glutInit(&argc, argv);
glutInitWindowSize(windowWidth, windowHeight); // Initial window width and height
glutInitWindowPosition(windowPosX, windowPosY); // Initial window top-left corner (x, y)
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Balling Game 3d");
glutDisplayFunc(display);
glutTimerFunc(0, Timer, 0); // First timer call immediately
glutSpecialFunc(specialKeys); // Register callback handler for special-key event
glutKeyboardFunc(keyboard); // Register callback handler for special-key event
glutMainLoop();
return 1;
}
Visual studio 2012 does not have list-initialization yet for the standard containers.
Take a look here and here to see the features already supported by VS. (or here for VS2013).

gluLookAt() doesn't work as expected

I have created a bowling game in OpenGL using Eclipse.
Now I want to change the view of camera upon key-pressed.
But When I press x button, everything disappears.
Here us the code: -
#include <GL/glut.h>
#include <stdlib.h>
int refreshMillis = 30; // Refresh period in milliseconds
int windowWidth = 640; // Windowed mode's width
int windowHeight = 480; // Windowed mode's height
int windowPosX = 50; // Windowed mode's top-left corner x
int windowPosY = 50; // Windowed mode's top-left corner y
bool fullScreenMode = false; // Full-screen or windowed mode?
GLfloat ballTSpeed = 0.15f; // Ball's speed in y directions
GLfloat x = 1.0f, y = 10.0f, z = 10.0f, i = 0.0f, j = 0.0f, k = 0.0f, a = 0.0f,
b = 0.0f, c = -1.0f;
bool moveBallUp = false, moveBallDown = false, isCollision = false, resetCall =
false;
//
GLfloat cone1[] = { 0.0f, 2.5f, -11.0f, /*rotated*/30.0f, -1.5, 0.0, 0.0 };
GLfloat cone2[] = { 2.0f, 2.5f, -11.0f, /*rotated*/30.0f, -1.5, 0.0, 0.0 };
GLfloat cone3[] = { -2.0f, 2.5f, -11.0f, /*rotated*/30.0f, -1.5, 0.0, 0.0 };
GLfloat ball[] = {/* X */0.0f, /* Y */-2.0f, /* Z */-6.0f, /*sphere*/1.0f, 50.0,
50.0 };
//
void resetGame() {
resetCall = true;
cone1[0] = 0.0f;
cone1[1] = 2.5f;
cone1[2] = -11.0f;
/*rotated*/
cone1[3] = 30.0f;
cone1[4] = -1.5;
cone1[5] = 0.0;
cone1[6] = 0.0;
cone2[0] = 2.0f;
cone2[1] = 2.5f;
cone2[2] = -11.0f;
/*rotated*/
cone2[3] = 30.0f;
cone2[4] = -1.5;
cone2[5] = 0.0;
cone2[6] = 0.0;
cone3[0] = -2.0f;
cone3[1] = 2.5f;
cone3[2] = -11.0f;
/*rotated*/
cone3[3] = 30.0f;
cone3[4] = -1.5;
cone3[5] = 0.0;
cone3[6] = 0.0;
}
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
static void resize(int width, int height) {
const float ar = (float) width / (float) height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-ar, ar, -1.0, 1.0, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(x, y, z, i, j, k, a, b, c);
// eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz
}
/* Called back when the timer expired */
void Timer(int value) {
glutPostRedisplay(); // Post a paint request to activate display()
glutTimerFunc(refreshMillis, Timer, 0); // subsequent timer call at milliseconds
}
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 27: // ESC key
exit(0);
break;
case 'r':
resetGame();
break;
case 'i':
x += 0.5;
gluLookAt(x, y, z, i, j, k, a, b, c);
// eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz
}
}
void specialKeys(int key, int x, int y) {
switch (key) {
case GLUT_KEY_F1: // F1: Toggle between full-screen and windowed mode
fullScreenMode = !fullScreenMode; // Toggle state
if (fullScreenMode) { // Full-screen mode
windowPosX = glutGet(GLUT_WINDOW_X ); // Save parameters for restoring later
windowPosY = glutGet(GLUT_WINDOW_Y );
windowWidth = glutGet(GLUT_WINDOW_WIDTH );
windowHeight = glutGet(GLUT_WINDOW_HEIGHT );
glutFullScreen(); // Switch into full screen
} else { // Windowed mode
glutReshapeWindow(windowWidth, windowHeight); // Switch into windowed mode
glutPositionWindow(windowPosX, windowPosX); // Position top-left corner
}
break;
case GLUT_KEY_UP:
if (!isCollision)
moveBallUp = true;
break;
case GLUT_KEY_PAGE_UP:
ballTSpeed *= 1.2f;
break;
}
}
static void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (moveBallUp) {
ball[1] += ballTSpeed;
ball[2] -= 0.02 + ballTSpeed;
}
if (ball[1] >= (cone1[1] - 0.4) && ball[1] <= cone1[1]) {
if (!isCollision)
{
cone1[0] -= 0.5;
cone1[4] -= 10.0;
cone1[5] += 10.0;
cone1[2] += -0.3;
cone2[0] += 0.5;
cone2[4] -= 10.0;
cone2[5] -= 10.0;
cone2[2] += -0.4;
cone3[0] += 0.5;
cone3[4] -= 10.0;
cone3[5] -= 10.0;
cone3[2] += -0.4;
}
isCollision = true;
moveBallUp = false; // stop moving the ball
}
if (resetCall) {
if ((ball[1] >= -2.0f && ball[1] <= -1.6f)
&& (ball[2] >= -6.0f && ball[2] <= -5.6f)) {
resetCall = false;
isCollision = false;
}
else {
ball[1] -= ballTSpeed;
ball[2] += 0.02 + ballTSpeed;
}
}
glColor3d(1, 1, 0);
glPushMatrix();
glTranslated(cone1[0], cone1[1], cone1[2]);
glRotated(cone1[3], cone1[4], cone1[5], cone1[6]);
glutSolidCone(1, 2, 50, 50);
glPopMatrix();
glColor3d(1, 0, 1);
glPushMatrix();
glTranslated(cone2[0], cone2[1], cone2[2]);
glRotated(cone2[3], cone2[4], cone2[5], cone2[6]);
glutSolidCone(1, 2, 50, 50);
glPopMatrix();
glColor3d(0, 0, 1);
glPushMatrix();
glTranslated(cone3[0], cone3[1], cone3[2]);
glRotated(cone3[3], cone3[4], cone3[5], cone3[6]);
glutSolidCone(1, 2, 50, 50);
glPopMatrix();
glColor3d(1, 0, 0);
glPushMatrix();
glTranslated(ball[0], ball[1], ball[2]);
glutSolidSphere(ball[3], ball[4], ball[5]);
glPopMatrix();
glPushMatrix();
glColor3d(0.6, 1, 0.20);
glBegin(GL_QUADS);
glVertex3f(16.0, 5.0, -25.0);
glVertex3f(-16.0, 5.0, -25.0);
glVertex3f(-6.0, -4.0, -5.0);
glVertex3f(6.0, -4.0, -5.0);
glEnd();
glColor3d(1, 1, 0);
glBegin(GL_QUADS);
glVertex3f(16.0, 15.0, -25.0);
glVertex3f(-16.0, 15.0, -25.0);
glVertex3f(-16.0, -4.0, -25.0);
glVertex3f(16.0, -4.0, -25.0);
glEnd();
glutSwapBuffers();
}
/* Program entry point */
int main(int argc, char *argv[]) {
glutInit(&argc, argv);
glutInitWindowSize(windowWidth, windowHeight); // Initial window width and height
glutInitWindowPosition(windowPosX, windowPosY); // Initial window top-left corner (x, y)
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Balling Game 3d");
glutReshapeFunc(resize);
glutDisplayFunc(display);
glClearColor(1, 1, 1, 1);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
glutTimerFunc(0, Timer, 0); // First timer call immediately
glutSpecialFunc(specialKeys); // Register callback handler for special-key event
glutKeyboardFunc(keyboard); // Register callback handler for special-key event
glutMainLoop();
return EXIT_SUCCESS;
}
In the code case 'i':
x += 0.5;
gluLookAt(x, y, z, i, j, k, a, b, c);
// eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz
Camera view should be changed as I guess but I know that I am doing wrong. Please tell me how to do this?
Never call OpenGL functions from input event handlers. Only misery and dispair comes out of this.
In your input event handlers set variables from the user input data and trigger a redraw. In the drawing function parameterize the rendering process from those variables.
You can remove the resize handler entirely. Setup viewport and projection in the display function
static void display(void) {
int const width = glutGet(GLUT_WINDOW_WIDTH);
int const height = glutGet(GLUT_WINDOW_HEIGHT);
float const ar = (float) width / (float) height;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-ar, ar, -1.0, 1.0, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(view_x, view_y, view_z, target_x, target_y, target_z, up_x, up_y, up_z);
// eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz
/* ... */
In the keyboard handler just set variables and trigger a redisplay
void keyboard(unsigned char key, int mouse_x, int mouse_y) {
switch (key) {
case 27: // ESC key
exit(0);
break;
case 'r':
resetGame();
break;
case 'i':
view_x += 0.5;
/* don't call gluLookAt here! */
}
glutPostRedisplay();
}