Model.h

Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 5 -*- */
00002 
00003 /*
00004  * Combination of a first person action and a real time strategy battle game.
00005  */
00006 
00007 /*
00008  * Model.h
00009  * MetalWarriors
00010  * $Revision: 1.23 $
00011  *
00012  * Created by Daniel Aarno on Sun Sep 28 2003.
00013  * Copyright (c) 2003 Daniel Aarno and Erik Ytterberg - All rights reserved.
00014  *
00015  *     This program is free software; you can redistribute it and/or modify
00016  *     it under the terms of the GNU General Public License as published by
00017  *     the Free Software Foundation; either version 2 of the License, or
00018  *     (at your option) any later version.
00019  *
00020  *     This program is distributed in the hope that it will be useful,
00021  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  *     GNU General Public License for more details.
00024  *
00025  *     You should have received a copy of the GNU General Public License
00026  *     along with this program; if not, write to the Free Software
00027  *     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00028  */
00029  
00030 #ifndef _MODEL_
00031 #define _MODEL_
00032 
00033 
00034 #include <sys/types.h>
00035 #include <sys/stat.h>
00036 #include <fcntl.h>
00037 #include <stdio.h>
00038 #include <list>
00039 #include "opengl.h"
00040 
00041 /*Splits a buffer (file) into convenient strings*/
00042 char *getString(char *buf, char *pos,  int fd);
00043 
00044 /*Returns length of a substring in a longer string*/
00045 int atos(char *str);
00046 
00047 
00048 typedef enum {
00049   CAP_DUMMY = 0,
00050   CAP_BODY = 1,
00051   
00052   CAP_TURRET = 10,
00053   CAP_TURRET_ROTATION,
00054   CAP_TURRET_ROT_LEFT,
00055   CAP_TURRET_ROT_RIGHT,
00056   
00057   CAP_BARREL = 20,
00058   CAP_BARREL_ELEVATION,
00059   CAP_BARREL_UP,
00060   CAP_BARREL_DOWN,
00061 
00062   CAP_WHEEL = 30,
00063   CAP_WHEEL_ROTATION_R,
00064   CAP_WHEEL_ROTATION_L,
00065   CAP_MAX
00066 }model_cap_t;
00067 
00068 #define MODEL_CAPS_USED 17
00069 
00070 #define MODEL_CAP_MAX_NAME_LEN 30
00071 
00072 typedef struct{
00073   model_cap_t cap;
00074   char name[MODEL_CAP_MAX_NAME_LEN];
00075 }model_cap_trans_t;
00076 
00077 //void initCaps(void);
00078 //model_cap_t findCapTrans(char *str);
00079 
00080 typedef struct{
00081   model_cap_t cap;
00082   float *value;
00083   float min;
00084   float max;
00085   float step;
00086   
00087 }model_cap_data_t;
00088 
00089 /*Some functions for easy manipulation of cap values*/
00090 void incCap(model_cap_data_t *cap_data);
00091 void decCap(model_cap_data_t *cap_data);
00092 void incCap(model_cap_data_t *cap_data, float steps);
00093 void decCap(model_cap_data_t *cap_data, float steps);
00094 
00100 #define MAX_CAPS 10
00101 
00102 class Model
00103 {
00104   friend class ModelResource;
00105 public:
00106      GLfloat *vertices;  
00107      GLuint *indices;    
00108 
00109      long indexCount;    
00110      long vertexCount;   
00111    
00112 
00113     
00114 #ifdef MEM_STATS
00115      size_t size;
00116 #endif
00117  protected:
00118      char rsrcKey[32];
00119 public:
00120      Model();
00121      Model(char *desc_file);
00126      virtual ~Model();
00127 
00136      int loadModel(const char *path);
00137 
00141      void clear();
00142 
00152      static void DrawVertexArray(const GLfloat* vertices, 
00153                                  const GLuint* indices, 
00154                                  GLint count);
00155  protected:
00156      int readVertices(FILE* modFile);
00157      int readFaces(FILE* modFile);
00158 };
00159 
00160 class CompModel : public Model
00161 {
00162 protected:
00163      static std::string filePrefix;
00164 
00165 public:
00166      enum {
00167           FOG_BIT = 0,
00168        LIGHTING_BIT,
00169        DEPTH_TEST_BIT,
00170        CULL_FACE_BIT,
00171        ALPHA_TEST_BIT,
00172        TEXTURE_2D_BIT,
00173        STENCIL_TEST_BIT, 
00174        BLEND_BIT
00175      };
00176 
00177      struct openglstate {
00178           GLuint tex;
00179           unsigned long state;
00180      };
00181 
00182      typedef openglstate openglstate_t;
00183      //typedef unsigned long openglstate_t;
00184 
00185      struct RGBA_t {
00186           RGBA_t() {}
00187           RGBA_t (GLfloat r_, GLfloat g_, GLfloat b_, GLfloat a_) 
00188                : r(r_), g(g_), b(b_), a(a_) {}
00189           
00190           static RGBA_t Black() { return RGBA_t(0.0f, 0.0f, 0.0f, 1.0f); }
00191           static RGBA_t White() { return RGBA_t(1.0f, 1.0f, 1.0f, 1.0f); }
00192 
00193           operator GLfloat* () { return &r; }
00194           RGBA_t& operator*=(GLfloat d);
00195 
00196           GLfloat r;
00197           GLfloat g;
00198           GLfloat b;
00199           GLfloat a;
00200      };
00201 
00202      struct Lighting {
00203           RGBA_t ambient;
00204           RGBA_t diffuse;
00205           RGBA_t specular;
00206           GLfloat shine;
00207      };
00208 
00209      float translation[3];
00210      float rotation[4];
00211      float scale[3];
00212   
00213      std::list<CompModel*> children;
00214      GLuint tex_id;
00215      char name[20];
00216      char texRsrcKey[32];
00217   
00218      std::list<model_cap_data_t*> caps;
00219      Lighting* lighting;
00220      openglstate_t state;
00221 
00222 protected:
00223      int parseDesc(int fd);
00224      int readDesc(int fd, char *buffer, char **str_pos, char *parent_name);
00225      virtual void paintVertexArray(void);
00226 
00227 public:
00228      CompModel(void);
00229      virtual ~CompModel();
00230      int initModel(std::string desc_file, std::string prefix);
00231      CompModel *getByName(char *search_name);
00232      CompModel *replaceByName(char *search_name, CompModel* replacement);
00233      float *getTrans(void){return translation;}
00234      float *getRot(void) {return rotation;}
00235      float *getScale(void) {return scale;}
00236 
00237      float *getCapValue(model_cap_t cap);
00238      void setCapValue(model_cap_t cap, float value);
00239      model_cap_data_t *getCapData(model_cap_t cap);
00240      int getAllCapData(std::list<model_cap_data_t*> *cap_data_list, 
00241                        model_cap_t cap);
00242      void incCap(model_cap_t cap);
00243      void decCap(model_cap_t cap);
00244      void incCap(model_cap_t cap, float steps);
00245      void decCap(model_cap_t cap, float steps);
00246      void paint();
00247 };
00248 
00249 #endif// _MODEL_

Generated on Sun Mar 11 15:33:23 2007 for MetalWarriors by  doxygen 1.4.7