OpenGL Lines is hidden by other objects - c++

I'm having a trouble with my OpenGL program. I draw three axis Ox, Oy and Oz, along with 4 pyramids lying on the axis. When I rotate the shapes, the axis should be visible, but they are hidden by the four triangles. What can I do to see the axis?
#include <iostream>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
using namespace std;
float x_angle, y_angle, z_angle;
void init() {
x_angle = y_angle = z_angle = 0;
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
}
void color3f(GLfloat red, GLfloat green, GLfloat blue) {
glColor3f(red / 255, green / 255, blue / 255);
}
void drawLine(GLfloat x1, GLfloat y1, GLfloat z1, GLfloat x2, GLfloat y2, GLfloat z2, GLfloat red, GLfloat green, GLfloat blue) {
color3f(red, green, blue);
glBegin(GL_LINES);
glVertex3f(x1, y1, z1);
glVertex3f(x2, y2, z2);
glEnd();
}
void drawTriangle(GLfloat x1, GLfloat y1, GLfloat z1, GLfloat x2, GLfloat y2, GLfloat z2, GLfloat x3, GLfloat y3, GLfloat z3, GLfloat red, GLfloat green, GLfloat blue) {
color3f(red, green, blue);
glBegin(GL_TRIANGLES);
glVertex3f(x1, y1, z1);
glVertex3f(x2, y2, z2);
glVertex3f(x3, y3, z3);
glEnd();
}
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(5.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-15.0, 15.0, -15.0, 15.0, -15.0, 15.0);
glViewport(100, 100, 500, 500);
glRotatef(x_angle, 1.0, 0.0, 0.0);
glRotatef(y_angle, 0.0, 1.0, 0.0);
glRotatef(z_angle, 0.0, 0.0, 1.0);
glPointSize(10.0);
drawLine(-10.0, 0.0, 0.0, 10.0, 0.0, 0.0, 255.0, 0.0, 0.0);
drawLine(0.0, -10.0, 0.0, 0.0, 10.0, 0.0, 0.0, 255.0, 0.0);
drawLine(0.0, 0.0, -10.0, 0.0, 0.0, 10.0, 0.0, 0.0, 255.0);
// draw pyramid
drawTriangle(0.0, 5.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 5.0, 255, 255, 255);
drawTriangle(0.0, 5.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, -5.0, 0, 255, 204);
drawTriangle(0.0, 5.0, 0.0, -5.0, 0.0, 0.0, 0.0, 0.0, -5.0, 102, 102, 255);
drawTriangle(0.0, 5.0, 0.0, -5.0, 0.0, 0.0, 0.0, 0.0, 5.0, 255, 255, 0);
glFlush();
}
void onKeyPressed(unsigned char key, int a, int b) {
switch (key) {
case 'x':
x_angle -= 3;
break;
case 'X':
x_angle += 3;
break;
case 'y':
y_angle -= 3;
break;
case 'Y':
y_angle += 3;
break;
case 'z':
z_angle -= 3;
break;
case 'Z':
z_angle += 3;
break;
default:
break;
}
display();
}
int main(int argv, char** argc) {
glutInit(&argv, argc);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(800, 800);
glutCreateWindow("3D program");
init();
glutKeyboardFunc(onKeyPressed);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
This is the result
while it should be like this

You can by removing the
glEnable(GL_DEPTH_TEST)
more information about DEPTH BUFFER: https://www.opengl.org/wiki/Depth_Buffer

I don't think the fix you mentioned in your answer works; you probably changed something else and didn't realize it had an effect. Here's a better, working version of your display function:
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(5.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-15.0, 15.0, -15.0, 15.0, -15.0, 15.0);
glViewport(100, 100, 500, 500);
glRotatef(x_angle, 1.0, 0.0, 0.0);
glRotatef(y_angle, 0.0, 1.0, 0.0);
glRotatef(z_angle, 0.0, 0.0, 1.0);
// draw pyramid
drawTriangle(0.0, 5.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 5.0, 255, 255, 255);
drawTriangle(0.0, 5.0, 0.0, 0.0, 0.0, -5.0, 5.0, 0.0, 0.0, 0, 255, 204);
drawTriangle(0.0, 5.0, 0.0, -5.0, 0.0, 0.0, 0.0, 0.0, -5.0, 102, 102, 255);
drawTriangle(0.0, 5.0, 0.0, 0.0, 0.0, 5.0, -5.0, 0.0, 0.0, 255, 255, 0);
glDisable(GL_DEPTH_TEST);
glPointSize(10.0);
drawLine(-10.0, 0.0, 0.0, 10.0, 0.0, 0.0, 255.0, 0.0, 0.0);
drawLine(0.0, -10.0, 0.0, 0.0, 10.0, 0.0, 0.0, 255.0, 0.0);
drawLine(0.0, 0.0, -10.0, 0.0, 0.0, 10.0, 0.0, 0.0, 255.0);
glFlush();
}
As you've already figured out, make sure you supply GLUT_DEPTH to the display mode.
Make sure depth testing is on for the pyramid
Make sure it is off for the lines
Draw the lines after the pyramid, so that they'll be drawn on top of it. Without depth testing, things are just drawn one on top of another, in the order they're sent.
For bonus points, I rearranged the vertex order of your pyramid, so that it is all consistently CCW. Now if you do glEnable(GL_CULL_FACE) you'll only see the pyramid from the outside.
Failure to use GLUT_DEPTH and glEnable(GL_DEPTH_TEST) for the pyramid will result in the sides being drawn in order, which will be bizarre and non-physical.

