Quad Pang
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Public Types | Public Member Functions | Private Member Functions | Private Attributes
Box Class Reference

The Box is a holder of all other Quads. More...

#include <Box.h>

Inheritance diagram for Box:
Quad_impl Quad_impl Quad Quad Quad Quad

List of all members.

Public Types

enum  Part {
  FLOOR = 0, WALL1, WALL2, WALL3,
  WALL4, ROOF, FLOOR = 0, WALL1,
  WALL2, WALL3, WALL4, ROOF
}
 Identifiers of the box parts. More...
enum  Part {
  FLOOR = 0, WALL1, WALL2, WALL3,
  WALL4, ROOF, FLOOR = 0, WALL1,
  WALL2, WALL3, WALL4, ROOF
}
 Identifiers of the box parts. More...

Public Member Functions

 Box ()
const double * Dim () const
 Box dimensions.
bool HasHitti () const
 Whether box will display place where it has been hit by the ball.
void HideWall (Part wall)
 Wall closest to viewer should be hidden.
 Box ()
const double * Dim () const
 Box dimensions.
bool HasHitti () const
 Whether box will display place where it has been hit by the ball.
void HideWall (Part wall)
 Wall closest to viewer should be hidden.
Override Quad_impl methods
bool Init (XRes *resource, XCongo *xcongo)
 Box object will be initied with values from resource.
bool Display ()
const char * ClassName () const
 Used for RTTI.
bool Init (XRes *resource, XCongo *xcongo)
 Read Quad from resource file.
bool Display ()
 Draw itself in global cotext. Since quads are intented to be used with opengl, drawing context is global.
const char * ClassName () const
 Used for RTTI.

Private Member Functions

bool LoadTexture (XRes *resource, const char *name, GLuint *id)
 Load texture bitmap.
bool LoadTexture (XRes *resource, const char *name, GLuint *id)
 Load texture bitmap.

Private Attributes

Part mWallToHide
 Wall that will not be displayed.
double mDim [3]
 Dimensions of the box.
int mbHitti
 Whether place where the box has been hit by the ball will be highlighted or not.
GLuint mFloorTexId
 id of floor texture
GLuint mBackFloorTexId
 id of ground level texture
GLuint mWallsTexId
 id of walls texture
double mWallsBitmapCoef
 how many times texture will be applied in Z coordinate
double mWallOpacity
 control wall opacity
Floor scheme related attributes

These Floor scheme can be used instead of texture. It can be controled through LUA.

int mbFields
 Whether scheme is enabled.
int mnFields [2]
 Number of fields in two dimensions X and Z.
double mFieldWidth [2]
 Width of field in each domension.
double mFieldLineWidth
 Width of the line separator.
double mFieldColor [3]
 Color of the field.

Detailed Description

The Box is a holder of all other Quads.

Definition at line 17 of file Box.h.


Member Enumeration Documentation

enum Box::Part

Identifiers of the box parts.

Enumerator:
FLOOR 
WALL1 
WALL2 
WALL3 
WALL4 
ROOF 
FLOOR 
WALL1 
WALL2 
WALL3 
WALL4 
ROOF 

Definition at line 23 of file Box.h.

              {
        FLOOR = 0,
        WALL1,
        WALL2,
        WALL3,
        WALL4,
        ROOF
    };
enum Box::Part

Identifiers of the box parts.

Enumerator:
FLOOR 
WALL1 
WALL2 
WALL3 
WALL4 
ROOF 
FLOOR 
WALL1 
WALL2 
WALL3 
WALL4 
ROOF 

Definition at line 23 of file Box.h.

              {
        FLOOR = 0,
        WALL1,
        WALL2,
        WALL3,
        WALL4,
        ROOF
    };

Constructor & Destructor Documentation

Box::Box ( )

Definition at line 17 of file Box.cpp.

References mWallOpacity, mWallsBitmapCoef, mWallToHide, and WALL4.

Box::Box ( )

Member Function Documentation

const char * Box::ClassName ( ) const [virtual]

Used for RTTI.

Returns:
Name of the actual class.
See also:
Factory

Reimplemented from Quad_impl.

Definition at line 419 of file Box.cpp.

{
    return "Box";
}
const char* Box::ClassName ( ) const [virtual]

Used for RTTI.

Returns:
Name of the actual class.
See also:
Factory

Reimplemented from Quad_impl.

const double * Box::Dim ( ) const

Box dimensions.

Returns:
Vector of dimensions in class internal memory

Definition at line 424 of file Box.cpp.

References mDim.

Referenced by CollisionMaster::Collision().

{
    return mDim;
}
const double* Box::Dim ( ) const

Box dimensions.

Returns:
Vector of dimensions in class internal memory
bool Box::Display ( ) [virtual]

Draw itself in global cotext. Since quads are intented to be used with opengl, drawing context is global.

Returns:
true if ok, false on error.

Reimplemented from Quad_impl.

bool Box::Display ( ) [virtual]

When displaying box we need to take care of following

  • hiding wall facking the viewer
  • make walls very transparent from outside and less transparent from inside

Reimplemented from Quad_impl.

Definition at line 190 of file Box.cpp.

References FLOOR, mBackFloorTexId, mbFields, Quad_impl::mColor, mDim, mFieldColor, mFieldLineWidth, mFieldWidth, mFloorTexId, mnFields, mWallOpacity, mWallsBitmapCoef, mWallsTexId, mWallToHide, ROOF, WALL1, WALL4, and x.

{
    // define colors, material and lights
    GLfloat opacity = 0.3;
    GLfloat mat_specular[] = { mColor[0]/2, mColor[1]/2, mColor[2]/2, opacity };
    GLfloat mat_diffuse[] = { mColor[0]/4, mColor[1]/4, mColor[2]/4, 1.0 };
    GLfloat mat_diffuseWall[] = { mColor[0]/4, mColor[1]/4, mColor[2]/4, mWallOpacity };
    GLfloat mat_diffuseback[] = { mColor[0]/4, mColor[1]/4, mColor[2]/4, opacity };
    GLfloat mat_diffuseLIG[] = { mColor[0]*2, mColor[1]*2, mColor[2]*2, 1.0 };
    GLfloat mat_diffusebackLIG[] = { mColor[0]/6, mColor[1]/6, mColor[2]/6, opacity };
      
    GLfloat mat_ambient[] = { mColor[0], mColor[1], mColor[2], opacity };
    GLfloat mat_ambientLIG[] = { mColor[0]*2, mColor[1]*2, mColor[2]*2, opacity };
    
    GLfloat mat_shininess[] = { 50.0 };
   
    // set blend algorithm  
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    // use two sided lighting
    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);

    // set material values
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_diffuse);
    glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffusebackLIG);
    glMaterialfv(GL_BACK, GL_DIFFUSE, mat_diffusebackLIG);

    // polygons are drawn in clockwise order
    glFrontFace(GL_CW);
 
    // set normals for all FLOOR, WALL1, WALL2, WALL3, WALL4 and ROOF
    GLfloat normals[][3] = { { 0.0, 1.0, 0.0 }, 
                 { 0.0, 0.0, -1.0 }, 
                 { -1.0, 0.0, 0.0 },
                 { 1.0, 0.0, 0.0 },
                 { -1.0, 0.0, 0.0 },
                 { 0.0, -1.0, 0.0 } };

    // simplify access to box dimension 
    GLfloat
        w0 = mDim[0],
        w1 = mDim[1],
        w2 = mDim[2];
    
    // all vertices combination 
    GLfloat 
        wb0[] = { 0.0, 0.0, 0.0 },
        wb1[] = { w0, 0.0, 0.0 },
        wb2[] = { 0.0, w1, 0.0 },   
        wb3[] = { w0, w1, 0.0 },    
        wb4[] = { 0.0, 0.0, w2 },
        wb5[] = { w0, 0.0, w2 },
        wb6[] = { 0.0, w1, w2 },
        wb7[] = { w0, w1, w2 };

    // vertices for each side
    GLfloat* vertices[][4] = { 
                 { wb0, wb1, wb5, wb4 }, //< floor
                 { wb4, wb5, wb7, wb6 }, //< wall 1
                 { wb5, wb1, wb3, wb7 }, //< wall 2
                 { wb0, wb4, wb6, wb2 }, //< wall 3
                 { wb1, wb0, wb2, wb3 }  //< wall 4
                 };                           
                                                        
    // Draw ground surface.
    // It will be transparently. Depth mask will be disabled
    // to make depth buffer read only and make objects that are
    // further visible
    glEnable(GL_BLEND);
    glDepthMask(GL_FALSE);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
    glMaterialfv(GL_BACK, GL_DIFFUSE, mat_diffusebackLIG);
    
    // enable textures
    glEnable(GL_TEXTURE_2D);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

    glBindTexture(GL_TEXTURE_2D, mBackFloorTexId);
    glBegin(GL_POLYGON);
        int coef = 2;
        int coef2 = 8;
        glNormal3fv(normals[FLOOR]);
        glTexCoord2f(1.0 * coef2, 1.0 * coef2);
        glVertex3f(-w0 * coef, -0.1, -w2 * coef);

        glNormal3fv(normals[FLOOR]);   
        glTexCoord2f(1.0 * coef2, 0.0);     
        glVertex3f(w0 * coef, -0.1, -w2 * coef);

        glNormal3fv(normals[FLOOR]);
        glTexCoord2f(0.0, 0.0);
        glVertex3f(w0 * coef, -0.1, w2 * coef);

        glNormal3fv(normals[FLOOR]);
        glTexCoord2f(0.0, 1.0 * coef2);
        glVertex3f(-w0 * coef, -0.1 ,w2 * coef);
    glEnd();

    // Draw BOX floor 
    glBindTexture(GL_TEXTURE_2D, mFloorTexId);

    glBegin(GL_POLYGON);
        double mFloorBitmapCoefX = 2.0;
        double mFloorBitmapCoefZ = 2.0;
        glNormal3fv(normals[FLOOR]);
        glTexCoord2f(mFloorBitmapCoefX, mFloorBitmapCoefZ);
        glVertex3fv(vertices[FLOOR][0]);

        glNormal3fv(normals[FLOOR]);   
        glTexCoord2f(mFloorBitmapCoefX, 0.0);     
        glVertex3fv(vertices[FLOOR][1]);

        glNormal3fv(normals[FLOOR]);
        glTexCoord2f(0.0, 0.0);
        glVertex3fv(vertices[FLOOR][2]);

        glNormal3fv(normals[FLOOR]);
        glTexCoord2f(0.0, mFloorBitmapCoefZ);
        glVertex3fv(vertices[FLOOR][3]);
    glEnd();

    // Draw Walls
    glBindTexture(GL_TEXTURE_2D, mWallsTexId);

    for (Part i=WALL1; i<=WALL4; i = Part((int(i)+1) ) )
    {
        if (i == mWallToHide)
            continue;
         
        glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuseWall);
        glMaterialfv(GL_BACK, GL_DIFFUSE, mat_diffusebackLIG);
        glBegin(GL_POLYGON);
            glNormal3fv(normals[i]);
            glTexCoord2f(mWallsBitmapCoef, 0.0);
            glVertex3fv(vertices[i][0]);
            
            glNormal3fv(normals[i]);        
            glTexCoord2f(0.0, 0.0);
            glVertex3fv(vertices[i][1]);
            
            glNormal3fv(normals[i]);
            glTexCoord2f(0.0, mWallsBitmapCoef);
            glVertex3fv(vertices[i][2]);
            
            glNormal3fv(normals[i]);
            glTexCoord2f(mWallsBitmapCoef, mWallsBitmapCoef);
            glVertex3fv(vertices[i][3]);
        glEnd();
    }
    // no need for texturing anymore
    glDisable(GL_TEXTURE_2D);

    // Draw ROOF
    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffusebackLIG);
    glMaterialfv(GL_BACK, GL_DIFFUSE, mat_diffusebackLIG);

    glBegin(GL_POLYGON);
        glNormal3fv(normals[ROOF]);
        glVertex3f(0.0, mDim[1], 0.0);

        glNormal3fv(normals[ROOF]);        
        glVertex3f(0.0, mDim[1], mDim[2]);
        
        glNormal3fv(normals[ROOF]);
        glVertex3f(mDim[0], mDim[1], mDim[2]);
        
        glNormal3fv(normals[ROOF]);
        glVertex3f(mDim[0], mDim[1], 0.0);
    glEnd();

    glDepthMask(GL_TRUE);
    glDisable(GL_BLEND);
   
    // if drawing field is enabled
    if (mbFields)
    {
        GLfloat mat_specular[] = { 
            mFieldColor[0]/3, 
            mFieldColor[1]/3,
            mFieldColor[2]/3,
            1.0 
        };

        GLfloat mat_ambient[] = { 
            mFieldColor[0],
            mFieldColor[1], 
            mFieldColor[2], 1.0 
        };
        
        glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
        glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
        glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_specular);
        glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);

        double lw = 0.05 * mFieldLineWidth;
        double x = 0;
        for (int i = 0; i <= mnFields[0]; i++)
        {
            glBegin(GL_POLYGON);
                glVertex3f(x+lw, 0.00, 0.0);
                glVertex3f(x+lw, 0.00, mDim[2]);
                glVertex3f(x-lw, 0.00, mDim[2]);
                glVertex3f(x-lw, 0.00, 0);   
            glEnd();
            
            x += mFieldWidth[0];
        }
        x = 0;
        for (int i = 0; i <= mnFields[1]; i++)
        {
            glBegin(GL_POLYGON);
                glVertex3f(0.0, 0.00, x - lw);
                glVertex3f(mDim[0], 0.00, x - lw);
                glVertex3f(mDim[0], 0.00, x + lw);
                glVertex3f(0, 0.00, x + lw);            
            glEnd();
            x += mFieldWidth[1];
        }
    }

    glDepthMask(GL_TRUE);
    glDisable(GL_BLEND);
    glFrontFace(GL_CCW);
    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
    return true;
}
bool Box::HasHitti ( ) const

Whether box will display place where it has been hit by the ball.

If set engine will add transparent circles of the ball color reprsenting place where box was hit.

Returns:
true if Hitti is enabled for the box, false if it is not

Definition at line 167 of file Box.cpp.

References mbHitti.

Referenced by CollisionMaster::Collision().

{ 
    return mbHitti != 0;
}
bool Box::HasHitti ( ) const

Whether box will display place where it has been hit by the ball.

If set engine will add transparent circles of the ball color reprsenting place where box was hit.

Returns:
true if Hitti is enabled for the box, false if it is not
void Box::HideWall ( Part  wall)

Wall closest to viewer should be hidden.

Parameters:
wallIdentifier of the wall to be hidden WALL1 to WALL4

Definition at line 172 of file Box.cpp.

References LOGE, mWallToHide, and WALL4.

{ 
    if (wall < WALL1 || wall > WALL4)
    {
        LOGE("Invalid wall: %d", wall);
        return;
    }
    if (mWallToHide == wall)
        return;

    mWallToHide = wall; 
}
void Box::HideWall ( Part  wall)

Wall closest to viewer should be hidden.

Parameters:
wallIdentifier of the wall to be hidden WALL1 to WALL4
bool Box::Init ( XRes resource,
XCongo xcongo 
) [virtual]

