• Advertisement
Sign in to follow this  

Order of code in OpenGL

This topic is 4271 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm not quite sure about what order the program will perform code contained in the 'int DrawGLScene(GLvoid)' block. Each time the contents of the block are run, I need certain variables to take on values based upon many other variables in the code. Q1) Does the program run through the whole contents of the block line by line, evaluating each one in turn, then when it reaches the end starts again? Q2) Will an arrangement like this (see below) alter the variables each time the code is run, based on the values at the end of the previous run through? GLFloat V_1 = V_2 + 2; GLFloat V_2 = V_1 * 5; This is an oversimplification of the actual equations, but will the program store the variable values until the value is changed (in order of code)? Or does it all happen at once? My questions seem really garbled, but I am finding it quite hard to explain. If anyone could advise me, I would be grateful.

Share this post


Link to post
Share on other sites
Advertisement
A1: yes, like other normal c code it does, but it doesn't start again by itself, DrawGLScene is part of a loop that runs it over and over again.

A2: it depends on where they are defined, openGL does not manage any variables you might set up, it only does that to the different states you can set.
So if you need those variables every time, you have to set them up as globals.
On another note, the equations you presented will produce strange results indeed.
GLFloat V_2 = (V_2 + 2) * 5;
this would be a simpler way to do it

Share this post


Link to post
Share on other sites
I was afraid you might need more info. I'm afraid this could be rather horrendous.


Earth_Acc_Sun = ((Earth_Mass * Sun_Mass * 6.673 * (10 ^ -11)) / ((((Earth_Pos_X - Sun_Pos_X) * (Earth_Pos_X - Sun_Pos_X)) + ((Earth_Pos_Y - Sun_Pos_Y) * (Earth_Pos_Y - Sun_Pos_Y)) + ((Earth_Pos_Z - Sun_Pos_Z) * (Earth_Pos_Z - Sun_Pos_Z))) * Earth_Mass)); // calculates acceleration due to Sun upon Earth

Earth_K_Sun = sqrt((Earth_Acc_Sun * Earth_Acc_Sun) / (((Earth_Pos_X - Sun_Pos_X) * (Earth_Pos_X - Sun_Pos_X)) + ((Earth_Pos_Y - Sun_Pos_Y) * (Earth_Pos_Y - Sun_Pos_Y)) + ((Earth_Pos_Z - Sun_Pos_Z) * (Earth_Pos_Z - Sun_Pos_Z)))); // Calculates the scaling for the position vector to generate acceleration vector

Earth_Acc_Moon = ((Earth_Mass * Moon_Mass * 6.673 * (10 ^ -11)) / ((((Earth_Pos_X - Moon_Pos_X) * (Earth_Pos_X - Moon_Pos_X)) + ((Earth_Pos_Y - Moon_Pos_Y) * (Earth_Pos_Y - Moon_Pos_Y)) + ((Earth_Pos_Z - Moon_Pos_Z) * (Earth_Pos_Z - Moon_Pos_Z))) * Earth_Mass));

Earth_K_Moon = sqrt((Earth_Acc_Sun * Earth_Acc_Sun) / (((Earth_Pos_X - Moon_Pos_X) * (Earth_Pos_X - Moon_Pos_X)) + ((Earth_Pos_Y - Moon_Pos_Y) * (Earth_Pos_Y - Moon_Pos_Y)) + ((Earth_Pos_Z - Moon_Pos_Z) * (Earth_Pos_Z - Moon_Pos_Z))));
.
. // Various calculations of new velocities and positions
.
translatef(Earth_Pos_X, Earth_Pos_Y, Earth_Pos_Z)
.
.Other planets/moons
.





That is what I've written so far. What I am making is a simulation of the Earth, Moon, Sun system that responds according to physical laws. I will specify the masses of the bodies, initial positions and velocities. Then, for each timestep, the code should calculate, based on current positions, velocities, gravitational effects etc, the new position after that timestep, then repeat.

If all goes to plan it should display the various planets orbiting around each other (Hopefully :) )

