OpenGL Perspective - opengl

I'm trying to depict a cube using a perspective projection, but all I get is the corner of a square. The face of the square is set at the origin and expands in the positive direction. Using glOrtho I can set the coordinate system, but I'm having trouble doing the same thing using glPerspective.
#include <gl/glut.h>
void mesh(void) {
float v[8][3] = { /* Vertices for 8 corners of a cube. */
{0.0, 0.0, 0.0}, {100.0, 0.0, 0.0}, {100.0, 100.0, 0.0}, {0.0, 100.0, 0.0},
{0.0, 0.0, -100.0}, {100.0, 0.0, -100.0}, {100.0, 100.0, -100.0}, {0.0, 100.0, -100.0} };
float n[6][3] = { /* Normals for the 6 faces of a cube. */
{0.0, 0.0, 1.0}, {1.0, 0.0, 0.0}, {0.0, 1.0, 0.0},
{-1.0, 0.0, 0.0}, {0.0, -1.0, 0.0}, {0.0, 0.0, -1.0} };
int f[6][4] = { /* Indexes of the vertices in v that make the 6 faces of a cube. */
{0, 1, 2, 3}, {1, 5, 6, 2}, {3, 2, 6, 7},
{0, 4, 7, 3}, {0, 1, 5, 4}, {4, 5, 6, 7} };
for (int j = 0; j < 6; j++) {
glBegin(GL_QUADS);
glNormal3fv(&n[j][0]);
glVertex3fv(&v[f[j][0]][0]);
glVertex3fv(&v[f[j][1]][0]);
glVertex3fv(&v[f[j][2]][0]);
glVertex3fv(&v[f[j][3]][0]);
glEnd();
glFlush();
}
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
mesh();
}
void main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB |GLUT_DEPTH |GLUT_SINGLE);
glutInitWindowSize(400, 300);
glutInitWindowPosition(200, 200);
glutCreateWindow("Mesh");
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glRotatef(15, 0.0, 0.0, 1.0);
//glOrtho(-400.0, 400.0, -300.0, 300.0, 200.0, -200.0);
gluPerspective(120,1,0,600);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glutDisplayFunc(display);
glutMainLoop();
}

You say you only see corners of the cube? Then your Field of view is too wide.. you are using gluPerspective() and providing your calculations are correct.. the values are a bit off imo, the function parameters are:
void gluPerspective(GLdouble fovy,
GLdouble aspect_ratio,
GLdouble zNear,
GLdouble zFar);
i propose changing that to something like
gluPerspective(45.0f,
width_of_window / height_of_window, //aspect ratio
0.1f,
500.0f);

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.

Rotation in X axis for object OpenGL

