OpenGL/GLUT Simple issue in making ride to be smooth - c++

I am almost done with the program that I am asked to do but I am stuck in a very simple logical issue and that is:
Increasing the speed of ride smoothly by left click of mouse till we reach a max speed or right click of mouse got hit.
Smoothly decrease the speed of ride by right click of mouse till we reach a zero speed or left click of mouse got hit.
I am using signal and speed int variables to achieve my goal but I have 2 problems:
If I use Sleep() function to slow down the finite while loop the program simply freezes and nothing works. If I don't use the Sleep() function simply the movement becomes instant jump and there is no sense of anything is moving at all.
In addition the right and left click of the mouse only works for 2 times and simply they don't work after that.
Screenshot of the program
Any hint would be great.
My Switch statement for mouse:
switch (button) {
case GLUT_LEFT_BUTTON:
signal = 0;
smothIncrease();
break;
case GLUT_MIDDLE_BUTTON:
case GLUT_RIGHT_BUTTON:
signal = 1;
smothDecrease();
break;
default:
break;
}
Helper functions:
void smothIncrease(){
while (true){
if (signal == 0){
if (speed == 15)
break;
angle++;
speed++;
Sleep(15);
glutPostRedisplay();
}
else if (signal == 1)
break;
}
}
void smothDecrease(){
while (true){
if (signal == 1){
if (speed == 0)
break;
angle--;
speed--;
Sleep(15);
glutPostRedisplay();
}
else if (signal == 0)
break;
}
}
Here is the complete source code:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define PI 3.14159265
static GLfloat lpos[] = { 0.0, 5.0, 4.0, 1.0 };
static GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 };
static GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
static GLfloat red[] = { 1.0, 0.0, 0.0, 1.0 };
static GLfloat lightgreen[] = { 0.5, 1.0, 0.5, 1.0 };
static float alpha = 0.0;
static float beta = PI / 6.0;
static float zoom = 25.0;
static bool lightSource = true;
float numberOfTriangles = 1;
static GLdouble cpos[3];
static double fenceHeight = -0.5;
static int angle = 0;
static int angle__IN_RANGE = 0.0;
static double radian__IN_RANGE = 0.0;
static int arrayOfAnglesInRange[181];
static int id = 0;
static int speed = 0;
static int signal = 0;
void writemessage()
{
}
void processAngle(){
angle__IN_RANGE = arrayOfAnglesInRange[abs(angle) % 181];
}
void setRadian_IN_RANGE(){
radian__IN_RANGE = ((float)angle__IN_RANGE / 180) * PI;
}
void fillArray(){
int j = -45;
for (int i = 0; i < 181; i++)
{
if (i < 90)
arrayOfAnglesInRange[i] = j++;
else
arrayOfAnglesInRange[i] = j--;
}
//for (int i = 0; i < 182; i++)
//{
// printf("%d\n", arrayOfAnglesInRange[i]);
//}
}
void keepTrackOfID(){
int tempAngle = angle;
//if (id % 4 == 0)
// angle += 0;
//else if (id % 4 == 1)
// angle += 60;
//else if (id % 4 == 2)
// angle += 120;
//else if (id % 4 == 3)
// angle += 180;
//if (id % 4 == 0)
// angle += 0;
//else if (id % 4 == 1)
// angle += 45;
//else if (id % 4 == 2)
// angle += 90;
//else if (id % 4 == 3)
// angle += 135;
if (id % 4 == 0)
angle += 0;
else if (id % 4 == 1)
angle += 30;
else if (id % 4 == 2)
angle += 60;
else if (id % 4 == 3)
angle += 90;
processAngle();
setRadian_IN_RANGE();
angle = tempAngle;
}
void smothIncrease(){
while (true){
if (signal == 0){
if (speed == 15)
break;
angle++;
speed++;
Sleep(15);
glutPostRedisplay();
}
else if (signal == 1)
break;
}
}
void smothDecrease(){
while (true){
if (signal == 1){
if (speed == 0)
break;
angle--;
speed--;
Sleep(15);
glutPostRedisplay();
}
else if (signal == 0)
break;
}
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat)w / (GLfloat)h, 0.01, 50.0);
glMatrixMode(GL_MODELVIEW);
}
void DrawSticksArroundYard(){
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
glMaterialfv(GL_BACK, GL_AMBIENT_AND_DIFFUSE, black);
GLUquadricObj *quadObj;
// Right-Line
glPushMatrix();
glTranslatef(6.8, 1.0 + fenceHeight, -7.0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.1, 0.1, 14.0, 10, 10);
glPopMatrix();
// Left-Line
glPushMatrix();
glTranslatef(-6.8, 1.0 + fenceHeight, -7.0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.1, 0.1, 14.0, 10, 10);
glPopMatrix();
// Back-Line
glPushMatrix();
glTranslatef(-6.8, 1.0 + fenceHeight, -7.0);
glRotatef(90, 0, 1, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.1, 0.1, 13.7, 10, 10);
glRotatef(-90, 0, 1, 0);
glPopMatrix();
// Front-Line
glPushMatrix();
glTranslatef(6.8, 1.0 + fenceHeight, 7.0);
glRotatef(-90, 0, 1, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.1, 0.1, 13.7, 10, 10);
glRotatef(90, 0, 1, 0);
glPopMatrix();
// Pin-Front-Right
glPushMatrix();
glTranslatef(6.8, 0, 7.0);
glRotatef(-90, 1, 0, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
glRotatef(90, 1, 0, 0);
glPopMatrix();
// Pin-Front-Left
glPushMatrix();
glTranslatef(-6.8, 0, 7.0);
glRotatef(-90, 1, 0, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
glRotatef(90, 1, 0, 0);
glPopMatrix();
// Pin-Back-Left
glPushMatrix();
glTranslatef(-6.8, 0, -7.0);
glRotatef(-90, 1, 0, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
glRotatef(90, 1, 0, 0);
glPopMatrix();
// Pin-Back-Right
glPushMatrix();
glTranslatef(6.8, 0, -7.0);
glRotatef(-90, 1, 0, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
glRotatef(90, 1, 0, 0);
glPopMatrix();
// Pin-Back-Center
glPushMatrix();
glTranslatef(0, 0, -7.0);
glRotatef(-90, 1, 0, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
glRotatef(90, 1, 0, 0);
glPopMatrix();
// Pin-Front-Center
glPushMatrix();
glTranslatef(0, 0, 7.0);
glRotatef(-90, 1, 0, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
glRotatef(90, 1, 0, 0);
glPopMatrix();
// Pin-Right-Center
glPushMatrix();
glTranslatef(6.8, 0, 0);
glRotatef(-90, 1, 0, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
glRotatef(90, 1, 0, 0);
glPopMatrix();
// Pin-Left-Center
glPushMatrix();
glTranslatef(-6.8, 0, 0);
glRotatef(-90, 1, 0, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
glRotatef(90, 1, 0, 0);
glPopMatrix();
}
void DrawYardFloor(){
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, lightgreen);
glMaterialfv(GL_BACK, GL_AMBIENT_AND_DIFFUSE, lightgreen);
glBegin(GL_POLYGON);
glNormal3f(0, 1, 0);
glVertex3f(-7.3, -0.005, -7.3);
glVertex3f(-7.3, -0.005, 7.3);
glVertex3f(7.3, -0.005, 7.3);
glVertex3f(7.3, -0.005, -7.3);
glEnd();
}
void DrawCenterPin(){
glRotatef(-90, 1, 0, 0);
GLUquadricObj *quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.2, 7, 10, 10);
glRotatef(90, 1, 0, 0);
}
void DrawBase(){
glRotatef(-90, 1, 0, 0);
GLUquadricObj *quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.5, 0.1, 2, 10, 10);
glRotatef(90, 1, 0, 0);
}
void DrawTop(){
glPushMatrix();
glTranslatef(0, 7, 0);
glRotatef(-90, 1, 0, 0);
GLUquadricObj *quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.0, 0.5, 10, 10);
glRotatef(90, 1, 0, 0);
glPopMatrix();
}
void DrawHorizontalStick(){
glLineWidth(15);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex3f(0.0, 7.0, 0.0);
glVertex3f(4.0 * cos(radian__IN_RANGE), 7.0 + 3.0 * sin(radian__IN_RANGE), 0.0);
glEnd();
}
void DrawVerticalStick(){
glLineWidth(5);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex3f(4.0 * cos(radian__IN_RANGE), 7.0 + 3.0 * sin(radian__IN_RANGE), 0.0);
glVertex3f(4.0 * cos(radian__IN_RANGE), 7.0 + 3.0 * sin(radian__IN_RANGE) - 1, 0.0);
glEnd();
}
void DrawCabin(){
// Back
glNormal3f(0.0, 0.0, -1.0);
glBegin(GL_POLYGON);
glVertex3f(0, 0, -1);
glVertex3f(0, 1, -1);
glVertex3f(2, 1, -1);
glVertex3f(2, 0, -1);
glEnd();
glNormal3f(0.0, 0.0, -1.0);
glBegin(GL_POLYGON);
glVertex3f(0, 1.7, -1);
glVertex3f(0, 2, -1);
glVertex3f(2, 2, -1);
glVertex3f(2, 1.7, -1);
glEnd();
glNormal3f(0.0, 0.0, -1.0);
glBegin(GL_POLYGON);
glVertex3f(0, 1, -1);
glVertex3f(0, 1.7, -1);
glVertex3f(0.2, 1.7, -1);
glVertex3f(0.2, 1, -1);
glEnd();
glNormal3f(0.0, 0.0, -1.0);
glBegin(GL_POLYGON);
glVertex3f(1.8, 1, -1);
glVertex3f(1.8, 1.7, -1);
glVertex3f(2, 1.7, -1);
glVertex3f(2, 1, -1);
glEnd();
// Front
glNormal3f(0.0, 0.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(2, 0, 1);
glVertex3f(2, 1, 1);
glVertex3f(0, 1, 1);
glVertex3f(0, 0, 1);
glEnd();
glNormal3f(0.0, 0.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(2, 1.7, 1);
glVertex3f(2, 2, 1);
glVertex3f(0, 2, 1);
glVertex3f(0, 1.7, 1);
glEnd();
glNormal3f(0.0, 0.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(0.2, 1, 1);
glVertex3f(0.2, 1.7, 1);
glVertex3f(0, 1.7, 1);
glVertex3f(0, 1, 1);
glEnd();
glNormal3f(0.0, 0.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(2, 1, 1);
glVertex3f(2, 1.7, 1);
glVertex3f(1.8, 1.7, 1);
glVertex3f(1.8, 1, 1);
glEnd();
// Floor
glNormal3f(0.0, -1.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(2, 0, -1);
glVertex3f(2, 0, 1);
glVertex3f(0, 0, 1);
glVertex3f(0, 0, -1);
glEnd();
// Top
glNormal3f(0.0, 1.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(2, 2, 1);
glVertex3f(2, 2, -1);
glVertex3f(0, 2, -1);
glVertex3f(0, 2, 1);
glEnd();
// Right
glNormal3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(2, 0, -1);
glVertex3f(2, 1, -1);
glVertex3f(2, 1, 1);
glVertex3f(2, 0, 1);
glEnd();
glNormal3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(2, 1.7, -1);
glVertex3f(2, 2, -1);
glVertex3f(2, 2, 1);
glVertex3f(2, 1.7, 1);
glEnd();
glNormal3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(2, 1, -1);
glVertex3f(2, 1.7, -1);
glVertex3f(2, 1.7, -0.8);
glVertex3f(2, 1, -0.8);
glEnd();
glNormal3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(2, 1, 0.8);
glVertex3f(2, 1.7, 0.8);
glVertex3f(2, 1.7, 1);
glVertex3f(2, 1, 1);
glEnd();
// Left
glNormal3f(-1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(0, 0, -1);
glVertex3f(0, 0, 1);
glVertex3f(0, 1, 1);
glVertex3f(0, 1, -1);
glEnd();
glNormal3f(-1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(0, 1.7, -1);
glVertex3f(0, 1.7, 1);
glVertex3f(0, 2, 1);
glVertex3f(0, 2, -1);
glEnd();
glNormal3f(-1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(0, 1, -1);
glVertex3f(0, 1, -0.8);
glVertex3f(0, 1.7, -0.8);
glVertex3f(0, 1.7, -1);
glEnd();
glNormal3f(-1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(0, 1, 0.8);
glVertex3f(0, 1, 1);
glVertex3f(0, 1.7, 1);
glVertex3f(0, 1.7, 0.8);
glEnd();
}
void darwCabin__FINAL(){
glPushMatrix();
glTranslatef(4.0 * cos(radian__IN_RANGE), 7.0 + 3.0 * sin(radian__IN_RANGE) - 3, 0.0);
glRotatef(angle, 0, 1, 0);
glPushMatrix();
glTranslatef(-1, 0, 0);
DrawCabin();
glPopMatrix();
glRotatef(-angle, 0, 1, 0);
glPopMatrix();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 64);
cpos[0] = zoom * cos(beta) * sin(alpha);
cpos[1] = zoom * sin(beta);
cpos[2] = zoom * cos(beta) * cos(alpha);
gluLookAt(cpos[0], cpos[1], cpos[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
if (lightSource == true){
glLightfv(GL_LIGHT0, GL_POSITION, lpos);
glMaterialfv(GL_FRONT, GL_EMISSION, white);
glPushMatrix();
glTranslatef(lpos[0], lpos[1], lpos[2]);
glutSolidSphere(0.1, 10, 8);
glPopMatrix();
glMaterialfv(GL_FRONT, GL_EMISSION, black);
}
DrawYardFloor();
DrawSticksArroundYard();
DrawCenterPin();
DrawBase();
DrawTop();
glRotatef(angle, 0, 1, 0);
for (int i = 0; i < 4; i++){
glPushMatrix();
glRotatef(i * 360 / 4, 0, 1, 0);
keepTrackOfID();
DrawHorizontalStick();
DrawVerticalStick();
darwCabin__FINAL();
id++;
glPopMatrix();
}
glRotatef(-angle, 0, 1, 0);
glutSwapBuffers();
glFlush();
}
void keyboard(unsigned char key, int x, int y)
{
static int polygonmode[2];
switch (key) {
case 27:
exit(0);
break;
case 'x':
if (lightSource == true)
lpos[0] = lpos[0] + 0.2;
glutPostRedisplay();
break;
case 'X':
if (lightSource == true)
lpos[0] = lpos[0] - 0.2;
glutPostRedisplay();
break;
case 'y':
if (lightSource == true)
lpos[1] = lpos[1] + 0.2;
glutPostRedisplay();
break;
case 'Y':
if (lightSource == true)
lpos[1] = lpos[1] - 0.2;
glutPostRedisplay();
break;
case 'z':
if (lightSource == true)
lpos[2] = lpos[2] + 0.2;
glutPostRedisplay();
break;
case 'Z':
if (lightSource == true)
lpos[2] = lpos[2] - 0.2;
glutPostRedisplay();
break;
case '+':
if (zoom != 1.5)zoom = zoom - 0.5;
glutPostRedisplay();
break;
case '-':
if (zoom != 30)zoom = zoom + 0.5;
glutPostRedisplay();
break;
case '0':
if (lightSource == true){
glDisable(GL_LIGHT0);
lightSource = false;
}
else{
glEnable(GL_LIGHT0);
lightSource = true;
}
glutPostRedisplay();
break;
case 'e':
if (fenceHeight < 2)
fenceHeight += 0.5;
glutPostRedisplay();
break;
case 'd':
if (fenceHeight > -0.5)
fenceHeight -= 0.5;
glutPostRedisplay();
break;
case 'w':
glGetIntegerv(GL_POLYGON_MODE, polygonmode);
if (polygonmode[0] == GL_FILL)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glutPostRedisplay();
break;
case 'n':
angle++;
processAngle();
setRadian_IN_RANGE();
glutPostRedisplay();
break;
case 'm':
angle--;
processAngle();
setRadian_IN_RANGE();
glutPostRedisplay();
break;
default:
break;
}
}
void mouse(int button, int state, int x, int y)
{
switch (button) {
case GLUT_LEFT_BUTTON:
signal = 0;
smothIncrease();
break;
case GLUT_MIDDLE_BUTTON:
case GLUT_RIGHT_BUTTON:
signal = 1;
smothDecrease();
break;
default:
break;
}
}
void specialkey(GLint key, int x, int y)
{
switch (key) {
case GLUT_KEY_RIGHT:
alpha = alpha + PI / 180;
if (alpha > 2 * PI) alpha = alpha - 2 * PI;
glutPostRedisplay();
break;
case GLUT_KEY_LEFT:
alpha = alpha - PI / 180;
if (alpha < 0) alpha = alpha + 2 * PI;
glutPostRedisplay();
break;
case GLUT_KEY_UP:
if (beta < 0.45*PI) beta = beta + PI / 180;
glutPostRedisplay();
break;
case GLUT_KEY_DOWN:
if (beta > -0.05*PI) beta = beta - PI / 180;
glutPostRedisplay();
break;
default:
break;
}
}
int main(int argc, char** argv)
{
writemessage();
fillArray();
processAngle();
setRadian_IN_RANGE();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(1200, 800);
glutInitWindowPosition(0, 0);
glutCreateWindow(argv[0]);
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
/* initially GL_FILL mode (default), later GL_LINE to show wireframe */
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_LIGHTING);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
glEnable(GL_LIGHT0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 5.0, 10.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutSpecialFunc(specialkey);
glutMainLoop();
return 0;
}

There are several problems with your code, I'll explain as I spot them.
First thing is that you're using int data type for all your variables (such as speed and angle), and you won't get anything smooth using integer, so you should change all those ints to float.
glutPostRedisplay will only mark your frame to be rendered again. In other words, the function will return immediately and nothing would happen until the next render frame event. The Sleep call makes your process freeze (it will interrupt any processing in your program including the glut render loop). There's no need to put an while loop on this function since you cannot control the render loop this way.
You should have a new variable to control the speed change, and in your display function you should change the speed of the movement. Your new smothIncrease may look like this:
float speedChange = 0.0f;
void smothIncrease(){
speedChange += 1.0f; // you may want to have other increment factor
glutPostRedisplay();
}
And in your display function you should change the speed by the factor you just incremented:
if (speed < 15.0f)
speed += speedChange;
But this way how do we redraw the frame when there's no event happening (mouse click, key press)? The best approach I know is to set an idle function and on this idle function just call glutPostRedisplay. This will guarantee your window is always being updated.
Take a look on those links
glutPostRedisplay reference
glutIdleFunc reference

Related

Texture applied through OpenGL isn't visible when rendering + add image directly in question

I tried to apply a checkerboard styled texture to the model below here using GLubyte and glTexImage2D along with glTexCoord2f.
I previously applied a material to it that works perfectly, but for some reasons my texture won't show up at all. I can't seem to see or figure out why because looking at the examples I'm following everything should be working perfectly.
What I should have
#include <glut.h>
float angle[4];
float LightAngle;
bool LowerFrontLegDown = true;
bool LowerBackLegDown = true;
GLfloat corners[8][3] = { {-0.5,0.5,-0.5},{0.5,0.5,-0.5},
{0.5,-0.5,-0.5},{-0.5,-0.5,-0.5},
{-0.5,0.5,0.5},{0.5,0.5,0.5},
{0.5,-0.5,0.5},{-0.5,-0.5,0.5} };
//Two Dimensional Array for corners
GLfloat normals[][3] = { {0.0,0.0,1.0},
{1.0,0.0,0.0},
{0.0,-1.0,0.0},
{0.0,1.0,0.0},
{0.0,0.0,-1.0},
{-1.0,0.0,0.0} };
typedef struct materialStruct {
GLfloat ambient[4];
GLfloat diffuse[4];
GLfloat specular[4];
GLfloat shininess;
};
materialStruct brassMaterial = {
{ 0.33, 0.22, 0.03, 1.00 },
{ 0.78, 0.57, 0.11, 1.00 },
{ 0.99, 0.91, 0.81, 1.00 },
27.80 };
materialStruct redPlasticMaterial = {
{ 0.30, 0.00, 0.00, 1.00 },
{ 0.60, 0.00, 0.00, 1.00 },
{ 0.80, 0.60, 0.60, 1.00 },
32.00 };
materialStruct* currentMaterial;
void setMaterial(materialStruct* materials) {
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, materials->ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, materials->diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, materials->specular);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, materials->shininess);
}
void drawFace(int a, int b, int c, int d) {
glBegin(GL_POLYGON);
glTexCoord2f(0.0, 0.0);
glVertex3fv(corners[a]);
glTexCoord2f(0.0, 1.0);
glVertex3fv(corners[b]);
glTexCoord2f(1.0, 1.0);
glVertex3fv(corners[c]);
glTexCoord2f(1.0, 0.0);
glVertex3fv(corners[d]);
glEnd();
} //Turns the corners from the two dimensional array into corner pieces for the model, allowing faces to be drawn
void ArrayCube() {
glNormal3fv(normals[0]);
drawFace(0, 3, 2, 1);
glNormal3fv(normals[1]);
drawFace(3, 0, 4, 7);
glNormal3fv(normals[2]);
drawFace(2, 3, 7, 6);
glNormal3fv(normals[3]);
drawFace(1, 2, 6, 5);
glNormal3fv(normals[4]);
drawFace(4, 5, 6, 7);
glNormal3fv(normals[5]);
drawFace(5, 4, 0, 1);
}
//Draws the faces of the model and creates a cube we can call later for the individual parts of the model.
void rotate() {
angle[0] += 1.0;
if (angle[0] > 360) angle[0] -= 360;
if (LowerFrontLegDown) angle[1] -= 0.2;
else angle[1] += 0.2;
if (angle[1] < 315) LowerFrontLegDown = false;
if (angle[1] > 360) LowerFrontLegDown = true;
angle[0] += 1.0;
if (angle[0] > 360) angle[0] -= 360;
if (LowerBackLegDown) angle[1] -= 0.2;
else angle[1] += 0.2;
if (angle[1] < 315) LowerBackLegDown = false;
if (angle[1] > 360) LowerBackLegDown = true;
glutPostRedisplay();
}
void MainBody()
{
glPushMatrix();
glScalef(1.25, 0.25, 0.5);
ArrayCube();
glPopMatrix();
}
void LowerNeck()
{
glPushMatrix();
glTranslatef(0.5, 0.25, 0);
glScalef(0.15, 0.5, 0.15);
ArrayCube();
glPopMatrix();
}
void UpperNeck()
{
glPushMatrix();
glTranslatef(0.5, 0.75, 0);
glScalef(0.15, 0.5, 0.15);
ArrayCube();
glPopMatrix();
}
void Head()
{
glPushMatrix();
glRotatef(90, 0.0, 0.0,1.0);
glTranslatef(1, -0.6, 0);
glScalef(0.1, 0.4, 0.15);
ArrayCube();
glPopMatrix();
}
void RightHorn()
{
glPushMatrix();
glRotatef(0, 0.0, 0.0,1);
glTranslatef(0.5, 1.15, 0.035);
glScalef(0.05, 0.15, 0.05);
ArrayCube();
glPopMatrix();
}
void LeftHorn()
{
glPushMatrix();
glRotatef(0, 0.0, 0.0, 1);
glTranslatef(0.5, 1.15, -0.035);
glScalef(0.05, 0.15, 0.05);
ArrayCube();
glPopMatrix();
}
void FrontUpperRightLeg()
{
glPushMatrix();
glTranslatef(0.5, -0.35, 0.15);
glScalef(0.15, 0.5, 0.15);
ArrayCube();
glPopMatrix();
}
void FrontLowerRightLeg()
{
glPushMatrix();
glTranslatef(0.5, -0.75, 0.15);
glRotatef(angle[1], 0.0, 0.0, 1.0);
glScalef(0.15, 0.5, 0.15);
ArrayCube();
glPopMatrix();
}
void FrontUpperLeftLeg()
{
glPushMatrix();
glTranslatef(0.5, -0.35, -0.15);
glScalef(0.15, 0.5, 0.15);
ArrayCube();
glPopMatrix();
}
void FrontLowerLeftLeg()
{
glPushMatrix();
glTranslatef(0.5, -0.75, -0.15);
glRotatef(angle[1], 0.0, 0.0, 1.0);
glScalef(0.15, 0.5, 0.15);
ArrayCube();
glPopMatrix();
}
void BackUpperRightLeg()
{
glPushMatrix();
glTranslatef(-0.5, -0.35, -0.15);
glScalef(0.15, 0.5, 0.15);
ArrayCube();
glPopMatrix();
}
void BackLowerRightLeg()
{
glPushMatrix();
glTranslatef(-0.5, -0.75, -0.15);
glRotatef(angle[1], 0.0, 0.0, 1.0);
glScalef(0.15, 0.5, 0.15);
ArrayCube();
glPopMatrix();
}
void BackUpperLeftLeg()
{
glPushMatrix();
glTranslatef(-0.5, -0.35, 0.15);
glScalef(0.15, 0.5, 0.15);
ArrayCube();
glPopMatrix();
}
void BackLowerLeftLeg()
{
glPushMatrix();
glTranslatef(-0.5, -0.75, 0.15);
glRotatef(angle[1], 0.0, 0.0, 1.0);
glScalef(0.15, 0.5, 0.15);
ArrayCube();
glPopMatrix();
}
void Tail()
{
glPushMatrix();
glTranslatef(-0.65, -0.25, 0);
glScalef(0.05, 0.75, 0.05);
ArrayCube();
glPopMatrix();
}
//Each of the below functions draws an individual part of the whole model and places those parts where they need to go once the program runs
void DrawGiraffe()
{
MainBody();
LowerNeck();
UpperNeck();
Head();
RightHorn();
LeftHorn();
FrontUpperRightLeg();
FrontLowerRightLeg();
FrontUpperLeftLeg();
FrontLowerLeftLeg();
BackUpperRightLeg();
BackLowerRightLeg();
BackUpperLeftLeg();
BackLowerLeftLeg();
Tail();
}
//Calls the above functions to render the final model
//The rotate function allows the camera to rotate around the model
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.6, 0.6, 0.6, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
DrawGiraffe();
glutSwapBuffers();
}
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
currentMaterial = &redPlasticMaterial;
setMaterial(currentMaterial);
glColor3f(1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 2.5);
GLfloat light_pos[] = {2.0,2.0,2.0, 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
}
int main(int argc, char** argv)
{
angle[0] = 0;
angle[1] = 360;
angle[2] = 315;
angle[3] = 0;
GLubyte image[64][64][3];
int i, j, r, c;
for(i = 0;i < 64; i++){
for(j = 0;j < 64; j++){
c = ((((i&0x8)== 0)^((j&0x8))==0))*255;
image[i][j][0] = (GLubyte) c;
image[i][j][1] = (GLubyte) c;
image[i][j][2] = (GLubyte) c;
}
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Giraffe");
glutDisplayFunc(display);
glutIdleFunc(rotate);
init();
glutMainLoop();
}
There are many things wrong with your code:
You are using glTexParameterf instead of glTexParameteri.
You are not binding a texture object, which means that calls to your glTexImage2D function will be useless, because glTexImage2D relies on the bounded texture of GL_TEXTURE_2D.
You are initializing the texture before you call the glut initialization functions. This means that any calls to OpenGL functions will be useless.
You should initialize your texture with something like this
// this should be declared as a global variable
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
GLubyte image[64][64][3];
int i, j, r, c;
for(i = 0;i < 64; i++){
for(j = 0;j < 64; j++){
c = ((((i&0x8)== 0)^((j&0x8))==0))*255;
image[i][j][0] = (GLubyte) c;
image[i][j][1] = (GLubyte) c;
image[i][j][2] = (GLubyte) c;
}
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
and move the initialization of the texture to the init function, just after the call to glEnable(GL_TEXTURE_2D);
Of course, you will need to bind this texture when you render by calling glBindTexture(GL_TEXTURE_2D, texture);

OpenGL NURBS surfaces

I am implementing NURBS surfaces. All I want is on each mouse click there is decrements on Y-axis, so it looks like there is a weight of Sun or other planets.
#include <stdlib.h>
#include <glut.h>
int PI = 3.145;
int y = 0;
here i am trying to do the same thing but there is no change in output
void myMouse(int button, int state, int x, int y) {
if (state == GLUT_LEFT_BUTTON && GLUT_DOWN) {
y++;
}
glutPostRedisplay();
}
Code continue here
GLfloat ctrlpoints[4][4][3] = {
{{-3.5, 1.0, -4.5}, {-0.5, 1.0,-4.5 }, {0.5, 1.0, -4.5 }, {3.5, 1.0,-4.5}},
{{-3.5, 1.0, -0.5}, {-0.5, -2.0 - y,-0.5 }, {0.5, -2.0 - y, -0.5 }, {3.5, 1.0,-0.5}},
{{-3.5, 1.0, 0.5}, {-0.5, 1.0, 0.5 }, {0.5, 1.0, 0.5 }, {3.5, 1.0, 0.5}},
{{-3.5, 1.0, 4.5}, {-0.5, 1.0, 4.5 }, {0.5, 1.0, 4.5 }, {3.5, 1.0, 4.5}}
};
void Sun() {
glPushMatrix();
glTranslatef(0, 1, 0);
glRotatef(2*PI, 1, 0, 0);
glutSolidSphere(0.5, 20, 20);
glPopMatrix();
}
void display(void)
{
int i, j;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.0, 1.0, 1.0);
glPushMatrix();
glRotatef(45.0, 1.0, 1.0, 1.0);
for (j = 0; j <= 30; j++) {
glBegin(GL_LINE_STRIP);
for (i = 0; i <= 100; i++)
glEvalCoord2f((GLfloat)i / 100.0, (GLfloat)j / 30.0);
glEnd();
glBegin(GL_LINE_STRIP);
for (i = 0; i <= 100; i++)
glEvalCoord2f((GLfloat)j / 30.0, (GLfloat)i / 100.0);
glEnd();
}
glPopMatrix();
glPushMatrix();
glColor3f(1, 1, 0);
Sun();
glPopMatrix();
glFlush();
}
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &ctrlpoints[0][0][0]);
glEnable(GL_MAP2_VERTEX_3);
glMapGrid2f(20, 0.0, 1.0, 20, 0.0, 1.0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-5.0, 5.0, -5.0*(GLfloat)h / (GLfloat)w, 5.0*(GLfloat)h / (GLfloat)w, -5.0, 5.0);
else
glOrtho(-5.0*(GLfloat)w / (GLfloat)h, 5.0*(GLfloat)w / (GLfloat)h, -5.0, 5.0, -5.0, 5.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(myMouse);
Sun();
glutMainLoop();
return 0;
}
This is my current hardcoded output of this code i want this to decrements on y axis every time i click mouse button:
ctrlpoints is a global variable. Once it is initialized it can be changed only by an assignment. There is no magic dynamic dependency on the variable y.
You have to change the filed of ctrlpoints by an assignment. After changing the content of ctrlpoints, the two-dimensional evaluator has to be redefined.
The correct condition which is true, when the left mouse button is presse is button == GLUT_LEFT_BUTTON && state == GLUT_DOWN.
Note since y is a name of a formal parameter in the function signature of myMouse(int button, int state, int x, int y), y ++ (inside myMouse) won't change the the vlaue of the globale variable y, but it will change the value of the parameter y.
Add a function initMap and change the functions myMouse and init as folows:
void initMap(void)
{
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &ctrlpoints[0][0][0]);
glEnable(GL_MAP2_VERTEX_3);
glMapGrid2f(20, 0.0, 1.0, 20, 0.0, 1.0);
}
void myMouse(int button, int state, int px, int py)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
ctrlpoints[1][1][1] -= 1.0f;
ctrlpoints[1][2][1] -= 1.0f;
}
initMap();
glutPostRedisplay();
}
void init(void)
{
//glClearColor(1.0, 1.0, 1.0, 0.0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
initMap();
}
Preview:

Moving a 3D object with a mouse in OpenGL

I have been trying to make a whale in OpenGL. We have been trying to move the whale using a mouse input. The problem we are facing is with the syntax as well as the logic. We understand that the whale we are making is in a 3 dimensional world whereas the mouse input in in 2D for x and y on the screen.
If someone can be very specific with the syntax of using mouse input relevant to our code, that'd be great!
#include <Windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdlib.h>
#include "glut.h"
#include <math.h>
GLUquadric *qobja;
float movez = 0;
#define W_SCREEN 1366
#define H_SCREEN 768
#define TERR_D 75
#define TERR_W 750
#define pi 3.14
int mou_x = 0, mou_y = 0;
float alpha = 0;
float fahad = 0;
GLfloat ctrlpoints[4][4][3] = {
{ { -1.5, -1.5, 4.0 },
{ -0.5, -1.5, 2.0 },
{ 0.5, -1.5, -1.0 },
{ 1.5, -1.5, 2.0 } },
{ { -1.5, -0.5, 1.0 },
{ -0.5, -0.5, 3.0 },
{ 0.5, -0.5, 0.0 },
{ 1.5, -0.5, -1.0 } },
{ { -1.5, -1.5, 4.0 },
{ -0.5, -1.5, 2.0 },
{ 0.5, -1.5, -1.0 },
{ 1.5, -1.5, 2.0 } },
{ { -1.5, 1.5, -2.0 },
{ -0.5, 1.5, -2.0 },
{ 0.5, 1.5, 0.0 },
{ 1.5, 1.5, -1.0 } }
};
float cam_xrot = 180, cam_yrot = 180, cam_zrot = 0;
GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 };
GLfloat mat_ambient_color[] = { 0.8, 0.8, 0.2, 1.0 };
GLfloat mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat no_shininess[] = { 0.0 };
GLfloat low_shininess[] = { 5.0 };
GLfloat high_shininess[] = { 100.0 };
GLfloat mat_emission[] = { 0.3, 0.2, 0.2, 0.0 };
// Function Prototypes ////////////////////////////////////
void drawTerrain();
void drawAxes();
//drawing function decs
void draw_frust(float inner, float outer, float height, float m_z);
void cleanup();
void camera();
void pyramid();
void draw_closed_cyl(float a, float b, float c);
void draw_whale();
/* Initialize z-buffer, projection matrix, light source,
* and lighting model. Do not specify a material property here.
*/
void initLight(void)
{
GLfloat ambient[] = { 0.2, 0.2, 0.2, 1.0 };
GLfloat position[] = { 0.0, 0.0, 2.0, 1.0 };
GLfloat mat_diffuse[] = { 0.6, 0.6, 0.6, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
}
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4,
0, 1, 12, 4, &ctrlpoints[0][0][0]);
glEnable(GL_MAP2_VERTEX_3);
glEnable(GL_AUTO_NORMAL);
glMapGrid2f(20, 0.0, 1.0, 20, 0.0, 1.0);
initLight();
}
///////////////////////////////////////////////////////////
void display(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
camera();
drawAxes();
qobja = gluNewQuadric();
gluQuadricNormals(qobja, GLU_SMOOTH);
glPushMatrix();
glTranslatef(0, 0, movez);
draw_whale();
glPopMatrix();
}
void draw_whale()
{
glPushMatrix();
glTranslatef(0, 2, 90);
glRotatef(180, 0.0, 0.0, 1.0);
glScalef(1, 1, 2);
glEvalMesh2(GL_FILL, 0, 25, 0, 25);
glPopMatrix();
glPushMatrix();
float count = 5;
float a = 5;
for (float i = 0; i <= 16; i = i + 1)
{
draw_frust(a*sqrt(i), a*sqrt((i + 1)), 2, count * 2);
count++;
alpha = i + 1;
}
//a few smooth runs
for (int j = 0; j < 3; j++)
{
draw_frust(a*sqrt(alpha), a*sqrt(alpha), 2, count * 2);
count++;
}
float co = count;
for (float i = 17; i >= 3; i = i - 1)
{
draw_frust(a*sqrt(i), a*sqrt(i - 1), 2, co * 2);
co++;
fahad = a*sqrt(i - 1);
}
draw_frust(7.07, 5, 2, co * 2);
co++;
draw_frust(5, 3, 2, co * 2);
co++;
draw_frust(3, 1, 2, co * 2);
glPushMatrix();
//fin left
glPushMatrix();
glTranslatef(-25.5, 10, 25);
glRotatef(100, 1, 0, 0);
glRotatef(135, 0, 1, 0);
glRotatef(45, 0, 0, 1);
glScalef(1, 6, 1);
draw_closed_cyl(0, 0, 0);
glPopMatrix();
//fin right
glPushMatrix();
glTranslatef(25.5, 10, 25);
glRotatef(100, 1, 0, 0);
glRotatef(-135, 0, 1, 0);
glRotatef(-45, 0, 0, 1);
glScalef(1, 6, 1);
draw_closed_cyl(0, 0, 0);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void draw_frust(float inner, float outer, float height, float m_z)
{
qobja = gluNewQuadric();
gluQuadricNormals(qobja, GLU_SMOOTH);
glPushMatrix();
glTranslatef(0, 0, m_z);
gluDisk(qobja, 0.0, inner, 200, 20);
gluCylinder(qobja, inner, outer, height, 200, 200);
glPopMatrix();
}
void draw_closed_cyl(float a, float b, float c)
{
qobja = gluNewQuadric();
gluQuadricNormals(qobja, GLU_SMOOTH);
glPushMatrix();
glTranslatef(a, b + 2, c);
glRotatef(120, 0, 0, 1);
gluCylinder(qobja, 3.0, 3.0, 2.0, 20, 20);
gluDisk(qobja, 0, 3.0, 20, 20);
glTranslatef(0, 0, 2.0);
gluDisk(qobja, 0, 3.0, 20, 20);
glPopMatrix();
}
void drawTerrain(){
GLfloat color[] = { 0.2, 0.8, 0.2 };
glMaterialfv(GL_FRONT, GL_AMBIENT, color);
glColor3f(0.2, 0.8, 0.2); // this line is not needed when lighting in enabled
glPushMatrix();
glTranslatef(-TERR_W / 2, 0.0, -TERR_D / 2);
glBegin(GL_POLYGON);
glVertex3f(0, 0, 0);
glVertex3f(TERR_W, 0, 0);
glVertex3f(TERR_W, 0, TERR_D);
glVertex3f(0, 0, TERR_D);
glVertex3f(0, 0, 0);
glEnd();
glPopMatrix();
}
void drawAxes(){
glColor3d(1, 0, 0);
glBegin(GL_LINES);
glVertex3f(0, 0, 0);
glVertex3f(3, 0, 0);
glEnd();
glColor3d(0, 1, 0);
glBegin(GL_LINES);
glVertex3f(0, 0, 0);
glVertex3f(0, 3, 0);
glEnd();
glColor3d(0, 0, 1);
glBegin(GL_LINES);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, 3);
glEnd();
}
///////////////////////////////////////////////////////////
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(1.5*60.0, (GLfloat)w / (GLfloat)h, 1, 1.5*120.0);
glMatrixMode(GL_MODELVIEW);
camera();
}
///////////////////////////////////////////////////////////
void camera(){
glLoadIdentity();
glTranslatef(0, 0, -110);
glRotatef(cam_xrot, 1, 0, 0);
glRotatef(cam_yrot, 0, 1, 0);
glRotatef(cam_zrot, 0, 0, 1);
}
///////////////////////////////////////////////////////////
void keyboard(unsigned char key, int x, int y)
{
// Camera controls - Rotation along principle axis
switch (key) {
case 'n':
if (movez <= -100)
movez = movez + 100;
else
movez = movez - 3;
break;
case 'q':
cam_xrot += 10;
if (cam_xrot >360) cam_xrot -= 360;
break;
case 'z':
cam_xrot -= 10;
if (cam_xrot < -360) cam_xrot += 360;
break;
case 'a':
cam_yrot += 10;
if (cam_yrot >360) cam_yrot -= 360;
break;
case 'd':
cam_yrot -= 10;
if (cam_yrot < -360) cam_yrot += 360;
break;
case 'w':
cam_zrot += 10;
if (cam_zrot >360) cam_zrot -= 360;
break;
case 'x':
cam_zrot -= 10;
if (cam_zrot < -360) cam_zrot += 360;
break;
case 27:
cleanup();
exit(0);
break;
default:
break;
}
glutPostRedisplay();
}
///////////////////////////////////////////////////////////
void cleanup() // call once when you exit program
{
}
///////////////////////////////////////////////////////////
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(W_SCREEN, H_SCREEN);
glutInitWindowPosition(0, 0);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(25, update, 0);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

OpenGL _ Front objects are covered with Back objects. so can't see it

The problem is, 'Objects on the table is covered with table board, so can't see it.'
( I using openGL 3.7 beta. Files that I installed is : http://ihoo1836.dothome.co.kr/opengl_vs2010+glutdlls37beta.zip )
All Codes are following.
#include<glut.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
float TableX = 5.0; //Table's X size
float TableY = 8.0; //Table's Y size
float TableHeight = 2.0;//Table's Height
int width=400, height=400; //Window Size
int ViewX = width/2; //for Change Viewpoint by Mouse position
int ViewY = height/2;
int ViewZ = 9;
GLUquadricObj* cyl;
void InitLight( ){
glEnable(GL_DEPTH_TEST); //for opaque
glEnable(GL_NORMALIZE); //normalize
glEnable(GL_SMOOTH); //for smooth color
glEnable(GL_LIGHTING); //light setting
glDepthMask(GL_TRUE);
GLfloat ambientLight[] = {0.3f, 0.3f, 0.3f, 1.0f};
GLfloat diffuseLight[] = {0.7f, 0.7f, 0.7f, 1.0f};
GLfloat specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat specref[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat position[]={400.0, 300.0, -700.0, 1.0};
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMateriali(GL_FRONT, GL_SHININESS, 128);
}
//Get Mouse Position to Change ViewPoint
void MyMouseMove(int button, int state, GLint X, GLint Y)
{
//Get Mouse Position X, Y
ViewX = X;
ViewY = Y;
glutPostRedisplay();
}
//Get Mouse Position to Change ViewPoint
void MyMotion(GLint X, GLint Y)
{
//Get Mouse Position X, Y
ViewX = X;
ViewY = Y;
glutPostRedisplay();
}
//Draw Table
void DrawTable(){
glPushMatrix();
glTranslatef(0.0,0.0,1.0);
glColor3f(0.5, 0.25, 0.0);
cyl = gluNewQuadric();
glRotatef(-90,1.0,0.0,0.0);
gluCylinder(cyl, 0.2, 0.2, TableHeight, 10, 20); //Leg of Table 1
glPushMatrix();
cyl = gluNewQuadric();
glTranslatef(TableX,0.0,0.0);
gluCylinder(cyl, 0.2, 0.2, TableHeight, 10, 20); //Leg of Table 2
glPushMatrix();
cyl = gluNewQuadric();
glTranslatef(0.0, TableY, 0.0);
gluCylinder(cyl, 0.2, 0.2, TableHeight, 10, 20); //Leg of Table 3
glPushMatrix();
cyl = gluNewQuadric();
glTranslatef(-TableX,0.0,0.0);
gluCylinder(cyl, 0.2, 0.2, TableHeight, 10, 20); //Leg of Table 4
glPushMatrix();
glTranslatef(TableX/2.0, -TableY/2, TableHeight);
glScalef(TableX+0.5, TableY+0.5, 0.5);
glutSolidCube(1); //Board of Table
glPopMatrix();
glPushMatrix(); //triangular1 (Beside of Net)
glTranslatef(0, -TableY/2, TableHeight);
glBegin(GL_TRIANGLES);
glVertex3f(TableY/16.0, 0, TableY/8.0);//1
glVertex3f(0, TableY/8.0, 0);
glVertex3f(0, -TableY/8.0, 0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//2
glVertex3f(0, -TableY/8.0, 0);
glVertex3f(TableY/16.0, -TableY/8.0, 0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//3
glVertex3f(TableY/16.0, -TableY/8.0, 0);
glVertex3f(TableY/16.0, TableY/8.0, 0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//4
glVertex3f(TableY/16.0, TableY/8.0, 0);
glVertex3f(0, TableY/8.0, 0);
glEnd();
glPushMatrix(); //triangular2 (Beside of Net)
glTranslatef(TableX - TableY/8.0, 0 , 0);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//1
glColor3f(0.0, 1.0, 0.0);
glVertex3f(TableY/16.0, TableY/8.0, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(TableY/16.0, -TableY/8.0, 0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//2
glColor3f(0.0, 1.0, 0.0);
glVertex3f(TableY/16.0, -TableY/8.0, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(TableY/8.0, -TableY/8.0, 0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//3
glColor3f(0.0, 1.0, 0.0);
glVertex3f(TableY/8.0, -TableY/8.0, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(TableY/8.0, TableY/8.0, 0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//4
glColor3f(0.0, 1.0, 0.0);
glVertex3f(TableY/8.0, TableY/8.0, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(TableY/16.0, TableY/8.0, 0);
glEnd();
glPopMatrix();
glPushMatrix(); //Net
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(TableY/16.0, 0.0, TableY/8.0);
glVertex3f((TableX - TableY/16.0), 0, TableY/8.0);
glVertex3f((TableX - TableY/16.0), 0, 0);
glVertex3f(TableY/16.0, 0.0, 0.0);
glEnd();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
}
//Display Callback Function
void MyDisplay( ){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity( );
gluPerspective(60.0, (GLfloat)width/height, 0.0, 10.0);
//Change Viewpoint by Mouse Position
gluLookAt((float)(ViewX - width/2)/width*20 + 2.5, (float)(height/2 - ViewY)/height*20 + 2.5, ViewZ, TableX/2, TableY/2, TableHeight, 0.0, 1.0, 0.0);
printf("eyex = %f , eyey = %f , eyez = %f \n",(float)(ViewX - width/2.0)/width*10, (float)(height/2 - ViewY)/height*10, (float)ViewZ);
DrawTable(); //Draw Table
glutSwapBuffers(); //for 'Double Buffering'
}
//for Reshape Window
void MyReshape (int w, int h){
width = w;
height = h;
printf("width = %d, height = %d \n", width, height);
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity( );
glOrtho (-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
}
//Main Function
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(width, height);
glutInitWindowPosition(200, 200);
glutCreateWindow("OpenGL Sample Drawing");
glClearColor(0.4, 0.4, 0.4, 1.0);
InitLight(); //set Light Setting
glutDisplayFunc(MyDisplay);
glutMouseFunc(MyMouseMove); //get Mouse Position, to Change Viewpoint
glutMotionFunc(MyMotion); //get Mouse Position, to Change Viewpoint
glutReshapeFunc(MyReshape);
glutMainLoop( );
}
The third argument to gluPerspective() should be non-zero, positive, and less than the forth argument:
#include <GL/glut.h>
#include <stdio.h>
float TableX = 5.0; //Table's X size
float TableY = 8.0; //Table's Y size
float TableHeight = 2.0;//Table's Height
int ViewX = 400/2; //for Change Viewpoint by Mouse position
int ViewY = 400/2;
int ViewZ = 9;
GLUquadricObj* cyl;
void InitLight( ){
glEnable(GL_DEPTH_TEST); //for opaque
glEnable(GL_NORMALIZE); //normalize
glEnable(GL_SMOOTH); //for smooth color
glEnable(GL_LIGHTING); //light setting
glDepthMask(GL_TRUE);
GLfloat ambientLight[] = {0.3f, 0.3f, 0.3f, 1.0f};
GLfloat diffuseLight[] = {0.7f, 0.7f, 0.7f, 1.0f};
GLfloat specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat specref[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat position[]={400.0, 300.0, -700.0, 1.0};
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMateriali(GL_FRONT, GL_SHININESS, 128);
}
//Get Mouse Position to Change ViewPoint
void MyMouseMove(int button, int state, GLint X, GLint Y)
{
//Get Mouse Position X, Y
ViewX = X;
ViewY = Y;
glutPostRedisplay();
}
//Get Mouse Position to Change ViewPoint
void MyMotion(GLint X, GLint Y)
{
//Get Mouse Position X, Y
ViewX = X;
ViewY = Y;
glutPostRedisplay();
}
//Draw Table
void DrawTable(){
glPushMatrix();
glTranslatef(0.0,0.0,1.0);
glColor3f(0.5, 0.25, 0.0);
cyl = gluNewQuadric();
glRotatef(-90,1.0,0.0,0.0);
gluCylinder(cyl, 0.2, 0.2, TableHeight, 10, 20); //Leg of Table 1
glPushMatrix();
cyl = gluNewQuadric();
glTranslatef(TableX,0.0,0.0);
gluCylinder(cyl, 0.2, 0.2, TableHeight, 10, 20); //Leg of Table 2
glPushMatrix();
cyl = gluNewQuadric();
glTranslatef(0.0, TableY, 0.0);
gluCylinder(cyl, 0.2, 0.2, TableHeight, 10, 20); //Leg of Table 3
glPushMatrix();
cyl = gluNewQuadric();
glTranslatef(-TableX,0.0,0.0);
gluCylinder(cyl, 0.2, 0.2, TableHeight, 10, 20); //Leg of Table 4
glPushMatrix();
glTranslatef(TableX/2.0, -TableY/2, TableHeight);
glScalef(TableX+0.5, TableY+0.5, 0.5);
glutSolidCube(1); //Board of Table
glPopMatrix();
glPushMatrix(); //triangular1 (Beside of Net)
glTranslatef(0, -TableY/2, TableHeight);
glBegin(GL_TRIANGLES);
glVertex3f(TableY/16.0, 0, TableY/8.0);//1
glVertex3f(0, TableY/8.0, 0);
glVertex3f(0, -TableY/8.0, 0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//2
glVertex3f(0, -TableY/8.0, 0);
glVertex3f(TableY/16.0, -TableY/8.0, 0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//3
glVertex3f(TableY/16.0, -TableY/8.0, 0);
glVertex3f(TableY/16.0, TableY/8.0, 0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//4
glVertex3f(TableY/16.0, TableY/8.0, 0);
glVertex3f(0, TableY/8.0, 0);
glEnd();
glPushMatrix(); //triangular2 (Beside of Net)
glTranslatef(TableX - TableY/8.0, 0 , 0);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//1
glColor3f(0.0, 1.0, 0.0);
glVertex3f(TableY/16.0, TableY/8.0, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(TableY/16.0, -TableY/8.0, 0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//2
glColor3f(0.0, 1.0, 0.0);
glVertex3f(TableY/16.0, -TableY/8.0, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(TableY/8.0, -TableY/8.0, 0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//3
glColor3f(0.0, 1.0, 0.0);
glVertex3f(TableY/8.0, -TableY/8.0, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(TableY/8.0, TableY/8.0, 0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//4
glColor3f(0.0, 1.0, 0.0);
glVertex3f(TableY/8.0, TableY/8.0, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(TableY/16.0, TableY/8.0, 0);
glEnd();
glPopMatrix();
glPushMatrix(); //Net
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(TableY/16.0, 0.0, TableY/8.0);
glVertex3f((TableX - TableY/16.0), 0, TableY/8.0);
glVertex3f((TableX - TableY/16.0), 0, 0);
glVertex3f(TableY/16.0, 0.0, 0.0);
glEnd();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
}
//Display Callback Function
void MyDisplay( ){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode (GL_PROJECTION);
glLoadIdentity( );
double width = glutGet( GLUT_WINDOW_WIDTH );
double height = glutGet( GLUT_WINDOW_HEIGHT );
gluPerspective(60.0, (GLfloat)width/height, 0.01, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity( );
//Change Viewpoint by Mouse Position
gluLookAt((float)(ViewX - width/2)/width*20 + 2.5, (float)(height/2 - ViewY)/height*20 + 2.5, ViewZ, TableX/2, TableY/2, TableHeight, 0.0, 1.0, 0.0);
printf("eyex = %f , eyey = %f , eyez = %f \n",(float)(ViewX - width/2.0)/width*10, (float)(height/2 - ViewY)/height*10, (float)ViewZ);
DrawTable(); //Draw Table
glutSwapBuffers(); //for 'Double Buffering'
}
//Main Function
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(400, 400);
glutInitWindowPosition(200, 200);
glutCreateWindow("OpenGL Sample Drawing");
glClearColor(0.4, 0.4, 0.4, 1.0);
InitLight(); //set Light Setting
glEnable( GL_DEPTH_TEST );
glutDisplayFunc(MyDisplay);
glutMouseFunc(MyMouseMove); //get Mouse Position, to Change Viewpoint
glutMotionFunc(MyMotion); //get Mouse Position, to Change Viewpoint
glutMainLoop( );
}

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.