PDA

View Full Version : solar system opengl please help asap



sid1990
01-18-2012, 01:32 PM
Hey there I am new to this forum and am somewhat of a begineer to opengl. I have a cw deadline by friday it would be great if i could get some guidance before it. (also im not looking to cheat, just some gentle spoon feeding and guidance will be great)

for my class assignment I have been asked make a solar system with opengl and have various planets rotating around the sun.

ive been having problems with quite a few things with this:

1) Im not sure how to distance the planets away from the sun.
I have used a translate but it causes the rotate to be unproportioned. i think the planets may be at different heights to one another and i would also like to change that so they are level.they maybe some higher or lower than some.

2) I have no idea how to make the orbits around the planets rotate around those individual planets and not just the sun(this ones a puzzler).


here is source code
#include <GL/glut.h>
using namespace std;
float angle[4];
GLUquadricObj*sphere;

void drawsun()
{
glPushMatrix();
glRotatef(angle[0],0.0,1.0,0.0);
gluSphere(sphere, 0.25, 50, 50);
glPopMatrix();
}
void draworangeplanet()
{
glPushMatrix();
glTranslatef(1.5,0.0,0.0);
glRotatef(angle[1],0.0,-1.0,0.0);
gluSphere(sphere,0.2, 50, 50);
glPopMatrix();
}
void drawpinkplanet()
{
glPushMatrix();
glTranslatef(3.0,0.0,1.0);
glRotatef(angle[1],0.0,-1.0,0.0);
gluSphere(sphere,0.2, 50, 50);
glPopMatrix();
}
void draworbit()
{
glPushMatrix();
glTranslatef(1.5,0.0,0.0);
glRotatef(angle[2],0.0,1.0,0.0);
gluSphere(sphere,0.1, 50, 50);
glPopMatrix();
}
void draworbit2()
{
glPushMatrix();
glTranslatef(3.0,0.0,1.0);
glRotatef(angle[2],0.0,1.0,0.0);
gluSphere(sphere,0.1, 50, 50);
glPopMatrix();
}
void rotate()
{
angle[0]+=0.2;
if (angle[0]>=360)
angle[0]-=360;

angle[1]+=0.3;
if (angle[1]>=360)
angle[1]-=360;
glutPostRedisplay();

angle[2]+=0.8;
if (angle[2]>=360)
angle[2]-=360;
glutPostRedisplay();
}
void display()
{

glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(3.0,3.0,3.0, 0.0,0.0,0.0, 0.0,1.0,0.0);
GLfloat light_pos[] = {0.0,9.5,0.0, 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
GLfloat ambient[] = {0.7,0.7,0.7};
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
GLfloat specular[] = {1.0,1.0,1.0};
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
//GLfloat diffuse[] = {1.0,1.0,1.0};
//glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);

glPushMatrix();
glRotatef(angle[0],0.0,1.0,0.0);
drawsun();
glPopMatrix();

glPushMatrix();
glRotatef(angle[1],0.0,1.0,0.0);
draworangeplanet();
draworbit();
glPopMatrix();

glPushMatrix();
glTranslatef(-0.5,-1.0,-1.0);
glRotatef(angle[1],0.0,-1.0,0.0);
drawpinkplanet();
draworbit2();
glPopMatrix();

glutSwapBuffers();
}

void init(void)
{
glMatrixMode(GL_PROJECTION);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLoadIdentity();
glOrtho(-4.0,4.0,-4.0,4.0,0.0,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
sphere = gluNewQuadric();
gluQuadricDrawStyle(sphere,GLU_FILL);
gluQuadricNormals(sphere,GLU_SMOOTH);
}
int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(1500,1100);
glutInitWindowPosition(100,100);
glutCreateWindow("Quadrics");
init();
glutDisplayFunc(display);
glutIdleFunc(rotate);
glutMainLoop();
}

sid1990
01-18-2012, 01:36 PM
oh yes and the linker files incase you need them are:

-lglut32 -lglu32 -lopengl32 -lwinmm -lgdi32

mobeen
01-18-2012, 09:16 PM
Two things.
1) Please use code or spoler tags when pasting code.
2) Try google and a couple of good reference implementations pop out like
a) http://www.math.ucsd.edu/~sbuss/MathCG/OpenGLsoft/
b) http://salahuddin66.blogspot.com/2008/07/simple-solar-system-in-opengl.html
c) http://www.csse.monash.edu.au/~jonmc/CSE3313/Resources/SampleCode/index.html