I need help for X axis rotation of this object.
Currently if I press the mouse left/right button, then it will rotate only y axis. 1st
2nd
3rd
4th & so on... I am currently using Codeblockson Windows 10 with open source Glut packages
In which lines I have to change for X axis rotation of that cone that chanages the cones position for x axis.
#include<windows.h>
#include<bits/stdc++.h>
#include <GL/glut.h>
#include <cmath>
#include <iostream>
#include <sstream>
#include <vector>
static int slices = 16;
static int stacks = 16;
int rotation_lock=0,smotoh_color_transition_lock=0,mouse_rotation_zoom_lock=0,i;
GLuint colorCounter = 0, angle=0;
GLfloat lightXPos = 0.0f;
GLfloat lightYPos = 0.f;
static void resize(int width, int height)
{
const float ar = (float) width / (float) height; //windows size
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION); //projection
glLoadIdentity();
glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0); //near, far value
glMatrixMode(GL_MODELVIEW);
glLoadIdentity() ;
}
static void display(void)
{
const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0; //time of camera/color change
const double a = t*90.0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslated(0,0,-6); //translate
glRotated(60,1,0,0); //rotate of angle, x,y
if (rotation_lock==1) //If auto rotate enabled
{
glRotated(a,0,0,1); //then it will rotate against
}
//glutSolidCube(1.0);
glutSolidCone(1,1,slices,stacks);
//glutSolidTeapot(1.0);
//glutSolidSphere(1.0,slices,stacks);
//glutSolidCube(1.5);
glPopMatrix();
//Color for the object
GLfloat diffColors[6][4] = {
{0.5, 0.5, 0.9, 1.0},
{0.9, 0.5, 0.5, 1.0},
{0.5, 0.9, 0.3, 1.0},
{0.9, 0.7, 0.2, 1.0},
{0.3, 0.8, 0.9, 1.0},
{1.0, 0.0, 0.0, 1.0}
}; //for c
GLfloat diffColors2[50][4] = {
{0, 0, 0.1, 1.0},
{0, 0, 0.2, 1.0},
{0, 0, 0.3, 1.0},
{0, 0, 0.4, 1.0},
{0, 0, 0.5, 1.0},
{0, 0, 0.6, 1.0},
{0, 0, 0.7, 1.0},
{0, 0, 0.8, 1.0},
{0, 0, 0.9, 1.0},
{0, 0, 1, 1.0},
{0.1, 0, 0, 1.0},
{0.2, 0, 0, 1.0},
{0.3, 0, 0, 1.0},
{0.4, 0, 0, 1.0},
{0.5, 0, 0, 1.0},
{0.6, 0, 0, 1.0},
{0.7, 0, 0, 1.0},
{0.8, 0, 0, 1.0},
{0.9, 0, 0, 1.0},
{1, 0, 0, 1.0},
{0, 0.1, 0, 1.0},
{0, 0.2, 0, 1.0},
{0, 0.3, 0, 1.0},
{0, 0.4, 0, 1.0},
{0, 0.5, 0, 1.0},
{0, 0.6, 0, 1.0},
{0, 0.7, 0, 1.0},
{0, 0.8, 0, 1.0},
{0, 0.9, 0, 1.0},
{0, 1, 0, 1.0},
{0.1, 0.1, 0, 1.0},
{0.2, 0.1, 0, 1.0},
{0.3, 0.1, 0, 1.0},
{0.4, 0.1, 0, 1.0},
{0.5, 0.1, 0, 1.0},
{0.6, 0.1, 0, 1.0},
{0.7, 0.1, 0, 1.0},
{0.8, 0.1, 0, 1.0},
{0.9, 0.1, 0, 1.0},
{1, 0.1, 0, 1.0},
{0, 0.1, 0, 1.0},
{0, 0.2, 0.1, 1.0},
{0, 0.3, 0.1, 1.0},
{0, 0.4, 0.1, 1.0},
{0, 0.5, 0.1, 1.0},
{0, 0.6, 0.1, 1.0},
{0, 0.7, 0.1, 1.0},
{0, 0.8, 0.1, 1.0},
{0, 0.9, 0.1, 1.0},
{0, 1, 0.1, 1.0}
}; //for transition
if(smotoh_color_transition_lock==1) //If auto color transition enabled
{
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffColors2[colorCounter]); //t count
}
else
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffColors[colorCounter]); // c count
// Define specular color and shininess
GLfloat specColor[] = {1.0, 1.0, 1.0, 1.0};
GLfloat shininess[] = {100.0};
// Note that the specular color and shininess can stay constant
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specColor);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
// Set light properties
// Light color (RGBA)
GLfloat Lt0diff[] = {1.0,1.0,1.0,1.0};
// Light position
GLfloat Lt0pos[] = {1.0f + lightXPos, 1.0f + lightYPos, 5.0f, 1.0f};
glLightfv(GL_LIGHT0, GL_DIFFUSE, Lt0diff);
glLightfv(GL_LIGHT0, GL_POSITION, Lt0pos); //light position change
glutSwapBuffers();
}
static void key(unsigned char key, int x, int y)
{
switch (key)
{
case 'w': //Move up
glTranslatef(0,0.1,0);
glutPostRedisplay();
break;
case 's': //Move down
glTranslatef(0,-0.1,0);
glutPostRedisplay();
break;
case 'd'://Move right
glTranslatef(0.1,0,0);
glutPostRedisplay();
break;
case 'a': //Move left
glTranslatef(-0.1,0,0);
glutPostRedisplay();
break;
case 'c': //Color toggle
smotoh_color_transition_lock=0;
colorCounter += 1;
if(colorCounter>5)
colorCounter=0;
break;
case 't': //Enable/Disable smooth color transition
smotoh_color_transition_lock=1; //c enabled
colorCounter += 1;
if(colorCounter>49) //
colorCounter=0;
break;
case 'r': //Enable/Disable auto rotation
if(rotation_lock==0)
rotation_lock=1;
else
rotation_lock=0;
break;
case 'x': //Switch between rotation or zoom using mouse action
if(mouse_rotation_zoom_lock==0)
mouse_rotation_zoom_lock=1;
else
mouse_rotation_zoom_lock=0;
break;
case 27 : //exit when ESC is pressed
case 'q': //exit when q is pressed
exit(0);
break;
default:
printf("Unhandled key press %c\n",key);
}
glutPostRedisplay();
}
void specialFunc( int key, int x, int y )
{
switch ( key )
{
case GLUT_KEY_UP: //UP key to move the light source UP
lightYPos += 0.5f; //ligh post change
break;
case GLUT_KEY_DOWN: //DOWN key to move the light source DOWN
lightYPos += -0.5f;
break;
case GLUT_KEY_LEFT: //LEFT key to move the light source LEFT
lightXPos += -0.5f;
break;
case GLUT_KEY_RIGHT: //RIGHT key to move the light source RIGHT
lightXPos += 0.5f;
break;
}
// this will refresh the screen so that the user sees the light position
glutPostRedisplay();
}
void mouse(int button, int state, int mousex, int mousey) //mouse function
{
if (mouse_rotation_zoom_lock==1) // If zoom enabled //x chap dile
{
if(button==GLUT_LEFT_BUTTON && state==GLUT_UP)
{
glScalef(1.2,1.2,1); //zoom in
}
else if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
{
glScalef(0.7,0.7,1); //zoom out
}
}
else // If rotation enabled // x na chaple
{
if(button==GLUT_LEFT_BUTTON && state==GLUT_UP)
{
glRotatef(25,0,0,1); //rotate
}
else if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
{
glRotatef(-25,0,0,1);
}
}
glutPostRedisplay();
}
static void idle(void)
{
glutPostRedisplay();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(500,500);
glutInitWindowPosition(250,250);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("OpenGL");
glutReshapeFunc(resize);
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutSpecialFunc(specialFunc);
glutMouseFunc(mouse);
glutIdleFunc(idle);
//glClearColor(1,1,1,1);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_LIGHTING);
glutMainLoop();
return EXIT_SUCCESS;
}

How to change colors in OpenGL?

I want to color every part of the coordinate plane in different color (y in red, x in blue, z in green) but it seems that he remembers only the color of the lighting(red). So if someone can help me with that problem, i would be grateful.
#include <GL/glut.h>
//func
void setCamera();
void drawCP();
void drawPoints();
void drawCrtice();
GLfloat light_diffuse[] = {1.0, 0.0, .0, 0.0}; /* Red diffuse light. */
GLfloat light_position[] = {0.0, 0.0, 1.0, 0.0}; /* Infinite light location. */
GLfloat n[6][3] = {/* Normals for the 6 faces of a cube. */
{-1.0, 0.0, 0.0},
{0.0, 1.0, 0.0},
{1.0, 0.0, 0.0},
{0.0, -1.0, 0.0},
{0.0, 0.0, 1.0},
{0.0, 0.0, -1.0}
};
GLint faces[6][4] = {/* Vertex indices for the 6 faces of a cube. */
{0, 1, 2, 3},
{3, 2, 6, 7},
{7, 6, 5, 4},
{4, 5, 1, 0},
{5, 6, 2, 1},
{7, 4, 0, 3}
};
GLfloat v[8][3]; /* Will be filled in with X,Y,Z vertexes. */
void
drawBox(void) {
int i;
for (i = 0; i < 6; i++) {
glBegin(GL_QUADS);
glNormal3fv(&n[i][0]);
glVertex3fv(&v[faces[i][0]][0]);
glVertex3fv(&v[faces[i][1]][0]);
glVertex3fv(&v[faces[i][2]][0]);
glVertex3fv(&v[faces[i][3]][0]);
glEnd();
}
}
void
display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawBox();
drawCP();
glClearColor(0.6f, 0.6f, 0.6f, 1.0f);
glRotatef(0.2, 0, 5, 0);
glutPostRedisplay();
glutSwapBuffers();}
void
init(void) {
/* Setup cube vertex data. */
v[0][0] = v[1][0] = v[2][0] = v[3][0] = -1;
v[4][0] = v[5][0] = v[6][0] = v[7][0] = 1;
v[0][1] = v[1][1] = v[4][1] = v[5][1] = -1;
v[2][1] = v[3][1] = v[6][1] = v[7][1] = 1;
v[0][2] = v[3][2] = v[4][2] = v[7][2] = 1;
v[1][2] = v[2][2] = v[5][2] = v[6][2] = -1;
/* Enable a single OpenGL light. */
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
/* Use depth buffering for hidden surface elimination. */
glEnable(GL_DEPTH_TEST);
setCamera();}
void drawLines() {
glBegin(GL_LINES);
for (int i = 10; i != 0; i--) {
// po x osi
glVertex2f(-i, -0.1);
glVertex2f(-i, 0.1);
glVertex2f(i, -0.1);
glVertex2f(i, 0.1);
//po y osi
glVertex2f(-0.1, -i);
glVertex2f(0.1, -i);
glVertex2f(-0.1, i);
glVertex2f(0.1, i);
//po z osi
glColor3f(1.0f, 0.0f, 0.0f); //seems it doesnt register
glVertex3f(0, -0.3, -i);
glVertex3f(0, 0.3, -i);
glVertex3f(0, -0.3, i);
glVertex3f(0, 0.3, i);}
glEnd();}
void drawCP() {
//drawPoints();
drawLines();
glBegin(GL_LINES);
glLineWidth(100);
glVertex2f(-10.0f, 0);
glVertex2f(10.0f, 0);
glVertex2f(0.0f, 10);
glVertex2f(0.0f, -10);
glEnd();
}
void setCamera() {
/* Setup the view of the cube. */
glMatrixMode(GL_PROJECTION);
gluPerspective(/* field of view in degree */ 50.0, /* aspect ratio */ 1, /* Z near */ 1, /* Z far */ 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/* Setup the view of the cube. */
gluLookAt(0, 0, 10 /*pozicija kamere*/, 0, 0, 0/* u koju tocku gleda ta kamera*/, 0, 1, 0 /*moran jos viti ca je to*/);
}
int
main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("red 3D lighted cube");
glutDisplayFunc(display);
init();
// glutTimerFunc(10, rotate, 10);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
Try adding in: glEnable(GL_COLOR_MATERIAL);. That tells OpenGL your material properties are going to be defined with the glColor() function, it can be expanded on but that should get you what you expect.

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.

OpenGL antialiasing not working

I'v been trying to anti alias with OGL. I found a code chunk that is supposed to do this but I see no antialiasing. I also reset my settings in Nvidia Control Panel but no luck.
Does this code in fact antialias the cube?
GLboolean polySmooth = GL_TRUE;
static void init(void)
{
glCullFace (GL_BACK);
glEnable (GL_CULL_FACE);
glBlendFunc (GL_SRC_ALPHA_SATURATE, GL_ONE);
glClearColor (0.0, 0.0, 0.0, 0.0);
}
#define NFACE 6
#define NVERT 8
void drawCube(GLdouble x0, GLdouble x1, GLdouble y0,
GLdouble y1, GLdouble z0, GLdouble z1)
{
static GLfloat v[8][3];
static GLfloat c[8][4] = {
{0.0, 0.0, 0.0, 1.0}, {1.0, 0.0, 0.0, 1.0},
{0.0, 1.0, 0.0, 1.0}, {1.0, 1.0, 0.0, 1.0},
{0.0, 0.0, 1.0, 1.0}, {1.0, 0.0, 1.0, 1.0},
{0.0, 1.0, 1.0, 1.0}, {1.0, 1.0, 1.0, 1.0}
};
/* indices of front, top, left, bottom, right, back faces */
static GLubyte indices[NFACE][4] = {
{4, 5, 6, 7}, {2, 3, 7, 6}, {0, 4, 7, 3},
{0, 1, 5, 4}, {1, 5, 6, 2}, {0, 3, 2, 1}
};
v[0][0] = v[3][0] = v[4][0] = v[7][0] = x0;
v[1][0] = v[2][0] = v[5][0] = v[6][0] = x1;
v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
v[0][2] = v[1][2] = v[2][2] = v[3][2] = z0;
v[4][2] = v[5][2] = v[6][2] = v[7][2] = z1;
#ifdef GL_VERSION_1_1
glEnableClientState (GL_VERTEX_ARRAY);
glEnableClientState (GL_COLOR_ARRAY);
glVertexPointer (3, GL_FLOAT, 0, v);
glColorPointer (4, GL_FLOAT, 0, c);
glDrawElements(GL_QUADS, NFACE*4, GL_UNSIGNED_BYTE, indices);
glDisableClientState (GL_VERTEX_ARRAY);
glDisableClientState (GL_COLOR_ARRAY);
#else
printf ("If this is GL Version 1.0, ");
printf ("vertex arrays are not supported.\n");
exit(1);
#endif
}
/* Note: polygons must be drawn from front to back
* for proper blending.
*/
void display(void)
{
if (polySmooth) {
glClear (GL_COLOR_BUFFER_BIT);
glEnable (GL_BLEND);
glEnable (GL_POLYGON_SMOOTH);
glDisable (GL_DEPTH_TEST);
}
else {
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable (GL_BLEND);
glDisable (GL_POLYGON_SMOOTH);
glEnable (GL_DEPTH_TEST);
}
glPushMatrix ();
glTranslatef (0.0, 0.0, -8.0);
glRotatef (30.0, 1.0, 0.0, 0.0);
glRotatef (60.0, 0.0, 1.0, 0.0);
drawCube(-0.5, 0.5, -0.5, 0.5, -0.5, 0.5);
glPopMatrix ();
glFlush ();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 't':
case 'T':
polySmooth = !polySmooth;
glutPostRedisplay();
break;
case 27:
exit(0); /* Escape key */
break;
default:
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB
| GLUT_ALPHA | GLUT_DEPTH);
glutInitWindowSize(200, 200);
glutCreateWindow(argv[0]);
init ();
glutReshapeFunc (reshape);
glutKeyboardFunc (keyboard);
glutDisplayFunc (display);
glutMainLoop();
return 0;
}
Thanks
You should really consider to
find a better (read "up to date") source of information than the redbook
use multisampling/supersampling instead of the almost-deprecated GL_POLYGON_SMOOTH
On your problem:
What is your graphic card ?
Which version of OpenGL are you using ?
Are you depth sorting all your polygons ?
Have you tried to supply GLUT_MULTISAMPLE to the glutInitDisplayMode(..) call? It's not sure your glut implementation supports it though.