Look at trasformation

Started by
3 comments, last by Anthony_N 18 years, 1 month ago
Hi all, I am doing the solar system example from a book and one of the taskes is to get the view point to view from the earth to the sun, then from the moon to the earth etc. but i am having a little trouble. can any one help

#include <windows.h>
#pragma warning(disable : 4244)     // MIPS
#pragma warning(disable : 4136)     // X86
#pragma warning(disable : 4051)     // ALPHA

//#include <glos.h>
#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;
	}

	glLoadIdentity();

	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();


	
	glFlush();
	auxSwapBuffers();
}

void OpenGLInit(void)
{
	glShadeModel(GL_FLAT);
	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);
	
	glLoadIdentity();
	
	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 13, 2006 2:35:29 PM]
Advertisement
Quote:Original post by Anthony_N
Hi all,

I am doing the solar system example from a book and one of the taskes is to get the view port to view from the earth to the sun, then from the moon to the earth etc. but i am having a little trouble. can any one help

*** Source Snippet Removed ***
What sort of trouble are you having?
I can get the viewport to rotate around the sun but not with the earth, Ie getting the coords from the earth and moon
I thing that the solution is the :

gluLookAt(float fEyex, float fEyey, float fEyez, float fAtx, float fAty, float fAtz, float fUpx, float fUpy, float fupz) command.

The first three parameters are the parameters of the camera position,
The second three parameters are the direction that it's looking,
and the other three are the parameters that tell to OpenGL Where is the
up side of our view.

to use this command you have to put it after the glLoadIdentity() command.

you can put at the camera position the position of the planet that the camera
is on.
At the view direction the position of the planet that you're looking at.

and there you have your camera.

glLookAt(earth.x, earth.y, earth.z, moon.x, moon.y, moon.z, 0.0f, 1.0f, 0.0f);

we are looking up to the y axis.

sorry about my english :)

thanks for the reply guys. I will give it a go

This topic is closed to new replies.

Advertisement