I've changed my code by moving the function glEnable(GL_DEPTH_TEST); from init() function to main() function like this
int main(int argv, char** argc) {
glutInit(&argv, argc);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(100, 100);
glutInitWindowSize(800, 800);
glutCreateWindow("3D program");
glEnable(GL_DEPTH_TEST);
glutKeyboardFunc(onKeyPressed);
glutDisplayFunc(display);
glutMainLoop();
init();
return 0;
}
Then, the program worked like I expected. I don't know why this works. However, thanks for your helps.

Related

Why does my second 3D object not have four faces in Open GL

As the title says I'm tyring to model a simple giraffe out of arraycubes in open GL wiht C++, now I got the concepts done, but ran into an issue, when I start on the neck for some reaosn I lose 5 out of the 6 faces of my cube, the example I'm following doesn't result in this. I linked a small video below to show the visual result and I'm wondering what might be causing this. If there's an easier way to go about this as well please do let me know.
Visual Result
Code Sample
#include <glut.h>
float angle[4];
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} };
void drawFace(int a, int b, int c, int d) {
glBegin(GL_POLYGON);
glVertex3fv(corners[a]);
glVertex3fv(corners[b]);
glVertex3fv(corners[c]);
glVertex3fv(corners[d]);
glEnd();
}
void ArrayCube() {
glColor3f(1.0, 1.0, 1.0);
drawFace(0, 3, 2, 1);
glColor3f(1.0, 1.0, 1.0);
drawFace(3, 0, 4, 7);
glColor3f(1.0, 1.0, 1.0);
drawFace(2, 3, 7, 6);
glColor3f(1.0, 1.0, 1.0);
drawFace(1, 2, 6, 5);
glColor3f(1.0, 1.0, 1.0);
drawFace(4, 5, 6, 7);
glColor3f(1.0, 1.0, 1.0);
drawFace(5, 4, 0, 1);
}
void LowerNeck()
{
glPushMatrix();
glTranslatef(0.5, 0.25, -0.125);
glScalef(0.0, 0.5, 0.25);
ArrayCube();
glPopMatrix();
}
void MainBody()
{
glPushMatrix();
glScalef(1.25, 0.25, 0.5);
ArrayCube();
glPopMatrix();
}
void DrawGiraffe()
{
glRotatef(angle[0], 0.0, 1.0, 0.0);
MainBody();
LowerNeck();
}
void rotate() {
angle[0] += 1.0;
if (angle[0] > 360) angle[0] -= 360;
glutPostRedisplay();
}
void display() {
glClear(GL_COLOR_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);
glColor3f(1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 2.5);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Basic 3D");
glutDisplayFunc(display);
init();
glutIdleFunc(rotate);
glutMainLoop();
}
For the second object (the neck) you apply a scale transformation on x that scales the x component of all the following drawn vertices to 0.0:
glScalef(0.0, 0.5, 0.25);
That 0.0 should've probably been a 1.0.
That's the reason you only see one quad in the render video: That's the quad/face (actually two faces) which still have a dimension in Y and Z. The faces that have a dimension on x are squished to degenerate quads and not displayed at all.

