Index: roclient/model.cpp =================================================================== --- roclient/model.cpp (revision 223) +++ roclient/model.cpp (revision 224) @@ -15,6 +15,9 @@ #include "os.h" // for size_t / memcpy.. #include "gl.h" + +#include "r_Buffer.h" + #include "log.h" #include "roalloc.h" #include "shader.h" @@ -23,7 +26,6 @@ #include "roRsm.h" #include "scene.h" #include "model.h" - #include // @@ -157,6 +159,7 @@ void roc::Model::loadFromRoRsm(){ roRsm *currsm = this->rsm; // safes lookups, and .. typing :D + this->nSurfacesTotal = 0; this->animLen = currsm->animLen; this->nMeshes = currsm->nNodes; this->isStaticModel = true; @@ -194,6 +197,8 @@ mesh->nChildren = 0; // initialize with 0, will be resolved later (after loading all meshes) mesh->children = new simpleVector(); + this->nSurfacesTotal += mesh->nSurfaces; + mesh->bbmax = vec3( -9999999.0f, -9999999.0f, -9999999.0f ); mesh->bbmin = vec3( 9999999.0f, 9999999.0f, 9999999.0f ); @@ -441,149 +446,66 @@ }//end: SetBBox2() + bool roc::Model::compile(){ + r::Buffer *vbo = new r::Buffer(r::BUFFERFORMAT_v3v3v2, "Model", (this->nMeshes * this->nTextures), this->nSurfacesTotal * 3); // *3 because of triangles - struct renderList{ - struct renderList *next; - float v[ (3*8) ]; // pos.v3 normals.v3 tex.v2 - }; - - // Initialize the rl pointes on all meshes of this model for(size_t i = 0; i < this->nMeshes; i++){ + struct roc::Model::Mesh *curmesh = &this->Mesh[i]; - this->Mesh[i].tmpList = (void**) roalloc( this->nTextures * sizeof(void*) ); - for(size_t j = 0; j < this->nTextures; j++){ - - this->Mesh[i].tmpList[j] = NULL; - - } - - } - - - - size_t numSurfaces = 0; - - for(size_t i = 0; i < this->nMeshes; i++){ - struct roc::Model::Mesh *curmesh = &this->Mesh[i]; - + // allocate vboinfo struct for rendering + curmesh->vboinfo = (struct roc::Model::Mesh::vboinfo*) roalloc(sizeof(struct roc::Model::Mesh::vboinfo) * this->nTextures ); + // loop over all surfaces in current mesh for(size_t k = 0; k < curmesh->nSurfaces; k++){ struct roc::Model::Mesh::Surface *cursf = &curmesh->Surface[k]; struct roc::Model::textureInfo *tex = &this->textures[cursf->texID]; - + + if(cursf->texID >= this->nTextures ){ putErr("Model::compile() -> Mesh has invalid Texture!\n"); continue; } + - vec3 n = vec3_normals( vec3( cursf->v[0][0], cursf->v[0][1], cursf->v[0][2]), vec3(cursf->v[1][0], cursf->v[1][1], cursf->v[1][2]), vec3(cursf->v[2][0], cursf->v[2][1], cursf->v[2][2]) ); + size_t col = (i * this->nTextures) + cursf->texID; + // position.v3 normals.v3 texcoord.v2 + vbo->addRow(col, cursf->v[0][0], cursf->v[0][1], cursf->v[0][2], n[0], n[1], n[2], cursf->t[0][0] * tex->ufactor, cursf->t[0][1] * tex->vfactor); // 1 + vbo->addRow(col, cursf->v[1][0], cursf->v[1][1], cursf->v[1][2], n[0], n[1], n[2], cursf->t[1][0] * tex->ufactor, cursf->t[1][1] * tex->vfactor); // 2 + vbo->addRow(col, cursf->v[2][0], cursf->v[2][1], cursf->v[2][2], n[0], n[1], n[2], cursf->t[2][0] * tex->ufactor, cursf->t[2][1] * tex->vfactor); // 3 + + // @Todo // Positions could be multiplied with mat2 *and even mat1 if the model is static), // mat1 * mat2 * vec(cursf->v[n]) // this would save a mat4 transfer for each model to gfx adapter upon draw. // + - struct renderList *rle = (struct renderList*) roalloc( sizeof(struct renderList) ); - - float vert[] = { - // position.v2 normal.v3 texCoord.v2 - cursf->v[0][0], cursf->v[0][1], cursf->v[0][2], n[0], n[1], n[2], cursf->t[0][0] * tex->ufactor, cursf->t[0][1] * tex->vfactor, // 1 - cursf->v[1][0], cursf->v[1][1], cursf->v[1][2], n[0], n[1], n[2], cursf->t[1][0] * tex->ufactor, cursf->t[1][1] * tex->vfactor, // 2 - cursf->v[2][0], cursf->v[2][1], cursf->v[2][2], n[0], n[1], n[2], cursf->t[2][0] * tex->ufactor, cursf->t[2][1] * tex->vfactor, // 3 - }; - memcpy(rle->v, vert, sizeof(float) * (3*8) ); - - - // add to renderlist of the current mesh for the current texture. - rle->next = (struct renderList*) curmesh->tmpList[ cursf->texID ]; - curmesh->tmpList[ cursf->texID ] = rle; - - - numSurfaces++; - }//endfor: each surface in current mesh }//endfor: each mesh in model + - // - // - float *buf = (float*) roalloc( (3*8) * numSurfaces * sizeof(float) ); - size_t bufOffset = 0; // in floats. + vbo->compile(); + this->vbo = vbo->getVBO(); - // - // Fillup "buf" - // + for(size_t i = 0; i < this->nMeshes; i++){ struct roc::Model::Mesh *curmesh = &this->Mesh[i]; - - // allocate vboinfo struct on mesh - curmesh->vboinfo = (struct roc::Model::Mesh::vboinfo*) roalloc(sizeof(struct roc::Model::Mesh::vboinfo) * this->nTextures ); - - for(size_t j = 0; j < this->nTextures; j++){ - struct renderList *rle, *rle_next; - - // save offset. - curmesh->vboinfo[j].offset = (GLint)(bufOffset/8); // 8 floats a row (vertex) - - - size_t numVertex = 0; - - // - rle = (struct renderList*) curmesh->tmpList[j]; - while(1){ - if(rle == NULL) - break; - - rle_next = rle->next; - - numVertex += 3; - - memcpy(&buf[bufOffset], rle->v, sizeof(float) * (3*8) ); - bufOffset += (3*8); - - rofree(rle); - rle = rle_next; - }//end - - curmesh->vboinfo[j].count = (GLsizei)numVertex; - - }//endforeach texture - - rofree(curmesh->tmpList); - curmesh->tmpList = NULL; - - }//endforeach mesh - - // Check: - // -#ifdef _DEBUG - if((numSurfaces * (3*8)) != bufOffset){ - putErr("Model::compile() -> calculated offset does not match bufOffset! %u != %u\n", numSurfaces * (3*5), bufOffset); + size_t col = (i * this->nTextures) + j; + curmesh->vboinfo[j].offset = vbo->getColOffset(col); + curmesh->vboinfo[j].count = vbo->getRowCount(col); + } } -#endif - // - // Generate Vertex buffer @ gfx adapter. - // - glGenBuffers(1, &this->vbo); - glBindBuffer(GL_ARRAY_BUFFER, this->vbo); - glBufferData(GL_ARRAY_BUFFER, bufOffset * sizeof(float), buf, GL_STATIC_DRAW); - - - // - // Free temp vertex buffer - // - rofree(buf); - - return true; }//end: compile()