The example *was* rather simplified, because I hadn't got around to working out the precise mathematics. Including all the related code for what I want to do in one line (as you suggest) might be feasible, but hopefully what I am trying to do will have the same effect and be more understandable for me.

Also, does '6.673 * (10 ^ -11)' work? (It should be the gravitational constant)

Many thanks for any advice.



[Edited by - evrae on August 10, 2006 8:49:03 AM]

Share this post


Link to post
Share on other sites
Regarding question 1, both the draw and update (function that handles key presses) functions are called by a loop; everything in these functions happen over and over again until the user presses escape.

Regarding question 2, and your code; if values like Earth_Acc_Sun are declared outside the draw and or update functions, then they will retain their values and your variables will be altered each frame.

Regarding your code; it seems it has reached the point where using floats to manage the properties of the plantets is insufficient to manage the complexity of the problem. I suggest using classes to represent each planet. You can then update the planets in the update function and pass it the appropriate delta time.

Regarding the gravitational constant, try:
const double kGravity = 6.673e-11;

Cheers,
- llvllatrix

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Thanks. From what you've said it sounds as if what I am doing *should* be possible.
However, I've finished writing the mathematical code, which I'm pretty certain is correct, and when I run my program, nothing appears. Just black all the way :(
This is my code from the drawGLScene section. All variables are declared right up the top, under the #includes.

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer



Earth_Acc_Sun = ((Earth_Mass * Sun_Mass * 6.673 * (10 ^ -11)) / ((((Earth_Pos_X - Sun_Pos_X) * (Earth_Pos_X - Sun_Pos_X)) + ((Earth_Pos_Y - Sun_Pos_Y) * (Earth_Pos_Y - Sun_Pos_Y)) + ((Earth_Pos_Z - Sun_Pos_Z) * (Earth_Pos_Z - Sun_Pos_Z))) * Earth_Mass)); // acceleration
Earth_K_Sun = ((-1) * sqrt((Earth_Acc_Sun * Earth_Acc_Sun) / (((Earth_Pos_X - Sun_Pos_X) * (Earth_Pos_X - Sun_Pos_X)) + ((Earth_Pos_Y - Sun_Pos_Y) * (Earth_Pos_Y - Sun_Pos_Y)) + ((Earth_Pos_Z - Sun_Pos_Z) * (Earth_Pos_Z - Sun_Pos_Z))))); // acceleration scaling 'K'

Earth_Acc_Moon = ((Earth_Mass * Moon_Mass * 6.673 * (10 ^ -11)) / ((((Earth_Pos_X - Moon_Pos_X) * (Earth_Pos_X - Moon_Pos_X)) + ((Earth_Pos_Y - Moon_Pos_Y) * (Earth_Pos_Y - Moon_Pos_Y)) + ((Earth_Pos_Z - Moon_Pos_Z) * (Earth_Pos_Z - Moon_Pos_Z))) * Earth_Mass));
Earth_K_Moon = ((-1) * sqrt((Earth_Acc_Moon * Earth_Acc_Moon) / (((Earth_Pos_X - Moon_Pos_X) * (Earth_Pos_X - Moon_Pos_X)) + ((Earth_Pos_Y - Moon_Pos_Y) * (Earth_Pos_Y - Moon_Pos_Y)) + ((Earth_Pos_Z - Moon_Pos_Z) * (Earth_Pos_Z - Moon_Pos_Z)))));

Sun_Acc_Earth = ((Earth_Mass * Sun_Mass * 6.673 * (10 ^ -11)) / ((((Sun_Pos_X - Earth_Pos_X) * (Sun_Pos_X - Earth_Pos_X)) + ((Sun_Pos_Y - Earth_Pos_Y) * (Sun_Pos_Y - Earth_Pos_Y)) + ((Sun_Pos_Z - Earth_Pos_Z) * (Sun_Pos_Z - Earth_Pos_Z))) * Sun_Mass));
Sun_K_Earth = ((-1) * sqrt((Sun_Acc_Earth * Sun_Acc_Earth) / (((Sun_Pos_X - Earth_Pos_X) * (Sun_Pos_X - Earth_Pos_X)) + ((Sun_Pos_Y - Earth_Pos_Y) * (Sun_Pos_Y - Earth_Pos_Y)) + ((Sun_Pos_Z - Earth_Pos_Z) * (Sun_Pos_Z - Earth_Pos_Z)))));

Sun_Acc_Moon = ((Sun_Mass * Moon_Mass * 6.673 * (10 ^ -11)) / ((((Sun_Pos_X - Moon_Pos_X) * (Sun_Pos_X - Moon_Pos_X)) + ((Sun_Pos_Y - Moon_Pos_Y) * (Sun_Pos_Y - Moon_Pos_Y)) + ((Sun_Pos_Z - Moon_Pos_Z) * (Sun_Pos_Z - Moon_Pos_Z))) * Sun_Mass));
Sun_K_Moon = ((-1) * sqrt((Sun_Acc_Moon * Sun_Acc_Moon) / (((Sun_Pos_X - Moon_Pos_X) * (Sun_Pos_X - Moon_Pos_X)) + ((Sun_Pos_Y - Moon_Pos_Y) * (Sun_Pos_Y - Moon_Pos_Y)) + ((Sun_Pos_Z - Moon_Pos_Z) * (Sun_Pos_Z - Moon_Pos_Z)))));

Moon_Acc_Earth = ((Earth_Mass * Moon_Mass * 6.673 * (10 ^ -11)) / ((((Moon_Pos_X - Earth_Pos_X) * (Moon_Pos_X - Earth_Pos_X)) + ((Moon_Pos_Y - Earth_Pos_Y) * (Moon_Pos_Y - Earth_Pos_Y)) + ((Moon_Pos_Z - Earth_Pos_Z) * (Moon_Pos_Z - Earth_Pos_Z))) * Moon_Mass));
Moon_K_Earth = ((-1) * sqrt((Moon_Acc_Earth * Moon_Acc_Earth) / (((Moon_Pos_X - Earth_Pos_X) * (Moon_Pos_X - Earth_Pos_X)) + ((Moon_Pos_Y - Earth_Pos_Y) * (Moon_Pos_Y - Earth_Pos_Y)) + ((Moon_Pos_Z - Earth_Pos_Z) * (Moon_Pos_Z - Earth_Pos_Z)))));

Moon_Acc_Sun = ((Sun_Mass * Moon_Mass * 6.673 * (10 ^ -11)) / ((((Moon_Pos_X - Sun_Pos_X) * (Moon_Pos_X - Sun_Pos_X)) + ((Moon_Pos_Y - Sun_Pos_Y) * (Moon_Pos_Y - Sun_Pos_Y)) + ((Moon_Pos_Z - Sun_Pos_Z) * (Moon_Pos_Z - Sun_Pos_Z))) * Moon_Mass));
Moon_K_Sun = ((-1) * sqrt((Moon_Acc_Sun * Moon_Acc_Sun) / (((Moon_Pos_X - Sun_Pos_X) * (Moon_Pos_X - Sun_Pos_X)) + ((Moon_Pos_Y - Sun_Pos_Y) * (Moon_Pos_Y - Sun_Pos_Y)) + ((Moon_Pos_Z - Sun_Pos_Z) * (Moon_Pos_Z - Sun_Pos_Z)))));

Earth_Acc_X = ((Earth_K_Moon * (Earth_Pos_X - Moon_Pos_X)) + (Earth_K_Sun * (Earth_Pos_X - Sun_Pos_X))); // X Acceleration
Earth_Acc_Y = ((Earth_K_Moon * (Earth_Pos_Y - Moon_Pos_Y)) + (Earth_K_Sun * (Earth_Pos_Y - Sun_Pos_Y))); // Y Acceleration
Earth_Acc_Z = ((Earth_K_Moon * (Earth_Pos_Z - Moon_Pos_Z)) + (Earth_K_Sun * (Earth_Pos_Z - Sun_Pos_Z))); // Z Acceleration
Moon_Acc_X = ((Moon_K_Earth * (Moon_Pos_X - Earth_Pos_X)) + (Moon_K_Sun * (Moon_Pos_X - Sun_Pos_X))); // X Acceleration
Moon_Acc_Y = ((Moon_K_Earth * (Moon_Pos_Y - Earth_Pos_Y)) + (Moon_K_Sun * (Moon_Pos_Y - Sun_Pos_Y)));
Moon_Acc_Z = ((Moon_K_Earth * (Moon_Pos_Z - Earth_Pos_Z)) + (Moon_K_Sun * (Moon_Pos_Z - Sun_Pos_Z)));
Sun_Acc_X = ((Sun_K_Earth * (Sun_Pos_X - Earth_Pos_X)) + (Sun_K_Moon * (Sun_Pos_X - Moon_Pos_X)));
Sun_Acc_Y = ((Sun_K_Earth * (Sun_Pos_Y - Earth_Pos_Y)) + (Sun_K_Moon * (Sun_Pos_Y - Moon_Pos_Y)));
Sun_Acc_Z = ((Sun_K_Earth * (Sun_Pos_Z - Earth_Pos_Z)) + (Sun_K_Moon * (Sun_Pos_Z - Moon_Pos_Z)));

Earth_Pos_X = ((0.5 * Earth_Acc_X * Timestep * Timestep) + (Earth_Vel_X * Timestep) + Earth_Pos_X); // X Position
Earth_Pos_Y = ((0.5 * Earth_Acc_Y * Timestep * Timestep) + (Earth_Vel_Y * Timestep) + Earth_Pos_Y); // Y Position
Earth_Pos_Z = ((0.5 * Earth_Acc_Z * Timestep * Timestep) + (Earth_Vel_Z * Timestep) + Earth_Pos_Z); // Z Position
Moon_Pos_X = ((0.5 * Moon_Acc_X * Timestep * Timestep) + (Moon_Vel_X * Timestep) + Moon_Pos_X); // X Position
Moon_Pos_Y = ((0.5 * Moon_Acc_Y * Timestep * Timestep) + (Moon_Vel_Y * Timestep) + Moon_Pos_Y); // Y Position
Moon_Pos_Z = ((0.5 * Moon_Acc_Z * Timestep * Timestep) + (Moon_Vel_Z * Timestep) + Moon_Pos_Z); // Z Position
Sun_Pos_X = ((0.5 * Sun_Acc_X * Timestep * Timestep) + (Sun_Vel_X * Timestep) + Sun_Pos_X); // X Position
Sun_Pos_Y = ((0.5 * Sun_Acc_Y * Timestep * Timestep) + (Sun_Vel_Y * Timestep) + Sun_Pos_Y); // Y Position
Sun_Pos_Z = ((0.5 * Sun_Acc_Z * Timestep * Timestep) + (Sun_Vel_Z * Timestep) + Sun_Pos_Z); // Z Position

Earth_Vel_X = (Earth_Vel_X + (Timestep * Earth_Acc_X)); // X Velocity
Earth_Vel_Y = (Earth_Vel_Y + (Timestep * Earth_Acc_Y)); // Y Velocity
Earth_Vel_Z = (Earth_Vel_Z + (Timestep * Earth_Acc_Z)); // Z Velocity
Moon_Vel_X = (Moon_Vel_X + (Timestep * Moon_Acc_X)); // X Velocity
Moon_Vel_Y = (Moon_Vel_Y + (Timestep * Moon_Acc_Y)); // Y Velocity
Moon_Vel_Z = (Moon_Vel_Z + (Timestep * Moon_Acc_Z)); // Z Velocity
Sun_Vel_X = (Sun_Vel_X + (Timestep * Sun_Acc_X)); // X Velocity
Sun_Vel_Y = (Sun_Vel_Y + (Timestep * Sun_Acc_Y)); // Y Velocity
Sun_Vel_Z = (Sun_Vel_Z + (Timestep * Sun_Acc_Z)); // Z Velocity

glLoadIdentity(); // Reset The View
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTranslatef(Sun_Pos_X, Sun_Pos_Y, Sun_Pos_Z);
gluSphere(quadratic,109.0f,32,32);

glLoadIdentity();

glTranslatef(Earth_Pos_X, Earth_Pos_Y, Earth_Pos_Z);

// glRotatef(246.5,1,0,0);
{
glBindTexture(GL_TEXTURE_2D, texture[0]);
gluSphere(quadratic,1.0f,16,16); // Draw A Sphere With A Radius Of 1 And 16 Longitude And 16 Latitude Segments

// glRotatef(Earth_day,0,0,1);
// Earth_year += 0.001*speed;
// Earth_day +=0.3652425*speed;
}
glLoadIdentity();
{
glBindTexture(GL_TEXTURE_2D, texture[2]);
//
glTranslatef(Moon_Pos_X, Moon_Pos_Y, Moon_Pos_Z);

// glRotatef(1.5424,1,0,0);
// {glRotatef(Luna_month,0,1,0);
gluSphere(quadratic,0.2724f,16,16);
// Luna_month += 0.027322*speed ;
// Luna_tilt += 0.027268*speed ; //
}

return TRUE; // Everything Went OK
}



This class thing sounds like it might be worth looking into. Do you have a link to a guide on it - my knowledge of C++ is *really* sketchy. I know enough to mess around a little, but not a proper knowledge of it :( My entire body of code is pretty much the one from NeHe's lesson 10, with some modifications to the specific shapes being drawn, keyboard commands etc.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
From what you've said it sounds as if what I am doing *should* be possible.


Of course :)

Quote:
Original post by Anonymous Poster
However, I've finished writing the mathematical code, which I'm pretty certain is correct, and when I run my program, nothing appears. Just black all the way :(


This is quite common with graphical programming. There are quite a few things that can cause nothing to show up. Once you get something to display on the screen, thins should be easier however.

Unfortunately, I have no time to look at your code right now. I should be able to take a look later on today. Could you also post your initialization code? For example, the code where you load your textures and initialize OpenGL.

Quote:
Original post by Anonymous Poster
This class thing sounds like it might be worth looking into. Do you have a link to a guide on it - my knowledge of C++ is *really* sketchy. I know enough to mess around a little, but not a proper knowledge of it :( My entire body of code is pretty much the one from NeHe's lesson 10, with some modifications to the specific shapes being drawn, keyboard commands etc.


Classes are fundamental to C++ programming and programming in general. The idea is that you can define a "thing" that has variables called properties and functions that act on these variables, which are called methods. You can then create these things like you would variables. For example, you could create a planet with mass, position, velocity, and acceleration properties. You can then create an update method, which updates the properties of the planet every frame.

I found this tutorial on the net:
http://cplus.about.com/od/beginnerctutorial/l/aa041002a.htm
These books are also avaliable for free online:
http://www.steveheller.com/whos/
http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html#HTMLFormat

Cheers,
- llvllatrix

Share this post


Link to post
Share on other sites
I'm not exactly sure, but is your question about whether variables are persistent between calls?

The function you define to be the DrawGLScene() is just a regular function. EVERY time the screen needs to be drawn, the function is called again.

What you are saying is that the variables will not be affected by the previous run.

void x()
{
int a = 0;
int b = a + 1;
a++;
// Line Referenced.
}


a and b are both going to be the exact same on the line referenced, the fact that this is OpenGL changes nothing.

To your Q1, Yes, it will, but when it reaches the end, it will clear all variables, and then begin again.
For Q2, the variables will not be altered each run. In fact, the code you gave in your original post should not even compile, as one of the variables is referenced before being defined.

If you want that behavior, make the variables static.

Share this post


Link to post
Share on other sites
Oh, I read the code again, and it seems I misunderstood your question. The code you posted later in the post seems that assuming all the math is correct, should work.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement