🔒 Locked
rendering question
Advertisement
Original Post
ok i still fairly new to opengl so ive been using glut just to get familiar with syntax and so on. now the question im asking is there anyway to render multiple objects using more then one render function. to make what ive just say a little clear, lets say i have a function called renderfunc() which will have an object to be rendered, in the main() i would call glutDisplayFunc(renderfunc), follow by glutIdleFunc(renderfunc to render the renderfunc(). now lets say i want to create another function called renderfunc2() that would have another object to rendered but this time rotated as well. i could rendered this function by calling glutPostRedisplay() and then another glutIdleFunc(renderfunc2) and i would have both objects rendering at the same time. if i wanted to stop the rendering process i would call glutIdleFunc(NULL) and that would stop the rendering process. now my problem if i only wanted to stop one object from rendering what would i do because the glutIdleFunc(NULL) stops all rendering. what i want is something like game engines usually have you know a timed event function func for each object i create so i can control each indepently. if any of ya could point me in the right direction i would be very thankful.
In the beginnig you should only have one function that does rendering. In your case that function is called RenderFunc (right?).
In RenderFunc, you can render all your objects either directly there or by calling other functions to render other objects.
Something like this:
void RenderFunc()
{
translate
render object1
rotate
render object2
....
}
Hope this helps...
In RenderFunc, you can render all your objects either directly there or by calling other functions to render other objects.
Something like this:
void RenderFunc()
{
translate
render object1
rotate
render object2
....
}
Hope this helps...
i got that part but if i want to render another object at a later time for example when i press the down arrow key the object rotates 90deg then stops. in the example you gave the 90deg rotation will continue rotating the object because the rendering loop. now if i put an if statement to try to stop the rotation at a certain point let say if(rotation>=90) set rotation to 0, it would just reset to 0 and start rotating 90deg again which is what i dont want.
Well... all rendering is done in the render function. If you have a keyboard input function that checks for a key press, then that function should just update a variable. Something like this...:
// Global rotation for object 1
float fRotateX = 0.0f;
float fRotateY = 0.0f;
float fRotateZ = 0.0f;
void OnKeyPress(char key)
{
if (key == 'x')
fRotateX += 0.01f;
else if (key == 'X')
fRotateX -= 0.01f;
else if (key == 'y')
fRotateY += 0.01f;
else if (key == 'Y')
fRotateY -= 0.01f;
else if (key == 'z')
fRotateZ += 0.01f;
else if (key == 'Z')
fRotateZ -= 0.01f;
}
void RenderFunc()
{
glPushMatrix();
glRotatef(fRotateX, fRotateY, fRotateZ);
// draw object
glPopMatrix();
}
Does that makes sense?
// Global rotation for object 1
float fRotateX = 0.0f;
float fRotateY = 0.0f;
float fRotateZ = 0.0f;
void OnKeyPress(char key)
{
if (key == 'x')
fRotateX += 0.01f;
else if (key == 'X')
fRotateX -= 0.01f;
else if (key == 'y')
fRotateY += 0.01f;
else if (key == 'Y')
fRotateY -= 0.01f;
else if (key == 'z')
fRotateZ += 0.01f;
else if (key == 'Z')
fRotateZ -= 0.01f;
}
void RenderFunc()
{
glPushMatrix();
glRotatef(fRotateX, fRotateY, fRotateZ);
// draw object
glPopMatrix();
}
Does that makes sense?
yeah that makes alot of sense and in fact is what ive been doing but the problem with that is that it keep rotating whether its negative or positive values and what i want is to rotated to a certain degree then stop. a good example is the psx game devil dice where you press a direction and the dice will rotated 90deg in that direction showing the full 90deg rotation.
If you want to hit a key once and have it rotate a certain amount then you need to set up a state on a key press. Then in an idle function you need to update that state... Somthing like this:
// Globals
int g_nRotateStateX = 0;
float g_fRotateX = 0.0f;
void OnKeyPress(char key)
{
// If the 'X' key was pressed and the object is not already rotating
if (key == 'X' && g_nRotateState == 0)
g_nRotateState = 1;
}
void Idle()
{
if (g_nRotateState == 1)
{
g_fRotateX += 0.01f;
if (g_fRotateX >= 1.57f )
{
g_nRotateStateX = 0; // Stop rotating
g_fRotateX = 1.57f;
}
}
}
void RenderFunc()
{
glPushMatrix();
glRotatef(g_fRotateX, 0.0f, 0.0f);
// draw object
glPopMatrix();
}
Stunderand?
}
}
// Globals
int g_nRotateStateX = 0;
float g_fRotateX = 0.0f;
void OnKeyPress(char key)
{
// If the 'X' key was pressed and the object is not already rotating
if (key == 'X' && g_nRotateState == 0)
g_nRotateState = 1;
}
void Idle()
{
if (g_nRotateState == 1)
{
g_fRotateX += 0.01f;
if (g_fRotateX >= 1.57f )
{
g_nRotateStateX = 0; // Stop rotating
g_fRotateX = 1.57f;
}
}
}
void RenderFunc()
{
glPushMatrix();
glRotatef(g_fRotateX, 0.0f, 0.0f);
// draw object
glPopMatrix();
}
Stunderand?
}
}
I can tell that you are new at programming and that is fine (no shame in learning)!
There is no real difference between a class and a struct except for the fact that structs have public data by default and classes have private data by default (unless otherwise specified).
But there are semantic differences. Generally speaking (and there are exceptions to every rule), structs group non homogenious data together in an organized package. Classes als od that as well as grouping operations on that data. Although you can have functions in structures just like in classes, you should think of classes as a living object that truly understands how to be created, destroyed as well as any other operation you can do to it. (Note to other programmers, please don't fire back with exceptions...!)
In your case, it would make more sense to create a class for your object. And your class should have a constructor and a destructor and at least an initialize, draw and update functions. Your update and draw functions will be called every frame to update the transformations and draw the object.
Then your RenderFunc could draw each instance of every class you create.
Game programming is one of the best avenues to learn programming in my opinion as you have the chance to visually see the results. Pretty soon you will see how easy it will be to start having generic base object classes and specific objects derived from them. Perfect for game programming... But lets not get a head of ourselves...
Good luck!
There is no real difference between a class and a struct except for the fact that structs have public data by default and classes have private data by default (unless otherwise specified).
But there are semantic differences. Generally speaking (and there are exceptions to every rule), structs group non homogenious data together in an organized package. Classes als od that as well as grouping operations on that data. Although you can have functions in structures just like in classes, you should think of classes as a living object that truly understands how to be created, destroyed as well as any other operation you can do to it. (Note to other programmers, please don't fire back with exceptions...!)
In your case, it would make more sense to create a class for your object. And your class should have a constructor and a destructor and at least an initialize, draw and update functions. Your update and draw functions will be called every frame to update the transformations and draw the object.
Then your RenderFunc could draw each instance of every class you create.
Game programming is one of the best avenues to learn programming in my opinion as you have the chance to visually see the results. Pretty soon you will see how easy it will be to start having generic base object classes and specific objects derived from them. Perfect for game programming... But lets not get a head of ourselves...
Good luck!
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement