Fix #34483: game engine multi UV glsl materials not working correct after changes

to support more than 2 UV maps. This code indirectly depended on the order of
OpenGL attribute ID's assigned by the OpenGL driver being the same as the
attributes being declared in the GLSL shader code, which is not always the case.
This commit is contained in:
Brecht Van Lommel 2013-03-01 20:45:42 +00:00
parent 70fba54545
commit 2822a14e5d
10 changed files with 37 additions and 32 deletions

@ -80,7 +80,7 @@ void BL_BlenderShader::SetAttribs(RAS_IRasterizer* ras, const BL_Material *mat)
{
GPUVertexAttribs attribs;
GPUMaterial *gpumat;
int i, attrib_num;
int i, attrib_num, uv = 0;
ras->SetAttribNum(0);
@ -103,7 +103,7 @@ void BL_BlenderShader::SetAttribs(RAS_IRasterizer* ras, const BL_Material *mat)
continue;
if (attribs.layer[i].type == CD_MTFACE)
ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_UV, attribs.layer[i].glindex);
ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_UV, attribs.layer[i].glindex, uv++);
else if (attribs.layer[i].type == CD_TANGENT)
ras->SetAttrib(RAS_IRasterizer::RAS_TEXTANGENT, attribs.layer[i].glindex);
else if (attribs.layer[i].type == CD_ORCO)

@ -396,7 +396,7 @@ public:
virtual void SetTexCoordNum(int num) = 0;
virtual void SetAttribNum(int num) = 0;
virtual void SetTexCoord(TexCoGen coords, int unit) = 0;
virtual void SetAttrib(TexCoGen coords, int unit) = 0;
virtual void SetAttrib(TexCoGen coords, int unit, int layer = 0) = 0;
virtual const MT_Matrix4x4& GetViewMatrix() const = 0;
virtual const MT_Matrix4x4& GetViewInvMatrix() const = 0;

@ -116,19 +116,19 @@ RAS_OpenGLRasterizer::RAS_OpenGLRasterizer(RAS_ICanvas* canvas, int storage)
if (m_storage_type == RAS_VBO /*|| m_storage_type == RAS_AUTO_STORAGE && GLEW_ARB_vertex_buffer_object*/)
{
m_storage = new RAS_StorageVBO(&m_texco_num, m_texco, &m_attrib_num, m_attrib);
m_failsafe_storage = new RAS_StorageIM(&m_texco_num, m_texco, &m_attrib_num, m_attrib);
m_storage = new RAS_StorageVBO(&m_texco_num, m_texco, &m_attrib_num, m_attrib, m_attrib_layer);
m_failsafe_storage = new RAS_StorageIM(&m_texco_num, m_texco, &m_attrib_num, m_attrib, m_attrib_layer);
m_storage_type = RAS_VBO;
}
else if ((m_storage_type == RAS_VA) || (m_storage_type == RAS_AUTO_STORAGE && GLEW_VERSION_1_1))
{
m_storage = new RAS_StorageVA(&m_texco_num, m_texco, &m_attrib_num, m_attrib);
m_failsafe_storage = new RAS_StorageIM(&m_texco_num, m_texco, &m_attrib_num, m_attrib);
m_storage = new RAS_StorageVA(&m_texco_num, m_texco, &m_attrib_num, m_attrib, m_attrib_layer);
m_failsafe_storage = new RAS_StorageIM(&m_texco_num, m_texco, &m_attrib_num, m_attrib, m_attrib_layer);
m_storage_type = RAS_VA;
}
else
{
m_storage = m_failsafe_storage = new RAS_StorageIM(&m_texco_num, m_texco, &m_attrib_num, m_attrib);
m_storage = m_failsafe_storage = new RAS_StorageIM(&m_texco_num, m_texco, &m_attrib_num, m_attrib, m_attrib_layer);
m_storage_type = RAS_IMMEDIATE;
}
}
@ -740,11 +740,13 @@ void RAS_OpenGLRasterizer::SetTexCoord(TexCoGen coords, int unit)
m_texco[unit] = coords;
}
void RAS_OpenGLRasterizer::SetAttrib(TexCoGen coords, int unit)
void RAS_OpenGLRasterizer::SetAttrib(TexCoGen coords, int unit, int layer)
{
// this changes from material to material
if (unit < RAS_MAX_ATTRIB)
if (unit < RAS_MAX_ATTRIB) {
m_attrib[unit] = coords;
m_attrib_layer[unit] = layer;
}
}
void RAS_OpenGLRasterizer::IndexPrimitives(RAS_MeshSlot& ms)

@ -109,6 +109,7 @@ protected:
int m_drawingmode;
TexCoGen m_texco[RAS_MAX_TEXCO];
TexCoGen m_attrib[RAS_MAX_ATTRIB];
int m_attrib_layer[RAS_MAX_ATTRIB];
int m_texco_num;
int m_attrib_num;
//int m_last_alphablend;
@ -296,7 +297,7 @@ public:
virtual void SetTexCoordNum(int num);
virtual void SetAttribNum(int num);
virtual void SetTexCoord(TexCoGen coords, int unit);
virtual void SetAttrib(TexCoGen coords, int unit);
virtual void SetAttrib(TexCoGen coords, int unit, int layer = 0);
void TexCoord(const RAS_TexVert &tv);

@ -39,11 +39,12 @@ extern "C"{
#include "BKE_DerivedMesh.h"
}
RAS_StorageIM::RAS_StorageIM(int *texco_num, RAS_IRasterizer::TexCoGen *texco, int *attrib_num, RAS_IRasterizer::TexCoGen *attrib) :
RAS_StorageIM::RAS_StorageIM(int *texco_num, RAS_IRasterizer::TexCoGen *texco, int *attrib_num, RAS_IRasterizer::TexCoGen *attrib, int *attrib_layer) :
m_texco_num(texco_num),
m_attrib_num(attrib_num),
m_texco(texco),
m_attrib(attrib)
m_attrib(attrib),
m_attrib_layer(attrib_layer)
{
}
RAS_StorageIM::~RAS_StorageIM()
@ -95,7 +96,6 @@ void RAS_StorageIM::TexCoord(const RAS_TexVert &tv)
}
if (GLEW_ARB_vertex_program) {
int uv = 0;
for (unit = 0; unit < *m_attrib_num; unit++) {
switch (m_attrib[unit]) {
case RAS_IRasterizer::RAS_TEXCO_ORCO:
@ -103,7 +103,7 @@ void RAS_StorageIM::TexCoord(const RAS_TexVert &tv)
glVertexAttrib3fvARB(unit, tv.getXYZ());
break;
case RAS_IRasterizer::RAS_TEXCO_UV:
glVertexAttrib2fvARB(unit, tv.getUV(uv++));
glVertexAttrib2fvARB(unit, tv.getUV(m_attrib_layer[unit]));
break;
case RAS_IRasterizer::RAS_TEXCO_NORM:
glVertexAttrib3fvARB(unit, tv.getNormal());

@ -34,7 +34,7 @@
class RAS_StorageIM : public RAS_IStorage
{
public:
RAS_StorageIM(int *texco_num, RAS_IRasterizer::TexCoGen *texco, int *attrib_num, RAS_IRasterizer::TexCoGen *attrib);
RAS_StorageIM(int *texco_num, RAS_IRasterizer::TexCoGen *texco, int *attrib_num, RAS_IRasterizer::TexCoGen *attrib, int *attrib_layer);
virtual ~RAS_StorageIM();
virtual bool Init();
@ -51,6 +51,7 @@ protected:
int* m_attrib_num;
RAS_IRasterizer::TexCoGen* m_texco;
RAS_IRasterizer::TexCoGen* m_attrib;
int* m_attrib_layer;
void TexCoord(const RAS_TexVert &tv);
void SetCullFace(bool enable);

@ -29,13 +29,14 @@
#include "GL/glew.h"
RAS_StorageVA::RAS_StorageVA(int *texco_num, RAS_IRasterizer::TexCoGen *texco, int *attrib_num, RAS_IRasterizer::TexCoGen *attrib) :
RAS_StorageVA::RAS_StorageVA(int *texco_num, RAS_IRasterizer::TexCoGen *texco, int *attrib_num, RAS_IRasterizer::TexCoGen *attrib, int *attrib_layer) :
m_texco_num(texco_num),
m_attrib_num(attrib_num),
m_last_texco_num(0),
m_last_attrib_num(0),
m_texco(texco),
m_attrib(attrib)
m_attrib(attrib),
m_attrib_layer(attrib_layer)
{
}
@ -214,7 +215,6 @@ void RAS_StorageVA::TexCoordPtr(const RAS_TexVert *tv)
}
if (GLEW_ARB_vertex_program) {
int uv = 0;
for (unit = 0; unit < *m_attrib_num; unit++) {
switch (m_attrib[unit]) {
case RAS_IRasterizer::RAS_TEXCO_ORCO:
@ -222,7 +222,7 @@ void RAS_StorageVA::TexCoordPtr(const RAS_TexVert *tv)
glVertexAttribPointerARB(unit, 3, GL_FLOAT, GL_FALSE, sizeof(RAS_TexVert), tv->getXYZ());
break;
case RAS_IRasterizer::RAS_TEXCO_UV:
glVertexAttribPointerARB(unit, 2, GL_FLOAT, GL_FALSE, sizeof(RAS_TexVert), tv->getUV(uv++));
glVertexAttribPointerARB(unit, 2, GL_FLOAT, GL_FALSE, sizeof(RAS_TexVert), tv->getUV(m_attrib_layer[unit]));
break;
case RAS_IRasterizer::RAS_TEXCO_NORM:
glVertexAttribPointerARB(unit, 3, GL_FLOAT, GL_FALSE, sizeof(RAS_TexVert), tv->getNormal());

@ -37,7 +37,7 @@ class RAS_StorageVA : public RAS_IStorage
{
public:
RAS_StorageVA(int *texco_num, RAS_IRasterizer::TexCoGen *texco, int *attrib_num, RAS_IRasterizer::TexCoGen *attrib);
RAS_StorageVA(int *texco_num, RAS_IRasterizer::TexCoGen *texco, int *attrib_num, RAS_IRasterizer::TexCoGen *attrib, int *attrib_layer);
virtual ~RAS_StorageVA();
virtual bool Init();
@ -59,6 +59,7 @@ protected:
RAS_IRasterizer::TexCoGen* m_texco;
RAS_IRasterizer::TexCoGen* m_attrib;
int* m_attrib_layer;
RAS_IRasterizer::TexCoGen m_last_texco[RAS_MAX_TEXCO];
RAS_IRasterizer::TexCoGen m_last_attrib[RAS_MAX_ATTRIB];

@ -101,10 +101,10 @@ void VBO::UpdateIndices()
glBufferData(GL_ELEMENT_ARRAY_BUFFER, space, &data->m_index[0], GL_STATIC_DRAW);
}
void VBO::Draw(int texco_num, RAS_IRasterizer::TexCoGen* texco, int attrib_num, RAS_IRasterizer::TexCoGen* attrib, bool multi)
void VBO::Draw(int texco_num, RAS_IRasterizer::TexCoGen* texco, int attrib_num, RAS_IRasterizer::TexCoGen* attrib, int *attrib_layer, bool multi)
{
int unit;
// Bind buffers
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, this->ibo);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, this->vbo_id);
@ -159,7 +159,6 @@ void VBO::Draw(int texco_num, RAS_IRasterizer::TexCoGen* texco, int attrib_num,
if (GLEW_ARB_vertex_program)
{
int uv = 0;
for (unit = 0; unit < attrib_num; ++unit)
{
switch (attrib[unit]) {
@ -169,8 +168,7 @@ void VBO::Draw(int texco_num, RAS_IRasterizer::TexCoGen* texco, int attrib_num,
glEnableVertexAttribArrayARB(unit);
break;
case RAS_IRasterizer::RAS_TEXCO_UV:
glVertexAttribPointerARB(unit, 2, GL_FLOAT, GL_FALSE, this->stride, (void*)((intptr_t)this->uv_offset+uv));
uv += sizeof(GLfloat)*2;
glVertexAttribPointerARB(unit, 2, GL_FLOAT, GL_FALSE, this->stride, (void*)((intptr_t)this->uv_offset+attrib_layer[unit]*sizeof(GLfloat)*2));
glEnableVertexAttribArrayARB(unit);
break;
case RAS_IRasterizer::RAS_TEXCO_NORM:
@ -204,11 +202,12 @@ void VBO::Draw(int texco_num, RAS_IRasterizer::TexCoGen* texco, int attrib_num,
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
}
RAS_StorageVBO::RAS_StorageVBO(int *texco_num, RAS_IRasterizer::TexCoGen *texco, int *attrib_num, RAS_IRasterizer::TexCoGen *attrib):
RAS_StorageVBO::RAS_StorageVBO(int *texco_num, RAS_IRasterizer::TexCoGen *texco, int *attrib_num, RAS_IRasterizer::TexCoGen *attrib, int *attrib_layer):
m_texco_num(texco_num),
m_attrib_num(attrib_num),
m_texco(texco),
m_attrib(attrib)
m_attrib(attrib),
m_attrib_layer(attrib_layer)
{
}
@ -240,7 +239,7 @@ void RAS_StorageVBO::IndexPrimitivesInternal(RAS_MeshSlot& ms, bool multi)
{
RAS_MeshSlot::iterator it;
VBO *vbo;
for (ms.begin(it); !ms.end(it); ms.next(it))
{
vbo = m_vbo_lookup[it.array];
@ -254,6 +253,6 @@ void RAS_StorageVBO::IndexPrimitivesInternal(RAS_MeshSlot& ms, bool multi)
vbo->UpdateData();
}
vbo->Draw(*m_texco_num, m_texco, *m_attrib_num, m_attrib, multi);
vbo->Draw(*m_texco_num, m_texco, *m_attrib_num, m_attrib, m_attrib_layer, multi);
}
}

@ -42,7 +42,7 @@ public:
VBO(RAS_DisplayArray *data, unsigned int indices);
~VBO();
void Draw(int texco_num, RAS_IRasterizer::TexCoGen* texco, int attrib_num, RAS_IRasterizer::TexCoGen* attrib, bool multi);
void Draw(int texco_num, RAS_IRasterizer::TexCoGen* texco, int attrib_num, RAS_IRasterizer::TexCoGen* attrib, int *attrib_layer, bool multi);
void UpdateData();
void UpdateIndices();
@ -66,7 +66,7 @@ class RAS_StorageVBO : public RAS_IStorage
{
public:
RAS_StorageVBO(int *texco_num, RAS_IRasterizer::TexCoGen *texco, int *attrib_num, RAS_IRasterizer::TexCoGen *attrib);
RAS_StorageVBO(int *texco_num, RAS_IRasterizer::TexCoGen *texco, int *attrib_num, RAS_IRasterizer::TexCoGen *attrib, int *attrib_layer);
virtual ~RAS_StorageVBO();
virtual bool Init();
@ -85,6 +85,7 @@ protected:
RAS_IRasterizer::TexCoGen* m_texco;
RAS_IRasterizer::TexCoGen* m_attrib;
int* m_attrib_layer;
std::map<RAS_DisplayArray*, class VBO*> m_vbo_lookup;