Dark Bit Factory & Gravity

PROGRAMMING => C / C++ /C# => Topic started by: NaniBot on July 10, 2013

Title: Drawing multiple scenes?
Post by: NaniBot on July 10, 2013
Im running visual c++ 98 and Im kinda stuck. This is the code that I have in my drawglscene.
This code only rotates a 3D triangle. What I want to know ---> Is there anyway by which I draw things one after another? Like drawing a 3D rotating triangle first then after some seconds drawing a rotating cube?

-----------------------------------THE CODE-----------------------------------------------------------------------------------

int DrawGLScene(GLvoid)                           {
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   // Clear Screen And Depth Buffer
   glLoadIdentity();
   glTranslatef(0.0,0.0f,-6.0f);
   glRotatef(rtri,1.0f,1.0f,1.0f);
   
   glBegin(GL_TRIANGLES);
     glColor3f(1.0f,0.0f,0.0f);                  // Red
      glVertex3f( 0.0f, 1.0f, 0.0f);               // Top Of Triangle (Front)
      glColor3f(0.0f,1.0f,0.0f);                  // Green
      glVertex3f(-1.0f,-1.0f, 1.0f);               // Left Of Triangle (Front)
      glColor3f(0.0f,0.0f,1.0f);                  // Blue
      glVertex3f( 1.0f,-1.0f, 1.0f);               // Right Of Triangle (Front)
      glColor3f(1.0f,0.0f,0.0f);                  // Red
      glVertex3f( 0.0f, 1.0f, 0.0f);               // Top Of Triangle (Right)
      glColor3f(0.0f,0.0f,1.0f);                  // Blue
      glVertex3f( 1.0f,-1.0f, 1.0f);               // Left Of Triangle (Right)
      glColor3f(0.0f,1.0f,0.0f);                  // Green
      glVertex3f( 1.0f,-1.0f, -1.0f);               // Right Of Triangle (Right)
      glColor3f(1.0f,0.0f,0.0f);                  // Red
      glVertex3f( 0.0f, 1.0f, 0.0f);               // Top Of Triangle (Back)
      glColor3f(0.0f,1.0f,0.0f);                  // Green
      glVertex3f( 1.0f,-1.0f, -1.0f);               // Left Of Triangle (Back)
      glColor3f(0.0f,0.0f,1.0f);                  // Blue
      glVertex3f(-1.0f,-1.0f, -1.0f);               // Right Of Triangle (Back)
      glColor3f(1.0f,0.0f,0.0f);                  // Red
      glVertex3f( 0.0f, 1.0f, 0.0f);               // Top Of Triangle (Left)
      glColor3f(0.0f,0.0f,1.0f);                  // Blue
      glVertex3f(-1.0f,-1.0f,-1.0f);               // Left Of Triangle (Left)
      glColor3f(0.0f,1.0f,0.0f);                  // Green
      glVertex3f(-1.0f,-1.0f, 1.0f);      
     glEnd();
    glTranslatef(3.0f,0.0f,0.0f);
   rtri+=0.5f;
   return TRUE;
}

Title: Re: Drawing multiple scenes?
Post by: combatking0 on July 10, 2013
You could set a timer which changes a variable which controls which effect is being displayed, then using a switch() call, you can select which effect runs:
Code: [Select]
switch(effectMode){
case 0:
// Code for a starfield
break;
case 1:
// Code for a spinning cube
break;
case 2:
// Code for a plasma effect
break;
}

That's my approach, but there may be a more efficient way.
Title: Re: Drawing multiple scenes?
Post by: ninogenio on July 10, 2013
yeah as combatking said,

you are going too need some sort of trigger too switch too each event.. a timer is the best option. also the switch method that ck suggested is good. just too expand a touch.

purely pesudo.

