Archived

This topic is now archived and is closed to further replies.

ARID

gluLookAt

Recommended Posts

ARID    122
I''m useing gluLookAt with my camera funtion but after i got a large part of the code done when i recompile i runs but without the objects on screen any suggestions?

Share this post


Link to post
Share on other sites
inrecovery    122
Hi all,
I agree with you here but dont quite know how to solve this dilemma.

quote:
Original post by LockePick
Perhaps the object is simply not on screen? You're most likely not plugging in the right coordinates for gluLookAt.




I am referring to this "Computer Graphics: Programming, Problem Solving, and Visual Communication(found here: "http://www.cs.csustan.edu/~rsc/CS3600F01/Syllabus.html)

In Chapter1, this is the exercise on Page 52. I have tried my level best to follow UPTIL the point that the author has suggested for the reader to try the exercise.

This is the code.

  

#include <gl\glut.h>

//Integer Identifier for the window

int myWin;

void display(void);
void reshape(int,int);
void myInit(void);
void drawHouse(void);

void main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
myWin = glutCreateWindow("My House");
myInit();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
}

void drawHouse(void)
{
typedef GLfloat point [3];
int i,j;

point myHouse[10] =
{
{ -1.0, -1.0, 2.0 }, { -1.0, 1.0, 2.0 },
{ 0.0, 2.0, 2.0 }, { 1.0, 1.0, 2.0 },
{ 1.0, -1.0, 2.0 }, { -1.0, -1.0, -2.0 },
{ -1.0, 1.0, -2.0 }, { 0.0, 2.0, -2.0 },
{ 1.0, 1.0, -2.0 }, { 1.0, -1.0, -2.0 }
};

glBegin(GL_LINE_STRIP);
for ( i=0; i<5; i++)
glVertex3fv(myHouse[i]);
glVertex3fv(myHouse[0]);
glEnd();

glBegin(GL_LINE_STRIP);
for ( i=0; i<5; i++)
glVertex3fv(myHouse[i+5]);
glVertex3fv(myHouse[5]);
glEnd();

for ( i=0; i<5; i++)
{
glBegin(GL_LINE_STRIP);
glVertex3fv(myHouse[i]);
glVertex3fv(myHouse[i+5]);
glEnd();
}


//Code that works

/*
point myHouse[4] = {{0.0, 0.0, 0.0} , {1.0,0.0,0.0}, {1.0,0.0,1.0}, {0.0,0.0,1.0}};
glBegin(GL_QUADS );
glColor3f(1.0,0.0,0.0);
for(i = 0; i < 4; i++)
{
//glColor3f(0.0,0.0,0.0);
glVertex3fv(myHouse[i]);
}
glEnd();
*/

}

void myInit(void)
{
glEnable(GL_DEPTH_TEST);
glClearColor(1.0,1.0,1.0,1.0);
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

gluLookAt(20, 20/2., 20/4., 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
glPushMatrix();
drawHouse();
glPopMatrix();
glutSwapBuffers();
}

void reshape(int width,int height)
{
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (float)width/(float)height, 1.0, 100.0);
}




I seem to be missing the big picture here and am unable to view anything.
In case, the code for the house is commented and only a quad is tried (with z-coords equal to 0), I can at least see soemthing on my window.

I dont know what I am doing wrong!

"Recovering Thinker"
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
"He conquers who endures."
-Persius
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!



[edited by - inrecovery on March 18, 2003 9:47:08 PM]

Share this post


Link to post
Share on other sites
inrecovery    122
Could you please tell us with soem small example? or take a look at the code above.PLEASE
quote:
Original post by LockePick
Perhaps the object is simply not on screen? You''re most likely not plugging in the right coordinates for gluLookAt.


Share this post


Link to post
Share on other sites
ARID    122
Heres the actual gluLookAt snippet.


  
gl_Camera.CameraPosition(0.0f, -10.0f, -6.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
....
....

gluLookAt(gl_Camera.cur_vPosition.X, gl_Camera.cur_vPosition.Y, gl_Camera.cur_vPosition.Z, gl_Camera.cur_vView.X, gl_Camera.cur_vView.Y, gl_Camera.cur_vView.Z, gl_Camera.cur_vVectorUp.X, gl_Camera.cur_vVectorUp.Y, gl_Camera.cur_vVectorUp.Z);

glBegin (GL_QUADS);

for(float i = 0; i < 100; i += 5)
{
glColor3ub(255, 255, 0); glVertex3f(-10 + i/5, i, 10 - i/5);

glColor3ub(255, 0, 0); glVertex3f(-10 + i/5, i, -10 + i/5);
glColor3ub(0, 255, 255); glVertex3f(10 - i/5, i, -10 + i/5);
glColor3ub(0, 0, 255);
glVertex3f(10 - i/5, i, 10 - i/5);
}

glEnd();

glFlush();


[edited by - ARID on March 18, 2003 11:18:59 PM]

[edited by - ARID on March 18, 2003 11:22:16 PM]

Share this post


Link to post
Share on other sites
ARID    122
Here is the Camera itself.

      
// CROSS PRODUCT


CVector3 Cross(CVector3 vVector1, CVector3 vVector2)
{
CVector3 vNormal; // Storage for the cross product


// X value of the vector is (V1.Y * V2.Z) - (V1.Z * V2.Y)

vNormal.X = ((vVector1.Y * vVector2.Z) - (vVector1.Z * vVector2.Y));

// Y value of the vector is (V1.Z * V2.X) - (V1.X * V2.Z)

vNormal.Y = ((vVector1.Z * vVector2.X) - (vVector1.X * vVector2.Z));

// Z value of the vector is (V1.X * V2.Y) - (V1.Y * V2.X)

vNormal.Z = ((vVector1.X * vVector2.Y) - (vVector1.Y * vVector2.X));

return vNormal;
}

// MAGNITUDE


float Magnitude(CVector3 vNormal)
{
return (float)sqrt( (vNormal.X * vNormal.X) +
(vNormal.Y * vNormal.Y) +
(vNormal.Z * vNormal.Z) );
}

// NORMALIZE


CVector3 Normalize(CVector3 vVector)
{
// Get the magnitude of the normal

float magnitude = Magnitude(vVector);

// Make unit vector

vVector = vVector / magnitude;

return vVector;
}


// Camera class constructor.

CCamera::CCamera()
{
CVector3 vZero = CVector3(0.0f, 0.0f, 0.0f); // Zero out the position

CVector3 vView = CVector3(0.0f, 1.0f, 0.5f); // View looking up and out

CVector3 vUp = CVector3(0.0f, 0.0f, 1.0f); // Standard up vector.


cur_vPosition = vZero; // Initilize the position

cur_vView = vView; // Initilize the view vector

cur_vVectorUp = vUp; // Initilize the up vector

};

// Initilize the Camera


GLvoid CCamera::CameraPosition(float X_Position, float Y_Position, float Z_Position,
float X_View, float Y_View, float Z_View,
float X_VectorUp, float Y_VectorUp, float Z_VectorUp)
{
CVector3 vPosition = CVector3(X_Position, Y_Position, Z_Position);
CVector3 vView = CVector3(X_View, Y_View, Z_View);
CVector3 vVectorUp = CVector3(X_VectorUp, Y_VectorUp, Z_VectorUp);

// Assign position view and up vector

cur_vPosition = vPosition;
cur_vView = vView;
cur_vVectorUp = vVectorUp;
}

// Mouse Viewing


void CCamera::SetViewByMouse()
{
POINT mousePos;
int middleX = SCREEN_WIDTH >> 1;
int middleY = SCREEN_HEIGHT >> 1;
float angleY = 0.0f;
float angleZ = 0.0f;
static float currentRotX = 0.0f;

// Find the mouse

GetCursorPos(&mousePos);

// See if the mouse moved

if((mousePos.x == middleX) && (mousePos.y == middleY))
return;

// Reset the mouse position to the center.

SetCursorPos(middleX, middleY);

// Get the vector the mouse moved

angleY = (float)((middleX - mousePos.x))/1000.0f;
angleZ = (float)((middleY - mousePos.y))/1000.0f;

// Restrict the vertical tilt

currentRotX -= angleZ;

// If the current rotation in radians is greater then 1,0 cap it.

if(currentRotX > 1.0f)
currentRotX = 1.0f;
// If the current rotation in radians is less then -1.0 cap it

else if(currentRotX < -1.0f)
currentRotX = -1.0f;
// Otherwise rotate the view

else
{
// Normalize

CVector3 vAxis = Cross(cur_vView - cur_vPosition, cur_vVectorUp);
vAxis = Normalize(vAxis);

// Rotate view

CameraRotate(angleZ, vAxis.X, vAxis.Y, vAxis.Z);
CameraRotate(angleY, 0.0f, 1.0f, 0.0f);
}
}

// Camera Rotation


void CCamera::CameraRotate(float angle, float X, float Y, float Z)
{
CVector3 vNewView;

// Get view vector

CVector3 vView = cur_vView - cur_vPosition;

// Calculate the sin and cos of the angle

float cosTheta = (float)cos(angle);
float sinTheta = (float)sin(angle);

// Find the X position for the rotated point.

vNewView.X = (cosTheta + (1 - cosTheta) * X * X) * vView.X;
vNewView.X += ((1 - cosTheta) * X * Y - Z * sinTheta) * vView.Y;
vNewView.X += ((1 - cosTheta) * X * Z + Y * sinTheta) * vView.Z;

// Find the Y position for the rotated point

vNewView.Y = ((1 - cosTheta) * X * Y + Z * sinTheta) * vView.X;
vNewView.Y += (cosTheta + (1 - cosTheta) * Y * Y) * vView.Y;
vNewView.Y += ((1 - cosTheta) * Y * Z - X * sinTheta) * vView.Z;

// Find the Z position for the rotated point

vNewView.Z = ((1 - cosTheta) * X * Z - Y * sinTheta) * vView.X;
vNewView.Z += ((1 - cosTheta) * Y * Z + X * sinTheta) * vView.Y;
vNewView.Z += (cosTheta + (1 - cosTheta) * Z * Z) * vView.Z;

// Add new rotated vector to the current position to set the new rotated view.

cur_vView = cur_vPosition + vNewView;
}


// Camera Movement


GLvoid CCamera::CameraMovement(float speed)
{
CVector3 vVector = cur_vView - cur_vPosition; // Init view vector.


// Move Camera based on acceleration and vector

cur_vPosition.X += vVector.X * speed; // acceleration added to positions X

cur_vPosition.Z += vVector.Z * speed; // acceleration added to positions Z

cur_vView.X += vVector.X * speed; // acceleration added to view X

cur_vView.Z += vVector.Z * speed; // acceleration added to view Z

}


[edited by - ARID on March 18, 2003 11:26:45 PM]

Share this post


Link to post
Share on other sites
ARID    122
Here is the Camera class


  
#include "Vectors.h" // Vector Header
#include "AggressiveOptimize.h" // Optimization Header


class CCamera
{
public:
CVector3 cur_vPosition; // Cameras position

CVector3 cur_vView; // Cameras view

CVector3 cur_vVectorUp; // Cameras Up Vector


CCamera(); // Constructor


void CameraPosition(float X_Position, float Y_Position, float Z_Position,
float X_View, float Y_View, float Z_View,
float X_VectorUp, float Y_VectorUp, float Z_VectorUp);

void SetViewByMouse();

void CameraRotate(float angle, float X, float Y, float Z);

void CameraMovement(float speed);
};

extern CCamera gl_Camera;


[edited by - ARID on March 18, 2003 11:27:35 PM]

Share this post


Link to post
Share on other sites
ARID    122
Here is the Vector class.


  
class CVector3 // The camera vector

{
public:
CVector3() {}

// This is our constructor that allows us to initialize our data upon creating an instance

CVector3(float X, float Y, float Z)
{
}

// Here we overload the + operator so we can add vectors together

CVector3 operator+(CVector3 vVector)
{
// Return the added vectors result.

return CVector3(vVector.X + X, vVector.Y + Y, vVector.Z + Z);
}

// Here we overload the - operator so we can subtract vectors

CVector3 operator-(CVector3 vVector)
{
// Return the subtracted vectors result

return CVector3(X - vVector.X, Y - vVector.Y, Z - vVector.Z);
}

// Here we overload the * operator so we can multiply by scalars

CVector3 operator*(float num)
{
// Return the scaled vector

return CVector3(X * num, Y * num, Z * num);
}

// Here we overload the / operator so we can divide by a scalar

CVector3 operator/(float num)
{
// Return the scale vector

return CVector3(X / num, Y / num, Z / num);
}

float X, Y, Z;
};


[edited by - ARID on March 18, 2003 11:28:07 PM]

Share this post


Link to post
Share on other sites