OpenGL _ Front objects are covered with Back objects. so can't see it - c++

The problem is, 'Objects on the table is covered with table board, so can't see it.'
( I using openGL 3.7 beta. Files that I installed is : http://ihoo1836.dothome.co.kr/opengl_vs2010+glutdlls37beta.zip )
All Codes are following.
#include<glut.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
float TableX = 5.0; //Table's X size
float TableY = 8.0; //Table's Y size
float TableHeight = 2.0;//Table's Height
int width=400, height=400; //Window Size
int ViewX = width/2; //for Change Viewpoint by Mouse position
int ViewY = height/2;
int ViewZ = 9;
GLUquadricObj* cyl;
void InitLight( ){
glEnable(GL_DEPTH_TEST); //for opaque
glEnable(GL_NORMALIZE); //normalize
glEnable(GL_SMOOTH); //for smooth color
glEnable(GL_LIGHTING); //light setting
glDepthMask(GL_TRUE);
GLfloat ambientLight[] = {0.3f, 0.3f, 0.3f, 1.0f};
GLfloat diffuseLight[] = {0.7f, 0.7f, 0.7f, 1.0f};
GLfloat specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat specref[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat position[]={400.0, 300.0, -700.0, 1.0};
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMateriali(GL_FRONT, GL_SHININESS, 128);
}
//Get Mouse Position to Change ViewPoint
void MyMouseMove(int button, int state, GLint X, GLint Y)
{
//Get Mouse Position X, Y
ViewX = X;
ViewY = Y;
glutPostRedisplay();
}
//Get Mouse Position to Change ViewPoint
void MyMotion(GLint X, GLint Y)
{
//Get Mouse Position X, Y
ViewX = X;
ViewY = Y;
glutPostRedisplay();
}
//Draw Table
void DrawTable(){
glPushMatrix();
glTranslatef(0.0,0.0,1.0);
glColor3f(0.5, 0.25, 0.0);
cyl = gluNewQuadric();
glRotatef(-90,1.0,0.0,0.0);
gluCylinder(cyl, 0.2, 0.2, TableHeight, 10, 20); //Leg of Table 1
glPushMatrix();
cyl = gluNewQuadric();
glTranslatef(TableX,0.0,0.0);
gluCylinder(cyl, 0.2, 0.2, TableHeight, 10, 20); //Leg of Table 2
glPushMatrix();
cyl = gluNewQuadric();
glTranslatef(0.0, TableY, 0.0);
gluCylinder(cyl, 0.2, 0.2, TableHeight, 10, 20); //Leg of Table 3
glPushMatrix();
cyl = gluNewQuadric();
glTranslatef(-TableX,0.0,0.0);
gluCylinder(cyl, 0.2, 0.2, TableHeight, 10, 20); //Leg of Table 4
glPushMatrix();
glTranslatef(TableX/2.0, -TableY/2, TableHeight);
glScalef(TableX+0.5, TableY+0.5, 0.5);
glutSolidCube(1); //Board of Table
glPopMatrix();
glPushMatrix(); //triangular1 (Beside of Net)
glTranslatef(0, -TableY/2, TableHeight);
glBegin(GL_TRIANGLES);
glVertex3f(TableY/16.0, 0, TableY/8.0);//1
glVertex3f(0, TableY/8.0, 0);
glVertex3f(0, -TableY/8.0, 0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//2
glVertex3f(0, -TableY/8.0, 0);
glVertex3f(TableY/16.0, -TableY/8.0, 0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//3
glVertex3f(TableY/16.0, -TableY/8.0, 0);
glVertex3f(TableY/16.0, TableY/8.0, 0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//4
glVertex3f(TableY/16.0, TableY/8.0, 0);
glVertex3f(0, TableY/8.0, 0);
glEnd();
glPushMatrix(); //triangular2 (Beside of Net)
glTranslatef(TableX - TableY/8.0, 0 , 0);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//1
glColor3f(0.0, 1.0, 0.0);
glVertex3f(TableY/16.0, TableY/8.0, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(TableY/16.0, -TableY/8.0, 0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//2
glColor3f(0.0, 1.0, 0.0);
glVertex3f(TableY/16.0, -TableY/8.0, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(TableY/8.0, -TableY/8.0, 0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//3
glColor3f(0.0, 1.0, 0.0);
glVertex3f(TableY/8.0, -TableY/8.0, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(TableY/8.0, TableY/8.0, 0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//4
glColor3f(0.0, 1.0, 0.0);
glVertex3f(TableY/8.0, TableY/8.0, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(TableY/16.0, TableY/8.0, 0);
glEnd();
glPopMatrix();
glPushMatrix(); //Net
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(TableY/16.0, 0.0, TableY/8.0);
glVertex3f((TableX - TableY/16.0), 0, TableY/8.0);
glVertex3f((TableX - TableY/16.0), 0, 0);
glVertex3f(TableY/16.0, 0.0, 0.0);
glEnd();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
}
//Display Callback Function
void MyDisplay( ){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity( );
gluPerspective(60.0, (GLfloat)width/height, 0.0, 10.0);
//Change Viewpoint by Mouse Position
gluLookAt((float)(ViewX - width/2)/width*20 + 2.5, (float)(height/2 - ViewY)/height*20 + 2.5, ViewZ, TableX/2, TableY/2, TableHeight, 0.0, 1.0, 0.0);
printf("eyex = %f , eyey = %f , eyez = %f \n",(float)(ViewX - width/2.0)/width*10, (float)(height/2 - ViewY)/height*10, (float)ViewZ);
DrawTable(); //Draw Table
glutSwapBuffers(); //for 'Double Buffering'
}
//for Reshape Window
void MyReshape (int w, int h){
width = w;
height = h;
printf("width = %d, height = %d \n", width, height);
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity( );
glOrtho (-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
}
//Main Function
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(width, height);
glutInitWindowPosition(200, 200);
glutCreateWindow("OpenGL Sample Drawing");
glClearColor(0.4, 0.4, 0.4, 1.0);
InitLight(); //set Light Setting
glutDisplayFunc(MyDisplay);
glutMouseFunc(MyMouseMove); //get Mouse Position, to Change Viewpoint
glutMotionFunc(MyMotion); //get Mouse Position, to Change Viewpoint
glutReshapeFunc(MyReshape);
glutMainLoop( );
}

The third argument to gluPerspective() should be non-zero, positive, and less than the forth argument:
#include <GL/glut.h>
#include <stdio.h>
float TableX = 5.0; //Table's X size
float TableY = 8.0; //Table's Y size
float TableHeight = 2.0;//Table's Height
int ViewX = 400/2; //for Change Viewpoint by Mouse position
int ViewY = 400/2;
int ViewZ = 9;
GLUquadricObj* cyl;
void InitLight( ){
glEnable(GL_DEPTH_TEST); //for opaque
glEnable(GL_NORMALIZE); //normalize
glEnable(GL_SMOOTH); //for smooth color
glEnable(GL_LIGHTING); //light setting
glDepthMask(GL_TRUE);
GLfloat ambientLight[] = {0.3f, 0.3f, 0.3f, 1.0f};
GLfloat diffuseLight[] = {0.7f, 0.7f, 0.7f, 1.0f};
GLfloat specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat specref[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat position[]={400.0, 300.0, -700.0, 1.0};
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMateriali(GL_FRONT, GL_SHININESS, 128);
}
//Get Mouse Position to Change ViewPoint
void MyMouseMove(int button, int state, GLint X, GLint Y)
{
//Get Mouse Position X, Y
ViewX = X;
ViewY = Y;
glutPostRedisplay();
}
//Get Mouse Position to Change ViewPoint
void MyMotion(GLint X, GLint Y)
{
//Get Mouse Position X, Y
ViewX = X;
ViewY = Y;
glutPostRedisplay();
}
//Draw Table
void DrawTable(){
glPushMatrix();
glTranslatef(0.0,0.0,1.0);
glColor3f(0.5, 0.25, 0.0);
cyl = gluNewQuadric();
glRotatef(-90,1.0,0.0,0.0);
gluCylinder(cyl, 0.2, 0.2, TableHeight, 10, 20); //Leg of Table 1
glPushMatrix();
cyl = gluNewQuadric();
glTranslatef(TableX,0.0,0.0);
gluCylinder(cyl, 0.2, 0.2, TableHeight, 10, 20); //Leg of Table 2
glPushMatrix();
cyl = gluNewQuadric();
glTranslatef(0.0, TableY, 0.0);
gluCylinder(cyl, 0.2, 0.2, TableHeight, 10, 20); //Leg of Table 3
glPushMatrix();
cyl = gluNewQuadric();
glTranslatef(-TableX,0.0,0.0);
gluCylinder(cyl, 0.2, 0.2, TableHeight, 10, 20); //Leg of Table 4
glPushMatrix();
glTranslatef(TableX/2.0, -TableY/2, TableHeight);
glScalef(TableX+0.5, TableY+0.5, 0.5);
glutSolidCube(1); //Board of Table
glPopMatrix();
glPushMatrix(); //triangular1 (Beside of Net)
glTranslatef(0, -TableY/2, TableHeight);
glBegin(GL_TRIANGLES);
glVertex3f(TableY/16.0, 0, TableY/8.0);//1
glVertex3f(0, TableY/8.0, 0);
glVertex3f(0, -TableY/8.0, 0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//2
glVertex3f(0, -TableY/8.0, 0);
glVertex3f(TableY/16.0, -TableY/8.0, 0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//3
glVertex3f(TableY/16.0, -TableY/8.0, 0);
glVertex3f(TableY/16.0, TableY/8.0, 0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//4
glVertex3f(TableY/16.0, TableY/8.0, 0);
glVertex3f(0, TableY/8.0, 0);
glEnd();
glPushMatrix(); //triangular2 (Beside of Net)
glTranslatef(TableX - TableY/8.0, 0 , 0);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//1
glColor3f(0.0, 1.0, 0.0);
glVertex3f(TableY/16.0, TableY/8.0, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(TableY/16.0, -TableY/8.0, 0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//2
glColor3f(0.0, 1.0, 0.0);
glVertex3f(TableY/16.0, -TableY/8.0, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(TableY/8.0, -TableY/8.0, 0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//3
glColor3f(0.0, 1.0, 0.0);
glVertex3f(TableY/8.0, -TableY/8.0, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(TableY/8.0, TableY/8.0, 0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(TableY/16.0, 0, TableY/8.0);//4
glColor3f(0.0, 1.0, 0.0);
glVertex3f(TableY/8.0, TableY/8.0, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(TableY/16.0, TableY/8.0, 0);
glEnd();
glPopMatrix();
glPushMatrix(); //Net
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(TableY/16.0, 0.0, TableY/8.0);
glVertex3f((TableX - TableY/16.0), 0, TableY/8.0);
glVertex3f((TableX - TableY/16.0), 0, 0);
glVertex3f(TableY/16.0, 0.0, 0.0);
glEnd();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
}
//Display Callback Function
void MyDisplay( ){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode (GL_PROJECTION);
glLoadIdentity( );
double width = glutGet( GLUT_WINDOW_WIDTH );
double height = glutGet( GLUT_WINDOW_HEIGHT );
gluPerspective(60.0, (GLfloat)width/height, 0.01, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity( );
//Change Viewpoint by Mouse Position
gluLookAt((float)(ViewX - width/2)/width*20 + 2.5, (float)(height/2 - ViewY)/height*20 + 2.5, ViewZ, TableX/2, TableY/2, TableHeight, 0.0, 1.0, 0.0);
printf("eyex = %f , eyey = %f , eyez = %f \n",(float)(ViewX - width/2.0)/width*10, (float)(height/2 - ViewY)/height*10, (float)ViewZ);
DrawTable(); //Draw Table
glutSwapBuffers(); //for 'Double Buffering'
}
//Main Function
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(400, 400);
glutInitWindowPosition(200, 200);
glutCreateWindow("OpenGL Sample Drawing");
glClearColor(0.4, 0.4, 0.4, 1.0);
InitLight(); //set Light Setting
glEnable( GL_DEPTH_TEST );
glutDisplayFunc(MyDisplay);
glutMouseFunc(MyMouseMove); //get Mouse Position, to Change Viewpoint
glutMotionFunc(MyMotion); //get Mouse Position, to Change Viewpoint
glutMainLoop( );
}

Related

Texture applied through OpenGL isn't visible when rendering + add image directly in question

I tried to apply a checkerboard styled texture to the model below here using GLubyte and glTexImage2D along with glTexCoord2f.
I previously applied a material to it that works perfectly, but for some reasons my texture won't show up at all. I can't seem to see or figure out why because looking at the examples I'm following everything should be working perfectly.
What I should have
#include <glut.h>
float angle[4];
float LightAngle;
bool LowerFrontLegDown = true;
bool LowerBackLegDown = true;
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} };
//Two Dimensional Array for corners
GLfloat normals[][3] = { {0.0,0.0,1.0},
{1.0,0.0,0.0},
{0.0,-1.0,0.0},
{0.0,1.0,0.0},
{0.0,0.0,-1.0},
{-1.0,0.0,0.0} };
typedef struct materialStruct {
GLfloat ambient[4];
GLfloat diffuse[4];
GLfloat specular[4];
GLfloat shininess;
};
materialStruct brassMaterial = {
{ 0.33, 0.22, 0.03, 1.00 },
{ 0.78, 0.57, 0.11, 1.00 },
{ 0.99, 0.91, 0.81, 1.00 },
27.80 };
materialStruct redPlasticMaterial = {
{ 0.30, 0.00, 0.00, 1.00 },
{ 0.60, 0.00, 0.00, 1.00 },
{ 0.80, 0.60, 0.60, 1.00 },
32.00 };
materialStruct* currentMaterial;
void setMaterial(materialStruct* materials) {
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, materials->ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, materials->diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, materials->specular);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, materials->shininess);
}
void drawFace(int a, int b, int c, int d) {
glBegin(GL_POLYGON);
glTexCoord2f(0.0, 0.0);
glVertex3fv(corners[a]);
glTexCoord2f(0.0, 1.0);
glVertex3fv(corners[b]);
glTexCoord2f(1.0, 1.0);
glVertex3fv(corners[c]);
glTexCoord2f(1.0, 0.0);
glVertex3fv(corners[d]);
glEnd();
} //Turns the corners from the two dimensional array into corner pieces for the model, allowing faces to be drawn
void ArrayCube() {
glNormal3fv(normals[0]);
drawFace(0, 3, 2, 1);
glNormal3fv(normals[1]);
drawFace(3, 0, 4, 7);
glNormal3fv(normals[2]);
drawFace(2, 3, 7, 6);
glNormal3fv(normals[3]);
drawFace(1, 2, 6, 5);
glNormal3fv(normals[4]);
drawFace(4, 5, 6, 7);
glNormal3fv(normals[5]);
drawFace(5, 4, 0, 1);
}
//Draws the faces of the model and creates a cube we can call later for the individual parts of the model.
void rotate() {
angle[0] += 1.0;
if (angle[0] > 360) angle[0] -= 360;
if (LowerFrontLegDown) angle[1] -= 0.2;
else angle[1] += 0.2;
if (angle[1] < 315) LowerFrontLegDown = false;
if (angle[1] > 360) LowerFrontLegDown = true;
angle[0] += 1.0;
if (angle[0] > 360) angle[0] -= 360;
if (LowerBackLegDown) angle[1] -= 0.2;
else angle[1] += 0.2;
if (angle[1] < 315) LowerBackLegDown = false;
if (angle[1] > 360) LowerBackLegDown = true;
glutPostRedisplay();
}
void MainBody()
{
glPushMatrix();
glScalef(1.25, 0.25, 0.5);
ArrayCube();
glPopMatrix();
}
void LowerNeck()
{
glPushMatrix();
glTranslatef(0.5, 0.25, 0);
glScalef(0.15, 0.5, 0.15);
ArrayCube();
glPopMatrix();
}
void UpperNeck()
{
glPushMatrix();
glTranslatef(0.5, 0.75, 0);
glScalef(0.15, 0.5, 0.15);
ArrayCube();
glPopMatrix();
}
void Head()
{
glPushMatrix();
glRotatef(90, 0.0, 0.0,1.0);
glTranslatef(1, -0.6, 0);
glScalef(0.1, 0.4, 0.15);
ArrayCube();
glPopMatrix();
}
void RightHorn()
{
glPushMatrix();
glRotatef(0, 0.0, 0.0,1);
glTranslatef(0.5, 1.15, 0.035);
glScalef(0.05, 0.15, 0.05);
ArrayCube();
glPopMatrix();
}
void LeftHorn()
{
glPushMatrix();
glRotatef(0, 0.0, 0.0, 1);
glTranslatef(0.5, 1.15, -0.035);
glScalef(0.05, 0.15, 0.05);
ArrayCube();
glPopMatrix();
}
void FrontUpperRightLeg()
{
glPushMatrix();
glTranslatef(0.5, -0.35, 0.15);
glScalef(0.15, 0.5, 0.15);
ArrayCube();
glPopMatrix();
}
void FrontLowerRightLeg()
{
glPushMatrix();
glTranslatef(0.5, -0.75, 0.15);
glRotatef(angle[1], 0.0, 0.0, 1.0);
glScalef(0.15, 0.5, 0.15);
ArrayCube();
glPopMatrix();
}
void FrontUpperLeftLeg()
{
glPushMatrix();
glTranslatef(0.5, -0.35, -0.15);
glScalef(0.15, 0.5, 0.15);
ArrayCube();
glPopMatrix();
}
void FrontLowerLeftLeg()
{
glPushMatrix();
glTranslatef(0.5, -0.75, -0.15);
glRotatef(angle[1], 0.0, 0.0, 1.0);
glScalef(0.15, 0.5, 0.15);
ArrayCube();
glPopMatrix();
}
void BackUpperRightLeg()
{
glPushMatrix();
glTranslatef(-0.5, -0.35, -0.15);
glScalef(0.15, 0.5, 0.15);
ArrayCube();
glPopMatrix();
}
void BackLowerRightLeg()
{
glPushMatrix();
glTranslatef(-0.5, -0.75, -0.15);
glRotatef(angle[1], 0.0, 0.0, 1.0);
glScalef(0.15, 0.5, 0.15);
ArrayCube();
glPopMatrix();
}
void BackUpperLeftLeg()
{
glPushMatrix();
glTranslatef(-0.5, -0.35, 0.15);
glScalef(0.15, 0.5, 0.15);
ArrayCube();
glPopMatrix();
}
void BackLowerLeftLeg()
{
glPushMatrix();
glTranslatef(-0.5, -0.75, 0.15);
glRotatef(angle[1], 0.0, 0.0, 1.0);
glScalef(0.15, 0.5, 0.15);
ArrayCube();
glPopMatrix();
}
void Tail()
{
glPushMatrix();
glTranslatef(-0.65, -0.25, 0);
glScalef(0.05, 0.75, 0.05);
ArrayCube();
glPopMatrix();
}
//Each of the below functions draws an individual part of the whole model and places those parts where they need to go once the program runs
void DrawGiraffe()
{
MainBody();
LowerNeck();
UpperNeck();
Head();
RightHorn();
LeftHorn();
FrontUpperRightLeg();
FrontLowerRightLeg();
FrontUpperLeftLeg();
FrontLowerLeftLeg();
BackUpperRightLeg();
BackLowerRightLeg();
BackUpperLeftLeg();
BackLowerLeftLeg();
Tail();
}
//Calls the above functions to render the final model
//The rotate function allows the camera to rotate around the model
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_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);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
currentMaterial = &redPlasticMaterial;
setMaterial(currentMaterial);
glColor3f(1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 2.5);
GLfloat light_pos[] = {2.0,2.0,2.0, 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
}
int main(int argc, char** argv)
{
angle[0] = 0;
angle[1] = 360;
angle[2] = 315;
angle[3] = 0;
GLubyte image[64][64][3];
int i, j, r, c;
for(i = 0;i < 64; i++){
for(j = 0;j < 64; j++){
c = ((((i&0x8)== 0)^((j&0x8))==0))*255;
image[i][j][0] = (GLubyte) c;
image[i][j][1] = (GLubyte) c;
image[i][j][2] = (GLubyte) c;
}
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Giraffe");
glutDisplayFunc(display);
glutIdleFunc(rotate);
init();
glutMainLoop();
}
There are many things wrong with your code:
You are using glTexParameterf instead of glTexParameteri.
You are not binding a texture object, which means that calls to your glTexImage2D function will be useless, because glTexImage2D relies on the bounded texture of GL_TEXTURE_2D.
You are initializing the texture before you call the glut initialization functions. This means that any calls to OpenGL functions will be useless.
You should initialize your texture with something like this
// this should be declared as a global variable
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
GLubyte image[64][64][3];
int i, j, r, c;
for(i = 0;i < 64; i++){
for(j = 0;j < 64; j++){
c = ((((i&0x8)== 0)^((j&0x8))==0))*255;
image[i][j][0] = (GLubyte) c;
image[i][j][1] = (GLubyte) c;
image[i][j][2] = (GLubyte) c;
}
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
and move the initialization of the texture to the init function, just after the call to glEnable(GL_TEXTURE_2D);
Of course, you will need to bind this texture when you render by calling glBindTexture(GL_TEXTURE_2D, texture);

glColor3f not coding in List background openGL

Currently creating a small animation of Neptune and its moon's revolving around the sun. With some help I managed to get a background of stars but instead of being white, they're now black. I've tried putting glColor3f(1.0, 1.0, 1.0) inside and outside of the matrix consisting the background and none of it seems to be working. Any solutions?
Background declaration: Display()
Background call: End of Display()
int triton = 0;
int proteus = 0;
int neptune = 0;
int sun = 0;
GLint buf, sbuf;
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
// Kind of Useless rn
glGetIntegerv(GL_SAMPLE_BUFFERS, &buf);
printf("Number of sample Buffers: %d\n", buf);
glGetIntegerv(GL_SAMPLES, &sbuf);
printf("Number of samples: %d\n", sbuf);
printf("Controls: \n A = Orbit Left. \n D = Orbit Right. \n S = Stop. \n (,) = Move Left. \n (.) = Move right.");
glShadeModel(GL_SMOOTH);
// Material Specs
GLfloat mat_specular[] = { 0.8, 0.8, 0.9, 0.1 };
GLfloat mat_shininess[] = { 40.0 };
GLfloat lightDiffuse[] = { 1.0, 1.0, 1.0, 0.8 };
GLfloat lmodel_ambient[] = { 0.1, 0.2, 0.7, 0.0 };
// Light 0 Initialized.
GLfloat light0[] = { 1.0, 1.0, 1.0, 0.9 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
// Spotlight (Sun)
// Mat Specs Implmentations.
glMaterialfv(GL_FRONT, GL_DIFFUSE, lightDiffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
// Light 0 implementation
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0);
glLightfv(GL_LIGHT0, GL_SPECULAR, light0);
// Ambient surrounding light on object.
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
// Antialias 3D Shape (In Progress)
/*glEnable(GL_BLEND);
glEnable(GL_POLYGON_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);*/
// Enable Lighting and Depth
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
void orbit(void)
{
triton = (triton - 2) % 360;
proteus = (proteus - 5) % 360;
neptune = (neptune - 1) % 360;
glutPostRedisplay();
}
void backorbit(void)
{
triton = (triton + 2) % 360;
proteus = (proteus + 5) % 360;
neptune = (neptune + 1) % 360;
glutPostRedisplay();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_COLOR_MATERIAL);
glPushMatrix();
glNewList(1, GL_COMPILE);
glBegin(GL_POINTS);
glColor3f(1.0, 1.0, 1.0);
for (int i = 0; i < 300; i++)
{
for (int j = 0; j < 300; j++)
{
if (((i + j) % 2) == 0)
{
glVertex2f(30 * i, 30 * j);
}
}
}
glEnd();
glEndList();
// Sun
glPushMatrix();
glColor3f(1.0, 0.35, 0.1);
glTranslatef(0.0, 0.0, 0.0);
glutSolidSphere(2.0, 100, 100);
// Neptune
glPushMatrix();
glRotatef((GLfloat)neptune, 0.0, 1.0, 0.0);
glTranslatef(3.0, 0.0, 0.0);
glColor3f(0.1, 0.1, 0.3);
glutSolidSphere(0.3, 100, 100);
// Triton
glPushMatrix();
glColor3f(0.85, 0.7, 0.8);
glRotatef((GLfloat)triton, 1.0, 1.0, 1.0);
glTranslatef(1.0, 0.0, 0.0);
glutSolidSphere(0.05, 100, 100);
glPopMatrix(); // Ends Triton
// Proteus
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glRotatef((GLfloat)proteus, 0.0, 1.0, 0.0);
glTranslatef(1.0, 0.0, 0.0);
glutSolidSphere(0.02, 100, 100);
glPopMatrix(); // Ends Proteus
glPopMatrix(); // Ends Neptune
glPopMatrix(); // Ends Sun
glEnable(GL_MULTISAMPLE);
// Stars
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, 1000, 0, 1000);
glColor3f(1.0, 1.0, 1.0);
glCallList(1);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glPopMatrix();
glDisable(GL_COLOR_MATERIAL);
glPopMatrix(); // Ends Solar System
glFlush();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
// Triton + Proteus Orbit.
case 'a':
glutIdleFunc(orbit);
break;
case 'd':
glutIdleFunc(backorbit);
break;
case 's':
glutIdleFunc(NULL);
break;
case ',':
glTranslatef(-0.3, 0.0, 0.0);
glutPostRedisplay();
break;
case '.':
glTranslatef(0.3, 0.0, 0.0);
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default:
break;
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB | GLUT_MULTISAMPLE);
glutInitWindowSize(1000, 1000);
glutInitWindowPosition(100, 100);
glutCreateWindow("Neptune and Space");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

MultiSampling Background Not Displaying

I'm trying to illustrate orbits of Triton and Proteus around Neptune with a background of stars. I decided to sketch a template background by multisampling an array of white dots ( stars ) as the background. Could someone explain why my array isn't displaying?
*Background is called in Display() created in Init().
Full Code here:
int triton = 0;
int proteus = 0;
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
// Material Specs
GLfloat mat_specular[] = { 0.8, 0.8, 0.9, 0.1 };
GLfloat mat_shininess[] = { 40.0 };
GLfloat lightDiffuse[] = { 1.0, 1.0, 1.0, 0.8 };
GLfloat lmodel_ambient[] = { 0.1, 0.2, 0.7, 0.0 };
// Light 0 Initialized.
GLfloat light0[] = { 1.0, 1.0, 1.0, 0.9 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
// Mat Specs Implmentations.
glMaterialfv(GL_FRONT, GL_DIFFUSE, lightDiffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
// Light 0 implementation
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0);
glLightfv(GL_LIGHT0, GL_SPECULAR, light0);
// Ambient surrounding light on object.
//glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
// Enable Lighting and Depth
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
// Background (Stars: In Progress)
glNewList(1, GL_COMPILE);
glBegin(GL_POINTS);
glColor3f(1.0, 1.0, 1.0);
for (int i = 0; i < 1000; i++)
{
for (int j = 0; j < 1000; j++)
{
if (((i + j) % 2) == 0)
{
glVertex2f(2*i, 2*j);
}
}
}
glEnd();
glEndList();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
// Neptune
glColor3f(0.1, 0.1, 0.3);
glutSolidSphere(2.0, 100, 100);
// Triton
glPushMatrix();
glColor3f(0.9, 0.7, 0.8);
glRotatef((GLfloat)triton, 1.0, 1.0, 1.0);
glTranslatef(2.5, 0.0, 0.0);
glRotatef((GLfloat)triton, 1.0, 0.0, 0.0);
glutSolidSphere(0.35, 100, 100);
glPopMatrix();
// Proteus
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glRotatef((GLfloat)proteus, 0.7, -0.4, 1.0);
glTranslatef(3.5, 0.0, 0.0);
glRotatef((GLfloat)proteus, 1.0, 0.0, 0.0);
glutSolidSphere(0.1, 100, 100);
glPopMatrix();
// Stars Background
glEnable(GL_MULTISAMPLE);
glPushMatrix();
glCallList(1); // Not Coding
glPopMatrix();
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
// Triton + Proteus Orbit.
case 'a':
triton = (triton - 2) % 360;
proteus = (proteus - 5) % 360;
glutPostRedisplay();
break;
case 'd':
triton = (triton + 2) % 360;
proteus = (proteus + 5) % 360;
glutPostRedisplay();
break;
case ',':
glTranslatef(- 0.3, 0.0, 0.0);
glutPostRedisplay();
break;
case '.':
glTranslatef(0.3, 0.0, 0.0);
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default:
break;
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB | GLUT_MULTISAMPLE);
glutInitWindowSize(1000, 1000);
glutInitWindowPosition(100, 100);
glutCreateWindow("Neptune and Space");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
You you draw the start, then the perspective projection matrix and the view matrix are stills set. The "stars" are not on the viewport.
Use a scale of about 1/500 to put the points into clip space:
glVertex2f(2*i / 500.0f, 2*j / 500.0f);
Or setup an orthographic projection, before you draw the stars:
// Stars Background
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glLoadIdentity();
glMatrixMode( GL_PROJECTION );
glPushMatrix();
glLoadIdentity();
gluOrtho2D( 0, 1000, 0, 1000 );
glCallList(1);
glPopMatrix();
glMatrixMode( GL_MODELVIEW );
glPopMatrix();

OpenGL - Why are my objects transparent?

I am trying to make a motorcycle with primitive shapes. For some reason, the shapes that I have made are see-through. I am not specifying any alpha anywhere; here is my code:
#include <GL/glut.h>
#include <math.h>
GLUquadricObj *quadratic;
static int isWire = 0; // Is wireframe?
static int distance = 10;
static float angleH = 0;
static float angleV = 0;
static float R = 2.0; // Radius of hemisphere.
static int p = 4; // Number of longitudinal slices.
static int q = 6; // Number of latitudinal slices.
#define PI 3.14159265358979324
static unsigned int pipe, seat, cover, wheel, wheelCenter, cycles; // parts of the motorcycle to make as display lists.
GLUquadricObj *cylinder;
void drawCoordinates();
void drawMotorcycle();
void drawTrailer();
void drawHemisphere();
void drawCylinder(float x, float y, float z);
void drawHandle(float x, float y, float z);
void drawLight();
void drawBase();
void setup();
void display () {
/* clear window */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(distance*cos(angleH), distance*cos(angleV), distance*sin(angleH), 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
/* future matrix manipulations should affect the modelview matrix */
glMatrixMode(GL_MODELVIEW);
if (isWire) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glPushMatrix();
drawCoordinates();
glPushMatrix();
glTranslatef(0.0, 0.0, 0.0); // Move the motorcycle around the world space
drawMotorcycle();
drawTrailer();
glPopMatrix();
glPopMatrix();
/* flush drawing routines to the window */
glFlush();
}
void drawCoordinates()
{
/***************** DRAW AXIS *****************/
glPushMatrix();
GLUquadricObj *xAxis;
xAxis=gluNewQuadric();
glColor3f(1,0,0);
glRotatef(-90, 0, 1, 0);
gluCylinder(xAxis,0.05,0.05,1,5,5);
glPopMatrix();
glPushMatrix();
GLUquadricObj *yAxis;
yAxis=gluNewQuadric();
glColor3f(0,1,0);
glRotatef(-90, 1, 0, 0);
gluCylinder(yAxis,0.05,0.05,1,5,5);
glPopMatrix();
glPushMatrix();
GLUquadricObj *zAxis;
zAxis=gluNewQuadric();
glColor3f(0,0,1);
gluCylinder(zAxis,0.05,0.05,1,5,5);
glPopMatrix();
/***************** END OF DRAW AXIS *****************/
}
void drawMotorcycle()
{
//DRAW ENGINE
glPushMatrix();
//drawCoordinates();
glColor3f(.6, 0, 0);
glScalef(1.4, 0.8, 1.0);
glutSolidSphere(1,8,8);
glPushMatrix();
//drawCoordinates();
glPopMatrix();
glPopMatrix();
//DRAW PIPES UNDER ENGINE
glPushMatrix();
glRotatef(-90, 1, 0, 0);
glRotatef(80, 0, 1, 0);
glTranslatef(0.5, 1.0, -1.5);
glCallList(pipe);
glTranslatef(0.0, -2.0, 0.0);
glCallList(pipe);
glPopMatrix();
//DRAW SEAT
glPushMatrix();
glPushMatrix();
glRotatef(15, 0, 0, 1);
glTranslatef(-2.0, -0.4, 0.0);
glScalef(2.0, 0.2, 1.2);
glCallList(seat);
glPopMatrix();
//DRAW BACK SEAT
glRotatef(-40, 0, 0, 1);
glTranslatef(-2.3, -2.8, 0.0);
glScalef(2.0, 0.2, 1.2);
glCallList(seat);
glPopMatrix();
//DRAW FRONT PLATE
glPushMatrix();
glRotatef(120, 0, 0, 1);
glTranslatef(0.8, -1.3, 0.0);
glScalef(2.0, 0.2, 1.35);
glColor3f(0.5,0.0,0.0);
glutSolidCube(1);
glPushMatrix();
//drawCoordinates();
glPopMatrix();
glPopMatrix();
//DRAW FRONT PIPES
glPushMatrix();
glRotatef(-90, 1, 0, 0);
glRotatef(-30, 0, 1, 0);
glTranslatef(1.3, -0.9, -5.7);
glScalef(1.0, 1.0, 2.5);
glCallList(pipe);
glTranslatef(0.0, 1.7, 0.0);
glCallList(pipe);
glPopMatrix();
//DRAW WHEEL COVERS
glPushMatrix();
glTranslatef(3.5, -3.0, 0.0);
glScalef(1.0, 0.5, 1.0);
glRotatef(45, 0, 0, 1);
glCallList(cover);
glTranslatef(-5.5, 0.0, 0.0);
glRotatef(-100, 0, 0, 1);
glTranslatef(-8.5, 0.2, 0.0);
glCallList(cover);
glPopMatrix();
//DRAW WHEELS
glPushMatrix();
glTranslatef(3.9, -4.1, 0.0);
glCallList(wheel);
glTranslatef(-9.2, 2.0, 0.0);
glCallList(wheel);
glPopMatrix();
//DRAW WHEEL CENTER PIECES
glPushMatrix();
glTranslatef(3.9, -4.1, 0.0);
glCallList(wheelCenter);
glTranslatef(-9.2, 2.0, 0.0);
glCallList(wheelCenter);
glPopMatrix();
//DRAW CYCLES AROUND WHEELS
glPushMatrix();
glTranslatef(3.9, -4.1, 0.0);
glRotatef(-90, 1, 0, 0);
for(int i=0; i<8; i++)
{
glRotatef(45, 0, 1, 0);
glPushMatrix();
glCallList(cycles);
glPopMatrix();
}
glTranslatef(-9.2, 0.0, 2.0);
for(int i=0; i<8; i++)
{
glRotatef(45, 0, 1, 0);
glPushMatrix();
glCallList(cycles);
glPopMatrix();
}
glPopMatrix();
//DRAW HANDLE BARS
glPushMatrix();
glTranslatef(0.2, 2.0, 0.0);
glRotatef(-45, 1, 0, 0);
glScalef(0.7, 0.7, 0.7);
glCallList(pipe);
glRotatef(-90, 1, 0, 0);
glCallList(pipe);
glPopMatrix();
//DRAW LIGHT
glPushMatrix();
glTranslatef(1.0, 1.0, 0.0);
glColor3f(0.5, 0.5, 0.0);
//glScalef(1.0, 0.5, 1.0);
glutSolidSphere(0.5, 5, 5);
glPushMatrix();
//drawCoordinates();
glPopMatrix();
glPopMatrix();
//DRAW BASE
glPushMatrix();
glRotatef(10.0, 0.0, 0.0, 1.0);
glScalef(3.5, 1.5, 1.0);
glTranslatef(-0.4, -1.0, 0.0);
glColor3f(0.3, 0.3, 0.3);
glutSolidCube(1);
glPopMatrix();
//GAS TANK
glPushMatrix();
glScalef(2.5, 1.0, 0.8);
glTranslatef(-0.8, -1.7, -1.4);
glCallList(pipe);
glPopMatrix();
}
void drawTrailer()
{
//DRAW BODY
glPushMatrix();
glColor3f(0.0, 0.0, 0.3);
glScalef(2.0, 2.5, 1.5);
glTranslatef(-4.5, -0.5, 0.0);
glutSolidCube(1);
glPopMatrix();
//DRAW WHEELS
glPushMatrix();
glPushMatrix();
glScalef(0.8, 0.8, 0.8);
glTranslatef(-12.0, -1.5, 2.0);
glCallList(wheel);
glCallList(wheelCenter);
glRotatef(90, 1, 0, 0);
for(int i=0; i<8; i++)
{
glRotatef(45, 0, 1, 0);
glPushMatrix();
glCallList(cycles);
glPopMatrix();
}
glPopMatrix();
glPushMatrix();
glScalef(0.8, 0.8, 0.8);
glTranslatef(-12.0, -1.5, -2.0);
glCallList(wheel);
glCallList(wheelCenter);
glRotatef(90, 1, 0, 0);
for(int i=0; i<8; i++)
{
glRotatef(45, 0, 1, 0);
glPushMatrix();
glCallList(cycles);
glPopMatrix();
}
glPopMatrix();
glPopMatrix();
//DRAW CONNECTION TO MOTORCYCLE
glPushMatrix();
glRotatef(90, 0, 1, 0);
glTranslatef(0.0, -1.0, -8.0);
glCallList(pipe);
glPopMatrix();
}
void drawHemisphere()
{
for(int j = 0; j < q; j++)
{
// One latitudinal triangle strip.
glBegin(GL_TRIANGLE_STRIP);
for(int i = 0; i <= p; i++)
{
glVertex3f( R * cos( (float)(j+1)/q * PI/2.0 ) * cos( 2.0 * (float)i/p * PI ),
R * sin( (float)(j+1)/q * PI/2.0 ),
R * cos( (float)(j+1)/q * PI/2.0 ) * sin( 2.0 * (float)i/p * PI ) );
glVertex3f( R * cos( (float)j/q * PI/2.0 ) * cos( 2.0 * (float)i/p * PI ),
R * sin( (float)j/q * PI/2.0 ),
R * cos( (float)j/q * PI/2.0 ) * sin( 2.0 * (float)i/p * PI ) );
}
glEnd();
}
}
void reshape (int w, int h)
{
// (Window of width = zero is not possible).
if(h == 0)
h = 1;
float ratio = 1.0* w / h;
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Set the correct perspective.
gluPerspective(90,ratio,-1,1);
}
// Keyboard input processing routine.
void keyInput(unsigned char key, int x, int y)
{
switch(key)
{
case 'c' : distance = 10; angleH=0; angleV=0.0; break;
case 'C' : distance = 10; angleH=0; angleV=0.0; break;
case 'f': distance = (distance == 4)? 4:distance-1; break;
case 'F': distance = (distance == 4)? 4:distance-1; break;
case 'b': distance = (distance == 20)? 20:distance+1; break;
case 'B': distance = (distance == 20)? 20:distance+1; break;
case 'w': if (isWire == 0) isWire = 1; else isWire = 0; break;
case 'W': if (isWire == 0) isWire = 1; else isWire = 0; break;
//case 27: exit(0); break;
default: break;
}
}
void specialKeyboard(int key, int x, int y) {
switch (key)
{
case GLUT_KEY_RIGHT:
angleH -= .2;
break;
case GLUT_KEY_LEFT:
angleH += .2;
break;
case GLUT_KEY_UP:
angleV += .2;
break;
case GLUT_KEY_DOWN:
angleV -= .2;
break;
}
}
void update(void){
glutPostRedisplay();
}
void setup()
{
// PARTS
pipe = glGenLists(1);
seat = glGenLists(1);
cover = glGenLists(1);
wheel = glGenLists(1);
wheelCenter = glGenLists(1);
cycles = glGenLists(1);
glNewList(pipe, GL_COMPILE); // Any cylinder on the motorcycle
GLUquadricObj *cylinder;
cylinder=gluNewQuadric();
glPushMatrix();
glColor3f(.5,.5,.5);
gluCylinder(cylinder,0.2,0.2,3,5,5);
glPushMatrix();
//drawCoordinates();
glPopMatrix();
glPopMatrix();
glEndList();
glNewList(seat, GL_COMPILE);
glPushMatrix();
glColor3f(0.5, 0.35, 0.05);
glutSolidCube(1);
glPushMatrix();
//drawCoordinates();
glPopMatrix();
glPopMatrix();
glEndList();
glNewList(cover, GL_COMPILE);
glPushMatrix();
glColor3f(0.5, 0.0, 0.0);
drawHemisphere();
glPushMatrix();
//drawCoordinates();
glPopMatrix();
glPopMatrix();
glEndList();
glNewList(wheel, GL_COMPILE);
glPushMatrix();
glColor3f(0.1, 0.1, 0.1);
glutSolidTorus(0.2, 1.2, 20, 20);
glPushMatrix();
//drawCoordinates();
glPopMatrix();
glPopMatrix();
glEndList();
glNewList(wheelCenter, GL_COMPILE);
glPushMatrix();
glColor3f(0.4, 0.5, 0.5);
glScalef(1.0, 0.5, 1.0);
glutSolidSphere(0.8, 5, 5);
glPushMatrix();
//drawCoordinates();
glPopMatrix();
glPopMatrix();
glEndList();
glNewList(cycles, GL_COMPILE);
glColor3f(0.5, 0.5, 0.5);
glScalef(0.25, 0.25, 0.25);
cylinder=gluNewQuadric();
gluCylinder(cylinder,0.5,0.5,5,15,5);
glEndList();
}
int main (int argc, char** argv) {
/* initialize GLUT, using any commandline parameters passed to the
program */
glutInit(&argc,argv);
/* setup the size, position, and display mode for new windows */
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH );
/* create and set up a window */
glutCreateWindow("Motorcycle");
setup(); // Build all the display lists, ready to use
glutIdleFunc(update);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyInput);
glutSpecialFunc(specialKeyboard);
/* set up depth-buffering */
glEnable(GL_DEPTH_TEST);
/* background color */
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
/* tell GLUT to wait for events */
glutMainLoop();
return 0;
}
You can rotate the camera with the arrow keys to see that the objects are see through. How can I fix this? Is there anything else I can do to improve my code?
In reshape():
// Set the correct perspective.
gluPerspective(90,ratio,-1,1);
I'm guessing you transliterated parameters from a glOrtho() call, where a negative zNear is perfectly legitimate.
From the gluPerspective() docs:
zNear: Specifies the distance from the viewer to the near clipping plane (always positive).
Try this:
gluPerspective(90,ratio,1,100);
You need to add glEnable(GL_DEPTH_TEST); to your initialization function.

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.