Sign in to follow this  
Vindicator

OpenGL Planet/moon rotation in OpenGL solar systems

Recommended Posts

Vindicator    134
Hi everyone. =) I am trying to create a solar system in OpenGL, and have some trouble with rotation. I have managed to make planets rotate around the sun, by using glRotatef(v, -0.1 , 1 , 0.2); right before I create the sun and all the planet, and placing the sun with the center at the point 0,0,0. However, what I also want to do is to make it possible for moons to rotate around the rotating planets, and making the planets rotate around their own axis (like they do in reality when switching between day and night - one side isn´t always facing the sun). This is the code for how I did to create the solar system:

//creates a planet, and binds the texture if tex isn´t higher than the texture index.
void createPlanet(GLfloat k, int x, int y, int tex) {
 if (tex < MAX_NO_TEXTURES) { glBindTexture(GL_TEXTURE_2D, texture_id[tex]);};
  glutSolidSphere(k, x, y);
  }


//Draws and rotates the solar system.
void createSolarsystem()
{
 glRotatef(v, -0.1 , 1 , 0.2);
 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
 glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
 
 createPlanet(2.5, 12, 12, 0); //creates the sun (a bit bigger than the planets, texture 0).
    
 glTranslatef(0.0, 0.0, 6.0);
 createPlanet(1.0, 10, 10, 1); //creates the earth, texture 1, 40% of the sun´s size.)
 glTranslatef(0.0, 0.0, -11.0);
 createPlanet(1.0, 12, 12 , 2); //creates mars, texture 2, the same size as the earth).
}




//display function
void display()
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glPushMatrix();
  glRotatef(angleX,1.0,0.0,0.0);
  glRotatef(angleY,0.0,1.0,0.0);
  glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  createSolarsystem(); //creates the solar system
  glPopMatrix();
  glutSwapBuffers();  
}


If any of you have any ideas about how I could do to rotate planets surface and moons, that would be great. Thanks. =)

Share this post


Link to post
Share on other sites
stevenmarky    369
You have your sun at the center of the solar system
and you gave the earth a matrix that makes it rotate with time.
You also have a similar matrix for the moon, but it's relative to itself (which you don't want) so to do it relativly you just multiply the matrices together (earth*moon)=moonabsolutematrix; this is the matrix that you draw the moon with.


Actually it's not a straight matrix multiplication because the orientation of the earth does not affect the orientation of the moon, so in this planet example you want:


vector3 moonDistance(100.0f, 0.0f, 0.0f); // However far away the moon is from the earth
vector3 absoluteMoonPosition=earthmatrix.transform(moonDistance); // Transform the moon position into world coordinates
vector3 moonRotation= what you want (constant speed rotation)
combine the position and rotation here into a single matrix for the moon


Edit: I hope you can understand this, maybe my description isn't as clear as it should be. Basically you need to multiply vectors by matrices. I'm not very experienced with opengl so I can't post code.

Share this post


Link to post
Share on other sites
Vindicator    134
Thanks. =)
I´m not quite sure that I understand this.. I´ve failed the course in Linear Algebra both this autumn and the year before that, so that might be why.
Is there some way to automatically generate the matrix with the earth corodinates, so that I can use it in the formulas you described above, to create the moon matrix?
Because all I did was to create the earth, then rotated it around the sun with glRotate - I never changed its position coordinates manually.

Do you know if it is possible to do it by having a rotation around a planet inside the rotation around the sun instead of using matrix multiplication?

Share this post


Link to post
Share on other sites
stevenmarky    369
I had a quick look at some opengl tutorials.
The easiest way is to do:

glPushMatrix()
// draw Earth here
glPushMatrix()
glTranslatef(10.0f, 0.0f, 0.0f); // The distance from earth to moon
// Draw moon here
glPopMatrix();
glPopMatrix();


This method is fine if your planets are totally spherical/symmetrical.
But if not you will see how the rotation of the moon changes with the earths.
I have no idea how to set the rotation not to be multiplied with the previous matrix in OpenGL yet :/
Wait...I think I've got it:


glPushMatrix();
glTranslate(100.0f, 0.0f, 0.0f); // The distance from sun to earth

glPushMatrix();
glTranslate(10.0f, 0.0f, 0.0f); // The distance from earth to moon
glRotatef(.....) // Your rotation for the moon (increment it each frame)
// Draw the moon here
glPopMatrix();

glRotate(....); // The rotation for the earth (increment it each frame)
// Draw the earth here
glPopMatrix();

Share this post


Link to post
Share on other sites
Vindicator    134
They are spheres... so that won´t be any problem. :)
However, I tried to do like you said:

glPushMatrix();
glTranslatef(0.0, 0.0, 6.0); //earth is 6 away from the sun
glPushMatrix();
glTranslatef(0.0, 0.0, -1.5); //moon is -1.5 away from the center of the earth
glRotatef((1.5 * v), -0.1 , 0.0 , 1); //moon rotation
createPlanet(0.3, 16, 16 , 2); //creates the moon
glPopMatrix();
glRotatef((2 * v), -0.1 , 0.1 , 1); //earth rotation
createPlanet(1.0, 16, 16, 1); //create earth
glPopMatrix();

but it seems like there is still something wrong. The moon is rotating around the sun, just like the earth is doing, and the earth is not rotating either.. so I guess that way didn´t work after all. :i

Share this post


Link to post
Share on other sites
Vindicator    134
I managed to make it work now. =) It wasn´t the code above which was the big problem, but that I had used both glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); and glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); earlier in the code - if I only use glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP), the textures rotate with the planets (although they look a bit worse, but ýou can barely see that because of the shadows).

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  

  • Similar Content

    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
    • By Picpenguin
      Hi
      I'm new to learning OpenGL and still learning C. I'm using SDL2, glew, OpenGL 3.3, linmath and stb_image.
      I started following through learnopengl.com and got through it until I had to load models. The problem is, it uses Assimp for loading models. Assimp is C++ and uses things I don't want in my program (boost for example) and C support doesn't seem that good.
      Things like glVertexAttribPointer and shaders are still confusing to me, but I have to start somewhere right?
      I can't seem to find any good loading/rendering tutorials or source code that is simple to use and easy to understand.
      I have tried this for over a week by myself, searching for solutions but so far no luck. With tinyobjloader-c and project that uses it, FantasyGolfSimulator, I was able to actually load the model with plain color (always the same color no matter what I do) on screen and move it around, but cannot figure out how to use textures or use its multiple textures with it.
      I don't ask much: I just want to load models with textures in them, maybe have lights affect them (directional spotlight etc). Also, some models have multiple parts and multiple textures in them, how can I handle those?
      Are there solutions anywhere?
      Thank you for your time. Sorry if this is a bit confusing, English isn't my native language
    • By dpadam450
      FINALLY, upgrading my engine to openGL 4. I was having some trouble so I started with a stripped down application and was wondering if VAO's are required, because I have a sample working, but if I remove the VAO then it doesn't seem to like drawing my triangle.
  • Popular Now