Using both glOrtho and gluPerspective in OpenGL C++ - c++

The Problem arises when I go from Welcome screen which is in glOrtho to a other screen which is in gluPerspective.
The only way I can render them both is when I resize the window after clicking to play from welcome screen, otherwise I receive black screen.
I think I need to fix the glVertex3f values for it to render properly as when I move directly to function without going to welcome screen, the polygon renders properly
Do check the gif below:
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (key1 == 3){
glOrtho(0.0, 100.0, 0.0, 100.0, -5.0 , 10.0);
glMatrixMode(GL_MODELVIEW);
}
if (key1 == 1 || key1 == 2){
gluPerspective(45.0, (float)w/(float)h, 0.1f, 200.0);
gluLookAt(0.0, 0.0, 5.0,0.0, 0.0, 0.0,0.0, 1.0, 0.0);
glMatrixMode(GL_MODELVIEW);
}
}
void par(float x1, float x2, float y1, float y2, float z1, float z2){
glColor3f(0.3,0.56,0.84);
glBegin(GL_POLYGON);
glVertex3f(x1, y1, z1);
glVertex3f(x2, y1, z1);
glVertex3f(x2, y2, z1);
glVertex3f(x1, y2, z1);
glEnd();
}
void Drawkey1/2(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();
glTranslatef(0.0, 0.0, -22.0);
int i;
par(-8.7, 9.2, 9.0, 9.2, 0.0, 0.0);
par(-8.7, 9.2, -8.5, -8.7, 0.0, 0.0);
par(-8.5, -8.7, -8.7, 9.2, 0.0, 0.0);
par( 9.2, 9.0, -8.7, 9.2, 0.0, 0.0);
while(p != NULL){
par((p -> x)/2.0,(p -> x)/2.0 + 0.4,(p -> y)/2.0,(p -> y)/2.0 + 0.4, 0.0, 0.0);
p = p -> nexploration_ratet;
}
par(food_x/2.0, food_x/2.0 + 0.4 , food_y/2.0 , food_y/2.0 + 0.4, 0.0 , 0.0);
}
void Drawkey3(){
glColor3f(0.3,0.56,0.84);
glBegin(GL_POLYGON);
glVertex3f(0.0,0.0,0.0);
glColor3f(0.137,0.137,0.556);
glVertex3f(100.0,0.0,0.0);
glColor3f(0.196,0.196,0.8);
glVertex3f(100.0,100.0,0.0);
glVertex3f(0.0,100.0,0.0);
glEnd();
glColor3f(0.196,0.196,0.8);
glRectf(39.5,39.5,60.5,45.5);
glColor3f(0.8,0.8,0.8);
glRectf(40,40,60,45);
glColor3f(0.137,0.137,0.556);
drawString(47,42,0,GLUT_BITMAP_HELVETICA_18,"USER");
glColor3f(0.196,0.196,0.8);
glRectf(39.5,29.5,60.5,35.5);
glColor3f(0.8,0.8,0.8);
glRectf(40,30,60,35);
glColor3f(0.137,0.137,0.556);
drawString(41,31,0,GLUT_BITMAP_HELVETICA_18,"NETWORK_PLAY");
glColor3f(0.196,0.196,0.8);
glRectf(39.5,19.5,60.5,25.5);
glColor3f(0.8,0.8,0.8);
glRectf(40,20,60,25);
glColor3f(0.137,0.137,0.556);
drawString(46,21,0,GLUT_BITMAP_HELVETICA_18,"HOW_TO");
glColor3f(0.196,0.196,0.8);
glRectf(39.5,9.5,60.5,15.5);
glColor3f(0.8,0.8,0.8);
glRectf(40,10,60,15);
glColor3f(0.137,0.137,0.556);
drawString(47,11,0,GLUT_BITMAP_HELVETICA_18,"EXIT");
glPushMatrix();
glColor3f(0.8,0.8,0.8);
drawString(25.5,92,0,GLUT_BITMAP_TIMES_ROMAN_24,"COMPUTER GRAPHICS PROJECT ");
drawString(35.5,80,0,GLUT_BITMAP_TIMES_ROMAN_24,"SRIJANA");
glPopMatrix();
glColor3f(0.137,0.137,0.556);
}

Update:
Fixed the error by making sure the glMatrixMode doesn't fetch GL_MODELVIEW when transition from GL_PROJECTION

Related

OpenGL moving object doesn't work

