1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
/* md2render.cpp -- OpenGL code for rendering Quake2 MD2 models */
#if defined(_MSC_VER) // If Microsoft compilers...
// Visual C++ 6.0: identifier was truncated to '255' characters in the debug information
#pragma warning(disable:4786)
#endif
#include <assert.h>
#include <map>
using namespace std;
#include <GL/glew.h>
#include "md2.h"
#include "gli.h"
#include "loadtex.h"
#include "md2render.h"
struct TexCoord {
GLshort s;
GLshort t;
};
struct VertexNormal {
GLfloat x, y, z;
GLfloat nx, ny, nz;
};
struct VertexData {
GLushort ndx;
GLushort newndx;
TexCoord tc;
};
struct ModelRenderInfo {
struct {
GLuint texCoordArray;
GLuint vertexNormalArray;
GLuint elementArray;
} bufferObject;
struct {
GLuint decal;
GLuint normalMap;
GLuint heightMap;
} textureObject;
int arrayIndicesPerFrame;
int frameCount;
int arrayElementsPerObject;
int refcnt;
ModelRenderInfo(const Md2Model *model);
~ModelRenderInfo();
void bindArrayElementsForGL();
void bindTexCoordsForGL(int texunit);
void bindPositionsAndNormalsForGL(int frameA, int frameB, int normals);
void drawModel();
void addDecalImage(gliGenericImage* &image);
void ref() { refcnt++; }
void deref() { refcnt--; if (refcnt == 0) { delete this; } }
};
// MD2 files have a separate position/normal index and texture
// coordinate set index per vertex. This routine re-indexes the
// vertex set so every vertex has a unique index to its
// position/normal and texture for easy rendering with vertex
// arrays stored in vertex buffer objects.
//
// Once this re-indexing is performed, three buffer objects
// are created: a per-model texture coordinate set buffer object,
// a buffer object with all the position/normal pairs for the
// full sequence of frames, and finally an element arrays for
// all of the model's independent triangles.
ModelRenderInfo::ModelRenderInfo(const Md2Model *model)
{
typedef pair<int,int> PosTexIndex;
typedef map<PosTexIndex, VertexData, less<PosTexIndex> > vertexMapType;
vertexMapType vertexMap;
const int numFrames = model->header.numFrames;
const int numTriangles = model->header.numTriangles;
int i;
vertexMapType::iterator m;
refcnt = 1;
frameCount = numFrames;
arrayElementsPerObject = 3*numTriangles;
// Make a map from each unique <vertexIndex,textureIndex> pair.
for (i=0; i<numTriangles; i++) {
const Md2Triangle *tri = &model->triangles[i];
for (int j=0; j<3; j++) {
PosTexIndex pti(tri->vertexIndices[j], tri->textureIndices[j]);
VertexData vd;
vd.ndx = tri->vertexIndices[j];
vd.tc.s = model->texCoords[tri->textureIndices[j]].s;
vd.tc.t = model->texCoords[tri->textureIndices[j]].t;
vd.newndx = 0; /* Satisfy gcc's desire to see this initialized. */
vertexMap[pti] = vd;
}
}
// Assign "new" indices to each unique <vertexIndex,textureIndex> pair
// and count unique arrayIndicesPerFrame.
arrayIndicesPerFrame = 0;
for (m = vertexMap.begin(); m != vertexMap.end(); ++m) {
m->second.newndx = arrayIndicesPerFrame;
arrayIndicesPerFrame++;
}
// Generate unused buffer object names for three buffer objects.
GLuint bufferObjs[3];
glGenBuffers(3, bufferObjs);
bufferObject.texCoordArray = bufferObjs[0];
bufferObject.vertexNormalArray = bufferObjs[1];
bufferObject.elementArray = bufferObjs[2];
GLuint texObjs[3];
glGenTextures(3, texObjs);
textureObject.decal = texObjs[0];
textureObject.normalMap = texObjs[1];
textureObject.heightMap = texObjs[2];
// Allocate a single texture coordinate array for model and load into buffer object.
TexCoord *texArray = new TexCoord[arrayIndicesPerFrame];
int newndx = 0;
for (m = vertexMap.begin(); m != vertexMap.end(); ++m) {
texArray[newndx] = m->second.tc;
newndx++;
}
glBindBuffer(GL_ARRAY_BUFFER, bufferObject.texCoordArray);
glBufferData(GL_ARRAY_BUFFER, sizeof(TexCoord)*arrayIndicesPerFrame, texArray, GL_STATIC_DRAW);
delete texArray;
// Allocate a vertex/normal array for model's frames and load into buffer object.
VertexNormal *vertexNormalArray = new VertexNormal[arrayIndicesPerFrame*numFrames];
newndx = 0;
for (int f = 0; f < model->header.numFrames; f++) {
Md2Frame *frame = &model->frames[f];
for (vertexMapType::iterator m = vertexMap.begin(); m != vertexMap.end(); ++m) {
assert(m->second.newndx == newndx % arrayIndicesPerFrame);
assert(m->second.newndx < arrayIndicesPerFrame);
int oldndx = m->second.ndx;
vertexNormalArray[newndx].x = frame->vertices[oldndx].vertex[0];
vertexNormalArray[newndx].y = frame->vertices[oldndx].vertex[1];
vertexNormalArray[newndx].z = frame->vertices[oldndx].vertex[2];
vertexNormalArray[newndx].nx = frame->vertices[oldndx].normal[0];
vertexNormalArray[newndx].ny = frame->vertices[oldndx].normal[1];
vertexNormalArray[newndx].nz = frame->vertices[oldndx].normal[2];
newndx++;
}
}
glBindBuffer(GL_ARRAY_BUFFER, bufferObject.vertexNormalArray);
glBufferData(GL_ARRAY_BUFFER, sizeof(VertexNormal)*arrayIndicesPerFrame*numFrames, vertexNormalArray, GL_STATIC_DRAW);
delete vertexNormalArray;
// Allocate a element array for model and load into buffer object.
int ndx = 0;
GLushort *elementArray = new GLushort[arrayElementsPerObject];
for (i=0; i<numTriangles; i++) {
const Md2Triangle *tri = &model->triangles[i];
// MD2 models have clockwise front-facing triangles so reverse
// index order to match OpenGL's default counter-clockwise default for glFrontFace
for (int j=2; j>=0; j--, ndx++) {
PosTexIndex pti(tri->vertexIndices[j], tri->textureIndices[j]);
VertexData vd = vertexMap[pti];
elementArray[ndx] = vd.newndx;
}
}
assert(arrayElementsPerObject == ndx);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferObject.elementArray);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort)*arrayElementsPerObject, elementArray, GL_STATIC_DRAW);
delete elementArray;
}
ModelRenderInfo::~ModelRenderInfo()
{
GLuint bufferObjs[3];
bufferObjs[0] = bufferObject.elementArray;
bufferObjs[1] = bufferObject.texCoordArray;
bufferObjs[2] = bufferObject.vertexNormalArray;
glDeleteBuffers(3, bufferObjs);
}
void ModelRenderInfo::bindArrayElementsForGL()
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferObject.elementArray);
}
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
void ModelRenderInfo::bindTexCoordsForGL(int texunit)
{
glClientActiveTexture(GL_TEXTURE0_ARB + texunit);
glBindBuffer(GL_ARRAY_BUFFER, bufferObject.texCoordArray);
glTexCoordPointer(2, GL_SHORT, sizeof(GLshort)*2, BUFFER_OFFSET(0));
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}
void ModelRenderInfo::bindPositionsAndNormalsForGL(int frameA, int frameB, int normals)
{
// Bind to array buffer with per-frame position/normal arrays.
glBindBuffer(GL_ARRAY_BUFFER, bufferObject.vertexNormalArray);
// Frame A positions.
glVertexPointer(3, GL_FLOAT, 6*sizeof(GLfloat), BUFFER_OFFSET(frameA*arrayIndicesPerFrame*6*sizeof(GLfloat)));
glEnableClientState(GL_VERTEX_ARRAY);
// Frame B positions.
glClientActiveTexture(GL_TEXTURE1);
glTexCoordPointer(3, GL_FLOAT, 6*sizeof(GLfloat), BUFFER_OFFSET(frameB*arrayIndicesPerFrame*6*sizeof(GLfloat)));
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
if (normals) {
// Frame A normals.
glNormalPointer(GL_FLOAT, 6*sizeof(GLfloat), BUFFER_OFFSET(3*sizeof(GLfloat) + frameA*arrayIndicesPerFrame*6*sizeof(GLfloat)));
glEnableClientState(GL_NORMAL_ARRAY);
// Frame B normals.
glClientActiveTexture(GL_TEXTURE2);
glTexCoordPointer(3, GL_FLOAT, 6*sizeof(GLfloat), BUFFER_OFFSET(3*sizeof(GLfloat) + frameB*arrayIndicesPerFrame*6*sizeof(GLfloat)));
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}
}
void ModelRenderInfo::drawModel()
{
glDrawElements(GL_TRIANGLES, arrayElementsPerObject, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0));
}
void ModelRenderInfo::addDecalImage(gliGenericImage* &image)
{
const int makeMipmaps = 1;
glBindTexture(GL_TEXTURE_2D, textureObject.decal);
image = loadTextureDecal(image, makeMipmaps);
gliFree(image);
image = NULL;
}
extern "C" {
MD2render *createMD2render(Md2Model *model)
{
ModelRenderInfo *mri = new ModelRenderInfo(model);
return (MD2render*) mri;
}
void drawMD2render(MD2render *m, int frameA, int frameB)
{
ModelRenderInfo *mri = reinterpret_cast<ModelRenderInfo *>(m);
mri->bindArrayElementsForGL();
mri->bindTexCoordsForGL(0);
mri->bindPositionsAndNormalsForGL(frameA, frameB, 1);
mri->drawModel();
}
}
|