How to draw a rhombus with freeglut?

I am trying to draw something like this image of the target output using OpenGL. I am using freeglut. I could draw three polygons, but I don't know how to give the three polygons right positioning. Below I attached the target output and my correct output.
The target output
My output
#include <GL/glut.h>
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-5.0, 5.0, -5.0, 5.0);
glMatrixMode(GL_MODELVIEW);
}
void drawSquare(void)
{
glBegin(GL_POLYGON);
glVertex2f(0.0f, -1.0f);
glVertex2f(2.0f, 0.0f);
glVertex2f(0.0f, 1.0f);
glVertex2f(-2.0f, 0.0f);
glEnd();
}
void myDraw1(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor3f(1.0, 0.0, 0.0);
drawSquare();
glTranslatef(2.0, 3.0, 0.0);
glRotatef(30, 0.0, 0.0, 1.0);
glColor3f(0.0, 1.0, 0.0);
drawSquare();
glLoadIdentity();
glTranslatef(-2.0, -3.0, 0.0);
glRotatef(-30, 0.0, 0.0, 1.0);
glColor3f(0.0, 0.0, 1.0);
drawSquare();
glFlush();
}
void myDraw2(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor3f(1.0, 0.0, 0.0);
drawSquare();
glPushMatrix();
glTranslatef(2.0, 3.0, 0.0);
glRotatef(30, 0.0, 0.0, 1.0);
glColor3f(0.0, 1.0, 0.0);
drawSquare();
glPopMatrix();
glTranslatef(-2.0, -3.0, 0.0);
glRotatef(-30, 0.0, 0.0, 1.0);
glColor3f(0.0, 0.0, 1.0);
drawSquare();
glFlush();
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(0, 0);
glutInitWindowSize(600, 600);
glutCreateWindow("Rotate");
init();
glutDisplayFunc(myDraw1);
glutMainLoop();
}
The angle between the polygons is 120°. The center of the polygons is (0, 0). You have to rotate around the point (-2, 0). Translate the polygon in that way that that (-2, 0) is moved to (0, 0) and rotate the polygon:
glRotatef(120.0, 0.0, 0.0, 1.0);
glTranslatef(2.0, 0.0, 0.0);
Note, operations like glTranslatef and glRotatef, specify a matrix and multiply the current matrix by the new specified matrix. Use glPushMatrix/glPopMatrix to save and restore the matrix (push on and pop from matrix stack).
e.g:
void myDraw1(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor3f(1.0, 0.0, 0.0);
glPushMatrix();
glRotatef(90.0, 0.0, 0.0, 1.0);
glTranslatef(2.0, 0.0, 0.0);
drawSquare();
glPopMatrix();
glColor3f(0.0, 1.0, 0.0);
glPushMatrix();
glRotatef(210.0, 0.0, 0.0, 1.0);
glTranslatef(2.0, 0.0, 0.0);
drawSquare();
glPopMatrix();
glColor3f(0.0, 0.0, 1.0);
glPushMatrix();
glRotatef(330.0, 0.0, 0.0, 1.0);
glTranslatef(2.0, 0.0, 0.0);
drawSquare();
glPopMatrix();
glFlush();
}

Object Rotating in OpenGL

I'm making a square arm with push-pop matrix and pop-up menu entries. I want to make the objects rotate individually, so I create the code below but the one finger moves along with the others. Why is it moving together?
void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
draw_lines();
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glTranslatef(-1.0, 0.0, 0.0);
glRotatef((GLfloat)shoulder, 0.0, 0.0, 1.0);
glTranslatef(1.0, 0.0, 0.0);
glPushMatrix();
glScalef(2.0, 0.4, 1.5);
glutWireCube(1.0); //First square(Shoulder)
glPopMatrix();
glTranslatef(1.0, 0.0, 0.0);
glRotatef((GLfloat)elbow, 0.0, 0.0, 1.0);
glTranslatef(1.0, 0.0, 0.0);
glPushMatrix();
glScalef(2.0, 0.4, 1.5);
glutWireCube(1.0); //Second Square(Elbow)
glPopMatrix();
glTranslatef(1.0, 0.0, 0.0);
glRotatef((GLfloat)finger_1, 0.0, 0.0, 1.0);
glTranslatef(0.25, 0.0, 0.7);
glPushMatrix();
glScalef(0.5, 0.2, 0.2);
glutWireCube(1.0); //First Finger
glPopMatrix();
glTranslatef(-0.3, 0.0, 0.0);
glRotatef((GLfloat)finger_2, 0.0, 0.0, 1.0);
glTranslatef(0.25, 0.0, -0.4);
glPushMatrix();
glScalef(0.5, 0.2, 0.2);
glutWireCube(1.0); //Second Finger
glPopMatrix();
glTranslatef(-0.3, 0.0, 0.0);
glRotatef((GLfloat)finger_3, 0.0, 0.0, 1.0);
glTranslatef(0.25, 0.0, -0.5);
glPushMatrix();
glScalef(0.5, 0.2, 0.2);
glutWireCube(1.0); //Third Finger
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
Object rotation is made by separate functions.

OpenGL analog clock doesn't show correctly?

I've a problem with my clock. In the first 10 minutes the hour hand doesn't show the correct hour and it changes when the minute hand's change but after 10 minutes it shows correctly. Could you please help me to fix it?
I think there is a problem in these lines but I am not sure:
static void TimeEvent(int te)
{
rx = 30 * cos( angle );
ry = 30 * sin( angle );
rz = 30 * cos( angle );
angle += 0.01;
if (angle > M_TWOPI) angle = 0;
glutPostRedisplay();
glutTimerFunc( 100, TimeEvent, 1);
}
Here is my whole code:
#include <GL/glut.h>
#include <gl/gl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
GLUquadricObj *Cylinder;
struct tm *newtime;
time_t l_time;
int M_TWOPI=0;
GLfloat rx, ry, rz, angle;
GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightDiffuse[]= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightPosition[]= { 5.0f, 25.0f, 15.0f, 1.0f };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
static int view_state = 1; // Ortho view = 1, Perspective = 0
void newLine(float Start, float End, float angle){
float c = cos(angle), s = sin(angle);
glVertex2f( -8.0f*Start*c, -8.0f*Start*s);
glVertex2f( -8.0f*End*c, -8.0f*End*s);
}
void Sprint( float x, float y, char *st)
{
int l,i;
l=strlen( st );
glRasterPos3f( x, y, -1);
for( i=0; i < l; i++)
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, st[i]);
}
}
static void TimeEvent(int te)
{
rx = 30 * cos( angle );
ry = 30 * sin( angle );
rz = 30 * cos( angle );
angle += 0.01;
if (angle > M_TWOPI) angle = 0;
glutPostRedisplay();
glutTimerFunc( 100, TimeEvent, 1);
}
void Draw_clock( GLfloat cx, GLfloat cy, GLfloat cz )
{
int hour_ticks , sec_ticks;
glPushMatrix();
glTranslatef(cx,cy,cz);
glRotatef(180, 1.0, 0.0, 0.0);
glPushMatrix();
glColor3f(1.0, 0.5, 0.0);
glTranslatef( 0, 0, 0.0);
glRotatef((360/12) * newtime->tm_hour + (360/60) * (60 / (newtime->tm_min+1)), 0.0, 0.0, 1.0);
glPushMatrix();
glTranslatef(0.0, 0.0, 2.0);
glPopMatrix();
glRotatef(90, 1.0, 0.0, 0.0);
gluCylinder(Cylinder, 0.75, 0, 4, 16, 16);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 0.0, 1.0);
glTranslatef( 0, 0, 0.0);
glRotatef( (360/60) * newtime->tm_min, 0.0, 0.0, 1.0);
glPushMatrix();
glTranslatef(0.0, 0.0, 3.0);
glScalef(0.5, 0.5, 1.0);
glPopMatrix();
glRotatef( 90, 1.0, 0.0, 0.0);
glutSolidCone (0.5, 6, 6, 16);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 0.0, 0.0);
glTranslatef( 0, 0, -0.0);
glRotatef( (360/60) * newtime->tm_sec, 0.0, 0.0, 1.0);
glPushMatrix();
glTranslatef(0.0, 0.0, 4.0);
glScalef(0.25, 0.25, 1.0);
glPopMatrix();
glRotatef( 90, 1.0, 0.0, 0.0);
gluCylinder(Cylinder, 0.25, 0, 6, 16, 16);
glPopMatrix();
for(hour_ticks = 0; hour_ticks < 12; hour_ticks++)
{
glPushMatrix();// Draw next arm axis.
glColor3f(0.0, 1.0, 1.0); // give it a color
glTranslatef(0.0, 0.0, 0.0);
glRotatef( (360/12) * hour_ticks, 0.0, 0.0, 1.0);
glTranslatef( 6.0, 0.0, 0.0);
glEnable(GL_LINE_SMOOTH);
glBegin(GL_LINES);
newLine(0.08f, 0.2f, 0.0f);
glEnd();
glPopMatrix();
}
for(sec_ticks = 0; sec_ticks < 60; sec_ticks++)
{
glPushMatrix();
glTranslatef(0.0, 0.0, 0.0);
glRotatef( (360/60) * sec_ticks, 0.0, 0.0, 1.0);
glTranslatef(6.0, 0.0, 0.0);
glutSolidCone(1.0, 2.0, 3, 4);
glPopMatrix();
}
glPopMatrix();
}
void num()
{
glColor3f( 0.0, 0.0, 1.0);
Sprint(-6.2,-0.2,"9"); //counting from center
Sprint(-0.2,-6.2,"6");
Sprint(2.8,-5.5,"5");
Sprint(5.0,-3.2,"4");
Sprint(5.0,+2.8,"2");
Sprint(2.8,+5.0,"1");
Sprint(-3.33,+4.95,"11");
Sprint(-0.2,-6.2,"6");
Sprint(-3.2,-5.45,"7");
Sprint(-0.4,5.7,"12");
Sprint(-5.35,-3.25,"8");
Sprint(-5.55,+2.8,"10");
Sprint(5.8,-0.2,"3");
}
void display_clock()
{
time(&l_time); // Get time
newtime = localtime(&l_time); // Convert to local time
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// easy way to put text on the screen.
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(-8.0, 8.0, -8.0, 8.0, 1.0, 60.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDisable(GL_LIGHTING);
glDisable(GL_COLOR_MATERIAL);
glColor3f( 1.0, 1.0, 1.0);
Sprint(-2, 3, " Clock");
Draw_clock( 0.0, 0.0, -14.0);
num();
glutSwapBuffers();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
display_clock();
glFlush();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (50, 50);
glutCreateWindow (argv[0]);
glutSetWindowTitle("Clock");
Cylinder = gluNewQuadric();
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc( 10, TimeEvent, 1);
glutMainLoop();
return 0;
}
You are doing integer arithmetic:
glRotatef((360/12) * newtime->tm_hour + (360/60) * (60 / (newtime->tm_min+1)), 0.0, 0.0, 1.0);
should be:
glRotatef(30.0 * newtime->tm_hour + 6.0) * (60.0 / (newtime->tm_min+1)), 0.0, 0.0, 1.0);
and so on.
Though, as 360/12 and 360/60 are constants you might as well replace them with the calculated values.
I believe your problem is here:
glRotatef((360/12) * newtime->tm_hour + (360/60) * (60 / (newtime->tm_min+1)), 0.0, 0.0, 1.0);
It must be:
glRotatef((360/12) * newtime->tm_hour + (360.0/12 / 60 ) * (newtime->tm_min+1), 0.0, 0.0, 1.0);

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