I just want to do horizontally moving wheel. There's a code but it doesn't work - wheel just moving as a circle, leaving traces. I've tried to glTranslatef(0.0f, moveY, 0.0f); but it doesn't work too.
#include "stdafx.h"
#include <math.h>
#include <GL/freeglut.h>
#include <GL\GL.h>
#define window_width 1080
#define window_height 720
GLfloat angle; // wheel's rotator
GLfloat moveX; // wheel's mover by X
void DrawCircle(float cx, float cy, float r, int num_segments) {
glLoadIdentity();
glTranslatef(0, 0, -10); // set place where wheel starts to draw
glRotatef(angle, 0.0f, 0.0f, -0.1f); // rotate our wheel. it works correctly I think
glTranslatef(moveX, 0.0f, 0.0f); // but it makes me wonder. moving circle by X doesn't work
// part of a circle
glBegin(GL_TRIANGLE_FAN);
glColor3f(255, 255, 255);
for (int ii = 0; ii < num_segments; ii++) {
double twicePi = 2.0 * 3.142;
float theta = 2.0f * 3.1415926f * float(ii) / float(num_segments);
float x = r * cosf(theta);
float y = r * sinf(theta);
glVertex2f(x + cx, y + cy);
}
glEnd();
// second part of a circle
int i, x, y;
double radius = 1.20;
glColor3ub(0.0, 0.0, 0.0);
double twicePi = 2.0 * 3.142;
x = 0, y = 0;
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x, y);
for (i = 0; i <= 20; i++) {
glVertex2f(
(x + (radius * cos(i * twicePi / 20))),
(y + (radius * sin(i * twicePi / 20)))
);
}
glEnd();
// there's an end of drawning of a circle
// this is where wheel starts to rotate and move
angle += 0.02f;
moveX += 0.00005f;
// there are spokes of a wheel
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, -1.2f, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 1.2f, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.2f, 0.0, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(-1.2f, 0.0, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.f, 1.f, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(-1.f, 1.f, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.f, -1.f, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(-1.f, -1.f, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.2f, 0.5f, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(-1.2f, -0.5f, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(-1.2f, -0.5f, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(-1.2f, 0.5f, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.2f, -0.5f, 0.0);
glEnd();
}
void main_loop_function() {
DrawCircle(0.0, 0.0, 1.45, 200);
glutSwapBuffers();
}
void GL_Setup(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
gluPerspective(45, (float) width / height, 0.1, 100);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitWindowSize(window_width, window_height);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("trata");
glutIdleFunc(main_loop_function);
GL_Setup(window_width, window_height);
glutMainLoop();
}
This is just a quick fix to make your code work so you can fuhrtherly develop and debug it. I see a number of critical issues in your code:
(0) Mixing POSIX paths with Windows paths:
#include <GL/freeglut.h>
#include <GL\GL.h>
(1) including custom headers and expecting the community to guess what is it about. I commented it out.
//#include "stdafx.h"
(2) Miswriting casts as functions - it should be:
float theta = 2.0f * 3.1415926f * (float)ii / (float)num_segments;
(3) No glutDisplayFunc in your code. You should register something like:
glutDisplayFunc(main_display_function);
before registering glutIdleFunc(). Then, in your code you should define it somewhat in the following way:
void main_display_function() {
DrawCircle(0.0, 0.0, 1.45, 200);
glutSwapBuffers();
}
(4) Even in that function you don't clear the "window content" before redrawing:
void DrawCircle(float cx, float cy, float r, int num_segments) {
glClearColor (0., 0., 0., 1);
glClear (GL_COLOR_BUFFER_BIT);
{… … … } //the rest of your code
}
(5) Passing color values of wrong data type:
glColor3f(255, 255, 255); //unsigned byte passed, float expected!
or
glColor3ub(0.0, 0.0, 0.0); //float passed, unsigned byte expected!
There are few more issues which directly address your question - you will easily understand and debug them yourself, once you fix the program to work correctly (the order of calling glRotatef and the second glTranslatef, correctly computing movex, drawing beams or spokes). Please, also take a look at this SO post.
PS. Your using a C++ compiler to build this program doesn't make it a C++ code. There is not a single line of C++ specific code in your program. Also, both OpenGL and GLUT are plain-C APIs.

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

OpenGL Lines is hidden by other objects

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.

How to find a point on the wall as a center point of a clock?

I create a cube as room and set camera inside it. I want to draw a clock on one of the walls. but when I draw a circle for showing clock, it doesn't work correctly. can anyone help me to draw a clock on the wall? here is my codes:
void room(){
if(flag){
glRotatef(rt,0.0f,1.0f,0.0f);
}
glEnable(GL_NORMALIZE);
glColor3f(1.0, 1.0 ,1.0); //roof
glNormal3f(0,-1,0);
glVertex3f( 3.0, 3.0,-3.0);
glVertex3f(-3.0, 3.0,-3.0);
glVertex3f(-3.0, 3.0, 3.0);
glVertex3f( 3.0, 3.0, 3.0);
glColor3f(1.0, 1.0 ,0.0); //floor
glNormal3f(0,1,0);
glVertex3f( 3.0,-3.0, 3.0);
glVertex3f(-3.0,-3.0, 3.0);
glVertex3f(-3.0,-3.0,-3.0);
glVertex3f( 3.0,-3.0,-3.0);
glColor3f(1.0, 0.0 ,0.0); //behind wall
glNormal3f(0,0,-1);
glVertex3f( 3.0, 3.0, 3.0);
glVertex3f(-3.0, 3.0, 3.0);
glVertex3f(-3.0,-3.0, 3.0);
glVertex3f( 3.0,-3.0, 3.0);
glColor3f( 0.0, 1.0,1.0); // front wall
glNormal3f(0,0,1);
glVertex3f( 3.0,-3.0,-3.0);
glVertex3f(-3.0,-3.0,-3.0);
glVertex3f(-3.0, 3.0,-3.0);
glVertex3f( 3.0, 3.0,-3.0);
glColor3f(0.0 ,0.0,1.0); //left wall
glNormal3f(1,0,0);
glVertex3f(-3.0, 3.0, 3.0);
glVertex3f(-3.0, 3.0,-3.0);
glVertex3f(-3.0,-3.0,-3.0);
glVertex3f(-3.0,-3.0, 3.0);
glColor3f( 0.2, 0.4, 0.0); // right wall
glNormal3f(-1,0,0);
glVertex3f( 3.0, 3.0,-3.0);
glVertex3f( 3.0, 3.0, 3.0);
glVertex3f( 3.0,-3.0, 3.0);
glVertex3f( 3.0,-3.0,-3.0);
}
void drawClock() {
char buff[100];
GLfloat x1=2.0;
GLfloat y1=2.0;
GLfloat z1=-3.0;
GLfloat radius = 1.0; // Radius
int angle;
glLoadIdentity();
//glClearColor(1.0, 1.0, 1.0, 1.0);
//glClear(GL_COLOR_BUFFER_BIT);
//glOrtho(0.0, 20.0, 0.0, 20.0, -10.0, 10.0);
// Draw circle
glPushMatrix();
glTranslatef(3.0,3.0,-3.0);
glBegin(GL_TRIANGLE_FAN);
glColor3f(0.5,1.0,0.0);
glVertex3f(x1, y1, z1);
for(angle=0; angle <= 360; angle +=1)
glVertex3f(x1 + cos(angle * PI/180.0f)* radius, y1 + sin(angle *PI/180.0f)* radius, z1);
glEnd();
glPopMatrix();
}
The center of the wall should be (0, 0, -3) not (2, 2, -3). Probably it's better to use however (0, 0, -2.9) so that the clock is slightly inside and not exactly on the wall.
Also I'd say that you need to add
glBegin(GL_QUADS);
at the start of room function and
glEnd();
at the end of it.
Also why are you calling glTranslatef before drawing the clock if it uses the same coordinate system as the walls?

Opengl: how to make this triangle centred on the window

#include <GL/glut.h>
GLint winWidth = 600, winHeight = 600;
GLfloat x0 = 100.0, y0 = 100.0, z0 = 50.0;
GLfloat xref = 50, yref = 50.0, zref = 0.0;
GLfloat Vx = 0.0, Vy = 1.0, Vz = 0.0;
GLfloat xwMin = -40.0, ywMin = -60.0, xwMax = 40.0, ywMax = 60.0;
GLfloat dnear = 25.0, dfar = 125.0;
void init (void)
{
glClearColor (1.0, 1.0, 1.0, 0.0);
//glMatrixMode(GL_MODELVIEW);
//gluLookAt(x0, y0, z0, xref, yref, zref, Vx, Vy, Vz);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(0,1,0,1, 0,0.1);
//gluOrtho2D(0, 1,0,1);
//gluPerspective(45, 1.2, 1, 10);
glFrustum(0, 1, 0, 1, 0, 1);
//gluPerspective(45.0, 1, 1, 15);
}
void displayFcn (void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.0, 1.0, 0.0);
//glPolygonMode(GL_FRONT, GL_FILL);
//glPolygonMode(GL_BACK, GL_FILL);
glBegin(GL_TRIANGLES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glVertex3f(0.5, 1.0, 0.0);
glEnd();
glFlush();
}
void reshapeFcn(GLint newWidth, GLint newHeight)
{
glViewport(0,0,newWidth, newHeight);
winWidth = newWidth;
winHeight = newHeight;
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(400,200);
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("Test");
init();
glutDisplayFunc(displayFcn);
glutReshapeFunc(reshapeFcn);
glutMainLoop();
}
This is the full source code, you can copy and paste to your VS solution and compile.
You'll need to have glut installed.
The result comes up like this:
The center of the window is 0,0 in opengl. So, when you calculate the vertices, you have to calculate them such that the center of the triangle is 0,0.
glBegin(GL_TRIANGLES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glVertex3f(0.5, 1.0, 0.0);
glEnd();
those coords will need to be updated, you can find a discussion on finding the centers of triangles at this question: finding center of 2D triangle which sounds like it's from a similar homework assignment.
glTranslatef(-0.5f, -0.5f, 0.0f);
The default perspective for OpenGL is the origin centered and the window x and y ranging from -1 to 1. You can either change this by changing the default viewing volume or changing the coordinates of your triangle.
Either
glTranslatef(-.5f, -.5f, .0f);
or
glBegin(GL_TRIANGLES);
glVertex3f(-.5, -.5, 0.0);
glVertex3f(.50, -.5, 0.0);
glVertex3f(0, .5, 0.0);
glEnd();
will work.
It looks like you're trying to use glFrustum where you want to use glOrtho
glFrustum is supposed to be used for generating a perspective matrix. zNear is never 0. The way you call it right now generates an error GL_INVALID_VALUE, so you get the default projection matrix, the identity.