Add head rotation

This commit is contained in:
Epicalert 2021-01-10 15:13:32 +08:00
parent f8fe94529f
commit 7b82b5c386
No known key found for this signature in database
GPG key ID: CAA46F858D0979BD
5 changed files with 21 additions and 8 deletions

View file

@ -62,17 +62,29 @@ void cvFrame() {
//send control information to graphics
float faceSize = landmarks[biggestFace][14].x - landmarks[biggestFace][2].x;
updateModel(glm::vec2(
updateModel(
//head position
glm::vec2(
(landmarks[biggestFace][2].x + landmarks[biggestFace][14].x) / 2
* 2 / (float)frame.cols - 1,
(landmarks[biggestFace][2].y + landmarks[biggestFace][14].y) / 2
* 2 / (float)frame.rows - 1
),
//face position
glm::vec2(
landmarks[biggestFace][30].x * 2 / (float)frame.cols - 1,
landmarks[biggestFace][30].y * 2 / (float)frame.rows - 1
),
//rotation
atanf((float)(landmarks[biggestFace][14].y - landmarks[biggestFace][2].y) /
(float)(landmarks[biggestFace][2].x - landmarks[biggestFace][14].x)),
//scale
faceSize * 6 / (float)frame.cols,
//mouth open/closed state
(landmarks[biggestFace][66].y - landmarks[biggestFace][62].y) / faceSize > 0.04f);
}
}

View file

@ -175,11 +175,11 @@ void initModel () {
}
void updateModel(glm::vec2 headPos, glm::vec2 facePos, float scale, bool mouthOpen) {
void updateModel(glm::vec2 headPos, glm::vec2 facePos, float rotation, float scale, bool mouthOpen) {
//calculate transforms
parts[0].setTransform(headPos, scale);
parts[1].setTransform(facePos, scale);
parts[2].setTransform(facePos, scale);
parts[0].setTransform(headPos, rotation, scale);
parts[1].setTransform(facePos, rotation, scale);
parts[2].setTransform(facePos, rotation, scale);
//set mouth texture to open or closed
parts[2].selectTexture(mouthOpen ? 1 : 0);

View file

@ -16,6 +16,6 @@ void initShader();
void printShaderCompileLog(GLuint shader);
void updateModel(glm::vec2 headPos, glm::vec2 facePos, float scale, bool mouthOpen);
void updateModel(glm::vec2 headPos, glm::vec2 facePos, float rotation, float scale, bool mouthOpen);
#endif

View file

@ -29,8 +29,9 @@ void ModelPart::bindAndDraw() {
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}
void ModelPart::setTransform(glm::vec2 position, float scale) {
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));
transMatrix = glm::rotate(transMatrix, rotation, glm::vec3(0.0f, 0.0f, 1.0f));
transMatrix = glm::scale(transMatrix, glm::vec3(scale, scale, scale));
}

View file

@ -20,7 +20,7 @@ class ModelPart {
void bindAndDraw();
void setTransform(glm::vec2 position, float scale);
void setTransform(glm::vec2 position, float rotation, float scale);
void addTexture(const char* texPath, size_t slot);