# OpenGL How to look at anything from anywhere

This topic is 4484 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Basically how do you look at a point in 3d space from a rotating object? If you look at the Pic named fig 1 I want to modify the viewpoint location of the solar system so that it looks from the centre of the earth object to the cords 0,0,0. I need to know where in the pipeline the transformation has to occur and how to do it. Or if anyone can point me in the direction for a good tutor around this subject, also the maths behind the transformations will help as this will stop me having to keep on asking ;) and maybe soon I can start helping others. Any help is appreciated Thanks Anthony Fig 1

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glu.h>
#include <GL/GLAux.h>
#include <math.h>
static GLenum spinMode = GL_TRUE;

void OpenGLInit(void);

static void CALLBACK Animate(void);
static void CALLBACK Key_a(void);
static void CALLBACK Key_up(void);
static void CALLBACK Key_down(void);
static void CALLBACK ResizeWindow(GLsizei w, GLsizei h);

static int HourOfDay = 0, DayOfYear = 0;
static double AnimateIncrement	= 1;//in hours

static void CALLBACK Key_a(void)
{
spinMode = !spinMode;
}
static void CALLBACK Key_up(void)
{
AnimateIncrement *= 2;
if(0 == AnimateIncrement)
AnimateIncrement = 1;
}
static void CALLBACK Key_down(void)
{
AnimateIncrement /=2;
}
static void CALLBACK Animate(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

GLfloat x,z;
GLfloat EarthAngle;

if (spinMode)
{
//AnimateIncrement = 0;
HourOfDay += AnimateIncrement;
DayOfYear += HourOfDay/24;

HourOfDay = HourOfDay%24;
DayOfYear = DayOfYear%365;
}

EarthAngle = (GLfloat)(360.0*DayOfYear/365.0);

////back off 10 units viewport trasformation
glTranslatef(0.0f,0.0f,-10.0f);
glRotatef(90,1.0f,0.0f,0.0f);

//sun
//glColor3f(1.0f,1.0f,1.0f);
auxWireSphere(1.0f);

//earth

glRotatef(EarthAngle,0.0f,1.0f,0.0f); //rotate the earth around the sun

glTranslatef(4.0f,0.0f,0.0f);

glPushMatrix();

glRotatef((GLfloat)(360.0*HourOfDay/24.0),0.0f,1.0f,0.0f); //rotate the earth around it's own axsis

glColor3f(0.2f,0.2f,1.0f);
auxWireSphere(0.2f);

glPopMatrix();

//moon

glRotatef((GLfloat)(360.0*12.5*DayOfYear/365.0),0.0f,1.0f,0.0f);

glTranslatef(0.5f,0.0f,0.0f);
glPushMatrix();
glColor3f(0.3f,0.3f,0.3f);
auxWireSphere(0.05f);
glPopMatrix();

//satilite
glRotatef((GLfloat)(360.0*30*DayOfYear/365.0),0.0f,1.0f,0.0f);//Rotate the sphere around the moon
glTranslatef(0.1f,0.0f,0.0f); //init start off place for the sphere

glColor3f(0.3f,0.3f,0.3f);
auxWireSphere(0.02f);

glPopMatrix();

gluLookAt(1,1,1 ,0,0,0,  0,1,0);

glFlush();
auxSwapBuffers();
}

void OpenGLInit(void)
{
glClearColor(0.0f,0.0f,0.0f,0.0f);

glClearDepth(1.0f);
glDepthFunc(GL_LEQUAL);

glEnable(GL_DEPTH_TEST);
}

static void CALLBACK ResizeWindow(GLsizei w, GLsizei h)
{
h = (h==0) ? 1 : h;
w = (w==0) ? 1 : w;

glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);

gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,1.0f,20.0f);
glMatrixMode(GL_MODELVIEW);

}

int main(int argc, char** argv)
{
auxInitDisplayMode(AUX_DOUBLE| AUX_RGB);
auxInitPosition(0,0,800,600);
auxInitWindow("Solar System Example");

OpenGLInit();

auxKeyFunc(AUX_UP,Key_up);
auxKeyFunc(AUX_DOWN,Key_down);
auxKeyFunc(AUX_a,Key_a);

auxReshapeFunc(ResizeWindow);

auxIdleFunc(Animate);
auxMainLoop(Animate);

return(0);
}


[Edited by - Anthony_N on March 14, 2006 7:05:20 AM]

##### Share on other sites
You could use gluLookAt. Here's the arguments it takes:

gluLookAt ( GLdouble eyeX , GLdouble eyeY , GLdouble eyeZ , GLdouble centerX , GLdouble centerY , GLdouble centerZ , GLdouble upX , GLdouble upY , GLdouble upZ );

The eye X, Y, Z is the position of the camera/eye. The Center X, Y, Z is the point at which the camera should be looking. The Up X, Y, Z determines which direction up should be for the camera. So if you want the camera to always be looking at 0, 0, 0, do this:

gluLookAt(Camera_X, Camera_Y, Camera_Z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

Note that the above is assuming your up is the positive y axis. I always make the z axis the up and down axis in my programs, though.

EDIT: Eh, also, since gluLookAt needs your x, y, and z. You''l have to calculate them based on your distance from the sun and your current rotation angle. To Get your x coordinate you would go:

x = cos(RotDegrees/180*PI)*DistanceFromSun;

(I'm dividing the degrees by 180 and multiplying by pi to convert them to radians since the C++ trigonometry functions take radians, not degrees) And you would your z coordinate like this:

z = sin(RotDegrees/180*PI)*DistanceFromSun;

Now, this only works assuming your earth is always level with the sun on the y axis. If it's not, then I dunno how to calculate what point your earth is at.

[Edited by - K A Z E on March 14, 2006 8:57:17 AM]

1. 1
2. 2
3. 3
4. 4
Rutin
16
5. 5

• 12
• 9
• 12
• 37
• 12
• ### Forum Statistics

• Total Topics
631417
• Total Posts
2999970
×