Sign in to follow this  
civrob

OpenGL I give up [Almost did] THANX EVERY1!!! esp thorin

Recommended Posts

Thanks everyone 4 ur help. My verdict on openGL having only ever done java before is that it is a mind puzzle! but things are starting to make sense now :D slowley. Dont even want to thing about lighting lol! >I finally admit it ive been beaten :-( >rotating the moon around the earth.... I have been at it for >hours on end [16hours] and although its simple I cant figure >it out :-( the files are at. >[url]http://www.nonlinea.co.uk/myOpenGL/earth.zip[/url] [Edited by - civrob on December 1, 2004 9:20:05 PM]

Share this post


Link to post
Share on other sites
The basic set of transformations that need to happen are:
1) translate moon out from origin the distance it is from the earth
2) rotate moon
3) translate moon by the distance the earth is from the origin.

Rotations are always about the origin. So to get the moon to rotate about the earth you need to make the earth the origin. This is in essence what step one is trying to do. Then you rotate the moon about the origin the amount you want. After that you will have to translate the earth back to its location in the solar system along with the moon. This is done in step 3.

To really make this work well the best way to go, in my opinion, is to get/write a scene graph. This makes doing this type of rotation very easy.

Chris

Share this post


Link to post
Share on other sites
thanks for your reply. It makes sense that the origin needs to be the earth. I tried setting it so that it is the eart but it just carried on as it is?
not sure im changing the right value? Does not matter wich one i change it still rotates on it self

thetaSphere is the Earth
thetaMoon is the Moon


....................x.........y.....z...??
glRotatef(thetaSphere, 0.0, 0.0, 0.0);

Share this post


Link to post
Share on other sites
Here is how I would do it, the earth I have at some spot other than the origin.

glPushMatrix()
glTranslate (to the earth position)
glPushMatrix()
glpushMatrix()
glrotate(the earths spin)
//Draw earth here
glPopMatrix()
glTranslate(the distance from the earth to the moon)
glRotate(the rotation of the moon around the earth)
//Draw moon here
glPopMatrix()
glPopMatrix()

since you are translating you would draw the moon/earth as if they were at the origin where its commented to do so.

BTW when you post code put it in [ source] [ /source] tags and dont post the entire program.

Share this post


Link to post
Share on other sites
thanks for your reply it is still not going around the earth.. here is what i typed.


void drawTextureSphere()
{
glPushMatrix();
// EARTHS POSISTION
glTranslatef(0.0, 0.0, 1.0);
glPushMatrix();
glPushMatrix();
// EARTHS SPIN
glRotatef(thetaSphere,0.0, 1.0, 0.0);
//Draw earth here
glBindTexture(GL_TEXTURE_2D, 5);
GLUquadricObj * object;
object= gluNewQuadric();
gluQuadricDrawStyle(object, GLU_FILL);
gluQuadricTexture( object, GL_TRUE );
gluSphere(object, 1.0, 50, 50);
gluDeleteQuadric(object);
glPopMatrix();
//the distance from the earth to the moon
glTranslatef(2.0, 0.0, 0.5);
glRotatef(thetaSphere, 0.0, 1.0, 0.0);
//Draw moon here
glBindTexture(GL_TEXTURE_2D, 6);
GLUquadricObj * object1;
object1= gluNewQuadric();
gluQuadricDrawStyle(object, GLU_FILL);
gluQuadricTexture( object, GL_TRUE );
gluSphere(object1, 0.25, 10, 10);
gluDeleteQuadric(object1);
glPopMatrix();
glPopMatrix();
}




Been working on this for 12 hours and I really dont want to give up :-)

Share this post


Link to post
Share on other sites

glLoadIdentity();

// EARTHS POSISTION
glTranslatef(0.0, 0.0, 1.0); //go to earth position
glPushMatrix(); //push position on the stack for later

// EARTHS SPIN
glRotatef(thetaSphere,0.0, 1.0, 0.0); //make it spin

//Draw earth here
glBindTexture(GL_TEXTURE_2D, 5);
GLUquadricObj * object;
object= gluNewQuadric();
gluQuadricDrawstyle(object, GLU_FILL);
gluQuadricTexture( object, GL_TRUE );
gluSphere(object, 1.0, 50, 50);
gluDeleteQuadric(object);

glPopMatrix(); //get our earth position back
glRotatef(angle, 0.0, 1.0, 0.0) //make moon rotate around earth

//the distance from the earth to the moon
glTranslatef(2.0, 0.0, 0.5); //go to our moon positio
glRotatef(thetaSphere, 0.0, 1.0, 0.0); //make moon spin around it's own axis

//Draw moon here
glBindTexture(GL_TEXTURE_2D, 6);
GLUquadricObj * object1;
object1= gluNewQuadric();
gluQuadricDrawstyle(object, GLU_FILL);
gluQuadricTexture( object, GL_TRUE );
gluSphere(object1, 0.25, 10, 10);
gluDeleteQuadric(object1);


dunno if that'll work.. been up for 36h :/

Share this post


Link to post
Share on other sites
doesnt work gives me a blank screen!!! I give up feel really gutted I mean how hard can drawing a stupid circle around another circle be?

[Edited by - civrob on December 1, 2004 7:50:44 PM]

Share this post


Link to post
Share on other sites
give up no solution found :-( can someone please delete my last posts cos they posted but did not include my text :-( really not my night tonight.

Share this post


Link to post
Share on other sites
To rotate the moon around the earth, you first have to understand how the viewing matrix works. Every point in your "universe" is multipled by a viewing matrix, which moves and rotates the points around. You can control this viewing matrix by issuing calls to glTranslate and glRotate (you can also use other more advanced functions, but don't worry about them right now).

Now, if you want to rotate an object in place, you would type something like
glRotate(x, 1, 0, 0);
. If you wanted to move an object, you'd type
glTranslate(x, y, z);
. These calls actually create a new matrix, and multiply it by the view matrix. Remember, when matrices are concerned A*B != B*A, so order is important!

Since you probably want to place each object at a different place in the universe, OpenGL gives you a matrix stack. You can call glPushMatrix(), which will save a copy of the view matrix on a stack. With a copy of the view matrix saved, you can now apply custom translations and rotations to a perticular object without screwing up the rest of the universe. Once your done with the object, call glPopMatrix(), which restores the saved view matrix.

So you want to create an sphere in the center of the screen, and rotate another sphere around it.

you'd

// make sure your matrix is clean by loading the identity matrix into it
glLoadIdentity();

drawSphere();

// save the view matrix
glPushMatrix();

// move the moon sphere out 10 units
glTranslate(10, 0, 0);

// now you will rotate the sphere around point (0, 0, 0)
// because you've translated the points, then rotated them in
// there translated position
glRotate(angx, angy, angz)

drawSphere();

// restore old stack (if you forget this step your program will crash)
glPopMatrix();

glFlush();

Share this post


Link to post
Share on other sites
I understand it all apart from when im rotating the moon, do i need to save the variables? just that no matter what I do i cant get it to spin around the planet :-(

Share this post


Link to post
Share on other sites
I managed to get it all working, Have a play round and see if you can see the logic in what Ive done...

The key is realising that the rotate / translate commands do not work with world axes, they work with local axes instead.

So thinking about it that way...

To make something rotate about a point.

You first translate to the point.

Then rotate so the object is looking at where it should be in orbit.
glRotatef(thetaSphere,0,1,0);

Translate to the point...
glTranslatef(0, 0, -3);

And undo the rotation. (if you want to)
glRotatef(-thetaSphere,0,1,0);


#pragma hdrstop
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>

#include "glut.h"

//---------------------------------------------------------------------------


static void myInit();
static void myDisplay(void);
static void myIdle(void);
static void myReshape(int w, int h);

#define ImageRows 512
#define ImageCols 512
#define Image3bytesPerPixel 3
#define Image1bytePerPixel 1

static GLubyte earthTexture[ImageRows/2] [ImageCols/2] [Image3bytesPerPixel]; // sphere

static GLsizei width, height, bytes;

void loadTexture(const no, const char *iFilename, GLsizei iRows, GLsizei iCols, GLsizei iBytesPerPixel, void *iPixels);
void loadImage(const char *iFilename, GLsizei iRows, GLsizei iCols, GLsizei iBytesPerPixel, void *iPixels);

typedef float vertex[3];
typedef float vector[3];

static GLfloat vertices[][3] = { {-1.0,-1.0,+1.0}, {-1.0,+1.0,+1.0}, {+1.0,+1.0,+1.0}, {+1.0,-1.0,+1.0},
{-1.0,-1.0,-1.0}, {-1.0,+1.0,-1.0}, {+1.0,+1.0,-1.0}, {+1.0,-1.0,-1.0} };


static GLfloat thetaSphere;
static GLfloat thetaMoon;

static GLfloat nearPlane = 4.0;
static GLfloat farPlane = 20.0;
static GLfloat distance = 10;


void loadImage(const char *iFilename, GLsizei iRows, GLsizei iCols, GLsizei iBytesPerPixel, void *iPixels)
{
FILE *iFile;

if ((iFile=fopen(iFilename,"r"))!=NULL)
{
fread (iPixels, (long) iRows*iCols*iBytesPerPixel, 1 , iFile); // read file
fclose (iFile);

width=iCols; height=iRows; bytes=iBytesPerPixel;
}
}

void loadTexture(const n, const char *iFilename, GLsizei iRows, GLsizei iCols, GLsizei iBytesPerPixel, void *iPixels)
{
loadImage(iFilename, iRows, iCols, iBytesPerPixel, iPixels);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);

glBindTexture(GL_TEXTURE_2D, n);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

GLenum GL_format;
if (iBytesPerPixel==1) GL_format=GL_LUMINANCE;
else GL_format=GL_RGB;
glTexImage2D(GL_TEXTURE_2D, 0, GL_format, iRows, iCols, 0, GL_format, GL_UNSIGNED_BYTE, iPixels);
};

void drawTextureSphere()
{
glBindTexture(GL_TEXTURE_2D, 5);

GLUquadricObj * object;
object= gluNewQuadric();
gluQuadricDrawStyle(object, GLU_FILL);
gluQuadricTexture( object, GL_TRUE );
gluSphere(object, 1.0, 50, 50);

gluDeleteQuadric(object);
}


void drawTextureMoon()
{
glBindTexture(GL_TEXTURE_2D, 6);

GLUquadricObj * object;
object= gluNewQuadric();
gluQuadricDrawStyle(object, GLU_FILL);
gluQuadricTexture( object, GL_TRUE );
gluSphere(object, 0.25, 10, 10);

gluDeleteQuadric(object);
}




int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

glutInitWindowSize (600, 400);
glutInitWindowPosition( 100, 100);
glutCreateWindow("VT Demonstration 6");

glutDisplayFunc(myDisplay);
glutIdleFunc(myIdle);
glutReshapeFunc(myReshape);

myInit();
glutMainLoop();

return 0;
}

static void myInit()
{

glEnable(GL_DEPTH_TEST); /* enable z buffer */

glClearColor (0.1, 0.1, 0.1, 1.0); //background is grey
glColor3f (1.0, 1.0, 1.0); //drawing colour is white

loadTexture(5, "earth.raw", ImageRows/2, ImageCols/2, Image3bytesPerPixel, earthTexture);
loadTexture(6, "moon.raw", ImageRows/2, ImageCols/2, Image3bytesPerPixel, earthTexture);

glEnable(GL_TEXTURE_2D);
}

static void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix(); // Save the matrix
{
glTranslatef(0, 0, -5); // Translate to the center of the system
glRotatef(45,1,0,0); // Rotate so we can see it from above

glRotatef(thetaSphere,0,1,0); // Rotate the axis so that z is pointing towards the earth
glTranslatef(0, 0, -3); // Translate along z (towards the earth)
glRotatef(-thetaSphere,0,1,0); // Rotate the axis back to where they were

glPushMatrix(); // Save the matrix
{
glRotatef(thetaSphere*5,0,1,0); // Rotate the axis to the direction that the earth is spinning
glRotatef(90,1,0,0); // Rotate the axis so that the earth is drawn upwards
drawTextureSphere(); // Draw earth
}
glPopMatrix(); // Restore the matrix

glPushMatrix(); // Save the matrix
{
glRotatef(thetaMoon,0,1,0); // Rotate the axis so that z is pointing towards the moon
glTranslatef(0, 0, -1.5); // Translate along z (towards the moon)
glRotatef(-thetaMoon,0,1,0); // Rotate the axis back to where they were

glPushMatrix(); // Save the matrix
{
glRotatef(thetaMoon*5,0,1,0); // Rotate the axis to the direction that the earth is spinning
glRotatef(90,1,0,0); // Rotate the axis so that the earth is drawn upwards
drawTextureMoon(); // Draw moon
}
glPopMatrix(); // Restore the matrix
}
glPopMatrix(); // Restore the matrix
}
glPopMatrix(); // Restore the matrix

glFlush();

glutSwapBuffers();
}


void myIdle()
{
thetaSphere += 1.1;
if ( thetaSphere > 360.0) thetaSphere -= 360.0;

thetaMoon -= 3;
if ( thetaMoon > 360.0) thetaMoon -= 360.0;

glutPostRedisplay();
}

void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(45,w/h,nearPlane,farPlane);

glMatrixMode(GL_MODELVIEW); // set and leave in MODELVIUEW mode
glLoadIdentity();

glTranslatef(0.0, 0.0, -distance); //set camera position

//redraw the objects
glutPostRedisplay();
}



Just mess around with it. I'm sure you'll see how it all works

Share this post


Link to post
Share on other sites
Thanks so much, yeah i started to realise that from the replies here its hard to get your head around. The closest I got was it rotating around half of it then dissapearing and coming out other end and doing it again lol it does some weird stuff.

Thankyou so much you have ended my 20 hour marathon and i cannot thank you enough.

:D :D where do I send the cash to?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this