Skip to content
This repository has been archived by the owner on Dec 31, 2020. It is now read-only.

Commit

Permalink
Reinforced vbo loading so that shader attributes can vary. Testing he…
Browse files Browse the repository at this point in the history
…re with no normals.
  • Loading branch information
num3ric committed May 16, 2013
1 parent 1f0a45a commit 33b38ee
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 34 deletions.
2 changes: 1 addition & 1 deletion include/ModelIo.h
Expand Up @@ -34,7 +34,7 @@ struct MaterialInfo

class BoneWeights {
public:
BoneWeights() : mActiveNbWeights( 0 ) { }
BoneWeights() : mActiveNbWeights( 0 ), mWeights({}) { }
static const int NB_WEIGHTS = 4;
void addWeight( const std::shared_ptr<Node>& bone, float weight );
float getWeight( int index ) const { return mWeights[index]; }
Expand Down
10 changes: 7 additions & 3 deletions include/ModelTargetSkinnedVboMesh.h
Expand Up @@ -27,10 +27,14 @@ class ModelTargetSkinnedVboMesh : public ModelTarget {
private:
SkinnedVboMesh* mSkinnedVboMesh;

template<class T> void bufferSubData( const T& buffer, size_t dim);
void setCustomAttribute( const std::string& name, int location );
void incrementOffsets( size_t dataSize );
void resetOffsets();

int mOffset;
template<class T> void bufferSubData( const T& buffer, size_t dataSize);
void setCustomAttribute( const std::string& name, GLuint location );

GLuint mAttribLocation;
ptrdiff_t mSubDataOffset;
};

} //end namespace model
21 changes: 21 additions & 0 deletions resources/skinning_frag_no_normals.glsl
@@ -0,0 +1,21 @@
#ifdef GL_ES
precision highp float;
#endif

varying vec2 Tc;
varying vec3 V;

uniform sampler2D texture;

void main (void)
{
vec4 Dm = texture2D( texture, Tc );

vec3 L = normalize(gl_LightSource[0].position.xyz);
vec3 E = normalize(-V);

vec4 Iamb = gl_FrontLightProduct[0].ambient * Dm;
vec4 Idiff = gl_FrontLightProduct[0].diffuse * Dm;

gl_FragColor = gl_FrontMaterial.emission + Iamb + Idiff;
}
2 changes: 1 addition & 1 deletion resources/skinning_frag_normals.glsl
Expand Up @@ -3,7 +3,7 @@ precision highp float;
#endif

varying vec2 Tc;
varying vec3 N, L, V;
varying vec3 V, N, L;

uniform sampler2D texture;

Expand Down
29 changes: 29 additions & 0 deletions resources/skinning_vert_no_normals.glsl
@@ -0,0 +1,29 @@
const int MAXBONES = 92;

attribute vec3 position;
attribute vec2 texcoord;
attribute vec4 boneWeights;
attribute vec4 boneIndices;

uniform bool isAnimated;
uniform mat4 boneMatrices[MAXBONES];
uniform mat4 invTransposeMatrices[MAXBONES];

varying vec2 Tc;
varying vec3 V;

void main()
{
vec4 pos = vec4(position, 1.0);
if( isAnimated ) {
pos = boneMatrices[int(boneIndices.x)] * pos * boneWeights.x +
boneMatrices[int(boneIndices.y)] * pos * boneWeights.y +
boneMatrices[int(boneIndices.z)] * pos * boneWeights.z +
boneMatrices[int(boneIndices.w)] * pos * boneWeights.w ;

pos.w = 1.0;
}
V = (gl_ModelViewMatrix * pos).xyz;
Tc = texcoord;
gl_Position = gl_ModelViewProjectionMatrix * pos;
}
2 changes: 1 addition & 1 deletion resources/skinning_vert_normals.glsl
Expand Up @@ -11,7 +11,7 @@ uniform mat4 boneMatrices[MAXBONES];
uniform mat4 invTransposeMatrices[MAXBONES];

varying vec2 Tc;
varying vec3 N, L, V;
varying vec3 V, N, L;

void main()
{
Expand Down
6 changes: 4 additions & 2 deletions samples/SeymourDemo/include/Resources.h
@@ -1,5 +1,7 @@
#pragma once
#include "cinder/CinderResources.h"

#define RES_SKINNING_VERT CINDER_RESOURCE( ../../../resources/, skinning_vert_normals.glsl, 128, GLSL )
#define RES_SKINNING_FRAG CINDER_RESOURCE( ../../../resources/, skinning_frag_normals.glsl, 129, GLSL )
#define RES_SKINNING_VERT CINDER_RESOURCE( ../../../resources/, skinning_vert_normals.glsl, 128, GLSL )
#define RES_SKINNING_FRAG CINDER_RESOURCE( ../../../resources/, skinning_frag_normals.glsl, 129, GLSL )
#define RES_SKINNING_VERT_NO_NORMALS CINDER_RESOURCE( ../../../resources/, skinning_vert_no_normals.glsl, 130, GLSL )
#define RES_SKINNING_FRAG_NO_NORMALS CINDER_RESOURCE( ../../../resources/, skinning_frag_no_normals.glsl, 131, GLSL )
Expand Up @@ -25,6 +25,8 @@
B00A7FAF17412BCF00131FD9 /* Actor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B00A7FAE17412BCF00131FD9 /* Actor.cpp */; };
B035754E16F92F41006B03A1 /* skinning_frag_normals.glsl in Resources */ = {isa = PBXBuildFile; fileRef = B035754C16F92F41006B03A1 /* skinning_frag_normals.glsl */; };
B035754F16F92F41006B03A1 /* skinning_vert_normals.glsl in Resources */ = {isa = PBXBuildFile; fileRef = B035754D16F92F41006B03A1 /* skinning_vert_normals.glsl */; };
B051926A1744ED95007A921C /* skinning_vert_no_normals.glsl in Resources */ = {isa = PBXBuildFile; fileRef = B01F84961744EA1700AA62D4 /* skinning_vert_no_normals.glsl */; };
B051926B1744ED95007A921C /* skinning_frag_no_normals.glsl in Resources */ = {isa = PBXBuildFile; fileRef = B01F84971744EA1700AA62D4 /* skinning_frag_no_normals.glsl */; };
B999A80CA3924F5A9E8F61DC /* Skeleton.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A9DDE6E7EAA6496D8D7E3844 /* Skeleton.cpp */; };
BFB888FB70554572B0D0D39A /* ModelTargetSkinnedMesh.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 05F4ABBFD6AC47A2A3C7D2D6 /* ModelTargetSkinnedMesh.cpp */; };
D1AE18B1746240DFB8BC018E /* ModelTargetSkinnedVboMesh.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2BC06C948E8B4006AD29A071 /* ModelTargetSkinnedVboMesh.cpp */; };
Expand Down Expand Up @@ -66,6 +68,8 @@
B00A7FAC17412BC500131FD9 /* AnimTrack.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = AnimTrack.h; path = ../../../include/AnimTrack.h; sourceTree = "<group>"; };
B00A7FAD17412BC500131FD9 /* Actor.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = Actor.h; path = ../../../include/Actor.h; sourceTree = "<group>"; };
B00A7FAE17412BCF00131FD9 /* Actor.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Actor.cpp; path = ../../../src/Actor.cpp; sourceTree = "<group>"; };
B01F84961744EA1700AA62D4 /* skinning_vert_no_normals.glsl */ = {isa = PBXFileReference; explicitFileType = sourcecode.glsl; name = skinning_vert_no_normals.glsl; path = ../../../resources/skinning_vert_no_normals.glsl; sourceTree = "<group>"; };
B01F84971744EA1700AA62D4 /* skinning_frag_no_normals.glsl */ = {isa = PBXFileReference; explicitFileType = sourcecode.glsl; name = skinning_frag_no_normals.glsl; path = ../../../resources/skinning_frag_no_normals.glsl; sourceTree = "<group>"; };
B035754C16F92F41006B03A1 /* skinning_frag_normals.glsl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = skinning_frag_normals.glsl; path = ../../../resources/skinning_frag_normals.glsl; sourceTree = "<group>"; };
B035754D16F92F41006B03A1 /* skinning_vert_normals.glsl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = skinning_vert_normals.glsl; path = ../../../resources/skinning_vert_normals.glsl; sourceTree = "<group>"; };
BBEA026A1577471A89F8FB62 /* ModelTargetSkinnedMesh.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = ModelTargetSkinnedMesh.h; path = ../../../include/ModelTargetSkinnedMesh.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -225,6 +229,8 @@
4D9769CC73854B94826B2591 /* resources */ = {
isa = PBXGroup;
children = (
B01F84961744EA1700AA62D4 /* skinning_vert_no_normals.glsl */,
B01F84971744EA1700AA62D4 /* skinning_frag_no_normals.glsl */,
B035754C16F92F41006B03A1 /* skinning_frag_normals.glsl */,
B035754D16F92F41006B03A1 /* skinning_vert_normals.glsl */,
);
Expand Down Expand Up @@ -291,6 +297,8 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
B051926A1744ED95007A921C /* skinning_vert_no_normals.glsl in Resources */,
B051926B1744ED95007A921C /* skinning_frag_no_normals.glsl in Resources */,
0F4795547ED4410E962DE84E /* CinderApp.icns in Resources */,
B035754E16F92F41006B03A1 /* skinning_frag_normals.glsl in Resources */,
B035754F16F92F41006B03A1 /* skinning_vert_normals.glsl in Resources */,
Expand Down
6 changes: 3 additions & 3 deletions src/ModelSourceAssimp.cpp
Expand Up @@ -22,7 +22,6 @@ namespace ai {
aiProcess_ImproveCacheLocality |
aiProcess_LimitBoneWeights |
aiProcess_RemoveRedundantMaterials |
aiProcess_Triangulate |
aiProcess_GenUVCoords |
aiProcess_SortByPType |
aiProcess_FindDegenerates |
Expand Down Expand Up @@ -340,7 +339,8 @@ ModelSourceAssimp::ModelSourceAssimp( const ci::fs::path& modelPath, const ci::f
mRootAssetFolderPath = rootAssetFolderPath;

mImporter = std::unique_ptr<Assimp::Importer>( new Assimp::Importer() );
mImporter->SetIOHandler( new CustomIOSystem() );
mImporter->SetPropertyInteger(AI_CONFIG_PP_SBP_REMOVE, aiPrimitiveType_POINT | aiPrimitiveType_LINE);
// mImporter->SetIOHandler( new CustomIOSystem() );
mAiScene = mImporter->ReadFile( mModelPath.string(), ai::flags );

//TODO: make own exception class to catch
Expand All @@ -355,7 +355,7 @@ ModelSourceAssimp::ModelSourceAssimp( const ci::fs::path& modelPath, const ci::f
for( unsigned int m=0; m < mAiScene->mNumMeshes; ++m ) {
aiMesh * mesh = mAiScene->mMeshes[m];
numBones += mesh->mNumBones;
mModelInfo.mHasNormals = mModelInfo.mHasNormals || mesh->HasNormals();
// mModelInfo.mHasNormals = mModelInfo.mHasNormals || mesh->HasNormals();

mModelInfo.mNumVertices.push_back( mesh->mNumVertices );
mModelInfo.mNumIndices.push_back( 3 * mesh->mNumFaces );
Expand Down
62 changes: 40 additions & 22 deletions src/ModelTargetSkinnedVboMesh.cpp
Expand Up @@ -7,32 +7,39 @@ namespace model {

ModelTargetSkinnedVboMesh::ModelTargetSkinnedVboMesh( SkinnedVboMesh * mesh )
: mSkinnedVboMesh( mesh )
, mOffset(0)
, mSubDataOffset(0)
, mAttribLocation(0)
{

}

void ModelTargetSkinnedVboMesh::incrementOffsets( size_t dataSize )
{
mSubDataOffset += dataSize;
mAttribLocation++;
}

template<class T>
void ModelTargetSkinnedVboMesh::bufferSubData( const T& buffer, size_t dim )
void ModelTargetSkinnedVboMesh::bufferSubData( const T& buffer, size_t dataSize )
{
size_t dataSize = sizeof(GLfloat) * dim * buffer.size();
ci::gl::VboMesh& vboMesh = mSkinnedVboMesh->getActiveSection()->getVboMesh();
vboMesh.getStaticVbo().bufferSubData(mOffset, dataSize, buffer.data());
vboMesh.getStaticVbo().bufferSubData( mSubDataOffset, dataSize, buffer.data() );
vboMesh.getStaticVbo().unbind();
mOffset += dataSize;
}

void ModelTargetSkinnedVboMesh::setCustomAttribute( const std::string& name, int location )
void ModelTargetSkinnedVboMesh::setCustomAttribute( const std::string& name, GLuint location )
{
mSkinnedVboMesh->getActiveSection()->getShader().bind();
mSkinnedVboMesh->getActiveSection()->getVboMesh().setCustomStaticLocation( location, mSkinnedVboMesh->getActiveSection()->getShader().getAttribLocation( name ) );
mSkinnedVboMesh->getActiveSection()->getShader().unbind();
MeshVboSectionRef sect = mSkinnedVboMesh->getActiveSection();
sect->getShader().bind();
sect->getVboMesh().setCustomStaticLocation( location, sect->getShader().getAttribLocation( name ) );
sect->getShader().unbind();
}

void ModelTargetSkinnedVboMesh::setActiveSection( int index )
{
mSkinnedVboMesh->setActiveSection( index );
mOffset = 0;
mSubDataOffset = 0;
mAttribLocation = 0;
}

std::shared_ptr<Skeleton> ModelTargetSkinnedVboMesh::getSkeleton() const
Expand All @@ -42,16 +49,20 @@ std::shared_ptr<Skeleton> ModelTargetSkinnedVboMesh::getSkeleton() const

void ModelTargetSkinnedVboMesh::loadVertexPositions( const std::vector<ci::Vec3f>& positions )
{
bufferSubData< std::vector<ci::Vec3f> >( positions, ci::Vec3f::DIM );
setCustomAttribute( "position", 0 );
size_t dataSize = sizeof(GLfloat) * ci::Vec3f::DIM * positions.size();
bufferSubData< std::vector<ci::Vec3f> >( positions, dataSize );
setCustomAttribute( "position", mAttribLocation );
incrementOffsets( dataSize );
}

void ModelTargetSkinnedVboMesh::loadVertexNormals( const std::vector<ci::Vec3f>& normals )
{
mSkinnedVboMesh->getActiveSection()->setHasNormals( true ); //FIXME: remove this

bufferSubData< std::vector<ci::Vec3f> >( normals, ci::Vec3f::DIM );
setCustomAttribute( "normal", 1 );

size_t dataSize = sizeof(GLfloat) * ci::Vec3f::DIM * normals.size();
bufferSubData< std::vector<ci::Vec3f> >( normals, dataSize );
setCustomAttribute( "normal", mAttribLocation );
incrementOffsets( dataSize );
}

void ModelTargetSkinnedVboMesh::loadIndices( const std::vector<uint32_t>& indices )
Expand All @@ -64,9 +75,11 @@ void ModelTargetSkinnedVboMesh::loadIndices( const std::vector<uint32_t>& indice
void ModelTargetSkinnedVboMesh::loadTex( const std::vector<ci::Vec2f>& texCoords, const MaterialInfo& matInfo )
{
mSkinnedVboMesh->getActiveSection()->setMatInfo( matInfo );

bufferSubData< std::vector<ci::Vec2f> >( texCoords, ci::Vec2f::DIM );
setCustomAttribute( "texcoord", 2 );

size_t dataSize = sizeof(GLfloat) * ci::Vec2f::DIM * texCoords.size();
bufferSubData< std::vector<ci::Vec2f> >( texCoords, dataSize);
setCustomAttribute( "texcoord", mAttribLocation );
incrementOffsets( dataSize );
}

void ModelTargetSkinnedVboMesh::loadSkeleton( const SkeletonRef& skeleton )
Expand Down Expand Up @@ -95,10 +108,15 @@ void ModelTargetSkinnedVboMesh::loadBoneWeights( const std::vector<BoneWeights>&
boneIndicesBuffer.push_back( vIndices );
}

bufferSubData< std::vector<ci::Vec4f> >( boneWeightsBuffer, ci::Vec4f::DIM );
bufferSubData< std::vector<ci::Vec4f> >( boneIndicesBuffer, ci::Vec4f::DIM );
setCustomAttribute( "boneWeights", 3 );
setCustomAttribute( "boneIndices", 4 );
size_t dataSize = sizeof(GLfloat) * ci::Vec4f::DIM * boneWeightsBuffer.size();

bufferSubData< std::vector<ci::Vec4f> >( boneWeightsBuffer, dataSize );
setCustomAttribute( "boneWeights", mAttribLocation );
incrementOffsets( dataSize );

bufferSubData< std::vector<ci::Vec4f> >( boneIndicesBuffer, dataSize);
setCustomAttribute( "boneIndices", mAttribLocation );
incrementOffsets( dataSize );

mSkinnedVboMesh->getActiveSection()->boneMatrices = &mSkinnedVboMesh->mBoneMatrices;
mSkinnedVboMesh->getActiveSection()->invTransposeMatrices = &mSkinnedVboMesh->mInvTransposeMatrices;
Expand Down
2 changes: 1 addition & 1 deletion src/SkinnedVboMesh.cpp
Expand Up @@ -18,7 +18,7 @@ SkinnedVboMesh::MeshSection::MeshSection()
: ASkinnedMesh()
{
try {
mSkinningShader = ci::gl::GlslProg( ci::app::loadResource(RES_SKINNING_VERT), ci::app::loadResource(RES_SKINNING_FRAG) );
mSkinningShader = ci::gl::GlslProg( ci::app::loadResource(RES_SKINNING_VERT_NO_NORMALS), ci::app::loadResource(RES_SKINNING_FRAG_NO_NORMALS) );
}
catch( ci::gl::GlslProgCompileExc &exc ) {
std::cout << "Shader compile error: " << std::endl;
Expand Down

0 comments on commit 33b38ee

Please sign in to comment.