Box object will be initied with values from resource.

Reimplemented from Quad_impl.

Definition at line 25 of file Box.cpp.

References XRes::DoubleValue(), XRes::IntValue(), LoadTexture(), LOGE, mBackFloorTexId, mbFields, mbHitti, Quad_impl::mColor, mDim, mFieldColor, mFieldLineWidth, mFieldWidth, mFloorTexId, mnFields, mWallOpacity, mWallsBitmapCoef, and mWallsTexId.

{
    if (!resource->DoubleValue("mDim.mXWidth", &mDim[0]) 
     || !resource->DoubleValue("mDim.mYWidth", &mDim[1])
     || !resource->DoubleValue("mDim.mZWidth", &mDim[2]) )
    {
        LOGE("reading Box dimenstions");
        return false;
    }
  
    if (!resource->DoubleValue("mColor.red", &mColor[0]) 
     || !resource->DoubleValue("mColor.green", &mColor[1])
     || !resource->DoubleValue("mColor.blue", &mColor[2]) )
    {
        LOGE("reading Box color");
        return false;
    }
  
    if (!resource->IntValue("mbFields", &mbFields) )
    {
        LOGE("reading mbFields");
        return false;
    }
    if (!resource->IntValue("mFields.mnX", &mnFields[0]) ||
        !resource->IntValue("mFields.mnZ", &mnFields[1]) )
    {
        LOGE("reading fieled dimensions");
        return false;
    }

    mFieldWidth[0] = mDim[0] / mnFields[0];
    mFieldWidth[1] = mDim[1] / mnFields[1];

    if (!resource->DoubleValue("mFields.mLineWidth", &mFieldLineWidth) )
    {
        LOGE("reading mFields.mDim");
        return false;
    }
  
  
    if (!resource->IntValue("mbHitti", &mbHitti) )
    {
        LOGE("reading Hitti state");
        return false;
    }
  
  
    if (!resource->DoubleValue("mFields.mBorderColor.red", &mFieldColor[0]) 
     || !resource->DoubleValue("mFields.mBorderColor.green", &mFieldColor[1])
     || !resource->DoubleValue("mFields.mBorderColor.blue", &mFieldColor[2]) )
    {
        LOGE("reading Box.Fields color");
        return false;
    }
  
    if (!LoadTexture(resource, "mFloorBitmap", &mFloorTexId) )
    {
        LOGE("Failed to load mWallsBitmap");
        return false;
    }

    if (!LoadTexture(resource, "mBackFloorBitmap", &mBackFloorTexId) )
    {
        LOGE("Failed to load mWallsBitmap");
        return false;
    }

    if (!LoadTexture(resource, "mWallsBitmap", &mWallsTexId) )
    {
        LOGE("Failed to load mWallsBitmap");
        return false;
    }
    resource->DoubleValue("mWallsBitmapCoef", &mWallsBitmapCoef);
    resource->DoubleValue("mWallOpacity", &mWallOpacity);
    return true;
}
bool Box::Init ( XRes resource,
XCongo xcongo 
) [virtual]

Read Quad from resource file.

Parameters:
resourceResource file contained description of Quad object.
xcongoGlobal settings file contained parameters not specific to this quad.
Returns:
true if ok, false on error.

Reimplemented from Quad_impl.

bool Box::LoadTexture ( XRes resource,
const char *  name,
GLuint *  id 
) [private]

Load texture bitmap.

Parameters:
resourceResource containing name of bitmap file
nameName of attribute that maps to bitmap file such as mFloorBitmap
idID of the texture in OPENGL context. Use this value In opengl releted calls
Returns:
true on success, false on failure
bool Box::LoadTexture ( XRes resource,
const char *  name,
GLuint *  id 
) [private]

Load texture bitmap.

Parameters:
resourceResource containing name of bitmap file
nameName of attribute that maps to bitmap file such as mFloorBitmap
idID of the texture in OPENGL context. Use this value In opengl releted calls
Returns:
true on success, false on failure

