Draw 3D cube with line loop - opengl

I had faced the problem of using a line loop to draw out a 3D cube by using open gl.
void cube1()
{
glBegin(GL_LINE_LOOP);
//Cube1
//Face1
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-0.1, 0, 0);
glVertex3f(0.4, 0, 0);
glVertex3f(0.4, -0.3, 0);
glVertex3f(-0.1, -0.3, 0);
//Face2
glColor3f(1.0, 1.0, 0.0);
glVertex3f(-0.1, -0.3, 0);
glVertex3f(-0.1, -0.3, 1);
glVertex3f(-0.1, 0, 1);
glVertex3f(-0.1, 0, 0);
//Face3
glColor3f(1.0, 0.0, 1.0);
glVertex3f(-0.1, 0, 0);
glVertex3f(-0.1, 0, 1);
glVertex3f(0.4, 0, 1);
glVertex3f(0.4, 0, 0);
//Face4
glColor3f(0.0, 1.0, 1.0);
glVertex3f(0.4, 0, 0);
glVertex3f(0.4, 0, 1);
glVertex3f(0.4, -0.3, 1);
glVertex3f(0.4, -0.3, 0);
//Face5
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.4, -0.3, 0);
glVertex3f(-0.1, -0.3, 0);
glVertex3f(-0.1, -0.3, 1);
glVertex3f(0.4, -0.3, 1);
//Face6
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.4, -0.3, 1);
glVertex3f(-0.1, -0.3, 1);
glVertex3f(-0.1, 0, 1);
glVertex3f(0.4, 0, 1);
glEnd();
}
However, I still could not form the correct shape for my cube.Below are my output. Can anyone know the solution?

A line loop is an endless primitive. It behaves differently from GL_QUADS or GL_TRIANGLES (see OpenGL - Primitive). Therefore, you must draw 6 separate line loops (or at least 4). One for each side:
//Face1
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex3f(-0.1, 0, 0);
glVertex3f(0.4, 0, 0);
glVertex3f(0.4, -0.3, 0);
glVertex3f(-0.1, -0.3, 0);
glEnd();
//Face2
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex3f(-0.1, -0.3, 0);
glVertex3f(-0.1, -0.3, 1);
glVertex3f(-0.1, 0, 1);
glVertex3f(-0.1, 0, 0);
glEnd();
// [...]

Related

glPushMatrix() glPopMatrix() doesn't work

I try to code two object move independent which mean both are moving two different direction it's worked but it can't move continuous. when i put glTranslatef() at outside the glPushMatrix() ... glPopMatrix() it work normally.
void display()
{
glClearColor(0.356, 0.701, 0.0, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// if put gltransate() at here the object will moving continuous
glPushMatrix();
glTranslatef(.5, 0, 0);
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(-0.8, 0.5);
glVertex2f(-0.8, 0.8);
glVertex2f(-0.2, 0.8);
glVertex2f(-0.2, 0.5);
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(-.5, 0, 0);
glBegin(GL_QUADS);
glColor3f(.0, .0, .0);
glVertex2f(-0.8, 0.2);
glVertex2f(-0.8, 0.5);
glVertex2f(-0.2, 0.5);
glVertex2f(-0.2, 0.2);
glEnd();
glPopMatrix();
}
i expect the for the first square objects will go to the right constantly but it seem like just translate once then stop at the position.
Note, in general I recommend to use a math library like OpenGL Mathematics to do the matrix calculations and glLoadMatrix() to load a matrix of type glm::mat4 to the current matrix.
Anyway, an operation like glTranslatef() creates a new matrix and multiplies the current matrix by the new matrix. That's why consecutive calls to glTranslatef cause progressive "movement".
glPushMatrix / glPopMatrix store and restore the matrix on the matrix stack. So the consecutive movement can't work, because the current matrix stays the same at the begin of each frame.
One solution would be to store the translation to a variable and increment the variable:
GLfloat trans_a = 0.0f;
GLflaot trans_b = 0.0f;
void display()
{
glClearColor(0.356, 0.701, 0.0, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
trans_a += 0.5f;
glTranslatef(trans_a, 0, 0);
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(-0.8, 0.5);
glVertex2f(-0.8, 0.8);
glVertex2f(-0.2, 0.8);
glVertex2f(-0.2, 0.5);
glEnd();
glPopMatrix();
glPushMatrix();
trans_b += 0.5f;
glTranslatef(trans_b, 0, 0);
glBegin(GL_QUADS);
glColor3f(.0, .0, .0);
glVertex2f(-0.8, 0.2);
glVertex2f(-0.8, 0.5);
glVertex2f(-0.2, 0.5);
glVertex2f(-0.2, 0.2);
glEnd();
glPopMatrix();
}
A more generalized solution is to get and store the current matrix (after the translation) by glGetFloatv(GL_MODELVIEW_MATRIX, ...) and to reload it by glLoadMatrix():
// init identity matrices
GLfloat mat_a[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
GLfloat mat_b[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
void display()
{
glClearColor(0.356, 0.701, 0.0, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glLoadMatrixf(mat_a)
glTranslatef(0.5f, 0, 0);
glGetFloatv(GL_MODELVIEW_MATRIX, mat_a);
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(-0.8, 0.5);
glVertex2f(-0.8, 0.8);
glVertex2f(-0.2, 0.8);
glVertex2f(-0.2, 0.5);
glEnd();
glPopMatrix();
glPushMatrix();
glLoadMatrixf(mat_b)
glTranslatef(0.5f, 0, 0);
glGetFloatv(GL_MODELVIEW_MATRIX, mat_b);
glBegin(GL_QUADS);
glColor3f(.0, .0, .0);
glVertex2f(-0.8, 0.2);
glVertex2f(-0.8, 0.5);
glVertex2f(-0.2, 0.5);
glVertex2f(-0.2, 0.2);
glEnd();
glPopMatrix();
}
Note, in this case mat_a and mat_b should be loaded after the initialization of the view matrix and when the view matrix changes, then the matrices would not consider that.

OpenGL texture mapping issue

I'm trying to do texture on my cube as the code below. However, the result that I got is that the image did not cover the entire cube surface while looks like it looped.
You can see the blue lining from the picture used:
I've checked the coordinates but doesn't seem to have any error. I check the image size (512x512), it's also the same as I declared when loading the texture. So, I had trouble figuring where is the problem.
void square(void) {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[0]); //bind our texture to our shape
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0);
glTexCoord3d(0.0, 0.0, 0.0); glVertex3d(500, -500, -500); //behind
glTexCoord3d(1.0, 0.0, 0.0); glVertex3d(500, 500, -500);
glTexCoord3d(1.0, 1.0, 0.0); glVertex3d(-500, 500, -500);
glTexCoord3d(0.0, 1.0, 0.0); glVertex3d(-500, -500, -500);
glEnd();
glBegin(GL_POLYGON);
glTexCoord3d(0.0, 0.0, 0.0); glVertex3f(500, -500, 500); //front
glTexCoord3d(1.0, 0.0, 0.0); glVertex3f(500, 500, 500);
glTexCoord3d(1.0, 1.0, 0.0); glVertex3f(-500, 500, 500);
glTexCoord3d(0.0, 1.0, 0.0); glVertex3f(-500, -500, 500);
glEnd();
glBegin(GL_POLYGON);
glTexCoord3d(0.0, 0.0, 0.0); glVertex3f(500, -500, -500); //right
glTexCoord3d(1.0, 0.0, 0.0); glVertex3f(500, 500, -500);
glTexCoord3d(1.0, 1.0, 0.0); glVertex3f(500, 500, 500);
glTexCoord3d(0.0, 1.0, 0.0); glVertex3f(500, -500, 500);
glEnd();
glBegin(GL_POLYGON);
glTexCoord3d(0.0, 0.0, 0.0); glVertex3f(-500, -500, 500); //left
glTexCoord3d(1.0, 0.0, 0.0); glVertex3f(-500, 500, 500);
glTexCoord3d(1.0, 1.0, 0.0); glVertex3f(-500, 500, -500);
glTexCoord3d(0.0, 1.0, 0.0); glVertex3f(-500, -500, -500);
glEnd();
glBindTexture(GL_TEXTURE_2D, texture[1]);
glBegin(GL_POLYGON);
glTexCoord3d(0.0, 0.0, 0.0); glVertex3f(500, 500, 500); //top
glTexCoord3d(1.0, 0.0, 0.0); glVertex3f(500, 500, -500);
glTexCoord3d(1.0, 1.0, 0.0); glVertex3f(-500, 500, -500);
glTexCoord3d(0.0, 1.0, 0.0); glVertex3f(-500, 500, 500);
glEnd();
glBindTexture(GL_TEXTURE_2D, texture[2]);
glBegin(GL_POLYGON);
glTexCoord3d(0.0, 0.0, 0.0); glVertex3f(500, -500, -500); //bottom
glTexCoord3d(1.0, 0.0, 0.0); glVertex3f(500, -500, 500);
glTexCoord3d(1.0, 1.0, 0.0); glVertex3f(-500, -500, 500);
glTexCoord3d(0.0, 1.0, 0.0); glVertex3f(-500, -500, -500);
glEnd();
glDisable(GL_TEXTURE_2D);
}
function to load the RAW file:
GLuint LoadTexture(const char * filename, int width, intbheight)
{
GLuint texture;
unsigned char * data;
FILE * file;
//The following code will read in our RAW file
file = fopen(filename, "rb");
if (file == NULL) return 0;
data = (unsigned char *)malloc(width * height * 3);
fread(data, width * height * 3, 1, file);
fclose(file);
glGenTextures(1, &texture); //generate the texture with the loaded data
glBindTexture(GL_TEXTURE_2D, texture); //bind the textureto it’s array
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
GL_MODULATE); //set texture environment parameters
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
GL_REPEAT);
//Generate the texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
GL_RGB, GL_UNSIGNED_BYTE, data);
free(data); //free the texture
return texture; //return whether it was successful
}

