Ver Fonte

- Changes in the logic behind a few controllers
- Texture gurds

Panagiotis Christopoulos Charitos há 15 anos atrás
pai
commit
9c4b163b89

+ 3 - 2
src/Main.cpp

@@ -179,8 +179,9 @@ void init()
 	imp = new SkelModelNode();
 	imp->init("models/imp/imp.smdl");
 	//imp->setLocalTransform(Transform(Vec3(0.0, 2.11, 0.0), Mat3(Euler(-M::PI/2, 0.0, 0.0)), 0.7));
-	imp->meshNodes[0]->meshSkelCtrl->skelNode->skelAnimCtrl->skelAnim.loadRsrc("models/imp/walk.imp.anim");
-	imp->meshNodes[0]->meshSkelCtrl->skelNode->skelAnimCtrl->step = 0.8;
+	SkelAnimCtrl* ctrl = new SkelAnimCtrl(*imp->meshNodes[0]->meshSkelCtrl->skelNode);
+	ctrl->skelAnim.loadRsrc("models/imp/walk.imp.anim");
+	ctrl->step = 0.8;
 
 	// cave map
 	/*for(int i=1; i<21; i++)

+ 39 - 3
src/Resources/Texture.cpp

@@ -4,6 +4,11 @@
 #include "Image.h"
 
 
+#define STD_CHECKS \
+	DEBUG_ERR(lastBindTexUnit != getActiveTexUnit()); \
+	DEBUG_ERR(glId != getBindedTexId(getActiveTexUnit())); ///@todo
+
+
 //======================================================================================================================
 // Statics                                                                                                             =
 //======================================================================================================================
@@ -20,8 +25,7 @@ Texture::Texture():
 	Resource(RT_TEXTURE),
 	glId(numeric_limits<uint>::max()),
 	target(GL_TEXTURE_2D)
-{
-}
+{}
 
 
 //======================================================================================================================
@@ -162,7 +166,7 @@ void Texture::bind(uint unit) const
 	if(unit >= static_cast<uint>(textureUnitsNum))
 		WARNING("Max tex units passed");
 
-	glActiveTexture(GL_TEXTURE0+unit);
+	glActiveTexture(GL_TEXTURE0 + unit);
 	glBindTexture(target, getGlId());
 }
 
@@ -228,3 +232,35 @@ void Texture::setRepeat(bool repeat) const
 		setTexParameter(GL_TEXTURE_WRAP_T, GL_CLAMP);
 	}
 }
+
+
+//======================================================================================================================
+// getBaseLevel                                                                                                        =
+//======================================================================================================================
+int Texture::getBaseLevel() const
+{
+
+}
+
+
+//======================================================================================================================
+// getActiveTexUnit                                                                                                    =
+//======================================================================================================================
+uint Texture::getActiveTexUnit()
+{
+	GLint unit;
+	glGetIntegerv(GL_ACTIVE_TEXTURE, &unit);
+	return unit - GL_TEXTURE0;
+}
+
+
+//======================================================================================================================
+// getBindedTexId                                                                                                      =
+//======================================================================================================================
+uint Texture::getBindedTexId(uint unit)
+{
+	GLint id;
+	glActiveTexture(GL_TEXTURE0 + unit);
+	glGetIntegerv(GL_TEXTURE_BINDING_2D, &id);
+	return id;
+}

+ 4 - 1
src/Resources/Texture.h

@@ -31,10 +31,13 @@ class Texture: public Resource
 		bool createEmpty2D(float width, float height, int internalFormat, int format, GLenum type_, bool mipmapping);
 		bool createEmpty2DMsaa(int samplesNum, int internalFormat, int width_, int height_, bool mimapping);
 
-		void bind(uint texUnit=0) const;
+		void bind(uint texUnit = 0) const;
 		void setRepeat(bool repeat) const;
 		void setTexParameter(GLenum paramName, GLint value) const;
 		void texParameter(GLenum paramName, GLfloat value) const;
+		int getBaseLevel() const;
+		static uint getActiveTexUnit();
+		static uint getBindedTexId(uint unit);
 
 	protected:
 		GLuint glId; ///< Identification for OGL

+ 28 - 29
src/Scene/Controllers/SkelAnimCtrl.cpp

@@ -9,27 +9,22 @@
 //======================================================================================================================
 // SkelAnimCtrl                                                                                              =
 //======================================================================================================================
-SkelAnimCtrl::SkelAnimCtrl(SkelNode* skelNode_):
+SkelAnimCtrl::SkelAnimCtrl(SkelNode& skelNode_):
 	Controller(CT_SKEL_ANIM),
 	skelNode(skelNode_)
-{
-	heads.resize(skelNode->skeleton->bones.size());
-	tails.resize(skelNode->skeleton->bones.size());
-	boneRotations.resize(skelNode->skeleton->bones.size());
-	boneTranslations.resize(skelNode->skeleton->bones.size());
-}
+{}
 
 
 //======================================================================================================================
 // interpolate                                                                                                         =
 //======================================================================================================================
-void SkelAnimCtrl::interpolate(SkelAnim* animation, float frame)
+void SkelAnimCtrl::interpolate(const SkelAnim& animation, float frame)
 {
-	DEBUG_ERR(frame >= animation->framesNum);
+	DEBUG_ERR(frame >= animation.framesNum);
 
 	// calculate the t (used in slerp and lerp) using the keyframs and the frame and
 	// calc the lPose and rPose witch indicate the pose ids in witch the frame lies between
-	const Vec<uint>& keyframes = animation->keyframes;
+	const Vec<uint>& keyframes = animation.keyframes;
 	float t = 0.0;
 	uint lPose = 0, rPose = 0;
 	for(uint j=0; j<keyframes.size(); j++)
@@ -51,10 +46,13 @@ void SkelAnimCtrl::interpolate(SkelAnim* animation, float frame)
 
 
 	// now for all bones update bone's poses
+	Vec<Mat3>& boneRotations = skelNode.boneRotations;
+	Vec<Vec3>& boneTranslations = skelNode.boneTranslations;
+
 	DEBUG_ERR(boneRotations.size()<1);
 	for(uint i=0; i<boneRotations.size(); i++)
 	{
-		const SkelAnim::BoneAnim& banim = animation->bones[i];
+		const SkelAnim::BoneAnim& banim = animation.bones[i];
 
 		Mat3& localRot = boneRotations[i];
 		Vec3& localTransl = boneTranslations[i];
@@ -93,34 +91,37 @@ void SkelAnimCtrl::updateBoneTransforms()
 	uint queue[128];
 	uint head = 0, tail = 0;
 
+	Vec<Mat3>& boneRotations = skelNode.boneRotations;
+	Vec<Vec3>& boneTranslations = skelNode.boneTranslations;
+
 	// put the roots
-	for(uint i=0; i<skelNode->skeleton->bones.size(); i++)
-		if(skelNode->skeleton->bones[i].parent == NULL)
+	for(uint i=0; i<skelNode.skeleton->bones.size(); i++)
+		if(skelNode.skeleton->bones[i].parent == NULL)
 			queue[tail++] = i; // queue push
 
 	// loop
 	while(head != tail) // while queue not empty
 	{
 		uint boneId = queue[head++]; // queue pop
-		const Skeleton::Bone& boned = skelNode->skeleton->bones[boneId];
+		const Skeleton::Bone& boned = skelNode.skeleton->bones[boneId];
 
 		// bone.final_transform = MA * ANIM * MAi
 		// where MA is bone matrix at armature space and ANIM the interpolated transformation.
 		combineTransformations(boneTranslations[boneId], boneRotations[boneId],
-		                        boned.tslSkelSpaceInv, boned.rotSkelSpaceInv,
-		                        boneTranslations[boneId], boneRotations[boneId]);
+		                       boned.tslSkelSpaceInv, boned.rotSkelSpaceInv,
+		                       boneTranslations[boneId], boneRotations[boneId]);
 
 		combineTransformations(boned.tslSkelSpace, boned.rotSkelSpace,
-		                        boneTranslations[boneId], boneRotations[boneId],
-		                        boneTranslations[boneId], boneRotations[boneId]);
+		                       boneTranslations[boneId], boneRotations[boneId],
+		                       boneTranslations[boneId], boneRotations[boneId]);
 
 		// and finaly add the parent's transform
 		if(boned.parent)
 		{
 			// bone.final_final_transform = parent.transf * bone.final_transform
 			combineTransformations(boneTranslations[boned.parent->id], boneRotations[boned.parent->id],
-		                          boneTranslations[boneId], boneRotations[boneId],
-		                          boneTranslations[boneId], boneRotations[boneId]);
+		                         boneTranslations[boneId], boneRotations[boneId],
+		                         boneTranslations[boneId], boneRotations[boneId]);
 		}
 
 		// now add the bone's childes
@@ -135,15 +136,15 @@ void SkelAnimCtrl::updateBoneTransforms()
 //======================================================================================================================
 void SkelAnimCtrl::deform()
 {
-	Skeleton* skeleton = skelNode->skeleton.get();
+	const Skeleton& skeleton = *skelNode.skeleton;
 
-	for(uint i=0; i<skeleton->bones.size(); i++)
+	for(uint i=0; i<skeleton.bones.size(); i++)
 	{
-		const Mat3& rot = boneRotations[i];
-		const Vec3& transl = boneTranslations[i];
+		const Mat3& rot = skelNode.boneRotations[i];
+		const Vec3& transl = skelNode.boneTranslations[i];
 
-		heads[i] = skeleton->bones[i].head.getTransformed(transl, rot);
-		tails[i] = skeleton->bones[i].tail.getTransformed(transl, rot);
+		skelNode.heads[i] = skeleton.bones[i].head.getTransformed(transl, rot);
+		skelNode.tails[i] = skeleton.bones[i].tail.getTransformed(transl, rot);
 	}
 }
 
@@ -159,10 +160,8 @@ void SkelAnimCtrl::update(float)
 		frame = 0.0;
 	}
 
-	interpolate(skelAnim.get(), frame);
+	interpolate(*skelAnim, frame);
 	updateBoneTransforms();
 	if(app->getMainRenderer()->dbg.isShowSkeletonsEnabled())
-	{
 		deform();
-	}
 }

+ 13 - 12
src/Scene/Controllers/SkelAnimCtrl.h

@@ -15,24 +15,25 @@ class SkelNode;
 /// Skeleton animation controller
 class SkelAnimCtrl: public Controller
 {
-	private:
-		// The 3 steps of skeletal animation
-		void interpolate(SkelAnim* animation, float frame);
-		void updateBoneTransforms();
-		void deform();  ///< Now with HW skinning it deforms only the debug skeleton
-
 	public:
 		RsrcPtr<SkelAnim> skelAnim; ///< Skeleton animation resource
-		SkelNode* skelNode;
-		Vec<Vec3> heads;
-		Vec<Vec3> tails;
-		Vec<Mat3> boneRotations;
-		Vec<Vec3> boneTranslations;
 		float step;
 		float frame;
 
-		SkelAnimCtrl(SkelNode* skelNode_);
+		SkelAnimCtrl(SkelNode& skelNode_);
 		void update(float time);
+
+	private:
+		SkelNode& skelNode;
+
+		/**
+		 * @name The 3 steps of skeletal animation in 3 funcs
+		 */
+		/**@{*/
+		void interpolate(const SkelAnim& animation, float frame);
+		void updateBoneTransforms();
+		void deform();  ///< Now with HW skinning it deforms only the debug skeleton
+		/**@}*/
 };
 
 

+ 4 - 4
src/Scene/MeshNode.cpp

@@ -50,11 +50,11 @@ void MeshNode::render(Material* mtl) const
 		DEBUG_ERR(!mtl->hasHWSkinning()); // it has skel controller but no skinning
 
 		// first the uniforms
-		mtl->stdUniVars[Material::SUV_SKINNING_ROTATIONS]->setMat3(&meshSkelCtrl->skelNode->skelAnimCtrl->boneRotations[0],
-		                                                              meshSkelCtrl->skelNode->skeleton->bones.size());
+		mtl->stdUniVars[Material::SUV_SKINNING_ROTATIONS]->setMat3(&meshSkelCtrl->skelNode->boneRotations[0],
+		                                                           meshSkelCtrl->skelNode->skeleton->bones.size());
 
-		mtl->stdUniVars[Material::SUV_SKINNING_TRANSLATIONS]->setVec3(&meshSkelCtrl->skelNode->skelAnimCtrl->boneTranslations[0],
-		                                                                 meshSkelCtrl->skelNode->skeleton->bones.size());
+		mtl->stdUniVars[Material::SUV_SKINNING_TRANSLATIONS]->setVec3(&meshSkelCtrl->skelNode->boneTranslations[0],
+		                                                              meshSkelCtrl->skelNode->skeleton->bones.size());
 
 		// then the attributes
 		mesh->vbos.vertWeights.bind();

+ 7 - 6
src/Scene/SkelNode.cpp

@@ -10,10 +10,8 @@
 // SkelNode                                                                                                         =
 //======================================================================================================================
 SkelNode::SkelNode(): 
-	SceneNode(SNT_SKELETON),
-	skelAnimCtrl(NULL)
-{
-}
+	SceneNode(SNT_SKELETON)
+{}
 
 
 //======================================================================================================================
@@ -22,7 +20,10 @@ SkelNode::SkelNode():
 void SkelNode::init(const char* filename)
 {
 	skeleton.loadRsrc(filename);
-	skelAnimCtrl = new SkelAnimCtrl(this);
+	heads.resize(skeleton->bones.size());
+	tails.resize(skeleton->bones.size());
+	boneRotations.resize(skeleton->bones.size());
+	boneTranslations.resize(skeleton->bones.size());
 }
 
 
@@ -39,6 +40,6 @@ void SkelNode::render()
 
 	for(uint i=0; i<skeleton->bones.size(); i++)
 	{
-		Renderer::Dbg::drawLine(skelAnimCtrl->heads[i], skelAnimCtrl->tails[i], Vec4(1.0));
+		Renderer::Dbg::drawLine(heads[i], tails[i], Vec4(1.0));
 	}
 }

+ 6 - 1
src/Scene/SkelNode.h

@@ -1,6 +1,7 @@
 #ifndef SKEL_NODE_H
 #define SKEL_NODE_H
 
+#include <memory>
 #include "Common.h"
 #include "SceneNode.h"
 #include "SkelAnimCtrl.h"
@@ -15,11 +16,15 @@ class SkelNode: public SceneNode
 {
 	public:
 		RsrcPtr<Skeleton> skeleton; ///< The skeleton resource
-		SkelAnimCtrl* skelAnimCtrl; ///< Hold the controller here as well @todo make it reference
+		Vec<Vec3> heads;
+		Vec<Vec3> tails;
+		Vec<Mat3> boneRotations;
+		Vec<Vec3> boneTranslations;
 
 		SkelNode();
 		void render();
 		void init(const char* filename);
+
 };