2011-11-10 00:11:28 +01:00
|
|
|
/* Copyright (C) 2011 Wildfire Games.
|
2009-04-18 19:00:33 +02:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-04-18 19:51:05 +02:00
|
|
|
/*
|
|
|
|
* Implementation of FixedFunctionModelRenderer
|
2005-10-25 03:43:07 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
2011-11-10 00:11:28 +01:00
|
|
|
#include "lib/bits.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "lib/ogl.h"
|
2011-11-10 00:11:28 +01:00
|
|
|
#include "lib/sysdep/rtl.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "maths/Vector3D.h"
|
|
|
|
#include "maths/Vector4D.h"
|
2005-10-25 03:43:07 +02:00
|
|
|
|
|
|
|
#include "ps/CLogger.h"
|
|
|
|
|
2006-09-26 03:44:20 +02:00
|
|
|
#include "graphics/SColor.h"
|
2005-10-25 03:43:07 +02:00
|
|
|
#include "graphics/Model.h"
|
|
|
|
#include "graphics/ModelDef.h"
|
|
|
|
|
|
|
|
#include "renderer/FixedFunctionModelRenderer.h"
|
|
|
|
#include "renderer/Renderer.h"
|
|
|
|
#include "renderer/RenderModifiers.h"
|
|
|
|
#include "renderer/VertexArray.h"
|
|
|
|
|
2012-02-13 17:41:17 +01:00
|
|
|
#if !CONFIG2_GLES
|
2005-10-25 03:43:07 +02:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// FixedFunctionModelRenderer implementation
|
|
|
|
|
|
|
|
struct FFModelDef : public CModelDefRPrivate
|
|
|
|
{
|
|
|
|
/// Indices are the same for all models, so share them
|
2011-03-13 19:58:09 +01:00
|
|
|
VertexIndexArray m_IndexArray;
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2005-10-25 03:43:07 +02:00
|
|
|
/// Static per-CModelDef vertex array
|
|
|
|
VertexArray m_Array;
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2005-10-25 03:43:07 +02:00
|
|
|
/// UV coordinates are stored in the static array
|
|
|
|
VertexArray::Attribute m_UV;
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2008-07-17 16:23:51 +02:00
|
|
|
FFModelDef(const CModelDefPtr& mdef);
|
2005-10-25 03:43:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-07-17 16:23:51 +02:00
|
|
|
FFModelDef::FFModelDef(const CModelDefPtr& mdef)
|
2011-03-13 19:58:09 +01:00
|
|
|
: m_IndexArray(GL_STATIC_DRAW), m_Array(GL_STATIC_DRAW)
|
2005-10-25 03:43:07 +02:00
|
|
|
{
|
|
|
|
size_t numVertices = mdef->GetNumVertices();
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2005-10-25 03:43:07 +02:00
|
|
|
m_UV.type = GL_FLOAT;
|
|
|
|
m_UV.elems = 2;
|
|
|
|
m_Array.AddAttribute(&m_UV);
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2005-10-25 03:43:07 +02:00
|
|
|
m_Array.SetNumVertices(numVertices);
|
|
|
|
m_Array.Layout();
|
|
|
|
|
|
|
|
VertexArrayIterator<float[2]> UVit = m_UV.GetIterator<float[2]>();
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2005-10-25 03:43:07 +02:00
|
|
|
ModelRenderer::BuildUV(mdef, UVit);
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2005-10-25 03:43:07 +02:00
|
|
|
m_Array.Upload();
|
|
|
|
m_Array.FreeBackingStore();
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2011-03-13 19:58:09 +01:00
|
|
|
m_IndexArray.SetNumVertices(mdef->GetNumFaces()*3);
|
|
|
|
m_IndexArray.Layout();
|
|
|
|
ModelRenderer::BuildIndices(mdef, m_IndexArray.GetIterator());
|
|
|
|
m_IndexArray.Upload();
|
|
|
|
m_IndexArray.FreeBackingStore();
|
2005-10-25 03:43:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct FFModel
|
|
|
|
{
|
|
|
|
/// Dynamic per-CModel vertex array
|
|
|
|
VertexArray m_Array;
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2005-10-25 03:43:07 +02:00
|
|
|
/// Position and lighting are recalculated on CPU every frame
|
|
|
|
VertexArray::Attribute m_Position;
|
|
|
|
VertexArray::Attribute m_Color;
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2011-03-13 19:58:09 +01:00
|
|
|
FFModel() : m_Array(GL_DYNAMIC_DRAW) { }
|
2005-10-25 03:43:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct FixedFunctionModelRendererInternals
|
|
|
|
{
|
2011-11-10 00:11:28 +01:00
|
|
|
/**
|
|
|
|
* Scratch space for normal vector calculation.
|
|
|
|
* Space is reserved so we don't have to do frequent reallocations.
|
|
|
|
* Allocated with rtl_AllocateAligned(normalsNumVertices*16, 16) for SSE writes.
|
|
|
|
*/
|
|
|
|
char* normals;
|
|
|
|
size_t normalsNumVertices;
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2005-10-25 03:43:07 +02:00
|
|
|
/// Previously prepared modeldef
|
|
|
|
FFModelDef* ffmodeldef;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Construction and Destruction
|
2011-04-07 00:07:13 +02:00
|
|
|
FixedFunctionModelRenderer::FixedFunctionModelRenderer()
|
2005-10-25 03:43:07 +02:00
|
|
|
{
|
|
|
|
m = new FixedFunctionModelRendererInternals;
|
2005-11-06 00:15:23 +01:00
|
|
|
m->ffmodeldef = 0;
|
2011-11-10 00:11:28 +01:00
|
|
|
m->normals = 0;
|
|
|
|
m->normalsNumVertices = 0;
|
2005-10-25 03:43:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FixedFunctionModelRenderer::~FixedFunctionModelRenderer()
|
|
|
|
{
|
2011-11-10 00:11:28 +01:00
|
|
|
rtl_FreeAligned(m->normals);
|
|
|
|
|
2005-10-25 03:43:07 +02:00
|
|
|
delete m;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Build model data (and modeldef data if necessary)
|
|
|
|
void* FixedFunctionModelRenderer::CreateModelData(CModel* model)
|
|
|
|
{
|
|
|
|
CModelDefPtr mdef = model->GetModelDef();
|
|
|
|
FFModelDef* ffmodeldef = (FFModelDef*)mdef->GetRenderData(m);
|
|
|
|
|
|
|
|
if (!ffmodeldef)
|
|
|
|
{
|
|
|
|
ffmodeldef = new FFModelDef(mdef);
|
|
|
|
mdef->SetRenderData(m, ffmodeldef);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the per-model data
|
|
|
|
FFModel* ffmodel = new FFModel;
|
|
|
|
|
2011-11-10 00:11:28 +01:00
|
|
|
// Positions must be 16-byte aligned for SSE writes.
|
|
|
|
// We can pack the color after the position; it will be corrupted by
|
|
|
|
// BuildPositionAndNormals, but that's okay since we'll recompute the
|
|
|
|
// colors afterwards.
|
2005-10-25 03:43:07 +02:00
|
|
|
|
|
|
|
ffmodel->m_Color.type = GL_UNSIGNED_BYTE;
|
|
|
|
ffmodel->m_Color.elems = 4;
|
|
|
|
ffmodel->m_Array.AddAttribute(&ffmodel->m_Color);
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2011-11-10 00:11:28 +01:00
|
|
|
ffmodel->m_Position.type = GL_FLOAT;
|
|
|
|
ffmodel->m_Position.elems = 3;
|
|
|
|
ffmodel->m_Array.AddAttribute(&ffmodel->m_Position);
|
|
|
|
|
2005-10-25 03:43:07 +02:00
|
|
|
ffmodel->m_Array.SetNumVertices(mdef->GetNumVertices());
|
|
|
|
ffmodel->m_Array.Layout();
|
|
|
|
|
2011-11-10 00:11:28 +01:00
|
|
|
// Verify alignment
|
|
|
|
ENSURE(ffmodel->m_Position.offset % 16 == 0);
|
|
|
|
ENSURE(ffmodel->m_Array.GetStride() % 16 == 0);
|
|
|
|
|
2005-10-25 03:43:07 +02:00
|
|
|
return ffmodel;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Fill in and upload dynamic vertex array
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
void FixedFunctionModelRenderer::UpdateModelData(CModel* model, void* data, int updateflags)
|
2005-10-25 03:43:07 +02:00
|
|
|
{
|
|
|
|
FFModel* ffmodel = (FFModel*)data;
|
2006-02-15 01:45:16 +01:00
|
|
|
|
|
|
|
if (updateflags & (RENDERDATA_UPDATE_VERTICES|RENDERDATA_UPDATE_COLOR))
|
2005-10-25 03:43:07 +02:00
|
|
|
{
|
|
|
|
CModelDefPtr mdef = model->GetModelDef();
|
|
|
|
size_t numVertices = mdef->GetNumVertices();
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2005-10-25 03:43:07 +02:00
|
|
|
// build vertices
|
2011-11-10 00:11:28 +01:00
|
|
|
|
|
|
|
// allocate working space for computing normals
|
|
|
|
if (numVertices > m->normalsNumVertices)
|
|
|
|
{
|
|
|
|
rtl_FreeAligned(m->normals);
|
|
|
|
|
|
|
|
size_t newSize = round_up_to_pow2(numVertices);
|
|
|
|
m->normals = (char*)rtl_AllocateAligned(newSize*16, 16);
|
|
|
|
m->normalsNumVertices = newSize;
|
|
|
|
}
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2005-10-25 03:43:07 +02:00
|
|
|
VertexArrayIterator<CVector3D> Position = ffmodel->m_Position.GetIterator<CVector3D>();
|
2011-11-10 00:11:28 +01:00
|
|
|
VertexArrayIterator<CVector3D> Normal = VertexArrayIterator<CVector3D>(m->normals, 16);
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2005-11-06 00:15:23 +01:00
|
|
|
ModelRenderer::BuildPositionAndNormals(model, Position, Normal);
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2005-10-25 03:43:07 +02:00
|
|
|
VertexArrayIterator<SColor4ub> Color = ffmodel->m_Color.GetIterator<SColor4ub>();
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2011-04-07 00:07:13 +02:00
|
|
|
ModelRenderer::BuildColor4ub(model, Normal, Color);
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2005-10-25 03:43:07 +02:00
|
|
|
// upload everything to vertex buffer
|
|
|
|
ffmodel->m_Array.Upload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Cleanup per-model data.
|
|
|
|
// Note that per-CModelDef data is deleted by the CModelDef itself.
|
2005-10-31 17:26:51 +01:00
|
|
|
void FixedFunctionModelRenderer::DestroyModelData(CModel* UNUSED(model), void* data)
|
2005-10-25 03:43:07 +02:00
|
|
|
{
|
|
|
|
FFModel* ffmodel = (FFModel*)data;
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2005-10-25 03:43:07 +02:00
|
|
|
delete ffmodel;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-06 00:15:23 +01:00
|
|
|
// Setup one rendering pass
|
2011-04-07 00:09:58 +02:00
|
|
|
void FixedFunctionModelRenderer::BeginPass(int streamflags)
|
2005-11-06 00:15:23 +01:00
|
|
|
{
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(streamflags == (streamflags & (STREAM_POS|STREAM_UV0|STREAM_COLOR)));
|
2006-03-26 01:54:20 +01:00
|
|
|
|
2005-11-06 00:15:23 +01:00
|
|
|
glEnableClientState(GL_VERTEX_ARRAY);
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2005-11-06 00:15:23 +01:00
|
|
|
if (streamflags & STREAM_UV0) glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
|
|
|
if (streamflags & STREAM_COLOR) glEnableClientState(GL_COLOR_ARRAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Cleanup one rendering pass
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
void FixedFunctionModelRenderer::EndPass(int streamflags)
|
2005-11-06 00:15:23 +01:00
|
|
|
{
|
|
|
|
if (streamflags & STREAM_UV0) glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
|
|
|
if (streamflags & STREAM_COLOR) glDisableClientState(GL_COLOR_ARRAY);
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2005-11-06 00:15:23 +01:00
|
|
|
glDisableClientState(GL_VERTEX_ARRAY);
|
2010-05-30 15:42:56 +02:00
|
|
|
|
|
|
|
CVertexBuffer::Unbind();
|
2005-11-06 00:15:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-25 03:43:07 +02:00
|
|
|
// Prepare UV coordinates for this modeldef
|
2012-02-09 18:55:25 +01:00
|
|
|
void FixedFunctionModelRenderer::PrepareModelDef(CShaderProgramPtr& UNUSED(shader), int streamflags, const CModelDefPtr& def)
|
2005-10-25 03:43:07 +02:00
|
|
|
{
|
|
|
|
m->ffmodeldef = (FFModelDef*)def->GetRenderData(m);
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(m->ffmodeldef);
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2005-11-06 00:15:23 +01:00
|
|
|
if (streamflags & STREAM_UV0)
|
2005-10-25 03:43:07 +02:00
|
|
|
{
|
|
|
|
u8* base = m->ffmodeldef->m_Array.Bind();
|
|
|
|
GLsizei stride = (GLsizei)m->ffmodeldef->m_Array.GetStride();
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2005-10-25 03:43:07 +02:00
|
|
|
glTexCoordPointer(2, GL_FLOAT, stride, base + m->ffmodeldef->m_UV.offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Render one model
|
2012-02-09 18:55:25 +01:00
|
|
|
void FixedFunctionModelRenderer::RenderModel(CShaderProgramPtr& UNUSED(shader), int streamflags, CModel* model, void* data)
|
2005-10-25 03:43:07 +02:00
|
|
|
{
|
|
|
|
CModelDefPtr mdldef = model->GetModelDef();
|
|
|
|
FFModel* ffmodel = (FFModel*)data;
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2005-10-25 03:43:07 +02:00
|
|
|
u8* base = ffmodel->m_Array.Bind();
|
|
|
|
GLsizei stride = (GLsizei)ffmodel->m_Array.GetStride();
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2011-03-13 19:58:09 +01:00
|
|
|
u8* indexBase = m->ffmodeldef->m_IndexArray.Bind();
|
|
|
|
|
2005-10-25 03:43:07 +02:00
|
|
|
glVertexPointer(3, GL_FLOAT, stride, base + ffmodel->m_Position.offset);
|
2005-11-06 00:15:23 +01:00
|
|
|
if (streamflags & STREAM_COLOR)
|
2006-02-15 01:45:16 +01:00
|
|
|
glColorPointer(3, ffmodel->m_Color.type, stride, base + ffmodel->m_Color.offset);
|
2005-10-25 03:43:07 +02:00
|
|
|
|
|
|
|
// render the lot
|
|
|
|
size_t numFaces = mdldef->GetNumFaces();
|
2006-09-23 18:04:54 +02:00
|
|
|
|
|
|
|
if (!g_Renderer.m_SkipSubmit) {
|
2010-10-05 17:54:04 +02:00
|
|
|
pglDrawRangeElementsEXT(GL_TRIANGLES, 0, (GLuint)mdldef->GetNumVertices()-1,
|
2011-03-13 19:58:09 +01:00
|
|
|
(GLsizei)numFaces*3, GL_UNSIGNED_SHORT, indexBase);
|
2006-09-23 18:04:54 +02:00
|
|
|
}
|
2005-10-25 03:43:07 +02:00
|
|
|
|
|
|
|
// bump stats
|
|
|
|
g_Renderer.m_Stats.m_DrawCalls++;
|
|
|
|
g_Renderer.m_Stats.m_ModelTris += numFaces;
|
|
|
|
}
|
2012-02-13 17:41:17 +01:00
|
|
|
|
|
|
|
#endif // !CONFIG2_GLES
|