Fix a crash and add other shader versions

This commit is contained in:
Skia 2016-06-15 13:24:43 +02:00
parent 1c9373a5ef
commit ac8253f2b1
6 changed files with 70 additions and 25 deletions

View file

@ -19,6 +19,7 @@ Camera c;
std::list<GLuint> shaderList;
GLuint shaderProg;
GLuint shaderObj;
float d_angle_x =0.0f;
@ -47,34 +48,34 @@ bool AddShader(GLenum ShaderType, const char* pFilename)
return false;
}
GLuint ShaderObj = glCreateShader(ShaderType);
shaderObj = glCreateShader(ShaderType);
if (ShaderObj == 0) {
if (shaderObj == 0) {
std::cerr << "Error creating shader type " << ShaderType << "\n";
return false;
}
// Save the shader object - will be deleted in the destructor
shaderList.push_back(ShaderObj);
shaderList.push_back(shaderObj);
const GLchar* p[1];
p[0] = s.c_str();
GLint Lengths[1] = { (GLint)s.size() };
glShaderSource(ShaderObj, 1, p, Lengths);
glShaderSource(shaderObj, 1, p, Lengths);
glCompileShader(ShaderObj);
glCompileShader(shaderObj);
GLint success;
glGetShaderiv(ShaderObj, GL_COMPILE_STATUS, &success);
glGetShaderiv(shaderObj, GL_COMPILE_STATUS, &success);
if (!success) {
GLchar InfoLog[1024];
glGetShaderInfoLog(ShaderObj, 1024, NULL, InfoLog);
glGetShaderInfoLog(shaderObj, 1024, NULL, InfoLog);
std::cerr << "Error compiling '" << pFilename << "': '" << InfoLog << "'\n";
return false;
}
glAttachShader(shaderProg, ShaderObj);
glAttachShader(shaderProg, shaderObj);
return true;
}
@ -129,6 +130,7 @@ void reshape(GLsizei width, GLsizei height) {
/* Manage ASCII key input */
void keyInput(unsigned char key, int x, int y){
glUseProgram(shaderProg);
switch(key){
case'1': scene->setNumAnimation(1);
scene->activateAnimation(true);
@ -251,12 +253,14 @@ int main(int argc, char** argv)
std::cerr << "Error creating shader program\n";
return 1;
}
if (!AddShader(GL_VERTEX_SHADER, "shader.vs")) {
AddShader(GL_VERTEX_SHADER, "shader_legacy.vs");
if (AddShader(GL_VERTEX_SHADER, "shader.vs")) {
std::cout << "loading 3.3 shader\n";
} else if (AddShader(GL_VERTEX_SHADER, "shader_legacy150.vs")) {
std::cout << "loading 1.5 shader\n";
} else if (AddShader(GL_VERTEX_SHADER, "shader_legacy120.vs")) {
std::cout << "loading 1.2 shader\n";
}
glLinkProgram(shaderProg);
std::cout << "GUY\n\n\n";
glUseProgram(shaderProg);
scene = new SceneHandler(argv[1], shaderProg);

View file

@ -61,16 +61,16 @@ void Mesh::render(bool anim) {
t[k] = boneStateList[this->getBoneIndex(v->getBonesID(k))].Transpose();
}
glVertexAttribPointer(shader_Weights, 3, GL_FLOAT, GL_FALSE, 0, &t);
// std::cout << t << "\n";
// glMultMatrixf(&t.a1);
// FIXME: we need to send the data to the shader!!!!
if (this->getNormal(index) != NULL) {
glVertexAttribPointer(shader_Normal, 3, GL_FLOAT, GL_FALSE, 0, &this->getNormal(index)->x);
}
glVertexAttribPointer(shader_Position, 3, GL_FLOAT, GL_FALSE, 0, &(this->getVertex(index)->getPosition()).x);
} else {
if (this->getNormal(index) != NULL) {
glNormal3fv(&this->getNormal(index)->x);
}
glVertex3fv(&(this->getVertex(index)->getPosition()).x);
}
// glPopMatrix();
if (this->getNormal(index) != NULL) {
glVertexAttribPointer(shader_Normal, 3, GL_FLOAT, GL_FALSE, 0, &this->getNormal(index)->x);
}
//glVertex3fv();
glVertexAttribPointer(shader_Position, 3, GL_FLOAT, GL_FALSE, 0, &(this->getVertex(index)->getPosition()).x);
}
glEnd();
}
@ -131,11 +131,12 @@ void CalcInterpolatedRotation(aiQuaternion& Out, float AnimationTime, BoneAnim*
Out = Out.Normalize();
}
void Mesh::updateBoneStateList(float AnimationTime, const aiNode* pNode, const aiMatrix4x4& ParentTransform)
bool Mesh::updateBoneStateList(float AnimationTime, const aiNode* pNode, const aiMatrix4x4& ParentTransform)
{
aiString NodeName(pNode->mName.data);
Animation* anim = animList[currentAnimation];
if (anim->getDuration() <= AnimationTime) return false;
aiMatrix4x4 NodeTransformation(pNode->mTransformation);
@ -173,4 +174,5 @@ void Mesh::updateBoneStateList(float AnimationTime, const aiNode* pNode, const a
for (unsigned int i = 0 ; i < pNode->mNumChildren ; i++) {
this->updateBoneStateList(AnimationTime, pNode->mChildren[i], GlobalTransformation);
}
return true;
}

2
mesh.h
View file

@ -96,7 +96,7 @@ class Mesh
void initAnimList(const aiScene *scene);
void updateBoneStateList(float AnimationTime, const aiNode* pNode, const aiMatrix4x4& ParentTransform);
bool updateBoneStateList(float AnimationTime, const aiNode* pNode, const aiMatrix4x4& ParentTransform);
void render(bool anim);

View file

@ -154,7 +154,12 @@ void SceneHandler::render() {
aiMatrix4x4 ident;
aiMatrix4x4::Scaling(aiVector3D(1, 1, 1), ident);
Mesh *my_mesh = meshList[i];
if (isAnimating) my_mesh->updateBoneStateList(runningTime, scene->mRootNode, ident);
if (isAnimating) {
if (!my_mesh->updateBoneStateList(runningTime, scene->mRootNode, ident)) {
isAnimating = false;
glUseProgram(0); // This is a pretty ugly hack
}
}
my_mesh->render(isAnimating);
}
glPopMatrix();

34
shader_legacy120.vs Normal file
View file

@ -0,0 +1,34 @@
#version 120
attribute vec3 Position;
// attribute vec3 Normal;
// attribute mat4 BoneTransform[4];
// attribute float Weights[4];
// varying vec2 TexCoord0;
// varying vec3 Normal0;
// varying vec3 WorldPos0;
// const int MAX_BONES = 100;
// uniform mat4 gWVP;
uniform mat4 gWorld;
// uniform mat4 gBones[MAX_BONES];
void main()
{
// mat4 Transform = BoneTransform[0] * Weights[0];
// Transform += BoneTransform[1] * Weights[1];
// Transform += BoneTransform[2] * Weights[2];
// Transform += BoneTransform[3] * Weights[3];
// vec4 PosL = Transform * vec4(Position, 1.0);
vec4 PosL = vec4(Position, 1.0);
// gl_Position = gWVP * PosL;
gl_Position = gWorld * PosL;
// gl_Position = vec4(Position, 0);
// vec4 NormalL = Transform * vec4(Normal, 0.0);
// Normal0 = (gWorld * NormalL).xyz;
// WorldPos0 = (gWorld * PosL).xyz;
}

View file

@ -1,4 +1,4 @@
#version 120
#version 150
// FIXME