facecam2d/src/modelpart.cpp

78 lines
2.3 KiB
C++
Raw Normal View History

2021-01-01 16:20:09 +00:00
#include <GL/glew.h>
#include <glm/mat4x4.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <graphics.hpp>
#include <modelpart.hpp>
#include <iostream>
std::map<std::string, int> bindStringToNum {
{"null", BIND_NULL},
{"head", BIND_HEAD},
{"face", BIND_FACE},
};
std::map<std::string, bool> triggerStringToNum {
{"null", TRIGGER_NULL},
{"mouth-open", TRIGGER_MOUTH_OPEN},
};
2021-01-01 16:20:09 +00:00
ModelPart::ModelPart() {
2021-01-01 16:20:09 +00:00
//create vbo, ebo, vao
initBuffers(&vao);
empty = false;
2021-01-01 16:20:09 +00:00
}
void ModelPart::bindAndDraw() {
glBindVertexArray(vao);
glBindTexture(GL_TEXTURE_2D, tex[texSelection]);
2021-01-01 16:20:09 +00:00
glUniformMatrix4fv(transUniform, 1, GL_FALSE, glm::value_ptr(transMatrix));
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}
void ModelPart::setBind(std::string bindName) {
bind = bindStringToNum[bindName];
}
void ModelPart::setFollowTarget(std::string followTarget) {
follow = bindStringToNum[followTarget];
}
2021-01-10 07:13:32 +00:00
void ModelPart::setTransform(glm::vec2 position, float rotation, float scale) {
transMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(position.x, -position.y, 0.0f) + glm::vec3(posOffset, 0.0f));
transMatrix = glm::rotate(transMatrix, rotation * rotFactor, glm::vec3(0.0f, 0.0f, 1.0f));
transMatrix = glm::scale(transMatrix, glm::vec3(1,1,1) + (scale - 1 + glm::vec3(scaleOffset, 0.0f)) * scaleFactor);
transMatrix = glm::translate(transMatrix, glm::vec3(-origin, 0.0f));
}
void ModelPart::processFaceData(struct FaceData faceData) {
// calculate position
glm::vec2 bindPosition = faceData.positions[bind];
glm::vec2 followPosition = faceData.positions[follow];
glm::vec2 followDirection = followPosition - bindPosition;
glm::vec2 newPosition = bindPosition + (followDirection * factor);
setTransform(newPosition, faceData.headRotation, faceData.scale);
// change textures
selectTexture(0); // if none are triggered, use the first one
for (size_t i = 0; i < texCount; i++) {
if (faceData.triggers[texTriggers[i]]) {
selectTexture(i);
break; // if several textures are triggered, the first one defined in model.toml will get priority
}
}
}
void ModelPart::addTexture(unsigned char* texBuffer, size_t texLength, size_t slot, std::string triggerName) {
initTexture(&tex[slot], texBuffer, texLength);
texTriggers[slot] = triggerStringToNum[triggerName];
texCount++;
}
void ModelPart::selectTexture(size_t slot) {
texSelection = slot;
2021-01-01 16:20:09 +00:00
}