Code: [Select]
  void appinit( void ){
        //all init based stuff
        .....
        .....
        .....
        /////////////////////
        int effectMode = 0;
        systime timer;
        starttimer(timer);
  }

  void processStuff( void ){
       gettimesms(timer);
       switch(effectMode){
       case 0:
              // Code for a starfield
              if ( timer > 2000 ) effectMode += 1;
        break;
        case 1:
             // Code for a spinning cube
             if ( timer > 4000 ) effectMode += 1;
        break;
        case 2:
             // Code for a plasma effect
             if ( timer > 6000 ) effectMode += 1;
        break;
        if ( effectMode > 2 ){
             effectMode = 0;
             starttimer(timer);
        }
}

would switch effects every 2 seconds and loop back round.
Title: Re: Drawing multiple scenes?
Post by: combatking0 on July 10, 2013
Or you could have the timer set up after the switch statement to increment the effectMode variable, and reduce it to 0 when it goes above a certain number to make it loop.

There's a few ways of approaching this.
Title: Re: Drawing multiple scenes?
Post by: Canopy on July 10, 2013
how you approach it is more of a conceptual thing..

myself i've made something like a scenegraph with "layers" that can be turned on or off.. and can have their order changed..

you could call these pages or anything else, partly its just how you organise things which in turn depends on your coding style and how you think
Title: Re: Drawing multiple scenes?
Post by: ninogenio on July 10, 2013
@Canopy so you mean sort of like a state machine?.. me and jim worked on a state machine in cpp years ago im still using the core of it atm.. where each demo effect is a virtual base class that the state machine loads in and stores on a stack. then you can override the state machine and change scene order or leave it too run in order.. it takes care of initialization and clean up on its own also..
Title: Re: Drawing multiple scenes?
Post by: Canopy on July 11, 2013
yup almost exactly like that..  i'm not using base classes though, am managing it all through an abstracted object struct type. (i'm not using c++)

the hierarchy goes
 - scenes manager (root)
    - layers
       - layer 1
           -layer tranforms
             - model + transform
       - layer 2
       - layer n..
    - objects
      - object  (vao/vbo)
      - models (geometry data buffers)

layers get processed top to bottom in the linked list and each layer is a scene of its own.. you can choose to draw them in order or just one.
there's no other ordering, but it wouldnt be hard to add, with such a small number and infrequent changes a 'bubble sort' after priority change would be easy

each layer and model within gets processed bottom to top (matrix multiplication order) and transforms are cached when possible with them with a 'dirty flag' so they only get recalculated when the data changes

i ended up like this accidentally when working out how to reuse some geometry in one scene, and keep an FPS counter across multple scenes that change underneath. i read about all kinds of scene management for a week or two, had a bit of an epiphany and ended up just taking the bits i wanted from the various things i read. its hybrid of a scene manager and something that manages objects globally.

any model, VBO, VOA, shader or anything else is within the objects bit which is freed during cleanup.
this allows model and buffer object reuse and allows nice cleanup. also some nice helpers for allocation too.

sounds busy, but its all really just a couple linked lists, the dirty flag stops unwanted processing of matrix stuff, keeping it C without C++ makes it a little more complicated to begin with, but its blindingly fast. once winter comes i'll be doing lots more work on it.

its in a state where i'm implementing frame buffer object support (render layer to a framebuffer rather than screen) and i'll be also adding pixel buffer support too.

right now i'm using keyboard + mouse input for manipulating scenes. i hope to add some kind of timeline/scripting capability, including any input adding events to the timeline. but i'm only adding functionality when i actually use it, rather than inventing things for the sake of it.

its super light weight, super flexible, and easy to add to. my goal is to have a really simple, general purpose set of functions to allow me to jump start into projects quickly.


Title: Re: Drawing multiple scenes?
Post by: ninogenio on July 11, 2013
 :goodpost:

thats pretty much more or less the same stuff as ive been bashing a way for weeks on. im also trying too integrate fbo's vbo's and vao's in a clean way so in future i can try my hand at things like deferred shading etc.

im in the process of completely ditching deprecated gl and doing everything by hand. i usually try and add at least one small thing a day and once a week spend a day going over the whole engine tidying up all the loose ends/trying too make everything make as much sense as possible.

when building a dynamic engine that can be used for years on something as diverse as modern opengl.. good layered structure is everything. a few weeks or even months spend building a good engine will pay dividends later
Title: Re: Drawing multiple scenes?
Post by: Canopy on July 11, 2013
cheers.. i was worried it was a bit waffley! :)

exactly my motivation, something lightweight (trying not to call it an engine after reading this http://scientificninja.com/blog/write-games-not-engines) .. so i'm calling it a library of reusable functions..

i started out by going down the opengl es road before changing to modern desktop gl, so i'm also not using any deprecated functions.. which makes life a lot harder in some cases as you have to buffer most things. (but ultimately, much faster)

i've been making little test apps for each thing i'm adding, call them micro, single effect demo's if you like. its also like my own little SDK.

i suspect whenver the next coding challenge rears its head (not til winter please!) i'll get a lot of work done. i learned a lot last time, along with having a lot of grief with VBO/VAO stuff, now thats in my library i dont have so many worries as its all looked after for me.
Title: Re: Drawing multiple scenes?
Post by: ash on July 13, 2013
Right now I'm slowly progressing on my first demo, and for timing I came with a simple solution.
I'll show how I would code your particular case.
Let me assume you want to show your triangle for 2 seconds, then your cube forever.

Code: [Select]
CLIP clips[] = {
    ShowTriangle,
    ShowCube
};
here ShowTriangle is a method that will use gl-methods to display triangle in your case.
similarly ShowCube draws cube.

for now assume CLIP is a datatype/typedef that somehow satisfies the compiler to compile the code.

How the actual ShowTriangle looks like:
Code: [Select]
bool ShowTriangle(float time, <what ever additional parameters you might need>)
{
    //actually draw you triangle here.
    //make use of the 'time' parameter in case you want it for animation/rotation.

    //return whether this Triangle animation is complete. (true if complete, false if still progressing)
    return time>2.0;
}
Let me explain what the above method means to me.
It does all the drawing stuff (could be animated drawing as well). Then it returns a boolean value indicating whether the piece of animation is complete; meaning, triangle animation is complete and can continue to next animation (the cube in your case).

...And who's going to manage calling these functions and do the timings?
The real little code that does the magic is,
Code: [Select]
int currentClip = 0; //initially start with the first clip (the triangle)
DoDraw(float time)
{
    CLIP clip = clips[currentClip];    //get the right clip..
    bool done = clip( time );          //...and execute it.
    if(done){    // move on to the next clip if its time.
        currentClip++;
    }
}
This simple code avoids ugly switch cases and costly timers too. All you need to do is give the current time and call the DoDraw every time. Also when ever you want to add a new clip just create that method and add that method to the clips array; that's all simple, clear and easy!

BTW it's in pure C language only!!


Oh yea, and the CLIP is defined as:
Code: [Select]
typedef bool (*CLIP)(float time);

..understand the code before you try it; will give you a good conceptual idea, maybe you'll write a better one than this.
Share your thoughts.
Title: Re: Drawing multiple scenes?
Post by: ninogenio on July 13, 2013
hey ash, yeah thats the rough basics of what me and canopy was discussing,

and its an excellent idea maybe a little hard for a beginner too wrap there head around though.. my implementation was too go the cpp route and do pretty much the same as that but with classes so each member of *CLIP* as it were, is a class whose own members are init(), main(), shutdown()..
and CLIP is a state machine class who internally processes its dynamically added members.

nice add mate!

Title: Re: Drawing multiple scenes?
Post by: NaniBot on July 14, 2013
yeah as combatking said,

you are going too need some sort of trigger too switch too each event.. a timer is the best option. also the switch method that ck suggested is good. just too expand a touch.

purely pesudo.

Code: [Select]
  void appinit( void ){
        //all init based stuff
        .....
        .....
        .....
        /////////////////////
        int effectMode = 0;
        systime timer;
        starttimer(timer);
  }

  void processStuff( void ){
       gettimesms(timer);
       switch(effectMode){
       case 0:
              // Code for a starfield
              if ( timer > 2000 ) effectMode += 1;
        break;
        case 1:
             // Code for a spinning cube
             if ( timer > 4000 ) effectMode += 1;
        break;
        case 2:
             // Code for a plasma effect
             if ( timer > 6000 ) effectMode += 1;
        break;
        if ( effectMode > 2 ){
             effectMode = 0;
             starttimer(timer);
        }
}

would switch effects every 2 seconds and loop back round.


Im very sorry if im being a noob but how do I place it in my source c++ file? And also do I have to include any more header files (xx.h)?
Title: Re: Drawing multiple scenes?
Post by: ninogenio on July 14, 2013
Quote
Im very sorry if im being a noob but how do I place it in my source c++ file

nothing too be sorry for were all here too help and learn mate,

if you can provide me with the whole of your main.cpp/c project file ill show you exactly how too integrate that switch statment.. it can be a bit tricky too get your head around at first but well worth it too have multiple scenes...
Title: Re: Drawing multiple scenes?
Post by: NaniBot on July 16, 2013
Here is my c++ project file. (Dbf did not allow me to upload the .cpp file so i have uploaded a txt file)
Title: Re: Drawing multiple scenes?
Post by: ninogenio on July 16, 2013
here you go mate just quickly built.. ive commented the source too hopefully make it quite clear whats going on.. if there is anything your not sure of please don't hesitate too ask what its doing. ive attached your main source file with the changes along with a little exe that switches scenes every 3 seconds and loops back round on the 3rd scene.
Title: Re: Drawing multiple scenes?
Post by: Canopy on July 17, 2013
k++ nino :)
Title: Re: Drawing multiple scenes?
Post by: NaniBot on July 17, 2013
here you go mate just quickly built.. ive commented the source too hopefully make it quite clear whats going on.. if there is anything your not sure of please don't hesitate too ask what its doing. ive attached your main source file with the changes along with a little exe that switches scenes every 3 seconds and loops back round on the 3rd scene.

Thank you very much.  :updance: