Instancing: Add support for scale transformation.

This commit is contained in:
Vincent Lejeune 2014-03-31 18:10:45 +02:00
parent e1e791ff73
commit bb311618e3
10 changed files with 71 additions and 33 deletions

View File

@ -4,6 +4,7 @@ uniform mat4 InverseViewMatrix;
in vec3 Origin;
in vec3 Orientation;
in vec3 Scale;
in vec3 Position;
in vec3 Normal;
@ -13,13 +14,13 @@ in vec4 Color;
out vec3 nor;
out vec2 uv;
mat4 getWorldMatrix(vec3 translation, vec3 rotation);
mat4 getInverseWorldMatrix(vec3 translation, vec3 rotation);
mat4 getWorldMatrix(vec3 translation, vec3 rotation, vec3 scale);
mat4 getInverseWorldMatrix(vec3 translation, vec3 rotation, vec3 scale);
void main()
{
mat4 ModelMatrix = getWorldMatrix(Origin + windDir * Color.r, Orientation);
mat4 TransposeInverseModelView = transpose(getInverseWorldMatrix(Origin + windDir * Color.r, Orientation) * InverseViewMatrix);
mat4 ModelMatrix = getWorldMatrix(Origin + windDir * Color.r, Orientation, Scale);
mat4 TransposeInverseModelView = transpose(getInverseWorldMatrix(Origin + windDir * Color.r, Orientation, Scale) * InverseViewMatrix);
gl_Position = ViewProjectionMatrix * ModelMatrix * vec4(Position, 1.);
nor = (TransposeInverseModelView * vec4(Normal, 0.)).xyz;
uv = Texcoord;

View File

@ -3,6 +3,7 @@ uniform mat4 InverseViewMatrix;
in vec3 Origin;
in vec3 Orientation;
in vec3 Scale;
in vec3 Position;
in vec3 Normal;
@ -11,13 +12,13 @@ in vec2 Texcoord;
out vec3 nor;
out vec2 uv;
mat4 getWorldMatrix(vec3 translation, vec3 rotation);
mat4 getInverseWorldMatrix(vec3 translation, vec3 rotation);
mat4 getWorldMatrix(vec3 translation, vec3 rotation, vec3 scale);
mat4 getInverseWorldMatrix(vec3 translation, vec3 rotation, vec3 scale);
void main(void)
{
mat4 ModelMatrix = getWorldMatrix(Origin, Orientation);
mat4 TransposeInverseModelView = transpose(getInverseWorldMatrix(Origin, Orientation) * InverseViewMatrix);
mat4 ModelMatrix = getWorldMatrix(Origin, Orientation, Scale);
mat4 TransposeInverseModelView = transpose(getInverseWorldMatrix(Origin, Orientation, Scale) * InverseViewMatrix);
gl_Position = ViewProjectionMatrix * ModelMatrix * vec4(Position, 1.);
nor = (TransposeInverseModelView * vec4(Normal, 0.)).xyz;
uv = Texcoord;

View File

@ -22,18 +22,29 @@ mat4 getMatrixFromRotation(vec3 rotation)
vec4(0., 0., 0., 1.)));
}
mat4 getWorldMatrix(vec3 translation, vec3 rotation)
mat4 getScaleMatrix(vec3 scale)
{
mat4 result = mat4(
vec4(scale.x, 0., 0., 0.),
vec4(0., scale.y, 0., 0.),
vec4(0., 0., scale.z, 0.),
vec4(0., 0., 0., 1.)
);
return result;
}
mat4 getWorldMatrix(vec3 translation, vec3 rotation, vec3 scale)
{
mat4 result = getMatrixFromRotation(rotation);
// translation
result[3].xyz += translation;
return result;
return result * getScaleMatrix(scale);
}
mat4 getInverseWorldMatrix(vec3 translation, vec3 rotation)
mat4 getInverseWorldMatrix(vec3 translation, vec3 rotation, vec3 scale)
{
mat4 result = transpose(getMatrixFromRotation(rotation));
// FIXME: it's wrong but the fourth column is not used
result[3].xyz -= translation;
return result;
return getScaleMatrix(1. / scale) * result;
}

View File

@ -511,7 +511,7 @@ namespace MeshShader
GLuint InstancedObjectPass1Shader::attrib_normal;
GLuint InstancedObjectPass1Shader::attrib_orientation;
GLuint InstancedObjectPass1Shader::attrib_origin;
GLuint InstancedObjectPass1Shader::attrib_scale;
GLuint InstancedObjectPass1Shader::uniform_MP;
GLuint InstancedObjectPass1Shader::uniform_VM;
@ -525,6 +525,7 @@ namespace MeshShader
attrib_origin = glGetAttribLocation(Program, "Origin");
attrib_orientation = glGetAttribLocation(Program, "Orientation");
attrib_position = glGetAttribLocation(Program, "Position");
attrib_scale = glGetAttribLocation(Program, "Scale");
attrib_normal = glGetAttribLocation(Program, "Normal");
uniform_MP = glGetUniformLocation(Program, "ViewProjectionMatrix");
uniform_VM = glGetUniformLocation(Program, "InverseViewMatrix");
@ -541,6 +542,7 @@ namespace MeshShader
GLuint InstancedGrassPass1Shader::attrib_normal;
GLuint InstancedGrassPass1Shader::attrib_origin;
GLuint InstancedGrassPass1Shader::attrib_orientation;
GLuint InstancedGrassPass1Shader::attrib_scale;
GLuint InstancedGrassPass1Shader::attrib_texcoord;
GLuint InstancedGrassPass1Shader::attrib_color;
GLuint InstancedGrassPass1Shader::uniform_MP;
@ -557,6 +559,7 @@ namespace MeshShader
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/objectref_pass1.frag").c_str());
attrib_origin = glGetAttribLocation(Program, "Origin");
attrib_orientation = glGetAttribLocation(Program, "Orientation");
attrib_scale = glGetAttribLocation(Program, "Scale");
attrib_position = glGetAttribLocation(Program, "Position");
attrib_normal = glGetAttribLocation(Program, "Normal");
attrib_color = glGetAttribLocation(Program, "Color");
@ -626,6 +629,7 @@ namespace MeshShader
GLuint InstancedObjectPass2Shader::attrib_texcoord;
GLuint InstancedObjectPass2Shader::attrib_origin;
GLuint InstancedObjectPass2Shader::attrib_orientation;
GLuint InstancedObjectPass2Shader::attrib_scale;
GLuint InstancedObjectPass2Shader::uniform_VP;
GLuint InstancedObjectPass2Shader::uniform_TM;
GLuint InstancedObjectPass2Shader::uniform_screen;
@ -643,6 +647,7 @@ namespace MeshShader
attrib_texcoord = glGetAttribLocation(Program, "Texcoord");
attrib_origin = glGetAttribLocation(Program, "Origin");
attrib_orientation = glGetAttribLocation(Program, "Orientation");
attrib_scale = glGetAttribLocation(Program, "Scale");
uniform_VP = glGetUniformLocation(Program, "ViewProjectionMatrix");
uniform_TM = glGetUniformLocation(Program, "TextureMatrix");
GLuint uniform_Albedo = glGetUniformLocation(Program, "Albedo");
@ -927,6 +932,7 @@ namespace MeshShader
GLuint InstancedGrassPass2Shader::attrib_color;
GLuint InstancedGrassPass2Shader::attrib_origin;
GLuint InstancedGrassPass2Shader::attrib_orientation;
GLuint InstancedGrassPass2Shader::attrib_scale;
GLuint InstancedGrassPass2Shader::uniform_VP;
GLuint InstancedGrassPass2Shader::uniform_screen;
GLuint InstancedGrassPass2Shader::uniform_ambient;
@ -945,6 +951,7 @@ namespace MeshShader
attrib_color = glGetAttribLocation(Program, "Color");
attrib_origin = glGetAttribLocation(Program, "Origin");
attrib_orientation = glGetAttribLocation(Program, "Orientation");
attrib_scale = glGetAttribLocation(Program, "Scale");
uniform_VP = glGetUniformLocation(Program, "ViewProjectionMatrix");
GLuint uniform_Albedo = glGetUniformLocation(Program, "Albedo");
GLuint uniform_DiffuseMap = glGetUniformLocation(Program, "DiffuseMap");

View File

