It is currently Fri Apr 28, 2017 2:48 pm

All times are UTC - 5 hours




 Page 1 of 1 [ 9 posts ] 
Author Message
 Post subject: Adding a Physics component to the Engine
PostPosted: Tue Feb 13, 2007 7:08 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
This class is used to create a flat surface in 3D that can have any size and resolution.


Offline
 Profile  
 
 Post subject:
PostPosted: Tue Mar 18, 2008 8:36 pm 

Joined: Mon Jul 02, 2007 10:06 am
Posts: 5
I think the FlatGrid::Render function is not fuly visible.
I think this is the code, I will leave it here to someone who needs it:

bool FlatGrid::Render(){
   
   switch(m_Renderer){
      case GR_OPENGL: {
         glColor3f(m_Color.m_fRed, m_Color.m_fGreen, m_Color.m_fBlue);
         glNormal3f(0,1,0);

         for (int z = 0; z < m_iDivZ; z++){
            glBegin(GL_TRIANGLE_STRIP);
            for (int x = 0; x < (m_iDivX+1); x++){
               glVertex3f(m_V[x + z   *(m_iDivX+1)].m_fx,m_V[x + z   *(m_iDivX+1)].m_fy, m_V[x + z   *(m_iDivX+1)].m_fz);
               glVertex3f(m_V[x +(z+1)*(m_iDivX+1)].m_fx,m_V[x +(z+1)*(m_iDivX+1)].m_fy, m_V[x +(z+1)*(m_iDivX+1)].m_fz);
            }
            glEnd();
         }
         break;                     
      }
   }

   return true;
}


Offline
 Profile  
 
 Post subject:
PostPosted: Wed Mar 19, 2008 7:18 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
I forgot to mention this in the VMK series, but rather than having to type out these long lines of code for x,y,z coordinates, you can use the vector equivalent for a lot of the openGL function calls. So instead of this:
glVertex3f(m_V[x + z   *(m_iDivX+1)].m_fx,m_V[x + z   *(m_iDivX+1)].m_fy, m_V[x + z   *(m_iDivX+1)].m_fz);
glVertex3f(m_V[x +(z+1)*(m_iDivX+1)].m_fx,m_V[x +(z+1)*(m_iDivX+1)].m_fy, m_V[x +(z+1)*(m_iDivX+1)].m_fz);


you can use

glVertex3fv(m_V[x+ z   *(m_iDivX+1)].m_f);
glVertex3fv(m_V[x+(z+1)*(m_iDivX+1)].m_f);


if you create a union inside your Vertex3 class similar to the one we created in the Vector3 class like so:

union {
   float m_f[3];
   struct {
      float m_fx;
      float m_fy;
      float m_fz;
   };
};


Offline
 Profile  
 
 Post subject:
PostPosted: Wed Mar 19, 2008 9:30 am 

Joined: Mon Jul 02, 2007 10:06 am
Posts: 5
Wow brilliant!
Using glVertex3fv with vectors realy helps and simplifies a lot!
Thanks Marek


Offline
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 3:33 am 

Joined: Fri Jan 04, 2008 2:11 pm
Posts: 72
my vector3 class doesn't have a union like this :

union {
   float m_f[3];
   struct {
      float m_fx;
      float m_fy;
      float m_fz;
   };
};


did u add this in later vmk's?? i'm curently doing the scene graph vmk


Offline
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 7:22 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
ir2pi wrote:
my vector3 class doesn't have a union ... did u add this in later vmk's??


I guess so. It might have been done on the DVD or in a later VMK.... I don't remember anymore. You can add the union now and take advantage of the shorter notation.


Offline
 Profile  
 
 Post subject: Darvin dangerous idea.
PostPosted: Wed Sep 03, 2008 9:30 am 

Joined: Wed Aug 06, 2008 7:53 pm
Posts: 182
Location: Russia
Oh, the idea to use union is a good shot.

One annoying remark. This code won’t work correctly if data alignment (DA) for members of structures is set on more then 4-byte boundaries. On Win32 (VC++ /Zp option) default value is 4 and this is OK. But on 64-bit platforms DA can be 8 or even 16. If DA > 4 you cannot use parts of union interchangeably, because members of our union will have unmatched memory placement.
To ensure that Vector3 always has 4-byte DA we should enclose our class in two special lines of code:


#include <pshpack4.h> // same as #pragma pack(push,4), but more portable

class Vector3 {
    ...
    union {
        float m_f[3];
        struct {
            float m_fx;
            float m_fy;
            float m_fz;
        };
    };
    ...
};

#include <poppack.h> // same as #pragma pack(pop), but more portable



These two headers are standard and designed to be inserted in any part of the code (they don’t have “onceâ€


Offline
 Profile  
 
 Post subject: Too much typing or Reducio ad absurdum
PostPosted: Fri Oct 03, 2008 11:26 pm 

Joined: Wed Aug 06, 2008 7:53 pm
Posts: 182
Location: Russia
When you go on a trip a guide can be big help.

The prolific idea to incorporate a union inside primitives (i.e. vertex, vector, color, etc) can be deepened even further, if we incorporate the next code inside each of them:

operator float * () { return m_v; } // the same code for all primitives!

An advantage is that we can write the shortest code possible.

For instance, the Vector3 will look like:

class Vector3 {
    ...
    union {
        float m_fv[3];
        struct {
            float m_fx;
            float m_fy;
            float m_fz;
        };
    };
    ...
    operator float * () { return m_fv; }
    ...
};

operator float * () is so called conversion operator. Love it!

Thereby, in the beginning we have had a tiresome error prone code:
glVertex3f( v1.m_x, v1.m_y, v2.m_z );
Then after the union was incorporated, we have got:
glVertex3fv( v1.m_v );
and at the end, after introduction of operator float * ():
glVertex3fv ( v1 );
The last one is the most natural way to express our intention. 8)

This subject was also discussed here:
http://www.marek-knows.com/phpBB3/viewtopic.php?t=121


Offline
 Profile  
 
 Post subject: Glamour life? Why not?
PostPosted: Sat Oct 04, 2008 9:08 pm 

Joined: Wed Aug 06, 2008 7:53 pm
Posts: 182
Location: Russia
Before it's too late, meaning, until we don't change the rendering code of FlatGrid by adding textures, some cool effects can be reached.
"VMK 17 - Flat Grid class" supplies us with following flat grid rendering code (as many of us, I have a slightly different implementation, but for our purpose the details is unimportant):
void FlatGrid::Render()
{
    glNormal3f( 0.0f, 1.0f, 0.0f );
    glColor3fv( m_color.v );

    for (size_t z = 0; z < m_divZ; ++z )
    {
        glBegin( GL_TRIANGLE_STRIP );
        for (size_t x = 0; x <= m_divX; ++x )
        {
            // THIS IS THE RIGHT PLACE TO PLAY
            glVertex3fv( Cell( x, z + 0 ).v );
            glVertex3fv( Cell( x, z + 1 ).v );
        }
        glEnd();
    }
}


What we need is to add some glColor() invocations before or between glVertex() calls. Like this:
        ...
        glBegin( GL_TRIANGLE_STRIP );
        for (size_t x = 0; x <= m_divX; ++x )
        {
                (x&1)? glColor3f( 0.9f, 0.0f, 0.0f ) : glColor3f( 0.0f, 0.9f, 0.0f );
//              glColor3f( 0.2f, 0.4f, 0.2f ); // cool! temporary
            glVertex3fv( Cell( x, z + 0 ).v );
//              !(x&1)? glColor3f( 0.2f, 0.8f, 0.8f ) : glColor3f( 0.2f, 0.8f, 0.0f );
//              glColor3f( 0.2f, 0.8f, 0.4f ); // cool! temporary
            glVertex3fv( Cell( x, z + 1 ).v );
        }
        glEnd();
        ...

Comment / uncomment different lines to get different coloring of the grid. Please, look at the picture below to better understand the idea ( and don't laugh at my FPS - This is not Sparta, this is a laptop ;) ).

Image


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 9 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  

cron