Browse Source

Interleaved meshes. Wont compile

Panagiotis Christopoulos Charitos 13 năm trước cách đây
mục cha
commit
0518935016
6 tập tin đã thay đổi với 126 bổ sung87 xóa
  1. 15 15
      include/anki/gl/Vao.h
  2. 11 11
      include/anki/resource/Mesh.h
  3. 1 1
      include/anki/resource/Model.h
  4. 10 10
      src/gl/Vao.cpp
  5. 57 33
      src/resource/Mesh.cpp
  6. 32 17
      src/resource/Model.cpp

+ 15 - 15
include/anki/gl/Vao.h

@@ -74,16 +74,16 @@ public:
 	///        be normalized
 	/// @param stride Specifies the byte offset between consecutive generic
 	///        vertex attributes
-	/// @param pointer Specifies a offset of the first component of the
+	/// @param offset Specifies a offset of the first component of the
 	///        first generic vertex attribute in the array
 	void attachArrayBufferVbo(
-	    const Vbo& vbo,
+	    const Vbo* vbo,
 	    const ShaderProgramAttributeVariable& attribVar,
-	    GLint size,
-	    GLenum type,
-	    GLboolean normalized,
-	    GLsizei stride,
-	    const GLvoid* pointer);
+	    const PtrSize size,
+	    const GLenum type,
+	    const Bool normalized,
+	    const PtrSize stride,
+	    const PtrSize offset);
 
 	/// Attach an array buffer VBO. See @link
 	/// http://www.opengl.org/sdk/docs/man3/xhtml/glVertexAttribPointer.xml
@@ -100,13 +100,13 @@ public:
 	/// @param pointer Specifies a offset of the first component of the
 	///        first generic vertex attribute in the array
 	void attachArrayBufferVbo(
-	    const Vbo& vbo,
-	    GLuint attribVarLocation,
-	    GLint size,
-	    GLenum type,
-	    GLboolean normalized,
-	    GLsizei stride,
-	    const GLvoid* pointer);
+	    const Vbo* vbo,
+	    const GLint attribVarLocation,
+	    const PtrSize size,
+	    const GLenum type,
+	    const Bool normalized,
+	    const PtrSize stride,
+	    const PtrSize offset);
 
 	U32 getAttachmentsCount() const
 	{
@@ -114,7 +114,7 @@ public:
 	}
 
 	/// Attach an element array buffer VBO
-	void attachElementArrayBufferVbo(const Vbo& vbo);
+	void attachElementArrayBufferVbo(const Vbo* vbo);
 
 	/// Bind it
 	void bind() const

+ 11 - 11
include/anki/resource/Mesh.h

@@ -17,14 +17,14 @@ class MeshBase
 public:
 	enum VertexAttribute
 	{
-		VA_POSITIONS,
-		VA_NORMALS,
-		VA_TANGENTS,
+		VA_POSITION,
+		VA_NORMAL,
+		VA_TANGENT,
 		VA_TEXTURE_COORDS,
 		VA_TEXTURE_COORDS_1,
-		VA_WEIGHTS_BONE_COUNT,
-		VA_WEIGHTS_BONE_IDS,
-		VA_WEIGHTS_BONE_WEIGHTS,
+		VA_BONE_COUNT,
+		VA_BONE_IDS,
+		VA_BONE_WEIGHTS,
 		VA_INDICES, 
 		VA_COUNT
 	};
@@ -34,15 +34,15 @@ public:
 
 	/// Get info on how to attach a VBO to a VAO
 	virtual void getVboInfo(
-		const VertexAttribute attrib, const U32 lod, Vbo* vbo, 
-		U32& size, GLenum& type, U32& stride, U32& offset) = 0;
+		const VertexAttribute attrib, const U32 lod, const Vbo*& vbo, 
+		U32& size, GLenum& type, U32& stride, U32& offset) const = 0;
 
 	U32 getVerticesCount() const
 	{
 		return vertsCount;
 	}
 
-	U32 getIndicesCount(U32 lod)
+	U32 getIndicesCount(U32 lod) const
 	{
 		return indicesCount[lod];
 	}
@@ -102,8 +102,8 @@ public:
 
 	/// Implements MeshBase::getVboInfo
 	void getVboInfo(
-		const VertexAttribute attrib, const U32 lod, Vbo* vbo, 
-		U32& size, GLenum& type, U32& stride, U32& offset);
+		const VertexAttribute attrib, const U32 lod, const Vbo*& vbo, 
+		U32& size, GLenum& type, U32& stride, U32& offset) const;
 
 private:
 	Vbo vbo;

+ 1 - 1
include/anki/resource/Model.h

@@ -30,7 +30,7 @@ public:
 	{
 		PassLevelToVaoMap::const_iterator it = vaosMap.find(key);
 		ANKI_ASSERT(it != vaosMap.end());
-		return *(*it);
+		return *(it->second);
 	}
 
 	/// Allias to MeshBase::getIndicesCount()

+ 10 - 10
src/gl/Vao.cpp

@@ -19,9 +19,9 @@ Vao::~Vao()
 }
 
 //==============================================================================
-void Vao::attachArrayBufferVbo(const Vbo& vbo, GLuint attribVarLocation,
-	GLint size, GLenum type, GLboolean normalized, GLsizei stride,
-	const GLvoid* pointer)
+void Vao::attachArrayBufferVbo(const Vbo* vbo, const GLint attribVarLocation,
+	const PtrSize size, const GLenum type, const Bool normalized, 
+	const PtrSize stride, const PtrSize offset)
 {
 	ANKI_ASSERT(isCreated());
 	ANKI_ASSERT(vbo.getBufferTarget() == GL_ARRAY_BUFFER
@@ -30,10 +30,10 @@ void Vao::attachArrayBufferVbo(const Vbo& vbo, GLuint attribVarLocation,
 	checkNonSharable();
 
 	bind();
-	vbo.bind();
+	vbo->bind();
 	glEnableVertexAttribArray(attribVarLocation);
 	glVertexAttribPointer(attribVarLocation, size, type, normalized,
-		stride, pointer);
+		stride, reinterpret_cast<const GLvoid*>(offset));
 	vbo.unbind();
 	unbind();
 
@@ -43,17 +43,17 @@ void Vao::attachArrayBufferVbo(const Vbo& vbo, GLuint attribVarLocation,
 }
 
 //==============================================================================
-void Vao::attachArrayBufferVbo(const Vbo& vbo,
+void Vao::attachArrayBufferVbo(const Vbo* vbo,
 	const ShaderProgramAttributeVariable& attribVar,
-	GLint size, GLenum type, GLboolean normalized, GLsizei stride,
-	const GLvoid* pointer)
+	const PtrSize size, const GLenum type, const Bool normalized, 
+	const PtrSize stride, const PtrSize offset)
 {
 	attachArrayBufferVbo(vbo, attribVar.getLocation(), size, type, normalized,
-		stride, pointer);
+		stride, offset);
 }
 
 //==============================================================================
-void Vao::attachElementArrayBufferVbo(const Vbo& vbo)
+void Vao::attachElementArrayBufferVbo(const Vbo* vbo)
 {
 	ANKI_ASSERT(isCreated());
 	ANKI_ASSERT(vbo.getBufferTarget() == GL_ELEMENT_ARRAY_BUFFER

+ 57 - 33
src/resource/Mesh.cpp

@@ -112,66 +112,90 @@ void Mesh::createVbos(const MeshLoader& meshData)
 
 //==============================================================================
 void Mesh::getVboInfo(
-	const VertexAttribute attrib, const U32 lod, Vbo* v, U32& size, 
-	GLenum& type, U32& stride, U32& offset)
+	const VertexAttribute attrib, const U32 lod, const Vbo*& v, U32& size, 
+	GLenum& type, U32& stride, U32& offset) const
 {
 	stride = calcVertexSize();
 
+	// Set all to zero
+	v = nullptr;
+	size = 0;
+	type = GL_NONE;
+	offset = 0;
+
 	switch(attrib)
 	{
-	case VA_POSITIONS:
+	case VA_POSITION:
 		v = &vbo;
 		size = 3;
 		type = GL_FLOAT;
 		offset = 0;
 		break;
-	case VA_NORMALS:
+	case VA_NORMAL:
 		v = &vbo;
 		size = 3;
 		type = GL_FLOAT;
 		offset = sizeof(Vec3);
 		break;
-	case VA_TANGENTS:
+	case VA_TANGENT:
 		v = &vbo;
 		size = 4;
 		type = GL_FLOAT;
 		offset = sizeof(Vec3) * 2;
 		break;
 	case VA_TEXTURE_COORDS:
-		v = &vbo;
-		size = 2;
-		type = GL_FLOAT;
-		offset = sizeof(Vec3) * 2 + sizeof(Vec4) + sizeof(Vec2);
+		if(texChannelsCount > 0)
+		{
+			v = &vbo;
+			size = 2;
+			type = GL_FLOAT;
+			offset = sizeof(Vec3) * 2 + sizeof(Vec4) + sizeof(Vec2);
+		}
 		break;
 	case VA_TEXTURE_COORDS_1:
-		ANKI_ASSERT(texChannelsCount > 1);
-		v = &vbo;
-		size = 2;
-		type = GL_FLOAT;
-		offset = sizeof(Vec3) * 2 + sizeof(Vec4) + sizeof(Vec2) * 2;
+		if(texChannelsCount > 1)
+		{
+			v = &vbo;
+			size = 2;
+			type = GL_FLOAT;
+			offset = sizeof(Vec3) * 2 + sizeof(Vec4) + sizeof(Vec2) * 2;
+		}
 		break;
-	case VA_WEIGHTS_BONE_COUNT:
-		v = &vbo;
-		size = 1;
-		type = GL_UNSIGNED_INT;
-		offset = sizeof(Vec3) * 2 + sizeof(Vec4) 
-			+ texChannelsCount * sizeof(Vec2);
+	case VA_BONE_COUNT:
+		if(weights)
+		{
+			v = &vbo;
+			size = 1;
+			type = GL_UNSIGNED_INT;
+			offset = sizeof(Vec3) * 2 + sizeof(Vec4) 
+				+ texChannelsCount * sizeof(Vec2);
+		}
 		break;
-	case VA_WEIGHTS_BONE_IDS:
-		v = &vbo;
-		size = 4;
-		type = GL_UNSIGNED_INT;
-		offset = sizeof(Vec3) * 2 + sizeof(Vec4) 
-			+ texChannelsCount * sizeof(Vec2) + sizeof(U32);
+	case VA_BONE_IDS:
+		if(weights)
+		{
+			v = &vbo;
+			size = 4;
+			type = GL_UNSIGNED_INT;
+			offset = sizeof(Vec3) * 2 + sizeof(Vec4) 
+				+ texChannelsCount * sizeof(Vec2) + sizeof(U32);
+		}
 		break;
-	case VA_WEIGHTS_BONE_WEIGHTS:
-		v = &vbo;
-		size = 4;
-		type = GL_FLOAT;
-		offset = sizeof(Vec3) * 2 + sizeof(Vec4) 
-			+ texChannelsCount * sizeof(Vec2) + sizeof(U32) + sizeof(U32) * 4;
+	case VA_BONE_WEIGHTS:
+		if(weights)
+		{
+			v = &vbo;
+			size = 4;
+			type = GL_FLOAT;
+			offset = sizeof(Vec3) * 2 + sizeof(Vec4) 
+				+ texChannelsCount * sizeof(Vec2) + sizeof(U32) 
+				+ sizeof(U32) * 4;
+		}
 	case VA_INDICES:
-		v = &indicesVbos[lod];
+		if(lod < indicesVbos.size())
+		{
+			v = &indicesVbos[lod];
+		}
 		break;
 	default:
 		ANKI_ASSERT(0);

+ 32 - 17
src/resource/Model.cpp

@@ -25,46 +25,61 @@ static const Array<Attrib, MeshBase::VA_COUNT - 1> attribs = {{
 	{"tangent", MeshBase::VA_TANGENT},
 	{"texCoords", MeshBase::VA_TEXTURE_COORDS},
 	{"texCoords1", MeshBase::VA_TEXTURE_COORDS_1},
-	{"bonesCount", MeshBase::VA_WEIGHTS_BONE_COUNT},
-	{"boneIds", MeshBase::VA_WEIGHTS_BONE_IDS},
-	{"boneWeights", MeshBase::VA_WEIGHTS_BONE_WEIGHTS}
+	{"bonesCount", MeshBase::VA_BONE_COUNT},
+	{"boneIds", MeshBase::VA_BONE_IDS},
+	{"boneWeights", MeshBase::VA_BONE_WEIGHTS}
 }};
 
 //==============================================================================
-Vao* ModelPatchBase::createVao(const Material& mtl,const MeshBase& meshb,
+void ModelPatchBase::createVao(const Material& mtl,const MeshBase& meshb,
 	const PassLevelKey& key, Vao& vao)
 {
 	vao.create();
 
 	const ShaderProgram& prog = mtl.findShaderProgram(key);
 
+	const Vbo* vbo;
+	U32 size;
+	GLenum type;
+	U32 stride;
+	U32 offset;
+
 	for(const Attrib& attrib : attribs)
 	{
-		const ShaderProgramAttributeVariable* attrib = 
+		const ShaderProgramAttributeVariable* attr = 
 			prog.tryFindAttributeVariable(attrib.name);
 
-		if(name == nullptr)
+		if(attr == nullptr)
 		{
 			continue;
 		}
 
-		Vbo* vbo;
-		U32 size;
-		GLenum type;
-		U32 stride;
-		U32 offset;
-
-		meshb.getVboInfo(attrib.id, key.lod, vbo, size, type,
+		meshb.getVboInfo(attrib.id, (U32)key.level, vbo, size, type,
 			stride, offset);
 
-		vao.attachArrayBufferVbo(*vbo, *attrib, size, type, GL_FALSE, stride,
+		if(vbo == nullptr)
+		{
+			throw ANKI_EXCEPTION("Material asks for attribute that the mesh "
+				"does not have: " + attrib.name);
+		}
+
+		vao.attachArrayBufferVbo(vbo, *attr, size, type, false, stride,
 			offset);
 	}
 
-	meshb.getVboInfo(MeshBase::VA_INDICES, key.lod, vbo, size, type,
+	// The indices VBO
+	meshb.getVboInfo(MeshBase::VA_INDICES, key.level, vbo, size, type,
 			stride, offset);
 
-	vao.attachElementArrayBufferVbo(*vbo);
+	if(vbo == nullptr)
+	{
+		// The desired LOD was not found. Use the 0
+		meshb.getVboInfo(MeshBase::VA_INDICES, 0, vbo, size, type,
+			stride, offset);
+	}
+
+	ANKI_ASSERT(vbo != nullptr);
+	vao.attachElementArrayBufferVbo(vbo);
 }
 
 //==============================================================================
@@ -83,7 +98,7 @@ void ModelPatchBase::createVaos(const Material& mtl,
 			createVao(mtl, meshb, key, vao);
 
 			vaos.push_back(std::move(vao));
-			vaosMap[key] = &vaos.back();
+			vaosMap[key] = &vaos[vaos.size() - 1];
 		}
 	}
 }