@ -81,7 +81,7 @@ class InstancedObjectPass1Shader
{
public:
static GLuint Program;
static GLuint attrib_position, attrib_normal, attrib_origin, attrib_orientation;
static GLuint attrib_position, attrib_normal, attrib_origin, attrib_orientation, attrib_scale;
static GLuint uniform_MP, uniform_VM;
static void init();
@ -92,7 +92,7 @@ class InstancedGrassPass1Shader
{
public:
static GLuint Program;
static GLuint attrib_position, attrib_normal, attrib_origin, attrib_orientation, attrib_color, attrib_texcoord;
static GLuint attrib_position, attrib_normal, attrib_origin, attrib_orientation, attrib_scale, attrib_color, attrib_texcoord;
static GLuint uniform_MP, uniform_IVM, uniform_windDir, uniform_tex;
static void init();
@ -115,7 +115,7 @@ class InstancedObjectPass2Shader
{
public:
static GLuint Program;
static GLuint attrib_position, attrib_texcoord, attrib_origin, attrib_orientation;
static GLuint attrib_position, attrib_texcoord, attrib_origin, attrib_orientation, attrib_scale;
static GLuint uniform_VP, uniform_TM, uniform_screen, uniform_ambient;
static GLuint TU_Albedo;
@ -198,7 +198,7 @@ class InstancedGrassPass2Shader
{
public:
static GLuint Program;
static GLuint attrib_position, attrib_texcoord, attrib_color, attrib_origin, attrib_orientation;
static GLuint attrib_position, attrib_texcoord, attrib_color, attrib_origin, attrib_orientation, attrib_scale;
static GLuint uniform_VP, uniform_TM, uniform_screen, uniform_ambient, uniform_windDir;
static GLuint TU_Albedo;

View File

@ -62,11 +62,14 @@ void STKInstancedSceneNode::initinstancedvaostate(GLMesh &mesh, GeometricMateria
glBindBuffer(GL_ARRAY_BUFFER, instances_vbo);
glBufferData(GL_ARRAY_BUFFER, instance_pos.size() * sizeof(float), instance_pos.data(), GL_STATIC_DRAW);
glEnableVertexAttribArray(MeshShader::InstancedObjectPass1Shader::attrib_origin);
glVertexAttribPointer(MeshShader::InstancedObjectPass1Shader::attrib_origin, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), 0);
glVertexAttribPointer(MeshShader::InstancedObjectPass1Shader::attrib_origin, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), 0);
glVertexAttribDivisor(MeshShader::InstancedObjectPass1Shader::attrib_origin, 1);
glEnableVertexAttribArray(MeshShader::InstancedObjectPass1Shader::attrib_orientation);
glVertexAttribPointer(MeshShader::InstancedObjectPass1Shader::attrib_orientation, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (GLvoid*)(3 * sizeof(float)));
glVertexAttribPointer(MeshShader::InstancedObjectPass1Shader::attrib_orientation, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (GLvoid*)(3 * sizeof(float)));
glVertexAttribDivisor(MeshShader::InstancedObjectPass1Shader::attrib_orientation, 1);
glEnableVertexAttribArray(MeshShader::InstancedObjectPass1Shader::attrib_scale);
glVertexAttribPointer(MeshShader::InstancedObjectPass1Shader::attrib_scale, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (GLvoid*)(6 * sizeof(float)));
glVertexAttribDivisor(MeshShader::InstancedObjectPass1Shader::attrib_scale, 1);
break;
case FPSM_GRASS:
mesh.vao_first_pass = createVAO(mesh.vertex_buffer, mesh.index_buffer,
@ -75,11 +78,14 @@ void STKInstancedSceneNode::initinstancedvaostate(GLMesh &mesh, GeometricMateria
glBindBuffer(GL_ARRAY_BUFFER, instances_vbo);
glBufferData(GL_ARRAY_BUFFER, instance_pos.size() * sizeof(float), instance_pos.data(), GL_STATIC_DRAW);
glEnableVertexAttribArray(MeshShader::InstancedGrassPass1Shader::attrib_origin);
glVertexAttribPointer(MeshShader::InstancedGrassPass1Shader::attrib_origin, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), 0);
glVertexAttribPointer(MeshShader::InstancedGrassPass1Shader::attrib_origin, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), 0);
glVertexAttribDivisor(MeshShader::InstancedGrassPass1Shader::attrib_origin, 1);
glEnableVertexAttribArray(MeshShader::InstancedGrassPass1Shader::attrib_orientation);
glVertexAttribPointer(MeshShader::InstancedGrassPass1Shader::attrib_orientation, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (GLvoid*)(3 * sizeof(float)));
glVertexAttribPointer(MeshShader::InstancedGrassPass1Shader::attrib_orientation, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (GLvoid*)(3 * sizeof(float)));
glVertexAttribDivisor(MeshShader::InstancedGrassPass1Shader::attrib_orientation, 1);
glEnableVertexAttribArray(MeshShader::InstancedGrassPass1Shader::attrib_scale);
glVertexAttribPointer(MeshShader::InstancedGrassPass1Shader::attrib_scale, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (GLvoid*)(6 * sizeof(float)));
glVertexAttribDivisor(MeshShader::InstancedGrassPass1Shader::attrib_scale, 1);
break;
default:
return;
@ -94,22 +100,28 @@ void STKInstancedSceneNode::initinstancedvaostate(GLMesh &mesh, GeometricMateria
MeshShader::InstancedObjectPass2Shader::attrib_position, MeshShader::InstancedObjectPass2Shader::attrib_texcoord, -1, -1, -1, -1, -1, mesh.Stride);
glBindBuffer(GL_ARRAY_BUFFER, instances_vbo);
glEnableVertexAttribArray(MeshShader::InstancedObjectPass2Shader::attrib_origin);
glVertexAttribPointer(MeshShader::InstancedObjectPass2Shader::attrib_origin, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), 0);
glVertexAttribPointer(MeshShader::InstancedObjectPass2Shader::attrib_origin, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), 0);
glVertexAttribDivisor(MeshShader::InstancedObjectPass2Shader::attrib_origin, 1);
glEnableVertexAttribArray(MeshShader::InstancedObjectPass2Shader::attrib_orientation);
glVertexAttribPointer(MeshShader::InstancedObjectPass2Shader::attrib_orientation, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (GLvoid*)(3 * sizeof(float)));
glVertexAttribPointer(MeshShader::InstancedObjectPass2Shader::attrib_orientation, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (GLvoid*)(3 * sizeof(float)));
glVertexAttribDivisor(MeshShader::InstancedObjectPass2Shader::attrib_orientation, 1);
glEnableVertexAttribArray(MeshShader::InstancedObjectPass2Shader::attrib_scale);
glVertexAttribPointer(MeshShader::InstancedObjectPass2Shader::attrib_scale, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (GLvoid*)(6 * sizeof(float)));
glVertexAttribDivisor(MeshShader::InstancedObjectPass2Shader::attrib_scale, 1);
break;
case SM_GRASS:
mesh.vao_second_pass = createVAO(mesh.vertex_buffer, mesh.index_buffer,
MeshShader::InstancedGrassPass2Shader::attrib_position, MeshShader::InstancedGrassPass2Shader::attrib_texcoord, -1, -1, -1, -1, MeshShader::InstancedGrassPass2Shader::attrib_color, mesh.Stride);
glBindBuffer(GL_ARRAY_BUFFER, instances_vbo);
glEnableVertexAttribArray(MeshShader::InstancedGrassPass2Shader::attrib_origin);
glVertexAttribPointer(MeshShader::InstancedGrassPass2Shader::attrib_origin, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), 0);
glVertexAttribPointer(MeshShader::InstancedGrassPass2Shader::attrib_origin, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), 0);
glVertexAttribDivisor(MeshShader::InstancedGrassPass2Shader::attrib_origin, 1);
glEnableVertexAttribArray(MeshShader::InstancedGrassPass2Shader::attrib_orientation);
glVertexAttribPointer(MeshShader::InstancedGrassPass2Shader::attrib_orientation, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (GLvoid*) (3 * sizeof(float)));
glVertexAttribPointer(MeshShader::InstancedGrassPass2Shader::attrib_orientation, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (GLvoid*) (3 * sizeof(float)));
glVertexAttribDivisor(MeshShader::InstancedGrassPass2Shader::attrib_orientation, 1);
glEnableVertexAttribArray(MeshShader::InstancedGrassPass2Shader::attrib_scale);
glVertexAttribPointer(MeshShader::InstancedGrassPass2Shader::attrib_scale, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (GLvoid*)(6 * sizeof(float)));
glVertexAttribDivisor(MeshShader::InstancedGrassPass2Shader::attrib_scale, 1);
break;
default:
return;
@ -141,7 +153,7 @@ void STKInstancedSceneNode::setFirstTimeMaterial()
isMaterialInitialized = true;
}
void STKInstancedSceneNode::addInstance(const core::vector3df &origin, const core::vector3df &orientation)
void STKInstancedSceneNode::addInstance(const core::vector3df &origin, const core::vector3df &orientation, const core::vector3df &scale)
{
instance_pos.push_back(origin.X);
instance_pos.push_back(origin.Y);
@ -149,6 +161,10 @@ void STKInstancedSceneNode::addInstance(const core::vector3df &origin, const cor
instance_pos.push_back(orientation.X);
instance_pos.push_back(orientation.Y);
instance_pos.push_back(orientation.Z);
instance_pos.push_back(1.);// scale.X);
instance_pos.push_back(1.);// scale.Y);
instance_pos.push_back(1.);// scale.Z);
printf("scale is %f %f %f\n", scale.X, scale.Y, scale.Z);
}
static void drawFSPMDefault(GLMesh &mesh, const core::matrix4 &ModelViewProjectionMatrix, size_t instance_count)
@ -226,13 +242,13 @@ void STKInstancedSceneNode::render()
if (!GeometricMesh[FPSM_DEFAULT].empty())
glUseProgram(MeshShader::InstancedObjectPass1Shader::Program);
for (unsigned i = 0; i < GeometricMesh[FPSM_DEFAULT].size(); i++)
drawFSPMDefault(*GeometricMesh[FPSM_DEFAULT][i], ModelViewProjectionMatrix, instance_pos.size() / 3);
drawFSPMDefault(*GeometricMesh[FPSM_DEFAULT][i], ModelViewProjectionMatrix, instance_pos.size() / 9);
windDir = getWind();
if (!GeometricMesh[FPSM_GRASS].empty())
glUseProgram(MeshShader::InstancedGrassPass1Shader::Program);
for (unsigned i = 0; i < GeometricMesh[FPSM_GRASS].size(); i++)
drawFSPMGrass(*GeometricMesh[FPSM_GRASS][i], ModelViewProjectionMatrix, windDir, instance_pos.size() / 3);
drawFSPMGrass(*GeometricMesh[FPSM_GRASS][i], ModelViewProjectionMatrix, windDir, instance_pos.size() / 9);
return;
}
@ -241,12 +257,12 @@ void STKInstancedSceneNode::render()
if (!ShadedMesh[SM_DEFAULT].empty())
glUseProgram(MeshShader::InstancedObjectPass2Shader::Program);
for (unsigned i = 0; i < ShadedMesh[FPSM_DEFAULT].size(); i++)
drawSMDefault(*ShadedMesh[FPSM_DEFAULT][i], ModelViewProjectionMatrix, instance_pos.size() / 3);
drawSMDefault(*ShadedMesh[FPSM_DEFAULT][i], ModelViewProjectionMatrix, instance_pos.size() / 9);
if (!ShadedMesh[SM_GRASS].empty())
glUseProgram(MeshShader::InstancedGrassPass2Shader::Program);
for (unsigned i = 0; i < ShadedMesh[SM_GRASS].size(); i++)
drawSMGrass(*ShadedMesh[SM_GRASS][i], ModelViewProjectionMatrix, windDir, instance_pos.size() / 3);
drawSMGrass(*ShadedMesh[SM_GRASS][i], ModelViewProjectionMatrix, windDir, instance_pos.size() / 9);
return;
}
}

View File

@ -25,7 +25,7 @@ public:
const irr::core::vector3df& scale = irr::core::vector3df(1.0f, 1.0f, 1.0f));
~STKInstancedSceneNode();
virtual void render();
void addInstance(const core::vector3df &origin, const core::vector3df &orientation);
void addInstance(const core::vector3df &origin, const core::vector3df &orientation, const core::vector3df &scale);
};
#endif

View File

@ -118,6 +118,7 @@ LODNode* ModelDefinitionLoader::instanciateAsLOD(const XMLNode* node, scene::ISc
STKInstancedSceneNode* ModelDefinitionLoader::instanciate(const irr::core::vector3df& position,
const irr::core::vector3df& rotation,
const irr::core::vector3df scale,
const std::string& name)
{
if (m_instancing_nodes.find(name) == m_instancing_nodes.end())
@ -134,7 +135,7 @@ STKInstancedSceneNode* ModelDefinitionLoader::instanciate(const irr::core::vecto
m_track->addNode(m_instancing_nodes[name]);
}
m_instancing_nodes[name]->addInstance(position, rotation);
m_instancing_nodes[name]->addInstance(position, rotation, scale);
return m_instancing_nodes[name];
}

View File

@ -85,6 +85,7 @@ public:
LODNode* instanciateAsLOD(const XMLNode* xml_node, scene::ISceneNode* parent);
STKInstancedSceneNode* instanciate(const core::vector3df& position,
const irr::core::vector3df& rotation,
const irr::core::vector3df scale,
const std::string& name);
void clear();

View File

@ -169,7 +169,7 @@ TrackObjectPresentationInstancing::TrackObjectPresentationInstancing(const XMLNo
m_node->setScale(m_init_scale);
m_node->updateAbsolutePosition();
m_instancing_group = model_def_loader.instanciate(m_node->getAbsolutePosition(),
m_node->getAbsoluteTransformation().getRotationDegrees(),
m_node->getAbsoluteTransformation().getRotationDegrees(), m_node->getAbsoluteTransformation().getScale(),
instancing_model);
}