OpenGL door animation - opengl

I'm trying to animate a door so that if I press a button the door will open. At the moment it compiles, and when I go to press the button it moves the camera and then I can't control the camera anymore. What is wrong with the code?
glPushMatrix;
glTranslatef (door_Xpos,0.0, 0.0);
glRotatef (door_Angle, 1,0,0);
glBegin(GL_QUADS);
//Door left
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-10.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-10.0, 15.0, -25.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(0.0, 15.0, -25.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(0.0, 0.0, -25.0);
//door right
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(10.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(10.0, 15.0, -25.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(0.0, 15.0, -25.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(0.0, 0.0, -25.0);
glPopMatrix;
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'a':
case 'A':
glTranslatef(5.0, 0.0, 0.0);
break;
case 'd':
case 'D':
glTranslatef(-5.0, 0.0, 0.0);
break;
case 'w':
case 'W':
glTranslatef(0.0, 0.0, 5.0);
break;
case 's':
case 'S':
glTranslatef(0.0, 0.0, -5.0);
break;
case 't':
case 'T':
if (is_depth)
{
is_depth = 0;
glDisable(GL_DEPTH_TEST);
}
else
{
is_depth = 1;
glEnable(GL_DEPTH_TEST);
}
#Fall-through...
case 'o':
case 'O':
door_Xpos += 90.0;
break;
}
display();
}

OpenGL is not a scene graph, glTranslate, glRotate and similar don't move objects around, they change the transformation applied to triangles drawn to the screen. Thus it makes no sense to call matrix manipulations functions in an event handler. Actually most OpenGL functions, that includes matrix manipulation, are to be called from the display function. The only exception to this rule are data uploads like textures or buffer object data.
Try animating your door from within the display function based on variables set by the event handlers.

Related

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

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

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

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

Black screen on OpenGL

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

Cube Rotation - OpenGL

i heed help with openGL and rotations. This is My code:
#include <math.h>
#include <GL/glut.h>
float transZ=50;
float rotateA=0;
float rotateAspeed=0.0;
void cube (float dimX, float dimY, float dimZ)
{
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(0,dimY/2,0);
glScalef(dimX/2, dimY/2, dimZ/2);
glBegin(GL_QUADS);
glColor3f(0.0, 1.0, 0.0); // Color Green - TOP
glVertex3f(1.0, 1.0, 1.0); // TOP-RIGHT-NEAR
glVertex3f(-1.0, 1.0, 1.0); // TOP-LEFT-NEAR
glVertex3f(-1.0, 1.0, -1.0); //TOP-LEFT-FAR
glVertex3f(1.0, 1.0, -1.0); // TOP-RIGHT-FAR
glColor3f(1.0, 0.0, 0.0); // Color RED - Bottom
glVertex3f(1.0, -1.0, 1.0); //BOTTOM-RIGHT-NEAR
glVertex3f(-1.0, -1.0, 1.0); //BOTTOM-LEFT-NEAR
glVertex3f(-1.0, -1.0, -1.0); //BOTTOM-LEFT-FAR
glVertex3f(1.0, -1.0, -1.0); //BOTTOM-RIGHT-FAR
glColor3f(1.0, 1.0, 0.0); // Color Yellow - Back
glVertex3f(1.0, 1.0, -1.0); //TOP-RIGHT-FAR
glVertex3f(-1.0, 1.0, -1.0); //TOP-LEFT-FAR
glVertex3f(-1.0, -1.0, -1.0); //BOTTOM-LEFT-FAR
glVertex3f(1.0, -1.0, -1.0); //BOTTOM-RIGHT-FAR
glColor3f(0.0, 0.0, 1.0); //Color Blue - RIGHT
glVertex3f(1.0, 1.0, 1.0); //TOP-FRONT-NEAR
glVertex3f(1.0, 1.0, -1.0); //TOP-BACK-FAR
glVertex3f(1.0, -1.0, -1.0); //BOTTOM-BACK-FAR
glVertex3f(1.0, -1.0, 1.0); //BOTTOM-FRONT-NEAR
glColor3f(1.0, 0.5, 0.0); //Color Orange - Left
glVertex3f(-1.0, 1.0, 1.0); //TOP-FRONT-NEAR
glVertex3f(-1.0, 1.0, -1.0); //TOP-BACK-FAR
glVertex3f(-1.0, -1.0, -1.0);//BOTTOM-BACK-FAR
glVertex3f(-1.0, -1.0, 1.0); //BOTTOM-FRONT-NEAR
glEnd();
glPopMatrix();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(transZ*cos(rotateA),10,transZ*sin(rotateA), 0,10,0, 0,1,0);
cube(30,30,30);
glFlush();
glutSwapBuffers();
}
void init (void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1, 1, -1, 1, 1, 1000);
glEnable(GL_DEPTH_TEST);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
case 'S':
transZ+=1.0f;
glutPostRedisplay();
break;
case 'W':
transZ-=1.0f;
if (transZ<0) transZ=0;
glutPostRedisplay();
break;
case 's':
transZ+=0.5f;
glutPostRedisplay();
break;
case 'w':
transZ-=0.5f;
if (transZ<0) transZ=0;
glutPostRedisplay();
break;
case 'A':
rotateAspeed+=0.001f;
glutPostRedisplay();
break;
case 'a':
rotateAspeed+=0.001f;
glutPostRedisplay();
break;
case 'D':
rotateAspeed-=0.001f;
glutPostRedisplay();
break;
case 'd':
rotateAspeed-=0.001f;
glutPostRedisplay();
break;
}
}
void idle(void)
{
rotateA=rotateA + rotateAspeed;
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Cube");
init ();
glutDisplayFunc(display);
glutIdleFunc(idle);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
When i press A or D the cube is rotated horizontally but i need to make, when i press, for example J the cube to start rotating vertically. Any help on how to do that?
You need to look at glRotate and how transformations are done in openGL. Then parameterize the rotation by three angles, called Euler angles, or use quaternions, or some other suitable rotation representation. From this the transformations will rotate the cube properly, it might look like this:
glPushMatrix()
glRotatef(alpha, 1, 0, 0); //rotate alpha around the x axis
glRotatef(beta, 0, 1, 0); //rotate beta around the y axis
glRotatef(gamma, 0, 0, 1); //rotate gamma around the z axis
//dram my cube
glPopMatrix();

OpenGL texturing level

I'm trying to learn OpenGL, and I want to start texturing but I got stuck. I've created a level with two rooms and a corridor, so I want diferent textures for different parts (that is, a texture for the floor, texture for the walls and so on) but after looking at the NeHe tutorial I'm still stuck, where do I need to put it in my code?
#include <windows.h>
#include <gl\gl.h>
#include <gl\glut.h>
#include <gl\glu.h>
void init(void);
void display(void);
void keyboard(unsigned char, int, int);
void resize(int, int);
int is_depth; /* depth testing flag */
int main (int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(40, 40);
glutCreateWindow("The Cube World");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
/* This time we're going to keep the aspect ratio
constant by trapping the window resizes. */
glutReshapeFunc(resize);
glutMainLoop();
return 0;
}
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
is_depth = 1;
glMatrixMode(GL_MODELVIEW);
}
void display(void)
{
if (is_depth)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
else
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
//floor
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-50.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-50.0, 0.0, 25.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(50.0, 0.0, 25.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(50.0, 0.0, -25.0);
//left wall
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-50.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-50.0, 00.0, 25.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(-50.0, 25.0, 25.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(-50.0, 25.0, -25.0);
//right wall
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(50.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(50.0, 00.0, 25.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(50.0, 25.0, 25.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(50.0, 25.0, -25.0);
//roof
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-50.0, 25.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-50.0, 25.0, 25.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(50.0, 25.0, 25.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(50.0, 25.0, -25.0);
//right panel
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(10.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(10.0, 15.0, -25.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(50.0, 15.0, -25.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(50.0, 0.0, -25.0);
//left panel
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-10.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-10.0, 15.0, -25.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(-50.0, 15.0, -25.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(-50.0, 0.0, -25.0);
//top panel
glColor3f(0.2f, 0.2f, 0.2f);
//bottom right
glVertex3f(50.0, 15.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
//top right
glVertex3f(50.0, 25.0, -25.0);
glColor3f(0.6f, 0.6f, 0.6f);
//top left
glVertex3f(-50.0, 25.0, -25.0);
glColor3f(0.8f, 0.8f, 0.8f);
//bottom left
glVertex3f(-50.0, 15.0, -25.0);
// corridor floor
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-10.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-10.0, 0.0, -275.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(10.0, 0.0, -275.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(10.0, 0.0, -25.0);
// corridor left wall
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-10.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-10.0, 00.0, -275.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(-10.0, 15.0, -275.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(-10.0, 15.0, -25.0);
// corridor right wall
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(10.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(10.0, 00.0, -275.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(10.0, 15.0, -275.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(10.0, 15.0, -25.0);
//corridor roof
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-10.0, 15.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-10.0, 15.0, -275.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(10.0, 15.0, -275.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(10.0, 15.0, -25.0);
//right panel room 2
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(10.0, 0.0, -275.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(10.0, 15.0, -275.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(50.0, 15.0, -275.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(50.0, 0.0, -275.0);
//left panel room 2
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-10.0, 0.0, -275.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-10.0, 15.0, -275.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(-50.0, 15.0, -275.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(-50.0, 0.0, -275.0);
//top panel room 2
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(50.0, 15.0, -275.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(50.0, 25.0, -275.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(-50.0, 25.0, -275.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(-50.0, 15.0, -275.0);
//right wall room 2
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(50.0, 0.0, -275.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(50.0, 00.0, -325.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(50.0, 25.0, -325.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(50.0, 25.0, -275.0);
//left wall room 2
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-50.0, 0.0, -275.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-50.0, 00.0, -325.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(-50.0, 25.0, -325.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(-50.0, 25.0, -275.0);
//roof room 2
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-50.0, 25.0, -275.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-50.0, 25.0, -325.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(50.0, 25.0, -325.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(50.0, 25.0, -275.0);
//back wall room 2
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(50.0, 0.0, -325.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(50.0, 25.0, -325.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(-50.0, 25.0, -325.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(-50.0, 0.0, -325.0);
//floor room 2
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-50.0, 0.0, -275.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-50.0, 0.0, -325.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(50.0, 0.0, -325.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(50.0, 0.0, -275.0);
glEnd();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'a':
case 'A':
glTranslatef(5.0, 0.0, 0.0);
break;
case 'd':
case 'D':
glTranslatef(-5.0, 0.0, 0.0);
break;
case 'w':
case 'W':
glTranslatef(0.0, 0.0, 5.0);
break;
case 's':
case 'S':
glTranslatef(0.0, 0.0, -5.0);
break;
case 't':
case 'T':
if (is_depth)
{
is_depth = 0;
glDisable(GL_DEPTH_TEST);
}
else
{
is_depth = 1;
glEnable(GL_DEPTH_TEST);
}
}
display();
}
void resize(int width, int height)
{
if (height == 0)
height = 1;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
/* Note we divide our width by our height to get the aspect ratio. */
gluPerspective(45.0, width / height, 1.0, 400.0);
/* Set initial position */
glTranslatef(0.0, -5.0, -150.0);
glMatrixMode(GL_MODELVIEW);
}
You don't seem to have any code for texture loading. I'd suggest you head back to NeHe and read this tutorial http://nehe.gamedev.net/tutorial/texture_mapping/12038/
It feels like you are getting too far ahead of yourself, and you should be going through most (if not all) of the tutorials in order. If you're trying to make a first person shooter, I'd also suggest loading the level from a model file (there are articles about this on NeHe too)