OpenGL/GLUT Simple issue in making ride to be smooth

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

Changing background color by button press in GLUT

been following a few tutorials and other questions people have been asking on here. Essentially trying to make it when you press the escape key the background colour changes into another colour.
Edited the post with the whole code.
#include "stdafx.h"
#include <glut.h>
void render(void);
void keyboard(int key, int x, int y);
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100); //Position of the window
glutInitWindowSize(620, 440); //Screen Size
glClearColor (0.0, 1.0, 0.0, 0.0 );
glutCreateWindow("Greeting Card"); //Creates the window and names it
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Enables Alpha channel
glutDisplayFunc(render);
glutDisplayFunc(draw);
glutKeyboardFunc(keyboard);
glutMainLoop(); //Finished, now render
}
void keyboard (unsigned char key, int x, int y)
{
GLfloat colors[][3] = { { 0.0f, 0.0f, 1.0f}, {1.0f, 0.0f, 0.0f } };
static int back;
switch (key) {
case 27:
exit(0);
default:
back ^= 1;
glClearColor(colors[back][0], colors[back][1], colors[back][2], 1.0f);
glutPostRedisplay();
}
}
void render(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// World Snow //
glPushMatrix();
glTranslatef(0, -0.35, 0); //Position of the shape
glBegin(GL_POLYGON); //Defines the type of shape
glColor3f(1, 1,1); //Colour of the shape 'RED, GREEN, BLUE'
glVertex2f(-1.5,-0.7); //Vertex 2F Gives the vertex some coords
glColor3f(1, 1, 1);
glVertex2f(-1.5, 0.7);
glColor3f(1, 1, 1);
glVertex2f( 1.5, 0.7);
glColor3f(1, 1, 1);
glVertex2f( 1.5,-0.7);
glEnd();
glPopMatrix();
glFlush();
// Grey gradient world
glPushMatrix();
glTranslatef(0, -0.35, 0);
glBegin(GL_POLYGON);
glColor3f(1, 1, 1);
glVertex2f(-1.5,-0.7);
glColor3f(1, 1, 1);
glVertex2f(-1.5, 0.7);
glColor3f(0.658824, 0.658824, 0.658824);
glVertex2f( 1.5, 0.7);
glColor3f(1, 1, 1);
glVertex2f( 1.5,-1.7);
glEnd();
glPopMatrix();
glFlush();
// Top of the first Tree //
glPushMatrix();
glTranslatef(-0.6, 0.5, 0);
glBegin(GL_TRIANGLES); //Defines the shape as being a triangle
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(-0.1, -0.1);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.1, -0.1);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.0, 0.1);
glEnd();
glPopMatrix();
// Middle of the first tree
glPushMatrix();
glTranslatef(-0.6, 0.4, 0);
glBegin(GL_TRIANGLES);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(-0.1, -0.1);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.1, -0.1);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.0, 0.1);
glEnd();
glPopMatrix();
// Bottom of the first tree
glPushMatrix();
glTranslatef(-0.6, 0.3, 0);
glBegin(GL_TRIANGLES);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(-0.1, -0.1);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.1, -0.1);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.0, 0.1);
glEnd();
glPopMatrix();
glFlush();
//Stump of first tree
glPushMatrix();
glTranslatef(-0.6, 0.16, 0);
glBegin(GL_POLYGON);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.02,-0.04);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.02, 0.04);
glColor3f( 0.647059, 0.164706, 0.164706);
glVertex2f( 0.02, 0.04);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f( 0.02,-0.04);
glEnd();
glPopMatrix();
// Large Tree TOP
glPushMatrix();
glTranslatef(-0.2, 0, 0);
glBegin(GL_TRIANGLES);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(-0.15, -0.15);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.15, -0.15);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.0, 0.15);
glEnd();
glPopMatrix();
glFlush();
//Large Tree MIDDLE
glPushMatrix();
glTranslatef(-0.2, -0.15, 0);
glBegin(GL_TRIANGLES);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(-0.15, -0.15);
glColor3f( 0.137255, 0.556863,0.137255);;
glVertex2f(0.15, -0.15);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.0, 0.15);
glEnd();
glPopMatrix();
glFlush();
//Large Tree Bottom
glPushMatrix();
glTranslatef(-0.2, -0.30, 0);
glBegin(GL_TRIANGLES);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(-0.15, -0.15);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.15, -0.15);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.0, 0.15);
glEnd();
glPopMatrix();
glFlush();
//Smaller tree Top
glPushMatrix();
glTranslatef(0.05, 0.45, 0);
glBegin(GL_TRIANGLES);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(-0.05, -0.05);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.05, -0.05);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.0, 0.05);
glEnd();
glPopMatrix();
glFlush();
//Smaller tree MIDDLE
glPushMatrix();
glTranslatef(0.05, 0.40, 0);
glBegin(GL_TRIANGLES);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(-0.05, -0.05);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.05, -0.05);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.0, 0.05);
glEnd();
glPopMatrix();
glFlush();
//smaller tree bottom
glPushMatrix();
glTranslatef(0.05, 0.50, 0);
glBegin(GL_TRIANGLES);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(-0.05, -0.05);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.05, -0.05);
glColor3f(0.32, 0.49, 0.46);
glVertex2f(0.0, 0.05);
glEnd();
glPopMatrix();
glFlush();
//Stump of smaller tree
glPushMatrix();
glTranslatef(0.05, 0.32, 0);
glBegin(GL_POLYGON);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.01,-0.03);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.01, 0.03);
glColor3f( 0.647059, 0.164706, 0.164706);
glVertex2f( 0.01, 0.03);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f( 0.01,-0.03);
glEnd();
glPopMatrix();
//Stump of MAIN tree
glPushMatrix();
glTranslatef(-0.2, -0.50, 0);
glBegin(GL_POLYGON);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.02,-0.05);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.02, 0.05);
glColor3f( 0.647059, 0.164706, 0.164706);
glVertex2f( 0.02, 0.05);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f( 0.02,-0.05);
glEnd();
glPopMatrix();
// Red Present
glPushMatrix();
glTranslatef(0, -0.5, 0);
glBegin(GL_POLYGON);
glColor3f( 1, 0, 1);
glVertex2f(-0.04,-0.05);
glColor3f( 1, 0, 0);
glVertex2f(-0.04, 0.05);
glColor3f( 1, 0, 0);
glVertex2f( 0.04, 0.05);
glColor3f( 1, 0, 0);
glVertex2f( 0.04,-0.05);
glEnd();
glPopMatrix();
//Blue Present
glPushMatrix();
glTranslatef(-0.2, -0.7, 0);
glBegin(GL_POLYGON);
glColor3f( 0, 0, 1);
glVertex2f(-0.07,-0.06);
glColor3f( 0, 0, 1);
glVertex2f(-0.07, 0.06);
glColor3f( 0, 0, 1);
glVertex2f( 0.07, 0.06);
glColor3f( 0.196078, 0.6, 0.8);
glVertex2f( 0.07,-0.06);
glEnd();
glPopMatrix();
// BLUE Ribbon RED present VERT
glPushMatrix();
glTranslatef(0, -0.5, 0);
glBegin(GL_POLYGON);
glColor3f( 0, 0, 1);
glVertex2f(-0.04,-0.01);
glColor3f( 0, 0, 1);
glVertex2f(-0.04, 0.01);
glColor3f( 0, 0, 1);
glVertex2f( 0.04, 0.01);
glColor3f( 0, 0, 1);
glVertex2f( 0.04,-0.01);
glEnd();
glPopMatrix();
//BLUE ribbon RED present HORIZ
glPushMatrix();
glTranslatef(0, -0.5, 0);
glBegin(GL_POLYGON);
glColor3f( 0, 0, 1);
glVertex2f(-0.01,-0.05);
glColor3f( 0, 0, 1);
glVertex2f(-0.01, 0.05);
glColor3f( 0, 0, 1);
glVertex2f( 0.01, 0.05);
glColor3f( 0, 0, 1);
glVertex2f( 0.01,-0.05);
glEnd();
glPopMatrix();
//Yellow Ribbon Blue Present VERT
glPushMatrix();
glTranslatef(-0.2, -0.7, 0);
glBegin(GL_POLYGON);
glColor3f( 0.6, 0.8, 0.196078);
glVertex2f(-0.07,-0.01);
glColor3f( 0.6, 0.8, 0.196078);
glVertex2f(-0.07, 0.01);
glColor3f( 0.6, 0.8, 0.196078);
glVertex2f( 0.07, 0.01);
glColor3f( 0.6, 0.8, 0.196078);
glVertex2f( 0.07,-0.01);
glEnd();
glPopMatrix();
// BLUE present YELLOW ribbon VERT
glPushMatrix();
glTranslatef(-0.2, -0.7, 0);
glBegin(GL_POLYGON);
glColor3f( 0.6, 0.8, 0.196078);
glVertex2f(-0.01,-0.06);
glColor3f( 0.6, 0.8, 0.196078);
glVertex2f(-0.01, 0.06);
glColor3f( 0.6, 0.8, 0.196078);
glVertex2f( 0.01, 0.06);
glColor3f( 0.6, 0.8, 0.196078);
glVertex2f( 0.01,-0.06);
glEnd();
glPopMatrix();
//Sign Post
glPushMatrix();
glTranslatef(0.5, -0.1, 0);
glBegin(GL_POLYGON);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.02,-0.25);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.02, 0.25);
glColor3f( 0.35, 0.16, 0.14);
glVertex2f( 0.02, 0.25);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f( 0.02,-0.25);
glEnd();
glPopMatrix();
//Sign, Attatched to the post
glPushMatrix();
glTranslatef(0.5, -0.001, 0);
glBegin(GL_POLYGON);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.15,-0.10);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.15, 0.10);
glColor3f( 0.35, 0.16, 0.14);
glVertex2f( 0.15, 0.10);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f( 0.15,-0.10);
glEnd();
glPopMatrix();
//Moon
glPushMatrix();
glTranslatef(-0.9, 0.90, 0);
glBegin(GL_POLYGON);
glColor4f( 0.90, 0.91, 0.98, 1); //RGBA
glVertex2f(-0.10,-0.2);
glColor4f( 0.329412, 0.329412, 0.329412, 1);
glVertex2f(-0.10, 0.2);
glColor4f( 0.90, 0.91, 0.98, 1);
glVertex2f( 0.10, 0.2);
glColor4f( 0.90, 0.91, 0.98, 1);
glVertex2f( 0.10,-0.2);
glEnd();
glPopMatrix();
//MAIN PRESENT UNDER SIGN
glPushMatrix();
glTranslatef(0.5, -0.6, 0);
glBegin(GL_POLYGON);
glColor3f( 0.89, 0.47, 0.20);
glVertex2f(-0.20,-0.20);
glColor3f( 0.89, 0.47, 0.20);
glVertex2f(-0.20, 0.20);
glColor3f( 0.89, 0.47, 0.20);
glVertex2f( 0.20, 0.20);
glColor3f( 1.0, 0.25, 0);
glVertex2f( 0.20,-0.20);
glEnd();
glPopMatrix();
//Orange Present Purple Ribbon VERT
glPushMatrix();
glTranslatef(0.5, -0.6, 0);
glBegin(GL_POLYGON);
glColor3f( 0.73, 0.16, 0.96);
glVertex2f(-0.20,-0.06);
glColor3f( 0.73, 0.16, 0.96);
glVertex2f(-0.20, 0.06);
glColor3f( 0.87, 0.58, 0.98);
glVertex2f( 0.20, 0.06);
glColor3f( 0.87, 0.58, 0.98);
glVertex2f( 0.20,-0.06);
glEnd();
glPopMatrix();
//Orange Present Purple Ribbon HORIZ
glTranslatef(0.5, -0.6, 0);
glBegin(GL_POLYGON);
glColor3f( 0.87, 0.58, 0.98);
glVertex2f(-0.06,-0.20);
glColor3f( 0.87, 0.58, 0.98);
glVertex2f(-0.06, 0.20);
glColor3f( 0.73, 0.16, 0.96);
glVertex2f( 0.06, 0.20);
glColor3f( 0.73, 0.16, 0.96);
glVertex2f( 0.06,-0.20);
glEnd();
glPopMatrix();
glFlush();
//'North Pole' TEXT sign
glPushMatrix();
glLoadIdentity();
glTranslatef(0.360, -0.010, 0);
glRotatef(90,0.0f,0.0f,0.0f);
glColor3f( 0.0, 0.0, 0.0 ); //Colour is black
glRasterPos3i(10,100,1);
char text[50]="North Pole"; //Text
for(int i=0; i<50; i++)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,(int)text[i]);
}
glPopMatrix();
glutSwapBuffers();
}
trying to make it when you press the escape key the background colour changes into another colour.
Then why are you exit()ing when you press it? Swap your case 27: and default: logic.
Reset your projection/modelview matrices each time through render():
void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
...
You also have an un-matched glPopMatrix() in "Orange Present Purple Ribbon HORIZ":
//Orange Present Purple Ribbon HORIZ
glTranslatef(0.5, -0.6, 0);
glBegin(GL_POLYGON);
glColor3f( 0.87, 0.58, 0.98);
glVertex2f(-0.06,-0.20);
glColor3f( 0.87, 0.58, 0.98);
glVertex2f(-0.06, 0.20);
glColor3f( 0.73, 0.16, 0.96);
glVertex2f( 0.06, 0.20);
glColor3f( 0.73, 0.16, 0.96);
glVertex2f( 0.06,-0.20);
glEnd();
glPopMatrix();
^^^^^^^^^^^^^ wha-hey?
Other than those things the background toggle seems to work.

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