# 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 on other sites
think im posting in the wrong place?

##### 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 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 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 on other sites
thanks for your reply it is still not going around the earth.. here is what i typed.

void drawTextureSphere(){    glPushMatrix();// EARTHS POSISTIONglTranslatef(0.0, 0.0, 1.0); glPushMatrix();glPushMatrix();// EARTHS SPINglRotatef(thetaSphere,0.0, 1.0, 0.0); //Draw earth hereglBindTexture(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 hereglBindTexture(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 on other sites
glLoadIdentity();// EARTHS POSISTIONglTranslatef(0.0, 0.0, 1.0);  //go to earth position glPushMatrix(); //push position on the stack for later// EARTHS SPINglRotatef(thetaSphere,0.0, 1.0, 0.0); //make it spin//Draw earth hereglBindTexture(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 backglRotatef(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 positioglRotatef(thetaSphere, 0.0, 1.0, 0.0);   //make moon spin around it's own axis//Draw moon hereglBindTexture(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 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 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 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

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 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 on other sites
I have sent you a PM

##### 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 1static GLubyte earthTexture[ImageRows/2] [ImageCols/2] [Image3bytesPerPixel]; // spherestatic 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 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 on other sites
lol, I've asked a few questions in my time, service is free.

Up my rating if you want

Seeya round

- Steve

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627676
• Total Posts
2978582
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 11
• 12
• 10
• 12
• 22