I am new to OpenGL. I want to write a program that displays two shapes of two different colors, a Torus and a Sphere and two independent light sources that rotate around them.
So far I have this code which makes a light source that rotates around the Torus nicely. However I am unable to figure out how to add a Sphere and a new light source that rotates around it.
EDIT:
Here is my whole code. The light rotates when you push the 's' or 'd' key on keyboard
#include "stdafx.h"
#include <windows.h>
#include <glut.h>
static int spin = 0;
bool updown = false;
bool leftright = false;
GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat yellow[] = { 1.0, 1.0, 0.0, 1.0 };
GLfloat red[] = { 1.0, 0.0, 0.0, 1.0 };
GLfloat cyan[] = { 0.0, 1.0, 1.0, 1.0 };
GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat direction[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat direction2[] = { 1.0, 0.0, 0.0, 0.0 };
void display() {
GLfloat position[] = { 0.0, 0.0, 1.5, 1.0 };
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cyan);
glMaterialfv(GL_FRONT, GL_SPECULAR, white);
glMaterialf(GL_FRONT, GL_SHININESS, 30);
glTranslatef(-2.0, 1.0, 0.0);
glPushMatrix ();
if(updown){
glRotated ((GLdouble) spin, 1.0, 0.0, 0.0);
}
if(leftright){
glRotated ((GLdouble) spin, 0.0, 1.0, 0.0);
}
glLightfv (GL_LIGHT0, GL_POSITION, position);
glTranslated (0.0, 0.0, 1.5);
glDisable (GL_LIGHTING);
glColor3f (0.0, 1.0, 1.0);
glEnable (GL_LIGHTING);
glPopMatrix();
glutSolidTorus(0.275, 0.85, 16, 40);
glPopMatrix();
glFlush();
}
void reshape(GLint w, GLint h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
GLfloat aspect = GLfloat(w) / GLfloat(h);
glLoadIdentity();
if (w <= h) {
glOrtho(-2.5, 2.5, -2.5/aspect, 2.5/aspect, -10.0, 10.0);
} else {
glOrtho(-2.5*aspect, 2.5*aspect, -2.5, 2.5, -10.0, 10.0);
}
}
void init() {
glLightfv(GL_LIGHT0, GL_AMBIENT, black);
glLightfv(GL_LIGHT0, GL_DIFFUSE, yellow);
glLightfv(GL_LIGHT0, GL_SPECULAR, white);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
void keyboard(unsigned char button, int x, int y)
{
switch (button) {
case 's':
updown = true;
leftright = false;
spin = (spin + 30) % 360;
glutPostRedisplay();
break;
case 'd':
updown = false;
leftright = true;
spin = (spin + 30) % 360;
glutPostRedisplay();
break;
default:
break;
}
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(80, 80);
glutInitWindowSize(800, 600);
glutCreateWindow("Shapes and light");
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
init();
glutMainLoop();
}
I believe the problem is more conceptual. OpenGL is stateful. What that means in practice is that you modify the state, draw something, modify the state again, draw something else. The state of OpenGL 1.x includes the matrix stack, materials, and the lights.
I believe in your mind, you want to add a new light object, then add a new object to draw, then have OpenGL render that. That is not how OpenGL works. There are no real objects in OpenGL.
What happens when you draw something, e.g. with glutSolidTorus, is that OpenGL puts pixels into buffers, including the screen. To determine the color of these pixels, it looks at the state you put it in before. So in order to draw two objects, you would first set up lighting, material, position for the first object, call glutSolidTorus, then set up the lighting, material, position for the second object, call glutSolidSphere. Note that you don't need to "add a new material", or "add a new light" for rendering the sphere. All you need to do is change the state before rendering, e.g. by moving GL_LIGHT0 to a different position.
Related
I have two 3D objects, and I need to set material only on one of them using glMaterial function. How can I accomplish this?
#include <GLUT/glut.h>
#include <cmath>
const int width = 1200, height = 600;
float xValue = 0.0;
float position = 0.0;
float angle = 0.0;
float shininess[] = { 1.0 };
float color[] = { 0.2, 0.2, 0.8 };
void init(void);
void display(void);
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(width, height);
glutInitWindowPosition(85, 75);
glutCreateWindow("3D объекты - освещение");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void init(void) {
glClearColor(0.0, 0.0, 0.0, 1.0);
glEnable(GL_LIGHTING);
float lightAmbient[] = { 0.5, 0.5, 0.5, 0.0 };
float lightDiffuse[] = { 0.5, 0.5, 0.5, 0.0 };
float lightSpecular[] = { 0.5, 0.5, 0.5, 0.0 };
float lightPosition[] = { -10.0, 5.0, 0.0, 1.0 };
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular);
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glOrtho(-10.0, 10.0, -5.0, 5.0, -5.0, 5.0);
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0.0, 0.0, width, height);
xValue += 0.01;
position = 5.0*cos(xValue);
angle = 270.0*sin(xValue);
glLineWidth(3);
glPushMatrix();
glTranslatef(position, 0.0, 0.0);
glRotatef(angle, 0, 1, 0);
glMaterialfv(GL_FRONT, GL_AMBIENT, color);
glMaterialfv(GL_FRONT, GL_DIFFUSE, color);
glMaterialfv(GL_FRONT, GL_SPECULAR, color);
glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
glutWireTorus(1, 2, 35, 35);
glPopMatrix();
glPushMatrix();
glTranslatef(-position, 0.0, 0.0);
glRotatef(angle, 0, 1, 0);
glutSolidTorus(0.5, 3.5, 35, 35);
glPopMatrix();
glutSwapBuffers();
glutPostRedisplay();
}
OpenGL is a state engine. Each state is kept until it is changed again, even beyond frames. For instance you can enable and disable lighting before drawing an object:
glEnable(GL_LIGHTING);
// darw object 1
glDisable(GL_LIGHTING);
// draw object 2
ALike the material settings can be set for each object individually, before drawing the object:
glMaterialfv(...);
// darw object 1
glMaterialfv(...);
// draw object 2
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();
I am trying to learn how to use a spot light in OpenGL. I wish to shine a spot (torch) light from a point marked (lighpos), and shown as grey dot, onto the side of a GLUT teapot centered at (possph) in 3D space. I have attached an example where I have tried to do this but I cannot get the light to focus on the teapot. I am expecting a almost touch light focus based on the parameters I have tried to use.
Could someone point out what I have missed / mistake I have made.
Thanks
Stuart
#include <windows.h>
#include <GL/glut.h>
#include <stdio.h>
GLfloat lighpos[] = { -300., 200., 250., 1.0 }; // Location of light
GLfloat possph[] = { -50., 350., 150. }; // Position of teapot
GLfloat ligdir[] = { 250., 150., -100. }; // Direction from light to teapot
void init(void) {
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_DEPTH_TEST);
glLightfv(GL_LIGHT0, GL_POSITION, lighpos);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, ligdir);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 10.0);
GLfloat ambientLight0[] = { 0.25, 0.25, 0.25, 1.0 };
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight0);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight0);
GLfloat diffuseLight0[] = { 1.0, 1.0, 1.0, 1.0 };
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight0);
GLfloat specularLight0[] = { 1.0, 1.0, 1.0, 1.0 };
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight0);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 128.0f);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
void drawAxis() {
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0); glVertex3f(-500., 0.0, 0.0); glVertex3f(500., 0.0, 0.0);
glColor3f(0.0, 1.0, 0.0); glVertex3f(0.0, -500., 0.0); glVertex3f(0.0, 500., 0.0);
glColor3f(0.0, 0.0, 1.0); glVertex3f(0.0, 0.0, -500.); glVertex3f(0.0, 0.0, 500.);
glEnd();
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
drawAxis();
glPushMatrix();
glPointSize(10);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POINTS);
glVertex3d(lighpos[0], lighpos[1], lighpos[2]);
glEnd();
glPopMatrix();
glColor3f(1., 0., 0.);
glPushMatrix();
glTranslated(possph[0], possph[1], possph[2]);
glutSolidTeapot(100.);
glPopMatrix();
glutSwapBuffers();
}
void reshape(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 4, 3000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1500.0, 1500.0, 1500.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(0.0, 0.0);
glutInitWindowSize(800, 800);
glutCreateWindow("spotlight");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
Fixed function pipeline illumination, which is what you are using in your code, performs lighting calculations only at the vertices. If your spotlight illuminates only a few or a single vertex you'll not get a pleasing spotlight effect. One possible solution would be to highly refine your models' meshes. The better, much more efficient and elegant solution is to drop using the fixed function pipeline and implement a illumination fragment shader, so that lighting is calculated for each pixel.
I'm writing a gravitational n-body simulation in C++, which is animated using OpenGL and GLUT (this is a hobby project). The animation works fine, for the most part, however I've had two major problems that I'm unable to resolve:
Although depth testing is enabled, it isn't working as expected, and
The surfaces are ... well, messy.
My question is, how do I fix these problems?
Both problems can be seen in the following images (apologies for the links but I've not enough rep for image posting). These are snapshots from a simulation of a simple orbit, viewed edge on.
Here the yellow sphere is drawn in front of the purple one, as it should be.
After half an orbit the yellow sphere is still drawn in front, even though it is further away.
The code used to create the animation is given below.
#include <GL/glut.h>
#include "Cluster.h" // My own class.
// Scale for animation. Each unit in the animation = 1/SCALE m.
const double SCALE = 1e-10;
// Size of spheres for animation.
const double SPHERE_SIZE = 2e10*SCALE;
// Cluster object contains bodies and updates their positions.
Cluster cluster();
// Array of rgb colors for spheres.
GLfloat colorArr[4][4] =
{
{0.7, 0.7, 0.0, 1.0},
{0.73, 0.24, 0.95, 1.0},
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt( 0.0*SCALE, 5e11*SCALE, 0.0*SCALE // eye is on y-axis outside orbit.
, 0.0, 0.0, 0.0
, 0.0, 0.0, 1.0 );
for (int i=0; i<N; i++) // N is the number of bodies in cluster.
{
glPushMatrix();
glTranslated( SCALE*cluster.getX(i) // Get coordinate of ith body.
, SCALE*cluster.getY(i)
, SCALE*cluster.getZ(i) );
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, colorArr[i]);
glutSolidSphere(SPHERE_SIZE, 50, 50);
glCullFace(GL_BACK);
glPopMatrix();
}
glutSwapBuffers();
}
void reshape(GLint w, GLint h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0, (GLfloat)w/(GLfloat)h, SCALE, 5e11*SCALE);
glMatrixMode(GL_MODELVIEW);
}
void animate()
{
// Update positions and redraw.
cluster.update();
display();
}
void init()
{
GLfloat black[] = {0.0, 0.0, 0.0, 1.0};
GLfloat white[] = {1.0, 1.0, 1.0, 0.5};
GLfloat direction[] = {1.0, 1.0, 1.0, 0.0};
glMaterialfv(GL_FRONT, GL_SPECULAR, white);
glMaterialf(GL_FRONT, GL_SHININESS, 10);
glLightfv(GL_LIGHT0, GL_AMBIENT, black);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
glLightfv(GL_LIGHT0, GL_SPECULAR, white);
glLightfv(GL_LIGHT0, GL_POSITION, direction);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("Test Orbit");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(animate);
init();
glutMainLoop();
}
OpenGL's depth buffer has limited precision, and the numbers you're using are large enough that you may be running into that precision limit. You may want to read this article about depth-buffer optimization, or simply use a larger unit than meters for representing astronomical distances.
I'm writing an OpenGL application. The program needs to have a simple shape at the center and the user can change the view from orthographic to perspective and vice versa. I don't have any problem in handling the orthographic view but I can't switch from ortho to perspective, even though gluPerspective seems pretty straightforward.
The code I wrote looks something like this:
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0, 0, -3);
glRotatef(xRot, 1.0, 0.0, 0.0);
glRotatef(yRot, 0.0, 1.0, 0.0);
glRotatef(zRot, 0.0, 0.0, 1.0);
glScalef(scale, scale, scale);
glPushAttrib(GL_ENABLE_BIT);
if (currentStyle) {
if (currentStyle == STYLE_DOTTED) glLineStipple(currentStyle, 0xAAAA);
else glLineStipple(currentStyle, 0x0F0F * currentStyle);
glEnable(GL_LINE_STIPPLE);
}
setColor();
drawObjectOfType();
glPopAttrib();
glFlush();
}
void myKeyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'p':
isOrtho = !isOrtho;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(!isOrtho) {
gluPerspective(55,1,5,3);
gluLookAt(0,0,-7,0,0,0,0,1,0);
} else {
glOrtho(-2.0, 2.0, -2.0, 2.0, -10.0, 10.0);
}
glMatrixMode(GL_MODELVIEW);
break;
}
glutPostRedisplay();
}
The program also has lighting in it. Here's the init function that handles the lighting.
void init()
{
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess = { 100.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 0.0, 0.0, 10.0, 0.0 };
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialf(GL_FRONT, GL_SHININESS, mat_shininess);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
createMenus();
glEnable(GL_DEPTH_TEST);
glClearColor(0.0, 0.0, 0.0, 1.0);
}
What happens with this code is the view changes from ortho to perspective, but distance between the camera and the polygons seems to be reverted, meaning the closer the polygon to the camera, the smaller it looks. For example, I have a cube as the shape and the face that is closer to me looks smaller whereas the oppposite should have been the case.
The last two parameters to gluPerspective() are zNear and zFar. You have set the near clipping plane further away than the far clipping plane, which reversed the perspective correction.
gluLookAt defines a viewing transformation, and should therefore be part of your MODELVIEW transformation. Your code multiplies it into the PROJECTION transformation. Try a sequence like this for your perspective setup case:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(55,1,3,5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,-7,0,0,0,0,1,0);
Then use glPushMatrix and glPopMatrix instead of glLoadIdentity in your myDisplay function.
Your near and far plane are quite close to each other. We don't see in the code you posted what the range of the coordinates for your object are. You'll want to be careful that it ends up between your near and far plance, so that it won't get clipped away.