Can't draw hexagonal prism when I press a key in OpenGL - c++

I tried to draw a hexagonal prism when I press a key but I have a big problem: it is drawn a random shape by default on the screen..this is my code. When I press p it is showned a pyramid, when I press c I can see a cube but by default it is drawn a random shape and I don't understand why... this is my code and the sample photo:
#include <stdlib.h>
#include <math.h>
#include "dependente\freeglut\freeglut.h"
#include "dependente\glfw\glfw3.h"
#include <stdio.h> //incluziuni librarii
#define RADDEG 57.29577951f //constanta
float XUP[3] = { 1,0,0 }, XUN[3] = { -1, 0, 0 }, //vector coordonate
YUP[3] = { 0,1,0 }, YUN[3] = { 0,-1, 0 },
ZUP[3] = { 0,0,1 }, ZUN[3] = { 0, 0,-1 },
ORG[3] = { 0,0,0 };
GLfloat viewangle = 0, tippangle = 0, traj[120][3]; //variabila pentru unghi camera
GLfloat d[3] = { 0.1, 0.1, 0.1 }; //vector directie
GLfloat xAngle = 0.0, yAngle = 0.0, zAngle = 0.0;
bool draw_pyramid = false; //variabila desenat figuri
bool draw_box = false;
bool draw_prism = false;
// Use arrow keys to rotate entire scene !!!
void Special_Keys(int key, int x, int y) //functie ptr taste sus jos stanga dreapta
{
switch (key) {
case GLUT_KEY_LEFT: viewangle -= 5; break;
case GLUT_KEY_RIGHT: viewangle += 5; break;
case GLUT_KEY_UP: tippangle -= 5; break;
case GLUT_KEY_DOWN: tippangle += 5; break;
default: printf("Special key %c == %d", key, key);
}
glutPostRedisplay();
}
void Pyramid(void) //draw the pyramid shape
{
glBegin(GL_TRIANGLE_FAN);//triangles have a common vertex, which is the central vertex
glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(0.0f, 1.0f, 0.0f); //V0(red)
glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); //V1(green)
glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(1.0f, -1.0f, 1.0f); //V2(blue)
glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(1.0f, -1.0f, -1.0f); //V3(green)
glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); //V4(blue)
glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); //V1(green)
glEnd();
}
void Draw_Box(void) //functie desenat cub
{
glBegin(GL_QUADS);//// Draw A Quad
glColor3f(0.0, 0.7, 0.1); // Front - green
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glColor3f(0.9, 1.0, 0.0); // Back - yellow
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glColor3f(0.2, 0.2, 1.0); // Top - blue
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glColor3f(0.7, 0.0, 0.1); // Bottom - red
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glEnd();
}
void hexagonalPrism()
{
glBegin(GL_QUADS);
glVertex3f(0.5, 0, 0.5);
glVertex3f(0.5, 0, -0.5);
glVertex3f(-0.5, 0, -0.5);
glVertex3f(-0.5, 0, 0.5);
glVertex3f(0.5, 0, -0.5);
glVertex3f(0.5, 1, -0.5);
glVertex3f(-0.5, 1, -0.5);
glVertex3f(-0.5, 0, -0.5);
glVertex3f(0.5, 1, -0.5);
glVertex3f(-0.5, 1, -0.5);
glVertex3f(-0.5, 0, 0.5);
glVertex3f(0.5, 0, 0.5);
glEnd();
glBegin(GL_TRIANGLES);
glVertex3f(0.5, 0, 0.5);
glVertex3f(0.5, 1, -0.5);
glVertex3f(0.5, 0, -0.5);
glVertex3f(-0.5, 0, 0.5);
glVertex3f(-0.5, 1, -0.5);
glVertex3f(-0.5, 0, -0.5);
glEnd();
}
void Keyboard(unsigned char key, int x, int y) //press a key to perform actions
{
switch (key) {
case 'd': d[0] += 0.1; break; //camera right
case 'a': d[0] -= 0.1; break; //camera left
case 'w': d[1] += 0.1; break; //camera up
case 's': d[1] -= 0.1; break; //camera down
case 'm': d[2] += 0.1; break; //magnify
case 'n': d[2] -= 0.1; break; //minify
case 'p': draw_pyramid = true; draw_box = false; break; //draw pyramid when key is pressed
case 'c': draw_box = true; draw_pyramid = false; break; //draw cube when key is pressed
case 't': draw_box = false; draw_pyramid = false; draw_prism = true; break; //draw prism when key is pressed
case 'x': xAngle += 5; break; //modify x axis angle
case 'y': yAngle += 5; break; //modify y axis angle
case 'z': zAngle += 5; break; //modify z axis angle
default: printf(" Keyboard %c == %d", key, key); //see what key it's pressed
}
glutPostRedisplay();
}
void Triad(void)
{
glColor3f(1.0, 1.0, 1.0); //set the dark grey color
glBegin(GL_LINES);
glVertex3fv(ORG); glVertex3fv(XUP);
glVertex3fv(ORG); glVertex3fv(YUP);
glVertex3fv(ORG); glVertex3fv(ZUP);
glEnd();
glRasterPos3f(1.1, 0.0, 0.0);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'X'); //draw the x axis
glRasterPos3f(0.0, 1.1, 0.0);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'Y'); //draw the y axis
glRasterPos3f(0.0, 0.0, 1.1);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'Z'); //draw the z axis
}
void redraw(void)
{
int v;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glLoadIdentity();
glTranslatef(0, 0, -3);
glRotatef(tippangle, 1, 0, 0); // Up and down arrow keys 'tip' view.
glRotatef(viewangle, 0, 1, 0); // Right/left arrow keys 'turn' view.
glDisable(GL_LIGHTING);
Triad();
glPushMatrix();
glTranslatef(d[0], d[1], d[2]); // Move box down X axis.
glScalef(0.2, 0.2, 0.2);
glRotatef(zAngle, 0, 0, 1);
glRotatef(yAngle, 0, 1, 0);
glRotatef(xAngle, 1, 0, 0);
if ( draw_pyramid )
Pyramid();
if (hexagonalPrism)
hexagonalPrism();
if ( draw_box )
Draw_Box();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize(900, 600);
glutInitWindowPosition(300, 300);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE);
glutCreateWindow("Big HW1");
glutDisplayFunc(redraw);
glutKeyboardFunc(Keyboard);
glutSpecialFunc(Special_Keys);
glClearColor(0.1, 0.0, 0.1, 1.0);
glMatrixMode(GL_PROJECTION);//specify which matrix is the current matrix, matrix that represents your camera's lens (aperture, far-field, near-field, etc).
gluPerspective(60, 1.5, 1, 10); //set up a perspective projection matrix
glMatrixMode(GL_MODELVIEW); //specify which matrix is the current matrix,matrix that represents your camera (position, pointing, and up vector).
glutMainLoop();
return 1;
}

