gluLookAt() doesn't work as expected - c++

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

Related

OpenGL/Glut: Making the camera rotate around X axis with arrow keys?

I'm trying to make the gluLookAt() function so that when I press the up & down key the camera moves around the X axis
I'm trying a method I saw at: http://www.lighthouse3d.com/tutorials/glut-tutorial/keyboard-example-moving-around-the-world/
but it's not working for me. Anyone knows an easier way? and where should I put the gluLookAt() so on myDisplay() func?
#include"glut.h"
#include<cmath>
#include<iostream>
using namespace std;
float xr = 0, yr = 0; //to control the object's movement from left to right
// actual vector representing the camera's direction
float lx = 0.0f, lz = -1.0f;
// XZ position of the camera
float x = 0.0f, z = 5.0f;
GLfloat angle = 0.0f;
int refreshmill = 1;
void timer(int value) { //to control the rotation of the object
glutPostRedisplay();
glutTimerFunc(refreshmill, timer, 0);
}
void myDisplay(void) {
//Circle One
float theta;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 0, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glBegin(GL_POLYGON);
for (int x = 0; x < 360; x++) {
theta = x * 3.142 / 180;
glVertex2f(150 * cos(theta)+xr,150 * sin(theta)+yr);
}
glEnd();
glPopMatrix();
//Circle Two
float theta2;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslatef(0.5f, 0.0f, 0.0f); // rotation
glRotatef(angle, 0.0f, 0.0f, -0.5f); // rotation
glBegin(GL_POLYGON);
glColor3f(0, 0, 1);
for (int x = 0; x < 360; x++) {
theta2 = x * 3.142 / 180;
glVertex2f(150 + 15 * cos(theta2) + xr, 15 * sin(theta2) + yr);
}
glutSwapBuffers();
angle += 0.2; // rotation
glEnd();
glPopMatrix();
//Draw Star
glColor3ub(119, 193, 15);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glBegin(GL_POLYGON);
glVertex2d(15+xr, 60+yr);
glVertex2d(75+xr, 75+yr); //right peak
glVertex2d(15+xr, 90+yr);
glVertex2d(0+xr, 150+yr); //Up-peak Changed
glVertex2d(-15+xr, 90+yr);
glVertex2d(-75+xr,75+yr);
glVertex2d(-15+xr, 60+yr);
glVertex2d(0+xr,0+yr);
glEnd();
glPopMatrix();
glFlush();
glutPostRedisplay();
glutSwapBuffers();
}
void renderScene(void) {
// Clear Color and Depth Buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset transformations
glLoadIdentity();
// Set the camera
gluLookAt(x, 1.0f, z,
x + lx, 1.0f, z + lz,
0.0f, 1.0f, 0.0f);
// Draw ground
glColor3f(0.9f, 0.9f, 0.9f);
glBegin(GL_QUADS);
glVertex3f(-100.0f, 0.0f, -100.0f);
glVertex3f(-100.0f, 0.0f, 100.0f);
glVertex3f(100.0f, 0.0f, 100.0f);
glVertex3f(100.0f, 0.0f, -100.0f);
glEnd();
myDisplay();
glutSwapBuffers();
}
//Move to left or right
void keyboard(int key, int x, int y) {
float fraction = 0.1f;
switch (key) {
case GLUT_KEY_RIGHT:
xr++;
cout << x << endl;
glutPostRedisplay();
break;
case GLUT_KEY_LEFT:
xr--;
cout << x << endl;
glutPostRedisplay();
break;
case GLUT_KEY_UP:
angle -= 0.01f;
lx = sin(angle);
lz = -cos(angle);
break;
case GLUT_KEY_DOWN:
angle += 0.01f;
lx = sin(angle);
lz = -cos(angle);
break;
}
}
void init() {
glClearColor(0, 0, 0, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-250, 250, -250, 250); //IMPORTANT- Define from negative to positive
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv) {
// init GLUT and create window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Homework: Circle");
// register callbacks
glutDisplayFunc(myDisplay);
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutTimerFunc(0,timer,0);
glutSpecialFunc(keyboard);
// OpenGL init
init();
// enter GLUT event processing cycle
glutMainLoop();
}
First of all there should be only 1 display call back function:
int main(int argc, char** argv) {
// [...]
// glutDisplayFunc(myDisplay); <----- DELETE!!!
glutDisplayFunc(renderScene);
// glutIdleFunc(renderScene); <----- DELETE!!!
// [...]
}
Setup an an orthographic projection with an extended near and far plane. If the object is rotated around the X axis, it takes space in the 3 dimensions:
void init() {
glClearColor(0, 0, 0, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-250, 250, -250, 250, -250, 250); // <----
glMatrixMode(GL_MODELVIEW);
}
Add a variable anglaX which is changed in the keyboard event:
float angleX = 0.0f;
void keyboard(int key, int x, int y) {
switch (key) {
case GLUT_KEY_RIGHT: xr++; break;
case GLUT_KEY_LEFT: xr--; break;
case GLUT_KEY_UP: angleX -= 1.0f; break;
case GLUT_KEY_DOWN: angleX += 1.0f; break;
}
}
Rotate the model, after the view was set:
gluLookAt(x, 0.0f, z, x, 0.0f, z-1.0f, 0.0f, 1.0f, 0.0f);
glRotatef(angleX, 1, 0, 0);
Don't do any calls to glutSwapBuffers(), glFlush() and glutPostRedisplay(), except at the end of renderScene:
void timer(int value) { //to control the rotation of the object
// glutPostRedisplay(); <--- DELETE
glutTimerFunc(refreshmill, timer, 0);
}
void myDisplay(void) {
//Circle One
float theta;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 0, 0);
glPushMatrix();
glBegin(GL_POLYGON);
for (int x = 0; x < 360; x++) {
theta = x * 3.142 / 180;
glVertex2f(150 * cos(theta)+xr,150 * sin(theta)+yr);
}
glEnd();
glPopMatrix();
//Circle Two
float theta2;
glPushMatrix();
glTranslatef(0.5f, 0.0f, 0.0f); // rotation
glRotatef(angle, 0.0f, 0.0f, -0.5f); // rotation
glBegin(GL_POLYGON);
glColor3f(0, 0, 1);
for (int x = 0; x < 360; x++) {
theta2 = x * 3.142 / 180;
glVertex2f(150 + 15 * cos(theta2) + xr, 15 * sin(theta2) + yr);
}
angle += 0.2; // rotation
glEnd();
glPopMatrix();
//Draw Star
glColor3ub(119, 193, 15);
glPushMatrix();
glBegin(GL_POLYGON);
glVertex2d(15+xr, 60+yr);
glVertex2d(75+xr, 75+yr); //right peak
glVertex2d(15+xr, 90+yr);
glVertex2d(0+xr, 150+yr); //Up-peak Changed
glVertex2d(-15+xr, 90+yr);
glVertex2d(-75+xr,75+yr);
glVertex2d(-15+xr, 60+yr);
glVertex2d(0+xr,0+yr);
glEnd();
glPopMatrix();
}
void renderScene(void) {
// Clear Color and Depth Buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset transformations
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Set the camera
gluLookAt(x, 0.0f, z, x, 0.0f, z-1.0f, 0.0f, 1.0f, 0.0f);
glRotatef(angleX, 1, 0, 0);
// Draw ground
glColor3f(0.9f, 0.9f, 0.9f);
glBegin(GL_QUADS);
glVertex3f(-100.0f, 0.0f, -100.0f);
glVertex3f(-100.0f, 0.0f, 100.0f);
glVertex3f(100.0f, 0.0f, 100.0f);
glVertex3f(100.0f, 0.0f, -100.0f);
glEnd();
myDisplay();
glFlush();
glutPostRedisplay();
glutSwapBuffers();
}
Further I recommend to use double buffering:
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
You can init your camera with gluLookAt at init(), then rotate it when arrow keys pressed. If you want to rotate camera around it's local x axis, assume your initial modelview matrix is V, newly happened rotation around x axis is R, you need to set modelview matrix to R*V, not V*R.
case GLUT_KEY_UP:
GLfloat temp[16];
glGetFloatv(GL_MODELVIEW_MATRIX, temp);
glLoadIdentity();
glRotate(stepAngle, 1, 0, 0); // calculate stepAngle by your self
glMultMatrixf(temp);
break;
You don need to reset modelview matrix during rendering, view part is already done, make sure you restore it after rendering the whole scene:
glPushMatrix(GL_MODELVIEW_MATRIX)
// don't call glLoadIdentity() here, you don't need to reset view part.
...
...
glPopMatrix()

Black screen on OpenGL

I have an openGL program that drawn 3 houses and another things but, when I compile with gcc, just appears a black screen - and this is happening with all my openGL programs
What can I do?
PS: If I resize my screen, the drawn appears.
OS: macOS Mojave
PC: MacBook Air 2017
Development in Visual Studio Code
gcc version: 10.0.0
#include <stdlib.h>
#include <stdio.h>
#include <glut/glut.h>
#include <math.h>
#define PI 3.14159265358979324
//teto
bool teto = true;
bool base = true;
bool janela = true;
bool porta = true;
void Teto(void)
{
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(20.0, 10.0, 0.0);
glVertex3f(40.0, 0.0, 0.0);
glEnd();
}
void Base(void)
{
glBegin(GL_QUADS);
glColor3f(0, 1, 0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 30.0, 0.0);
glVertex3f(35.0, 30.0, 0.0);
glVertex3f(35.0, 0.0, 0.0);
glEnd();
}
void Porta(void)
{
glBegin(GL_QUADS);
glColor3f(0, 0, 1);
glVertex3f(0, 0.0, 0.0);
glVertex3f(0, 10.0, 0.0);
glVertex3f(5.0, 10.0, 0.0);
glVertex3f(5.0, 0.0, 0.0);
glEnd();
}
void Janela()
{
glBegin(GL_QUADS);
glColor3f(0, 0, 0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 5.0, 0.0);
glVertex3f(5.0, 5.0, 0.0);
glVertex3f(5.0, 0.0, 0.0);
glEnd();
}
void Sun()
{
static float R = 10.0; // Radius of circle.
static float X = 20.0; // X-coordinate of center of circle.
static float Y = 70.0; // Y-coordinate of center of circle.
static int numVertices = 30; // Number of vertices on circle.
float t = 0; // Angle parameter.
int i;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 0.0);
// Draw a line loop with vertices at equal angles apart on a circle
// with center at (X, Y) and radius R, The vertices are colored randomly.
glBegin(GL_LINE_LOOP);
for(i = 0; i < numVertices; ++i)
{
glColor3f(1.0, 1.0, 0.0);
glVertex3f(X + R * cos(t), Y + R * sin(t), 0.0);
t += 2 * PI / numVertices;
}
glEnd();
}
// Drawing routine.
void drawHouse(void)
{
glPopMatrix();
glPushMatrix();
//glPopMatrix();
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
//Sun();
glTranslatef(30.0f,30.0f,0.0f);
if(teto)
Teto();
glTranslatef(3.0f,-30.0f,0.0f);
if(base)
Base();
glTranslatef(15.0f,0.0f,0.0f);
if(porta)
Porta();
glTranslatef(-10.0f,20.0f,0.0f);
if(janela)
Janela();
glTranslatef(20.0f,0.0f,0.0f);
if(janela)
Janela();
glPopMatrix();
glScalef(0.75f, 0.75f, 0.0f);
glTranslatef(3.0f,30.0f,0.0f);
if(teto)
Teto();
glTranslatef(3.0f,-30.0f,0.0f);
if(base)
Base();
glTranslatef(15.0f,0.0f,0.0f);
if(porta)
Porta();
glTranslatef(-10.0f,20.0f,0.0f);
if(janela)
Janela();
glTranslatef(20.0f,0.0f,0.0f);
if(janela)
Janela();
glPopMatrix();
glScalef(1.75f, 1.75f, 0.0f);
glScalef(1.25f, 1.25f, 0.0f);
//glScalef(0.75f, 0.75f, 0.0f);
glTranslatef(25.0f,20.0f,0.0f);
if(teto)
Teto();
glTranslatef(3.0f,-30.0f,0.0f);
if(base)
Base();
glTranslatef(15.0f,0.0f,0.0f);
if(porta)
Porta();
glTranslatef(-10.0f,20.0f,0.0f);
if(janela)
Janela();
glTranslatef(20.0f,0.0f,0.0f);
if(janela)
Janela();
glFlush();
}
// Initialization routine.
void setup(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
}
// OpenGL window reshape routine.
void resize(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 130.0, 0.0, 100.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// Keyboard input processing routine.
void keyInput(unsigned char key, int x, int y)
{
switch(key)
{
case 27:
exit(0);
break;
case 'c':
base = !base;
break;
case 't':
teto = !teto;
break;
case 'j':
janela = !janela;
break;
case 'p':
porta = !porta;
break;
case 's':
Sun();
//glRotatef(10.0, 0.0f, 0.0f, 1.0f);
break;
default:
break;
}
glutPostRedisplay();
}
// Main routine.
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(900, 700);
glutInitWindowPosition(100, 100);
glutCreateWindow("House.cpp");
glutDisplayFunc(drawHouse);
glutReshapeFunc(resize);
glutKeyboardFunc(keyInput);
setup();
glutMainLoop();
}

Opengl camera and render problems

I'm acutally trying open gl and have some questions:
I actually have this code:
#include <freeglut.h>
#include <glut.h>
#include <stdio.h>
using namespace std;
char presse;
int anglex=-45, angley=45, x, y, xold, yold;
/* Prototype des fonctions (function prototypes) */
void affichage();
void clavier(unsigned char touche, int x, int y);
void reshape(int x, int y);
void idle();
void mouse(int bouton, int etat, int x, int y);
void mousemotion(int x, int y);
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[] = { 1, 2, 1, 1 };
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 };
int main(int argc, char **argv)
{
/* initialisation de glut et creation
de la fenetre (glut initialization and window creation) */
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(200, 200);
glutInitWindowSize(1000, 1000);
glutCreateWindow("cube");
/* Initialisation d'OpenGL (OpenGL initialisation) */
glClearColor(0.0, 0.0, 0.0, 0.0);
glColor3f(1.0, 1.0, 1.0);
glPointSize(2.0);
glEnable(GL_DEPTH_TEST); // Active le test de profondeur
(activate depth testing)
glEnable(GL_LIGHTING); // Active l'éclairage
(activate lighting)
glEnable(GL_LIGHT0); // Allume la lumière n°1
(turn on light no. 1)
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
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);
/* enregistrement des fonctions de rappel (register callbacks) */
glutDisplayFunc(affichage);
glutKeyboardFunc(clavier);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMotionFunc(mousemotion);
/* Entree dans la boucle principale glut (entry of the main glut loop) */
glutMainLoop();
return 0;
}
double a = 0;
int LightPos[4] = { -1, 4, -1, 1 };
int MatSpec[4] = { 1, 1, 1, 1 };
void affichage()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_MODELVIEW);
glViewport(0, 0, 1000, 1000);
a = +1;
glLoadIdentity();
glRotatef(angley, 1.0, 0.0, 0.0);
glRotatef(anglex, 0.0, 1.0, 0.0);
for (int k = 0; k < 5; k++){
if (k == 0){
glBegin(GL_LINES);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(0, 0, 0);
glVertex3f(1, 0, 0);
glEnd();
}
else {
if (k > 0){
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0);
glVertex3f(0.5, 0, 0);
glVertex3f(0.5, 0, 0.5);
glEnd();
}
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0);
glVertex3f(k, 0, 0);
glVertex3f(k, 0, 0.5);
glEnd();
}
}
for (int k = 0; k < 5; k++){
if (k == 0){
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(0, 0, 0);
glVertex3f(0, 5, 0);
glEnd();
}
else {
if (k >0){
GLfloat n = k / 2;
glBegin(GL_LINES);
glColor3f(0.0, 1.0, 0);
glVertex3f(0, 0.5, 0);
glVertex3f(0.5, 0.5, 0);
glEnd();
}
glBegin(GL_LINES);
glColor3f(0.0, 1.0, 0);
glVertex3f(0, k, 0);
glVertex3f(0.5, k, 0);
glEnd();
}
}
for (int k = 0; k < 5; k++){
if (k == 0){
glBegin(GL_LINES);
glColor3f(1.0, 0.6, 1.0);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, 5);
glEnd();
}
else {
if (k >0){
GLfloat n = k / 2;
glBegin(GL_LINES);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0, 0, 0.5);
glVertex3f(0, 0.5, 0.5);
glEnd();
}
glBegin(GL_LINES);
glColor3f(0.0, 0.0, 10);
glVertex3f(0, 0, k);
glVertex3f(0, 0.5, k);
glEnd();
}
}
// Par terre scene
(ground scene)
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glVertex3d(0,-0.1,0);
glVertex3d(5, -0.1, 0);
glVertex3d(5, -0.1, 5);
glVertex3d(0, -0.1, 5);
glEnd();
glFlush();
glutSwapBuffers();
}
void clavier(unsigned char touche, int x, int y)
{
switch (touche)
{
case 'o': /* eteindre la lumiere (turn off the light) */
glDisable(GL_LIGHT0);
glutPostRedisplay();
//std::cout << "lumiere eteinte";
break;
case 'a': /* eteindre la lumiere (turn off the light) */
glEnable(GL_LIGHT0);
glutPostRedisplay();
break;
case 'p': /* affichage du carre plein (normal fill mode, interior of the polygon is filled) */
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glutPostRedisplay();
break;
case 'f': /* affichage en mode fil de fer (draw polygon edges as lines) */
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glutPostRedisplay();
break;
case 's': /* Affichage en mode sommets seuls (draw polygon vertices as points) */
glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
glutPostRedisplay();
break;
case 'd':
glEnable(GL_DEPTH_TEST);
glutPostRedisplay();
break;
case 'D':
glDisable(GL_DEPTH_TEST);
glutPostRedisplay();
break;
case 'q': /*la touche 'q' permet de quitter le programme (the key 'q' exits the program) */
exit(0);
}
}
void reshape(int x, int y)
{
if (x<y)
glViewport(0, (y - x) / 2, x, x);
else
glViewport((x - y) / 2, 0, y, y);
}
void mouse(int button, int state, int x, int y)
{
/* si on appuie sur le bouton gauche (if you press the left button) */
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
presse = 1; /* le booleen presse passe a 1 (vrai) (the boolean presse is set to 1 (true)) */
xold = x; /* on sauvegarde la position de la souris (save the position of the mouse) */
yold = y;
}
/* si on relache le bouton gauche (if you release the left button) */
if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
presse = 0; /* le booleen presse passe a 0 (faux) (the boolean presse is set to 0 (false)) */
}
void mousemotion(int x, int y)
{
if (presse) /* si le bouton gauche est presse (if the left button is pressed) */
{
/* on modifie les angles de rotation de l'objet
en fonction de la position actuelle de la souris et de la derniere
position sauvegardee (modify the rotation angles of the object
depending on the current position of the mouse and the last
saved position) */
anglex = anglex + (x - xold);
angley = angley + (y - yold);
glutPostRedisplay();
}
xold = x;
yold = y;
}
But I can't manage the scene to just draw the x,y and z axis to it's full extend. The rendered object are "cut" in the screen. How can I first draw them entirely and then move the camera backwards? I happily already managed the move the camera a bit etc.. but it's still freaking complicated...
Any advice is welcome.
You are not setting up any specific projection matrix, so you will get the default of an identity matrix. This means that the viewing volume will range from -1 to 1 along all three axis. Hence, objects are cut if they reach outside of this volume.

C++ and GLUT smooth movement

I am new at using graphics with C++ and I have the following code from this
tutorial
here is the code
#include <windows.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>
#include <math.h>
float angle = 0.0f;
float lx = 0.0f, lz = -1.0f;
float x = 0.0f, z = 5.0f;
float deltaAngle = 0.0f;
float deltaMove = 0;
void drawSnowMan(){
glColor3f(1.0f, 1.0f, 1.0f);
glTranslatef(0.0f, 0.75f, 0.0f);
glutSolidSphere(0.75f, 20, 20);
glTranslatef(0.0f, 1.0f, 0.0f);
glutSolidSphere(0.25f, 20, 20);
glPushMatrix();
glColor3f(0.0f, 0.0f, 0.0f);
glTranslatef(0.5f, 0.10f, 0.18f);
glutSolidSphere(0.05f, 10, 10);
glTranslatef(-0.1f, 0.0f, 0.0f);
glutSolidSphere(0.05f, 10, 10);
glPopMatrix();
glColor3f(1.0f, 0.5f, 0.5f);
glutSolidCone(0.8f, 0.5f, 10, 2);
}
void changeSize(int w, int h){
if(h == 0)
h = 1;
float ratio = 1.0*w/h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0,w,h);
gluPerspective(45.0f, ratio, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
}
void computePos(float deltaMove){
x += deltaMove * lx * 0.1f;
z += deltaMove * lz * 0.1f;
}
void computeDir(float deltaAngle){
angle += deltaAngle;
lx = sin(angle/75.0);
lz = -cos(angle/75.0);
}
void pressKey(int key, int xx, int yy){
switch(key){
case GLUT_KEY_LEFT: deltaAngle = -0.5f; break;
case GLUT_KEY_RIGHT: deltaAngle = 0.5; break;
case GLUT_KEY_UP: deltaMove = 0.5f; break;
case GLUT_KEY_DOWN: deltaMove = -0.5f; break;
}
}
void relaseKey(int key, int x, int y){
switch(key){
case GLUT_KEY_LEFT:
case GLUT_KEY_RIGHT: deltaAngle = 0.0f; break;
case GLUT_KEY_UP:
case GLUT_KEY_DOWN: deltaMove = 0; break;
}
}
void renderScene(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
if(deltaMove)
computePos(deltaMove);
if(deltaAngle)
computeDir(deltaAngle);
gluLookAt( x, 1.0f, z,
x+lx, 1.0f, z+lz,
0.0f, 1.0f, 0.0f
);
glColor3f(0.9f, 0.9f, 0.9f);
glBegin(GL_QUADS);
glVertex3f(-100.0f, 0.0f, -100.0f);
glVertex3f(-100.0f, 0.0f, 100.0f);
glVertex3f(100.0f, 0.0f, 100.0f);
glVertex3f(100.0f, 0.0f, -100.0f);
glEnd();
angle += 0.1f;
for(int i = -3; i < 3; i++){
for(int j = -3; j < 3; j++){
glPushMatrix();
glTranslatef(i*10.0,0, j*10.0);
drawSnowMan();
glPopMatrix();
}
}
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(320,320);
glutInitWindowPosition(100,100);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Lighthouse 3D");
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
glutIdleFunc(renderScene);
glutSpecialFunc(pressKey);
glutIgnoreKeyRepeat(1);
glutSpecialUpFunc(relaseKey);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 1;
}
My problem is the following, when i try to turn to the left and i keep the left key pressed everything works perfectly fine. When i try to hit the left once the action is not smooth at all. In some cases if the button stroke is too fast it even turns to the opposite direction!
I assume you mean keyboard instead of mouse, in which case to have a "smoother" and "slower" feel you need to adjust your deltaAngle values, which might be too high.
This is only a temporary fix, since those values may affect other machines in different ways - some may be faster, some may be slower. You need to calculate the host machine's FPS in your render loop and multiply your delta values with it, to have the same performance on every computer. This is called time-based movement and is an essential feature of every modern graphics application.
This is exactly what is happening in the following tutorial, when a dot is moving too fast on the screen.
http://lazyfoo.net/SDL_tutorials/lesson32/index.php
A glutIdleFunc() will eat all your battery/CPU/GPU, use a glutTimerFunc() instead to redraw on a predictable, sane schedule.
Accumulate your input state between frames and apply it in your glutTimerFunc() instead of trying to do it right in your keyup/keydown callbacks.
No need for a glutResizeFunc(), do all that just before your draw.
You're using C++ so you should use the C++ versions of C headers, like <cmath> instead of <math.h>.
All together:
#include <GL/glut.h>
#include <cmath>
#include <map>
std::map< int, bool > keys;
void pressKey(int key, int xx, int yy)
{
keys[ key ] = true;
}
void relaseKey(int key, int x, int y)
{
keys[ key ] = false;
}
float angle = 0.0f;
float lx = 0.0f, lz = -1.0f;
float x = 0.0f, z = 5.0f;
void update()
{
const float angleStep = 0.5f;
if( keys[ GLUT_KEY_LEFT ] ) angle -= angleStep;
if( keys[ GLUT_KEY_RIGHT ] ) angle += angleStep;
const float moveStep = 0.5f;
float move = 0.0f;
if( keys[ GLUT_KEY_DOWN ] ) move -= moveStep;
if( keys[ GLUT_KEY_UP ] ) move += moveStep;
x += move * lx * 0.1f;
z += move * lz * 0.1f;
lx = sin(angle/75.0);
lz = -cos(angle/75.0);
}
void drawSnowMan()
{
glColor3f(1.0f, 1.0f, 1.0f);
glTranslatef(0.0f, 0.75f, 0.0f);
glutSolidSphere(0.75f, 20, 20);
glTranslatef(0.0f, 1.0f, 0.0f);
glutSolidSphere(0.25f, 20, 20);
glPushMatrix();
glColor3f(0.0f, 0.0f, 0.0f);
glTranslatef(0.5f, 0.10f, 0.18f);
glutSolidSphere(0.05f, 10, 10);
glTranslatef(-0.1f, 0.0f, 0.0f);
glutSolidSphere(0.05f, 10, 10);
glPopMatrix();
glColor3f(1.0f, 0.5f, 0.5f);
glutSolidCone(0.8f, 0.5f, 10, 2);
}
void renderScene(void)
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
const double w = glutGet( GLUT_WINDOW_WIDTH );
const double h = glutGet( GLUT_WINDOW_HEIGHT );
gluPerspective(45.0f, w / h, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt
(
x, 1.0f, z,
x+lx, 1.0f, z+lz,
0.0f, 1.0f, 0.0f
);
glColor3f(0.9f, 0.9f, 0.9f);
glBegin(GL_QUADS);
glVertex3f(-100.0f, 0.0f, -100.0f);
glVertex3f(-100.0f, 0.0f, 100.0f);
glVertex3f(100.0f, 0.0f, 100.0f);
glVertex3f(100.0f, 0.0f, -100.0f);
glEnd();
for(int i = -3; i < 3; i++)
{
for(int j = -3; j < 3; j++)
{
glPushMatrix();
glTranslatef(i*10.0,0, j*10.0);
drawSnowMan();
glPopMatrix();
}
}
glutSwapBuffers();
}
void timer( int value )
{
update();
glutTimerFunc( 16, timer, 0 );
glutPostRedisplay();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(320,320);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Lighthouse 3D");
glutDisplayFunc(renderScene);
glutSpecialFunc(pressKey);
glutSpecialUpFunc(relaseKey);
glutTimerFunc( 0, timer, 0 );
glutMainLoop();
return 1;
}

`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).