only 1 render function?

Started by
14 comments, last by Lord_Evil 17 years, 1 month ago
Hi Smitty

Yes you're right, I alredy knew that but wrote it wrong here :D

Is there any sample code which calls an external function so I can see that it SHOULD really work?
Advertisement
I think a lot of people are a bit confused as to exactly what problem you're having. To me it just sounds like you don't understand how functions work (forgive me if I'm wrong). For example, what exactly do you mean by an "external function"?

As Simian Man said, there's nothing special or magical about calling opengl from different functions. So for example:

void RenderScene(){    glClear(GL_COLOR_BUFFER_BIT);    RenderTriangle();    Sdl_GL_SwapBuffers();}void RenderTriangle(){    glBegin(GL_TRIANGLES);    glVertex3f(0,1,0);    glVertex3f(-1,0,0);    glVertex3f(0,0,1);    glEnd();}


is the same as:

void RenderScene(){    glClear(GL_COLOR_BUFFER_BIT);    glBegin(GL_TRIANGLES);    glVertex3f(0,1,0);    glVertex3f(-1,0,0);    glVertex3f(0,0,1);    glEnd();    Sdl_GL_SwapBuffers();}


So you get the same result whether the code is in one function or two (note that I didn't test this specific code).

I suggest you check out the tutorials on http://nehe.gamedev.net/ . They're how a lot of people (myself included) started out. They have detailed explanations and will show you how opengl code can be split between functions (one for initialisation, another for shutting down, others for rendering, etc).
@Simian Man:

this is true, your code does work but i alredy knew that.
now because all are confused, forget what i told.

my situation:

I want to draw a line (or whatever else) as long as I
press a button but i do not want to to check for the button state
IN the while-loop.

the exact problem is, that i swap the buffer before any
events are handled, so nothing will be drawn.

(doubleBuffered) i tried to add glutSwapBuffers() in the key event handler
but it looks crappy....

When you do your event handling, store the key state in a bool (true for pressed, false for not).

When rendering, check the state of the bool. If it's false, don't render the line.
<quote>When rendering, check the state of the bool. If it's false, don't render the line.</quote>

i exactely try to get rid of that.


but i see, you don't got any solutions (or it isn't possible).

thanks everybody
If you want to display a line / polygon / mesh only while a button is pressed you have 2 options:

1) Just as mentioned before check a bool i the render loop. The bool is updated by press / release events. This is the easiest way to go.

2) Use a function pointer or a delegate. This means your press / release events change the delegate or function that renders the line etc. The onPress() event would set the function pointer to a function that renders the line, the onRelease() event would set it to a function that doesn't render anything. In your render loop you just call the function pointed to by the function pointer. Note that this approach is much more difficult to implement.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!

This topic is closed to new replies.

Advertisement