[...] now it looks like it's a triangular prism
Of course, a hexagon has 6 sides, but in the function hexagonalPrism you only draw 3 quads for the sides and 2 triangles for the top and the bottom.
Define the 6 points for the corner points of the Hexagon:
x: 0.866, 0.0, -0.866, -0.866, 0.0, 0.866
y: 0.5, 1.0, 0.5, -0.5, -1.0, -0.5
Use the point to draw the 6 quads for the sides and the a the polygon for the top and the bottom. e.g.:
void hexagonalPrism()
{
float x[] = { 0.866f, 0.0f, -0.866f, -0.866f, 0.0f, 0.866f };
float y[] = { 0.5f, 1.0f, 0.5f, -0.5f, -1.0f, -0.5f };
glBegin(GL_QUADS);
for (int i1 = 0; i1 < 6; ++i1)
{
glColor4f(
i1 < 2 || i1 > 4 ? 1.0f : 0.0f,
i1 > 0 && i1 < 5 ? 1.0f : 0.0f,
i1 > 2 ? 1.0f : 0.0f,
1.0f
);
int i2 = (i1 + 1) % 6;
glVertex3f(x[i1], 0.0f, y[i1]);
glVertex3f(x[i2], 0.0f, y[i2]);
glVertex3f(x[i2], 1.0f, y[i2]);
glVertex3f(x[i1], 1.0f, y[i1]);
}
glEnd();
glColor4f( 1, 1, 1, 1 );
glBegin(GL_POLYGON);
for (int i = 0; i < 6; ++i)
glVertex3f(x[i], 0.0f, y[i]);
glEnd();
glBegin(GL_POLYGON);
for (int i = 0; i < 6; ++i)
glVertex3f(x[i], 1.0f, y[i]);
glEnd();
}

Maybe, the problem is here:
if (hexagonalPrism) // should be if(draw_prism)
hexagonalPrism();

Related

Is there a way to cycle options in C++/GLUT

I am making a program that will cycle the color of a triangle with the up and down arrows. I am pretty new to GLUT and C++, so I can't figure out a way to "cycle" colors.
Here is the triangle code:
void render(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt( 0.0f, 0.0f, 10.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
glRotatef(angle, 0.0f, 1.0f, 0.0f);
glColor3f(red, green, blue);
glBegin(GL_TRIANGLES);
glVertex3f(-2, 0, 0.0);
glVertex3f(2, 0, 0.0);
glVertex3f(2, 2, 0.0);
glEnd();
angle+=0.1f;
glutSwapBuffers();
}
and the input code:
void processSpecialKey(int key, int x, int y) {
switch(key) {
case GLUT_KEY_UP :
red += 1.0;
green += 0.0;
blue += 0.0; break;
case GLUT_KEY_DOWN :
red += 0.0;
green += 1.0;
blue += 0.0; break;
}
}

Can't draw shape when I press a key in OpenGL

I'm having a problem when I draw some shapes. When I execute the program I should only see the coordinates, and then after pressing a key to draw the shape, but by default it is already drawn the cube and after this the pyramid. How can I do to draw the pyramid when I press 'p' key and cube when I press 'c'? I tried something but it seems that it's not working :(
#include <stdlib.h>
#include <math.h>
#include "dependente\freeglut\freeglut.h"
#include "dependente\glfw\glfw3.h"
#include <stdio.h>
#define RADDEG 57.29577951f
float XUP[3] = { 1,0,0 }, XUN[3] = { -1, 0, 0 },
YUP[3] = { 0,1,0 }, YUN[3] = { 0,-1, 0 },
ZUP[3] = { 0,0,1 }, ZUN[3] = { 0, 0,-1 },
ORG[3] = { 0,0,0 };
GLfloat viewangle = 0, tippangle = 0, traj[120][3];
GLfloat d[3] = { 0.1, 0.1, 0.1 };
GLfloat xAngle = 0.0, yAngle = 0.0, zAngle = 0.0;
// Use arrow keys to rotate entire scene !!!
void Special_Keys(int key, int x, int y)
{
switch (key) {
case GLUT_KEY_LEFT: viewangle -= 5; break;
case GLUT_KEY_RIGHT: viewangle += 5; break;
case GLUT_KEY_UP: tippangle -= 5; break;
case GLUT_KEY_DOWN: tippangle += 5; break;
default: printf("Special key %c == %d", key, key);
}
glutPostRedisplay();
}
void Pyramid(void) //draw the pyramid shape
{
glBegin(GL_TRIANGLE_FAN);
glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(0.0f, 1.0f, 0.0f); //V0(red)
glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); //V1(green)
glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(1.0f, -1.0f, 1.0f); //V2(blue)
glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(1.0f, -1.0f, -1.0f); //V3(green)
glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); //V4(blue)
glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); //V1(green)
glEnd();
}
void Draw_Box(void)
{
glBegin(GL_QUADS);
glColor3f(0.0, 0.7, 0.1); // Front - green
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glColor3f(0.9, 1.0, 0.0); // Back - yellow
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glColor3f(0.2, 0.2, 1.0); // Top - blue
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glColor3f(0.7, 0.0, 0.1); // Bottom - red
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glEnd();
}
void Keyboard(unsigned char key, int x, int y) //press a key to perform actions
{
switch (key) {
case 'd': d[0] += 0.1; break;
case 'a': d[0] -= 0.1; break;
case 'w': d[1] += 0.1; break;
case 's': d[1] -= 0.1; break;
case 'm': d[2] += 0.1; break;
case 'n': d[2] -= 0.1; break;
case 'p': Pyramid(); break;
case 'c': Draw_Box(); break;
case 'x': xAngle += 5; break;
case 'y': yAngle += 5; break;
case 'z': zAngle += 5; break;
default: printf(" Keyboard %c == %d", key, key);
}
glutPostRedisplay();
}
void Triad(void)
{
glColor3f(1.0, 1.0, 1.0); //set the dark grey color
glBegin(GL_LINES);
glVertex3fv(ORG); glVertex3fv(XUP);
glVertex3fv(ORG); glVertex3fv(YUP);
glVertex3fv(ORG); glVertex3fv(ZUP);
glEnd();
glRasterPos3f(1.1, 0.0, 0.0);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'X'); //draw the x axis
glRasterPos3f(0.0, 1.1, 0.0);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'Y'); //draw the y axis
glRasterPos3f(0.0, 0.0, 1.1);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'Z'); //draw the z axis
}
void redraw(void)
{
int v;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glLoadIdentity();
glTranslatef(0, 0, -3);
glRotatef(tippangle, 1, 0, 0); // Up and down arrow keys 'tip' view.
glRotatef(viewangle, 0, 1, 0); // Right/left arrow keys 'turn' view.
glDisable(GL_LIGHTING);
Triad();
glPushMatrix();
glTranslatef(d[0], d[1], d[2]); // Move box down X axis.
glScalef(0.2, 0.2, 0.2);
glRotatef(zAngle, 0, 0, 1);
glRotatef(yAngle, 0, 1, 0);
glRotatef(xAngle, 1, 0, 0);
//Draw_Box(); // if i delete comment the cube it's drawn
//Pyramid(); // if i delete this the pyramid it's drawn inside the cube
glPopMatrix();
glutSwapBuffers();
}
//---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize(900, 600);
glutInitWindowPosition(300, 300);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE);
glutCreateWindow("Big HW1");
glutDisplayFunc(redraw);
glutKeyboardFunc(Keyboard);
glutSpecialFunc(Special_Keys);
glClearColor(0.1, 0.0, 0.1, 1.0);
glMatrixMode(GL_PROJECTION);
gluPerspective(60, 1.5, 1, 10);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();
return 1;
}
There are several ways to solve that. One is to create a global variable that will tell you if you want to draw the shapes or not:
bool draw_pyramid = false;
bool draw_box = false;
Then, in you keyboard handle function, do this:
case 'p': draw_pyramid = true; break;
case 'c': draw_box = true; break;
and finally, at your redraw function, do a conditional draw:
if ( draw_pyramid )
Pyramid();
if ( draw_box )
Draw_Box();

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

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

OpenGL - Why are my objects transparent?

I am trying to make a motorcycle with primitive shapes. For some reason, the shapes that I have made are see-through. I am not specifying any alpha anywhere; here is my code:
#include <GL/glut.h>
#include <math.h>
GLUquadricObj *quadratic;
static int isWire = 0; // Is wireframe?
static int distance = 10;
static float angleH = 0;
static float angleV = 0;
static float R = 2.0; // Radius of hemisphere.
static int p = 4; // Number of longitudinal slices.
static int q = 6; // Number of latitudinal slices.
#define PI 3.14159265358979324
static unsigned int pipe, seat, cover, wheel, wheelCenter, cycles; // parts of the motorcycle to make as display lists.
GLUquadricObj *cylinder;
void drawCoordinates();
void drawMotorcycle();
void drawTrailer();
void drawHemisphere();
void drawCylinder(float x, float y, float z);
void drawHandle(float x, float y, float z);
void drawLight();
void drawBase();
void setup();
void display () {
/* clear window */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(distance*cos(angleH), distance*cos(angleV), distance*sin(angleH), 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
/* future matrix manipulations should affect the modelview matrix */
glMatrixMode(GL_MODELVIEW);
if (isWire) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glPushMatrix();
drawCoordinates();
glPushMatrix();
glTranslatef(0.0, 0.0, 0.0); // Move the motorcycle around the world space
drawMotorcycle();
drawTrailer();
glPopMatrix();
glPopMatrix();
/* flush drawing routines to the window */
glFlush();
}
void drawCoordinates()
{
/***************** DRAW AXIS *****************/
glPushMatrix();
GLUquadricObj *xAxis;
xAxis=gluNewQuadric();
glColor3f(1,0,0);
glRotatef(-90, 0, 1, 0);
gluCylinder(xAxis,0.05,0.05,1,5,5);
glPopMatrix();
glPushMatrix();
GLUquadricObj *yAxis;
yAxis=gluNewQuadric();
glColor3f(0,1,0);
glRotatef(-90, 1, 0, 0);
gluCylinder(yAxis,0.05,0.05,1,5,5);
glPopMatrix();
glPushMatrix();
GLUquadricObj *zAxis;
zAxis=gluNewQuadric();
glColor3f(0,0,1);
gluCylinder(zAxis,0.05,0.05,1,5,5);
glPopMatrix();
/***************** END OF DRAW AXIS *****************/
}
void drawMotorcycle()
{
//DRAW ENGINE
glPushMatrix();
//drawCoordinates();
glColor3f(.6, 0, 0);
glScalef(1.4, 0.8, 1.0);
glutSolidSphere(1,8,8);
glPushMatrix();
//drawCoordinates();
glPopMatrix();
glPopMatrix();
//DRAW PIPES UNDER ENGINE
glPushMatrix();
glRotatef(-90, 1, 0, 0);
glRotatef(80, 0, 1, 0);
glTranslatef(0.5, 1.0, -1.5);
glCallList(pipe);
glTranslatef(0.0, -2.0, 0.0);
glCallList(pipe);
glPopMatrix();
//DRAW SEAT
glPushMatrix();
glPushMatrix();
glRotatef(15, 0, 0, 1);
glTranslatef(-2.0, -0.4, 0.0);
glScalef(2.0, 0.2, 1.2);
glCallList(seat);
glPopMatrix();
//DRAW BACK SEAT
glRotatef(-40, 0, 0, 1);
glTranslatef(-2.3, -2.8, 0.0);
glScalef(2.0, 0.2, 1.2);
glCallList(seat);
glPopMatrix();
//DRAW FRONT PLATE
glPushMatrix();
glRotatef(120, 0, 0, 1);
glTranslatef(0.8, -1.3, 0.0);
glScalef(2.0, 0.2, 1.35);
glColor3f(0.5,0.0,0.0);
glutSolidCube(1);
glPushMatrix();
//drawCoordinates();
glPopMatrix();
glPopMatrix();
//DRAW FRONT PIPES
glPushMatrix();
glRotatef(-90, 1, 0, 0);
glRotatef(-30, 0, 1, 0);
glTranslatef(1.3, -0.9, -5.7);
glScalef(1.0, 1.0, 2.5);
glCallList(pipe);
glTranslatef(0.0, 1.7, 0.0);
glCallList(pipe);
glPopMatrix();
//DRAW WHEEL COVERS
glPushMatrix();
glTranslatef(3.5, -3.0, 0.0);
glScalef(1.0, 0.5, 1.0);
glRotatef(45, 0, 0, 1);
glCallList(cover);
glTranslatef(-5.5, 0.0, 0.0);
glRotatef(-100, 0, 0, 1);
glTranslatef(-8.5, 0.2, 0.0);
glCallList(cover);
glPopMatrix();
//DRAW WHEELS
glPushMatrix();
glTranslatef(3.9, -4.1, 0.0);
glCallList(wheel);
glTranslatef(-9.2, 2.0, 0.0);
glCallList(wheel);
glPopMatrix();
//DRAW WHEEL CENTER PIECES
glPushMatrix();
glTranslatef(3.9, -4.1, 0.0);
glCallList(wheelCenter);
glTranslatef(-9.2, 2.0, 0.0);
glCallList(wheelCenter);
glPopMatrix();
//DRAW CYCLES AROUND WHEELS
glPushMatrix();
glTranslatef(3.9, -4.1, 0.0);
glRotatef(-90, 1, 0, 0);
for(int i=0; i<8; i++)
{
glRotatef(45, 0, 1, 0);
glPushMatrix();
glCallList(cycles);
glPopMatrix();
}
glTranslatef(-9.2, 0.0, 2.0);
for(int i=0; i<8; i++)
{
glRotatef(45, 0, 1, 0);
glPushMatrix();
glCallList(cycles);
glPopMatrix();
}
glPopMatrix();
//DRAW HANDLE BARS
glPushMatrix();
glTranslatef(0.2, 2.0, 0.0);
glRotatef(-45, 1, 0, 0);
glScalef(0.7, 0.7, 0.7);
glCallList(pipe);
glRotatef(-90, 1, 0, 0);
glCallList(pipe);
glPopMatrix();
//DRAW LIGHT
glPushMatrix();
glTranslatef(1.0, 1.0, 0.0);
glColor3f(0.5, 0.5, 0.0);
//glScalef(1.0, 0.5, 1.0);
glutSolidSphere(0.5, 5, 5);
glPushMatrix();
//drawCoordinates();
glPopMatrix();
glPopMatrix();
//DRAW BASE
glPushMatrix();
glRotatef(10.0, 0.0, 0.0, 1.0);
glScalef(3.5, 1.5, 1.0);
glTranslatef(-0.4, -1.0, 0.0);
glColor3f(0.3, 0.3, 0.3);
glutSolidCube(1);
glPopMatrix();
//GAS TANK
glPushMatrix();
glScalef(2.5, 1.0, 0.8);
glTranslatef(-0.8, -1.7, -1.4);
glCallList(pipe);
glPopMatrix();
}
void drawTrailer()
{
//DRAW BODY
glPushMatrix();
glColor3f(0.0, 0.0, 0.3);
glScalef(2.0, 2.5, 1.5);
glTranslatef(-4.5, -0.5, 0.0);
glutSolidCube(1);
glPopMatrix();
//DRAW WHEELS
glPushMatrix();
glPushMatrix();
glScalef(0.8, 0.8, 0.8);
glTranslatef(-12.0, -1.5, 2.0);
glCallList(wheel);
glCallList(wheelCenter);
glRotatef(90, 1, 0, 0);
for(int i=0; i<8; i++)
{
glRotatef(45, 0, 1, 0);
glPushMatrix();
glCallList(cycles);
glPopMatrix();
}
glPopMatrix();
glPushMatrix();
glScalef(0.8, 0.8, 0.8);
glTranslatef(-12.0, -1.5, -2.0);
glCallList(wheel);
glCallList(wheelCenter);
glRotatef(90, 1, 0, 0);
for(int i=0; i<8; i++)
{
glRotatef(45, 0, 1, 0);
glPushMatrix();
glCallList(cycles);
glPopMatrix();
}
glPopMatrix();
glPopMatrix();
//DRAW CONNECTION TO MOTORCYCLE
glPushMatrix();
glRotatef(90, 0, 1, 0);
glTranslatef(0.0, -1.0, -8.0);
glCallList(pipe);
glPopMatrix();
}
void drawHemisphere()
{
for(int j = 0; j < q; j++)
{
// One latitudinal triangle strip.
glBegin(GL_TRIANGLE_STRIP);
for(int i = 0; i <= p; i++)
{
glVertex3f( R * cos( (float)(j+1)/q * PI/2.0 ) * cos( 2.0 * (float)i/p * PI ),
R * sin( (float)(j+1)/q * PI/2.0 ),
R * cos( (float)(j+1)/q * PI/2.0 ) * sin( 2.0 * (float)i/p * PI ) );
glVertex3f( R * cos( (float)j/q * PI/2.0 ) * cos( 2.0 * (float)i/p * PI ),
R * sin( (float)j/q * PI/2.0 ),
R * cos( (float)j/q * PI/2.0 ) * sin( 2.0 * (float)i/p * PI ) );
}
glEnd();
}
}
void reshape (int w, int h)
{
// (Window of width = zero is not possible).
if(h == 0)
h = 1;
float ratio = 1.0* w / h;
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Set the correct perspective.
gluPerspective(90,ratio,-1,1);
}
// Keyboard input processing routine.
void keyInput(unsigned char key, int x, int y)
{
switch(key)
{
case 'c' : distance = 10; angleH=0; angleV=0.0; break;
case 'C' : distance = 10; angleH=0; angleV=0.0; break;
case 'f': distance = (distance == 4)? 4:distance-1; break;
case 'F': distance = (distance == 4)? 4:distance-1; break;
case 'b': distance = (distance == 20)? 20:distance+1; break;
case 'B': distance = (distance == 20)? 20:distance+1; break;
case 'w': if (isWire == 0) isWire = 1; else isWire = 0; break;
case 'W': if (isWire == 0) isWire = 1; else isWire = 0; break;
//case 27: exit(0); break;
default: break;
}
}
void specialKeyboard(int key, int x, int y) {
switch (key)
{
case GLUT_KEY_RIGHT:
angleH -= .2;
break;
case GLUT_KEY_LEFT:
angleH += .2;
break;
case GLUT_KEY_UP:
angleV += .2;
break;
case GLUT_KEY_DOWN:
angleV -= .2;
break;
}
}
void update(void){
glutPostRedisplay();
}
void setup()
{
// PARTS
pipe = glGenLists(1);
seat = glGenLists(1);
cover = glGenLists(1);
wheel = glGenLists(1);
wheelCenter = glGenLists(1);
cycles = glGenLists(1);
glNewList(pipe, GL_COMPILE); // Any cylinder on the motorcycle
GLUquadricObj *cylinder;
cylinder=gluNewQuadric();
glPushMatrix();
glColor3f(.5,.5,.5);
gluCylinder(cylinder,0.2,0.2,3,5,5);
glPushMatrix();
//drawCoordinates();
glPopMatrix();
glPopMatrix();
glEndList();
glNewList(seat, GL_COMPILE);
glPushMatrix();
glColor3f(0.5, 0.35, 0.05);
glutSolidCube(1);
glPushMatrix();
//drawCoordinates();
glPopMatrix();
glPopMatrix();
glEndList();
glNewList(cover, GL_COMPILE);
glPushMatrix();
glColor3f(0.5, 0.0, 0.0);
drawHemisphere();
glPushMatrix();
//drawCoordinates();
glPopMatrix();
glPopMatrix();
glEndList();
glNewList(wheel, GL_COMPILE);
glPushMatrix();
glColor3f(0.1, 0.1, 0.1);
glutSolidTorus(0.2, 1.2, 20, 20);
glPushMatrix();
//drawCoordinates();
glPopMatrix();
glPopMatrix();
glEndList();
glNewList(wheelCenter, GL_COMPILE);
glPushMatrix();
glColor3f(0.4, 0.5, 0.5);
glScalef(1.0, 0.5, 1.0);
glutSolidSphere(0.8, 5, 5);
glPushMatrix();
//drawCoordinates();
glPopMatrix();
glPopMatrix();
glEndList();
glNewList(cycles, GL_COMPILE);
glColor3f(0.5, 0.5, 0.5);
glScalef(0.25, 0.25, 0.25);
cylinder=gluNewQuadric();
gluCylinder(cylinder,0.5,0.5,5,15,5);
glEndList();
}
int main (int argc, char** argv) {
/* initialize GLUT, using any commandline parameters passed to the
program */
glutInit(&argc,argv);
/* setup the size, position, and display mode for new windows */
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH );
/* create and set up a window */
glutCreateWindow("Motorcycle");
setup(); // Build all the display lists, ready to use
glutIdleFunc(update);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyInput);
glutSpecialFunc(specialKeyboard);
/* set up depth-buffering */
glEnable(GL_DEPTH_TEST);
/* background color */
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
/* tell GLUT to wait for events */
glutMainLoop();
return 0;
}
You can rotate the camera with the arrow keys to see that the objects are see through. How can I fix this? Is there anything else I can do to improve my code?
In reshape():
// Set the correct perspective.
gluPerspective(90,ratio,-1,1);
I'm guessing you transliterated parameters from a glOrtho() call, where a negative zNear is perfectly legitimate.
From the gluPerspective() docs:
zNear: Specifies the distance from the viewer to the near clipping plane (always positive).
Try this:
gluPerspective(90,ratio,1,100);
You need to add glEnable(GL_DEPTH_TEST); to your initialization function.