It is an example on my text book that I couldn’t get it to work after I read it and trace for several times .Only black screan appears .Please Post the code shown below on you PC and help me realize where the problem is .
Thanks
////////////////////////////////////
// the usual includes
#include <GL/glut.h>
#include <GL/GL.h>
#include <GL/GLU.h>
#include <math.h>
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
using namespace std;
//---------------------------------------------------------------
class Point3{
public:
float x,y,z;
void set(float dx, float dy, float dz){x = dx; y = dy; z = dz;}
void set(Point3& p){x = p.x; y = p.y; z = p.z;}
Point3(float xx,float yy, float zz){x = xx; y = yy; z = zz;}
Point3(){x = y = z = 0;}
void build4tuple(float v[])
{// load 4-tuple with this color: v[3] = 1 for homogeneous
v[0] = x; v[1] = y; v[2] = z; v[3] = 1.0f;
}
};
//@@@@@@@@@@@@@@@@@@ Vector3 class @@@@@@@@@@@@@@@@
class Vector3{
public:
float x,y,z;
void set(float dx, float dy, float dz){ x = dx; y = dy; z = dz;}
void set(Vector3& v){ x = v.x; y = v.y; z = v.z;}
void flip(){x = -x; y = -y; z = -z;} // reverse this vector
void setDiff(Point3& a, Point3& b)//set to difference a - b
{ x = a.x - b.x; y = a.y - b.y; z = a.z - b.z;}
void normalize()//adjust this vector to unit length
{
double sizeSq = x * x + y * y + z * z;
if(sizeSq < 0.0000001)
{
//cerr << "
normalize() sees vector (0,0,0)!";
return; // does nothing to zero vectors;
}
float scaleFactor = 1.0/(float)sqrt(sizeSq);
x = scaleFactor; y = scaleFactor; z = scaleFactor;
}
Vector3(float xx, float yy, float zz){x = xx; y = yy; z = zz;}
Vector3(Vector3& v){x = v.x; y = v.y; z = v.z;}
Vector3(){x = y = z = 0;} //default constructor
Vector3 cross(Vector3 b) //return this cross b
{
Vector3 c(yb.z - zb.y, zb.x - xb.z, xb.y - y*b.x);
return c;
}
float dot(Vector3 b) // return this dotted with b
{return x * b.x + y * b.y + z * b.z;}
};
//-------------------------------------------------------------
class Camera{
private:
Point3 eye;
Vector3 u,v,n;
double viewAngle, aspect, nearDist, farDist; // view volume shape
void setModelViewMatrix(); // tell OpenGL where the camera is
public:
Camera(){}; // default constructor
void set(Point3 eye, Point3 look, Vector3 up); // like gluLookAt()
void roll(float angle); // roll it
void pitch(float angle); // increase pitch
void yaw(float angle); // yaw it
void slide(float delU, float delV, float delN); // slide it
void setShape(float vAng, float asp, float nearD, float farD);
};
//-------------------------------------------------
void Camera :: setModelViewMatrix(void)
{ // load modelview matrix with existing camera values
float m[16];
Vector3 eVec(eye.x, eye.y, eye.z); // a vector version of eye
m[0] = u.x; m[4] = u.y; m[8] = u.z; m[12] = -eVec.dot(u);
m[1] = v.x; m[5] = v.y; m[9] = v.z; m[13] = -eVec.dot(v);
m[2] = n.x; m[6] = n.y; m[10] = n.z; m[14] = -eVec.dot(n);
m[3] = 0.0; m[7] = 0.0; m[11] = 0.0; m[15] = 1.0;
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(m); // load OpenGL’s modelview matrix
}
//------------------------------------------------
void Camera :: set(Point3 Eye, Point3 look, Vector3 up)
{ // create a modelview matrix and send it to OpenGL
eye.set(Eye); // store the given eye position
n.set(eye.x - look.x, eye.y - look.y, eye.z - look.z); // make n
u.set(up.cross(n)); // make u = up X n
n.normalize();
u.normalize(); // make them unit length
v.set(n.cross(u)); // make v = n X u
setModelViewMatrix(); // tell OpenGL
}
//-------------------------------------------------
void Camera :: roll(float angle)
{ // roll the camera through angle degrees
float cs = cos(3.14159265/180 * angle);
float sn = sin(3.14159265/180 * angle);
Vector3 t(u); // remember old u
u.set(cst.x - snv.x, cst.y - snv.y, cst.z - snv.z);
v.set(snt.x + csv.x, snt.y + csv.y, snt.z + csv.z);
setModelViewMatrix();
}
//--------------------------------------------------------------
void Camera :: slide(float delU, float delV, float delN)
{ // slide the camera through angle degrees
eye.x+=delU * u.x + delV * v.x + delN * n.x;
eye.y+=delU * u.y + delV * v.y + delN * n.y;
eye.z+=delU * u.z + delV * v.z + delN * n.z;
setModelViewMatrix();
}
//-------------------------------------------------------------
void Camera :: setShape(float vAng, float asp, float nearD, float farD)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(vAng,asp,nearD,farD);
}//end setShape
//-------------------------------------------------------------
Camera cam; // global camera object
//<<<<<<<<<<<<<<<<<<<<<<<< myKeyboard >>>>>>>>>>>>>>>>>>>>>>
void myKeyboard(unsigned char key, int x, int y)
{
switch(key)
{
// controls for camera
case ‘r’: cam.roll(30.0); break; // slide camera forward
case ‘s’: cam.slide(0,0,-0.2); break; //slide camera back
// add up/down and left/right controls
case ‘e’: cam.slide(0,0,0.2); break;
default : break;
//case ‘P’- 64: cam.pitch( 1.0); break;*/
// add roll and yaw controls
}//end case
glutPostRedisplay(); // draw it again
}
//<<<<<<<<<<<<<<<<<<<<<<< myDisplay >>>>>>>>>>>>>>>>>>>>>>>>>>
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT | | GL_DEPTH_BUFFER_BIT);
glutWireTeapot(1.0); // draw the teapot
glFlush();
glutSwapBuffers(); // display the screen just made
}
//<<<<<<<<<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>>>>>
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(50, 50);
glutCreateWindow(“fly a camera around a teapot”);
glutKeyboardFunc(myKeyboard);
glutDisplayFunc(myDisplay);
glClearColor(1.0f,1.0f,1.0f,1.0f); // background is white
glColor3f(0.0f,0.0f,0.0f); // set color of stuff
glViewport(0, 0, 640, 480);
Point3 eyex(4.0,4.0,4.0), lookx(0.0,1.0,0.0); //eye ,look
Vector3 upx(0.0,1.0,0.0); //up
cam.set(eyex,lookx,upx); // make the initial camera
cam.setShape(15.0f, 64.0f/48.0f, 0.5f, 50.0f);
glutMainLoop();
return 0;
}