PDA

View Full Version : Problem in displaying 3d scene using the camera



spworld
12-18-2003, 06:16 AM
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 << "\nnormalize() 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(y*b.z - z*b.y, z*b.x - x*b.z, x*b.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(cs*t.x - sn*v.x, cs*t.y - sn*v.y, cs*t.z - sn*v.z);
v.set(sn*t.x + cs*v.x, sn*t.y + cs*v.y, sn*t.z + cs*v.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 &#0124; &#0124; 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;
}

dj_indo_420
12-18-2003, 08:07 AM
you should be setting your color in your myDisplay()
glClearColor(1.0f,1.0f,1.0f,1.0f); // background is white
glColor3f(0.0f,0.0f,0.0f); // set color of stuff unction

it looks like you are calling myDisplay which calls
glClear(GL_COLOR_BUFFER_BIT | | GL_DEPTH_BUFFER_BIT);
then in the main function
glClearColor(1.0f,1.0f,1.0f,1.0f); // background is white
glColor3f(0.0f,0.0f,0.0f); // set color of stuff
this is called after your myDisplay function, so after the program loops around it clears everything before you can draw the teapot, you should keep all you rendering/color calls in a single function, instead of spreading them out like you have here

12-18-2003, 12:30 PM
Also, in the begining of your display function, you should probably set the matrix mode back to modelview and load the identity matrix:
glMatrixMode(MODEL_VIEW);
glLoadIdentity();

and you might need to "draw" your camera so to speak before you draw the teapot. In the graphics class I just took last semester, we used a toolkit that students in previous years had developed which had an implementation of a camera class, and the camera had to be rendered in the display function before other things were drawn

hope this helps