2005-10-30 02:22:22 +02:00
|
|
|
/**
|
|
|
|
* =========================================================================
|
|
|
|
* File : InstancingModelRenderer.cpp
|
|
|
|
* Project : Pyrogenesis
|
|
|
|
* Description : Implementation of InstancingModelRenderer
|
|
|
|
*
|
2006-07-26 16:04:52 +02:00
|
|
|
* @author Nicolai Haehnle <nicolai@wildfiregames.com>
|
2005-10-30 02:22:22 +02:00
|
|
|
* =========================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "lib/ogl.h"
|
2005-10-30 02:22:22 +02:00
|
|
|
#include "lib/res/graphics/ogl_shader.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "maths/Vector3D.h"
|
|
|
|
#include "maths/Vector4D.h"
|
2005-10-30 02:22:22 +02:00
|
|
|
|
|
|
|
#include "ps/CLogger.h"
|
|
|
|
|
|
|
|
#include "graphics/Color.h"
|
2006-02-13 15:18:20 +01:00
|
|
|
#include "graphics/LightEnv.h"
|
2005-10-30 02:22:22 +02:00
|
|
|
#include "graphics/Model.h"
|
|
|
|
#include "graphics/ModelDef.h"
|
|
|
|
|
|
|
|
#include "renderer/InstancingModelRenderer.h"
|
|
|
|
#include "renderer/Renderer.h"
|
|
|
|
#include "renderer/RenderModifiers.h"
|
|
|
|
#include "renderer/RenderPathVertexShader.h"
|
|
|
|
#include "renderer/VertexArray.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define LOG_CATEGORY "graphics"
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// InstancingModelRenderer implementation
|
|
|
|
|
|
|
|
struct IModelDef : public CModelDefRPrivate
|
|
|
|
{
|
|
|
|
/// Static per-CModel vertex array
|
|
|
|
VertexArray m_Array;
|
2006-02-13 15:18:20 +01:00
|
|
|
|
2005-10-30 02:22:22 +02:00
|
|
|
/// Position, normals and UV are all static
|
|
|
|
VertexArray::Attribute m_Position;
|
|
|
|
VertexArray::Attribute m_Normal;
|
|
|
|
VertexArray::Attribute m_UV;
|
2006-02-13 15:18:20 +01:00
|
|
|
|
2005-10-30 02:22:22 +02:00
|
|
|
/// Indices are the same for all models, so share them
|
|
|
|
u16* m_Indices;
|
2006-02-13 15:18:20 +01:00
|
|
|
|
2005-10-30 02:22:22 +02:00
|
|
|
|
|
|
|
IModelDef(CModelDefPtr mdef);
|
2005-10-30 22:01:42 +01:00
|
|
|
~IModelDef() { delete[] m_Indices; }
|
2005-10-30 02:22:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
IModelDef::IModelDef(CModelDefPtr mdef)
|
|
|
|
: m_Array(false)
|
|
|
|
{
|
|
|
|
size_t numVertices = mdef->GetNumVertices();
|
2006-02-13 15:18:20 +01:00
|
|
|
|
2005-10-30 02:22:22 +02:00
|
|
|
m_Position.type = GL_FLOAT;
|
|
|
|
m_Position.elems = 3;
|
|
|
|
m_Array.AddAttribute(&m_Position);
|
2006-02-13 15:18:20 +01:00
|
|
|
|
2005-10-30 02:22:22 +02:00
|
|
|
m_Normal.type = GL_FLOAT;
|
|
|
|
m_Normal.elems = 3;
|
|
|
|
m_Array.AddAttribute(&m_Normal);
|
2006-02-13 15:18:20 +01:00
|
|
|
|
2005-10-30 02:22:22 +02:00
|
|
|
m_UV.type = GL_FLOAT;
|
|
|
|
m_UV.elems = 2;
|
|
|
|
m_Array.AddAttribute(&m_UV);
|
2006-02-13 15:18:20 +01:00
|
|
|
|
2005-10-30 02:22:22 +02:00
|
|
|
m_Array.SetNumVertices(numVertices);
|
|
|
|
m_Array.Layout();
|
|
|
|
|
|
|
|
VertexArrayIterator<CVector3D> Position = m_Position.GetIterator<CVector3D>();
|
|
|
|
VertexArrayIterator<CVector3D> Normal = m_Normal.GetIterator<CVector3D>();
|
|
|
|
VertexArrayIterator<float[2]> UVit = m_UV.GetIterator<float[2]>();
|
2006-02-13 15:18:20 +01:00
|
|
|
|
2005-10-30 02:22:22 +02:00
|
|
|
ModelRenderer::CopyPositionAndNormals(mdef, Position, Normal);
|
|
|
|
ModelRenderer::BuildUV(mdef, UVit);
|
2006-02-13 15:18:20 +01:00
|
|
|
|
2005-10-30 02:22:22 +02:00
|
|
|
m_Array.Upload();
|
|
|
|
m_Array.FreeBackingStore();
|
2006-02-13 15:18:20 +01:00
|
|
|
|
2005-10-30 02:22:22 +02:00
|
|
|
m_Indices = new u16[mdef->GetNumFaces()*3];
|
|
|
|
ModelRenderer::BuildIndices(mdef, m_Indices);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct InstancingModelRendererInternals
|
|
|
|
{
|
|
|
|
/// Currently used RenderModifier
|
|
|
|
RenderModifierPtr modifier;
|
2006-02-13 15:18:20 +01:00
|
|
|
|
2005-10-30 02:22:22 +02:00
|
|
|
/// Previously prepared modeldef
|
|
|
|
IModelDef* imodeldef;
|
2006-03-26 01:54:20 +01:00
|
|
|
|
|
|
|
/// If true, primary color will only contain the diffuse term
|
|
|
|
bool colorIsDiffuseOnly;
|
|
|
|
|
|
|
|
/// After BeginPass, this points to the instancing matrix interface
|
|
|
|
VS_Instancing* instancingConfig;
|
2005-10-30 02:22:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Construction and Destruction
|
2006-03-26 01:54:20 +01:00
|
|
|
InstancingModelRenderer::InstancingModelRenderer(bool colorIsDiffuseOnly)
|
2005-10-30 02:22:22 +02:00
|
|
|
{
|
|
|
|
m = new InstancingModelRendererInternals;
|
2005-11-06 00:15:23 +01:00
|
|
|
m->imodeldef = 0;
|
2006-03-26 01:54:20 +01:00
|
|
|
m->colorIsDiffuseOnly = colorIsDiffuseOnly;
|
2005-10-30 02:22:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
InstancingModelRenderer::~InstancingModelRenderer()
|
|
|
|
{
|
|
|
|
delete m;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check hardware support
|
|
|
|
bool InstancingModelRenderer::IsAvailable()
|
|
|
|
{
|
|
|
|
return g_Renderer.m_VertexShader != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Build modeldef data if necessary - we have no per-CModel data
|
|
|
|
void* InstancingModelRenderer::CreateModelData(CModel* model)
|
|
|
|
{
|
|
|
|
CModelDefPtr mdef = model->GetModelDef();
|
|
|
|
IModelDef* imodeldef = (IModelDef*)mdef->GetRenderData(m);
|
|
|
|
|
|
|
|
debug_assert(!model->GetBoneMatrices());
|
2006-02-13 15:18:20 +01:00
|
|
|
|
2005-10-30 02:22:22 +02:00
|
|
|
if (!imodeldef)
|
|
|
|
{
|
|
|
|
imodeldef = new IModelDef(mdef);
|
|
|
|
mdef->SetRenderData(m, imodeldef);
|
|
|
|
}
|
2006-02-13 15:18:20 +01:00
|
|
|
|
2005-10-31 04:36:50 +01:00
|
|
|
return NULL;
|
2005-10-30 02:22:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-31 17:26:51 +01:00
|
|
|
void InstancingModelRenderer::UpdateModelData(CModel* UNUSED(model), void* UNUSED(data), u32 UNUSED(updateflags))
|
2005-10-30 02:22:22 +02:00
|
|
|
{
|
|
|
|
// We have no per-CModel data
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-31 17:26:51 +01:00
|
|
|
void InstancingModelRenderer::DestroyModelData(CModel* UNUSED(model), void* UNUSED(data))
|
2005-10-30 02:22:22 +02:00
|
|
|
{
|
|
|
|
// We have no per-CModel data, and per-CModelDef data is deleted by the CModelDef
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-06 00:15:23 +01:00
|
|
|
// Setup one rendering pass.
|
2006-03-26 01:54:20 +01:00
|
|
|
void InstancingModelRenderer::BeginPass(uint streamflags, const CMatrix3D* texturematrix)
|
2005-11-06 00:15:23 +01:00
|
|
|
{
|
2006-03-26 01:54:20 +01:00
|
|
|
debug_assert(streamflags == (streamflags & (STREAM_POS|STREAM_UV0|STREAM_COLOR|STREAM_TEXGENTOUV1)));
|
|
|
|
|
|
|
|
RenderPathVertexShader* rpvs = g_Renderer.m_VertexShader;
|
|
|
|
|
2005-11-06 00:15:23 +01:00
|
|
|
glEnableClientState(GL_VERTEX_ARRAY);
|
2006-02-13 15:18:20 +01:00
|
|
|
|
2005-11-06 00:15:23 +01:00
|
|
|
if (streamflags & STREAM_UV0) glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
|
|
|
if (streamflags & STREAM_COLOR)
|
|
|
|
{
|
2006-02-13 15:18:20 +01:00
|
|
|
const CLightEnv& lightEnv = g_Renderer.GetLightEnv();
|
2006-03-26 01:54:20 +01:00
|
|
|
VS_GlobalLight* lightConfig;
|
|
|
|
|
|
|
|
if (streamflags & STREAM_TEXGENTOUV1)
|
|
|
|
{
|
|
|
|
ogl_program_use(rpvs->m_InstancingLightP);
|
|
|
|
lightConfig = &rpvs->m_InstancingLightP_Light;
|
|
|
|
m->instancingConfig = &rpvs->m_InstancingLightP_Instancing;
|
|
|
|
|
|
|
|
rpvs->m_InstancingLightP_PosToUV1.SetMatrix(*texturematrix);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ogl_program_use(rpvs->m_InstancingLight);
|
|
|
|
lightConfig = &rpvs->m_InstancingLight_Light;
|
|
|
|
m->instancingConfig = &rpvs->m_InstancingLight_Instancing;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m->colorIsDiffuseOnly)
|
|
|
|
lightConfig->SetAmbient(RGBColor(0,0,0));
|
|
|
|
else
|
|
|
|
lightConfig->SetAmbient(lightEnv.m_UnitsAmbientColor);
|
|
|
|
lightConfig->SetSunDir(lightEnv.GetSunDir());
|
|
|
|
lightConfig->SetSunColor(lightEnv.m_SunColor);
|
2005-11-06 00:15:23 +01:00
|
|
|
|
|
|
|
glEnableClientState(GL_NORMAL_ARRAY);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-03-26 01:54:20 +01:00
|
|
|
if (streamflags & STREAM_TEXGENTOUV1)
|
|
|
|
{
|
|
|
|
ogl_program_use(rpvs->m_InstancingP);
|
|
|
|
m->instancingConfig = &rpvs->m_InstancingP_Instancing;
|
|
|
|
|
|
|
|
rpvs->m_InstancingP_PosToUV1.SetMatrix(*texturematrix);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ogl_program_use(rpvs->m_Instancing);
|
|
|
|
m->instancingConfig = &rpvs->m_Instancing_Instancing;
|
|
|
|
}
|
2005-11-06 00:15:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup rendering pass.
|
|
|
|
void InstancingModelRenderer::EndPass(uint streamflags)
|
|
|
|
{
|
|
|
|
if (streamflags & STREAM_UV0) glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
|
|
|
if (streamflags & STREAM_COLOR) glDisableClientState(GL_NORMAL_ARRAY);
|
|
|
|
|
|
|
|
pglUseProgramObjectARB(0);
|
|
|
|
glDisableClientState(GL_VERTEX_ARRAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-30 02:22:22 +02:00
|
|
|
// Prepare UV coordinates for this modeldef
|
2005-11-06 00:15:23 +01:00
|
|
|
void InstancingModelRenderer::PrepareModelDef(uint streamflags, CModelDefPtr def)
|
2005-10-30 02:22:22 +02:00
|
|
|
{
|
|
|
|
m->imodeldef = (IModelDef*)def->GetRenderData(m);
|
2006-02-13 15:18:20 +01:00
|
|
|
|
2005-10-30 02:22:22 +02:00
|
|
|
debug_assert(m->imodeldef);
|
|
|
|
|
|
|
|
u8* base = m->imodeldef->m_Array.Bind();
|
|
|
|
GLsizei stride = (GLsizei)m->imodeldef->m_Array.GetStride();
|
2006-02-13 15:18:20 +01:00
|
|
|
|
2005-10-30 02:22:22 +02:00
|
|
|
glVertexPointer(3, GL_FLOAT, stride, base + m->imodeldef->m_Position.offset);
|
2005-11-06 00:15:23 +01:00
|
|
|
if (streamflags & STREAM_COLOR)
|
2005-10-30 02:22:22 +02:00
|
|
|
{
|
|
|
|
glNormalPointer(GL_FLOAT, stride, base + m->imodeldef->m_Normal.offset);
|
|
|
|
}
|
2005-11-06 00:15:23 +01:00
|
|
|
if (streamflags & STREAM_UV0)
|
2005-10-30 02:22:22 +02:00
|
|
|
{
|
|
|
|
glTexCoordPointer(2, GL_FLOAT, stride, base + m->imodeldef->m_UV.offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Render one model
|
2005-11-06 00:15:23 +01:00
|
|
|
void InstancingModelRenderer::RenderModel(uint streamflags, CModel* model, void* UNUSED(data))
|
2005-10-30 02:22:22 +02:00
|
|
|
{
|
|
|
|
CModelDefPtr mdldef = model->GetModelDef();
|
|
|
|
const CMatrix3D& mat = model->GetTransform();
|
2006-02-13 15:18:20 +01:00
|
|
|
|
2005-11-06 00:15:23 +01:00
|
|
|
if (streamflags & STREAM_COLOR)
|
2005-10-30 02:22:22 +02:00
|
|
|
{
|
|
|
|
CColor sc = model->GetShadingColor();
|
|
|
|
glColor3f(sc.r, sc.g, sc.b);
|
|
|
|
}
|
|
|
|
|
2006-03-26 01:54:20 +01:00
|
|
|
m->instancingConfig->SetMatrix(mat);
|
|
|
|
|
2005-10-30 02:22:22 +02:00
|
|
|
// render the lot
|
|
|
|
size_t numFaces = mdldef->GetNumFaces();
|
2006-09-23 18:04:54 +02:00
|
|
|
|
|
|
|
if (!g_Renderer.m_SkipSubmit) {
|
|
|
|
pglDrawRangeElementsEXT(GL_TRIANGLES, 0, (GLuint)mdldef->GetNumVertices(),
|
|
|
|
(GLsizei)numFaces*3, GL_UNSIGNED_SHORT, m->imodeldef->m_Indices);
|
|
|
|
}
|
2005-10-30 02:22:22 +02:00
|
|
|
|
|
|
|
// bump stats
|
|
|
|
g_Renderer.m_Stats.m_DrawCalls++;
|
|
|
|
g_Renderer.m_Stats.m_ModelTris += numFaces;
|
|
|
|
}
|
|
|
|
|