Bitmap filename and dimensions will be read from resource Texture::GetTexture() helper cash will be used to get bitmap bits

See also:
Texture

Definition at line 108 of file Box.cpp.

References Texture::GetBits(), Texture::GetTexture(), XRes::IntValue(), and XRes::StringValue().

Referenced by Init().

{
    // temp buffer used to format resource names
    char buf[100];
    bool ret_val = true;
    char* bitmapName;
    int width, height;
    const Texture* tex = 0;
    
    // read filename of the bitmap
    if (!resource->StringValue(name, &bitmapName) )
    {
        return true;
    }
    
    // read width of the bitmap
    sprintf(buf, "%s%s", name, "Width");
    if (!resource->IntValue(buf, &width) )
    {
        printf("error: %s must be supplied for %s\n", buf, name);
        ret_val = false;
        goto end;
    }

    // read height of the bitmap
    sprintf(buf, "%s%s", name, "Height");
    if (!resource->IntValue(buf, &height) )
    {
        printf("error: %s must be supplied for %s\n", buf, name);
        ret_val = false;
        goto end;
    }

    // read texture from cashe or disk (if it's not read already).
    if (!(tex = Texture::GetTexture(bitmapName, width, height) ) )
    {
        printf("error: failed to get texture: %s\n", name);
        ret_val = false;
        goto end;
    }
   
    // push texture to OpenGL Stack and acquire id
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glGenTextures(1, id);
    glBindTexture(GL_TEXTURE_2D, *id);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 
        0, GL_BGR, GL_UNSIGNED_BYTE, tex->GetBits() );

end:
    // cleanup
    delete bitmapName;
    return ret_val;
}

Member Data Documentation

GLuint Box::mBackFloorTexId [private]

id of ground level texture

Definition at line 113 of file Box.h.

Referenced by Display(), and Init().

int Box::mbFields [private]

Whether scheme is enabled.

Definition at line 128 of file Box.h.

Referenced by Display(), and Init().

int Box::mbHitti [private]

Whether place where the box has been hit by the ball will be highlighted or not.

Definition at line 109 of file Box.h.

Referenced by HasHitti(), and Init().

double Box::mDim [private]

Dimensions of the box.

  • mDim[0] is x-coordinate (rigth to left)
  • mDim[1] is y-coordinate (down to up)
  • mDim[2] is z-coordinate (front to back)

Definition at line 104 of file Box.h.

Referenced by Dim(), Display(), and Init().

double Box::mFieldColor [private]

Color of the field.

Definition at line 136 of file Box.h.

Referenced by Display(), and Init().

double Box::mFieldLineWidth [private]

Width of the line separator.

Definition at line 134 of file Box.h.

Referenced by Display(), and Init().

double Box::mFieldWidth [private]

Width of field in each domension.

Definition at line 132 of file Box.h.

Referenced by Display(), and Init().

GLuint Box::mFloorTexId [private]

id of floor texture

Definition at line 111 of file Box.h.

Referenced by Display(), and Init().

int Box::mnFields [private]

Number of fields in two dimensions X and Z.

Definition at line 130 of file Box.h.

Referenced by Display(), and Init().

double Box::mWallOpacity [private]

control wall opacity

Definition at line 119 of file Box.h.

Referenced by Box(), Display(), and Init().

double Box::mWallsBitmapCoef [private]

how many times texture will be applied in Z coordinate

Definition at line 117 of file Box.h.

Referenced by Box(), Display(), and Init().

GLuint Box::mWallsTexId [private]

id of walls texture

Definition at line 115 of file Box.h.

Referenced by Display(), and Init().

Wall that will not be displayed.

This is front wall facing the viewer.

Definition at line 96 of file Box.h.

Referenced by Box(), Display(), and HideWall().


The documentation for this class was generated from the following files: