Animated Sonic

This commit is contained in:
Riku Avelar 2016-12-04 01:24:57 -05:00
parent c3b9f6f816
commit b1e7de08fb
7 changed files with 90 additions and 38 deletions

View File

@ -11,30 +11,30 @@ usemtl floor
s off
f 2//1 1//1 3//1 4//1
o Cube_Cube.008
v 8.485309 -0.011262 -0.420843
v 8.485309 -0.011262 -0.662478
v 9.620116 -0.011262 -0.662478
v 9.620116 -0.011262 -0.420843
v 8.485309 1.123545 -0.420843
v 8.485309 1.123545 -0.662478
v 9.620116 1.123545 -0.662478
v 9.620116 1.123545 -0.420843
v 9.698260 -0.316609 -0.662478
v 9.698260 -0.316609 -0.420843
v 9.698260 1.428891 -0.662478
v 9.698260 1.428891 -0.420843
v 8.448704 -0.316609 -0.420843
v 8.448704 -0.316609 -0.662478
v 8.448704 1.428891 -0.420843
v 8.448704 1.428891 -0.662478
v 10.557929 0.461606 -0.662478
v 10.557929 0.461606 -0.420843
v 10.557929 0.650676 -0.662478
v 10.557929 0.650676 -0.420843
v 7.589035 0.461606 -0.420843
v 7.589035 0.461606 -0.662478
v 7.589035 0.650676 -0.420843
v 7.589035 0.650676 -0.662478
v 0.0000 0.0000 0.0000
v 0.0000 0.0000 0.0000
v 0.0000 0.0000 0.0000
v 0.0000 0.0000 0.0000
v 0.0000 0.0000 0.0000
v 0.0000 0.0000 0.0000
v 0.0000 0.0000 0.0000
v 0.0000 0.0000 0.0000
v 0.0000 0.0000 0.0000
v 0.0000 0.0000 0.0000
v 0.0000 0.0000 0.0000
v 0.0000 0.0000 0.0000
v 0.0000 0.0000 0.0000
v 0.0000 0.0000 0.0000
v 0.0000 0.0000 0.0000
v 0.0000 0.0000 0.0000
v 0.0000 0.0000 0.0000
v 0.0000 0.0000 0.0000
v 0.0000 0.0000 0.0000
v 0.0000 0.0000 0.0000
v 0.0000 0.0000 0.0000
v 0.0000 0.0000 0.0000
v 0.0000 0.0000 0.0000
v 0.0000 0.0000 0.0000
vn -0.0000 0.0000 1.0000
vn 0.0000 0.0000 -1.0000
vn -0.9688 -0.2479 0.0000

View File

@ -75,6 +75,7 @@ bool Loader::loadFile(const std::string& filename)
defaultMat.Kd[0] = 1.0; defaultMat.Kd[1] = 1.0; defaultMat.Kd[2] = 1.0; defaultMat.Kd[3] = 1.0;
defaultMat.Ks[0] = 1.0; defaultMat.Ks[1] = 1.0; defaultMat.Ks[2] = 1.0; defaultMat.Ks[3] = 1.0;
defaultMat.Kn = 128;
defaultMat.d = 1.0;
defaultMat.name = "(Default)";
_materials.push_back(defaultMat);
@ -319,6 +320,7 @@ void Loader::loadMtlFile(const std::string& filename)
newMtl.Kd[0] = 0.0; newMtl.Kd[1] = 0.0; newMtl.Kd[2] = 0.0; newMtl.Kd[3] = 0.0;
newMtl.Ks[0] = 0.0; newMtl.Ks[1] = 0.0; newMtl.Ks[2] = 0.0; newMtl.Ks[3] = 0.0;
newMtl.Kn = 0;
newMtl.d = 1.0;
// Get its name
std::string dummy;
@ -374,6 +376,16 @@ void Loader::loadMtlFile(const std::string& filename)
mat.Ke[3] = 1.0f;
}
}
else if (line[0] == 'd')
{
// Alpha
std::stringstream ss(line);
std::string dummy;
float alpha;
ss >> dummy >> alpha;
_materials[currentMaterial].d = alpha;
}
}
// Close file

View File

@ -14,6 +14,7 @@ namespace OBJLoader
float Kd[4]; // Diffuse color
float Ks[4]; // Specular color
float Kn; // Specular exponent
float d; // Alpha component
std::string name; // Material's name
};

View File

@ -8,6 +8,7 @@ uniform bool isPhong;
uniform vec3 pointLight[3];
uniform vec4 pointLightCol[3];
uniform mat3 normalMatrix;
uniform float alpha;
in vec3 fNormal;
in vec3 fPosition;
@ -32,7 +33,7 @@ vec4 calcDirLight(vec4 tex, vec3 fPos, vec3 fNorm, vec3 lightDir) {
vec3 totalLight = diffV + specV;
//return vec4(Kd + Ks, 1)
return vec4(totalLight, 1);
return vec4(totalLight, alpha);
}
vec4 calcPointLight(vec4 tex, vec3 fPos, vec3 fNorm, int i) {
@ -56,13 +57,13 @@ vec4 calcPointLight(vec4 tex, vec3 fPos, vec3 fNorm, int i) {
// Compute ambient component
vec3 totalLight = attenuation * (diffV + specV);
return vec4(totalLight, 1);
return vec4(totalLight, alpha);
}
void
main()
{
vec4 texColor;
vec4 texColor = vec4(1, 1, 1, alpha);
// Get lighting vectors
vec3 LightDirection = normalize(lDirection);
@ -70,6 +71,10 @@ main()
vec3 nviewDirection = normalize(fPosition);
fColor = 0.3 * calcDirLight(texColor, fPosition, fNormal, lDirection)
+ 0.6 * calcDirLight(texColor, fPosition, fNormal, vec3(0.0)-fPosition);
fColor.a = 1.0;
+ 0.4 * calcDirLight(texColor, fPosition, fNormal, vec3(-0.2)-fPosition)
+ 0.1 * calcPointLight(texColor, fPosition, fNormal, 0)/4
+ 0.1 * calcPointLight(texColor, fPosition, fNormal, 1)/4
+ 0.1 * calcPointLight(texColor, fPosition, fNormal, 2)/4
+ 0.14 * vec4(Kd, 1);
fColor.a = alpha;
}

View File

@ -4,6 +4,7 @@ uniform mat4 projMatrix;
uniform mat3 normalMatrix;
uniform bool isPhong;
uniform vec3 lDirection;
uniform float alpha;
uniform vec3 pointLight[3];
@ -30,7 +31,7 @@ vec4 calcDirLight(vec4 eye, vec3 fPos, vec3 fNorm) {
// Compute ambient component
vec3 amb = vec3(0.1);
return vec4(amb + diff + spec, 1);
return vec4(amb + diff + spec, alpha);
}
vec4 calcPointLight(vec4 eye, vec3 fPos, vec3 fNorm, int i) {
@ -53,7 +54,7 @@ vec4 calcPointLight(vec4 eye, vec3 fPos, vec3 fNorm, int i) {
// Compute ambient component
vec3 amb = attenuation * vec3(0.1);
return 0.3 * vec4(amb + diff + spec, 1);
return 0.3 * vec4(amb + diff + spec, alpha);
}
void

View File

@ -63,6 +63,14 @@ namespace
SceneGroup* selection;
PickedGeom selectedObj;
struct ToolAnimation {
double startFrame;
int length;
bool active;
};
ToolAnimation swing;
int currentPoint = 0; // VERY lazy way of tracking light balls
}
@ -158,6 +166,7 @@ void Viewer::loadToolObj() {
meshGL.diffuse = QVector3D(Kd[0], Kd[1], Kd[2]);
meshGL.specular = QVector3D(Ks[0], Ks[1], Ks[2]);
meshGL.specularExponent = materials[meshes[i].materialID].Kn;
meshGL.alpha = materials[meshes[i].materialID].d;
// Create its VAO and VBO object
glGenVertexArrays(1, &meshGL.vao);
@ -305,6 +314,7 @@ void Viewer::mousePressEvent(QMouseEvent* e) {
selectedObj.shape = selectedGeom.shape;
selectedObj.shape->getParent()->addChild(selection);
cubeSelected(true);
startSwingAnimation();
}
if(!selectedGeom.position.isIdentity() && e->modifiers().testFlag(Qt::ShiftModifier))
@ -314,7 +324,8 @@ void Viewer::mousePressEvent(QMouseEvent* e) {
SceneGroup* container = new SceneGroup;
container->transform = selectedGeom.position;
container->addChild(c);
root.addChild(container);
root.addChild(container);
startSwingAnimation();
}
else{
QGLViewer::mousePressEvent(e);
@ -354,6 +365,7 @@ void Viewer::keyPressEvent(QKeyEvent* e) {
selectedTexture = e->key() - 0x31;
if(selectedObj.shape != nullptr ){
selectedObj.shape->setType(selectedTexture);
startSwingAnimation();
}
break;
@ -603,6 +615,8 @@ void Viewer::initShaders()
o_u_pointLightPos[i] = objShader->uniformLocation(posAttr.str().c_str());
o_u_pointLightCol[i] = objShader->uniformLocation(colAttr.str().c_str());
}
if ((o_u_ka = objShader->uniformLocation("alpha")) < 0)
qDebug() << "Unable to find m_shader location for" << "alpha" << objShader->log();
o_u_kd = objShader->uniformLocation("Kd");
o_u_ks = objShader->uniformLocation("Ks");
@ -887,14 +901,21 @@ void Viewer::drawTool() {
//camera()->getModelViewMatrix(modelViewMatrix);
QMatrix4x4 scale = QMatrix4x4();
QMatrix4x4 toolboxPos = QMatrix4x4();
float swingMult = 1;
//scale.translate(QVector3D(0,1.5,0));
scale.translate(0.5, -0.25, -2);
scale.rotate(100, 1, 0, 0);
scale.rotate(-90, 0, 1, 0);
scale.scale(0.1);
toolboxPos.translate(0.6, -0.25, -1.6);
toolboxPos.rotate(-100, 0, 1, 0);
if(swing.active) {
swingMult = 0.5 + 0.5 * cos(fmin(swing.length, frame - swing.startFrame) * 2 * M_PI / swing.length);
if(frame - swing.startFrame > swing.length) {
swing.active = false;
}
}
toolboxPos.rotate(swingMult * -65, 0, 0, 1);
toolboxPos.scale(0.06);
objShader->setUniformValue(o_u_mvMatrix, modelViewMatrix * scale);
objShader->setUniformValue(o_u_mvMatrix, modelViewMatrix * toolboxPos);
objShader->setUniformValue(o_u_nmMatrix, modelViewMatrix.normalMatrix());
// Draw the meshes
@ -904,6 +925,7 @@ void Viewer::drawTool() {
objShader->setUniformValue(o_u_kd, _meshesGL[i].diffuse);
objShader->setUniformValue(o_u_ks, _meshesGL[i].specular);
objShader->setUniformValue(o_u_kn, _meshesGL[i].specularExponent);
objShader->setUniformValue(o_u_ka, _meshesGL[i].alpha);
// Draw the mesh
glBindVertexArray(_meshesGL[i].vao);
@ -1050,6 +1072,12 @@ void Viewer::toggleNormalMaps(bool state) {
m_program->setUniformValue(m_useNormalMap, state);
}
void Viewer::startSwingAnimation() {
swing.active = true;
swing.startFrame = frame;
swing.length = 30;
}
void Viewer::deleteSelected() {
m_program->bind();
int pointCLocs[3] = {m_c1Loc, m_c2Loc, m_c3Loc};
@ -1083,6 +1111,8 @@ void Viewer::deleteSelected() {
selectedObj.shape = nullptr; // Rebind to "null"
cubeSelected(false);
startSwingAnimation();
}
void Viewer::changeColor(QColor c){

View File

@ -109,6 +109,7 @@ private:
void initBuffers();
void deselect();
void loadToolObj();
void startSwingAnimation();
PickedGeom pickGeom(int, int);
void animate();
@ -168,6 +169,7 @@ private:
int o_u_kd;
int o_u_ks;
int o_u_kn;
int o_u_ka;
int o_a_vPos;
int o_a_vNorm;
@ -248,6 +250,7 @@ private:
QVector3D diffuse;
QVector3D specular;
GLfloat specularExponent;
GLfloat alpha;
unsigned int numVertices;
};