add class for model parts

This commit is contained in:
Epicalert 2021-01-02 00:20:09 +08:00
parent 62779b6f78
commit f1b6f5b80a
No known key found for this signature in database
GPG key ID: CAA46F858D0979BD
5 changed files with 69 additions and 31 deletions

View file

@ -9,6 +9,6 @@ find_package( glm REQUIRED )
find_package( FreeGLUT REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
include_directories( ${PROJECT_SOURCE_DIR}/src )
add_executable( fc2d src/main.cpp src/graphics.cpp )
add_executable( fc2d src/main.cpp src/graphics.cpp src/modelpart.cpp )
target_link_libraries( fc2d ${OpenCV_LIBS} -lOpenGL -lglut -lGLEW )
set( CMAKE_BUILD_TYPE Debug )

View file

@ -10,33 +10,20 @@
#include <iostream>
#include <graphics.hpp>
//VAOs for different parts
//TODO: make this more modular
GLuint head; //head object
GLuint face; //face object
#include <modelpart.hpp>
GLuint shader; //standard shader program used for all elements
GLuint transUniform; //location of the "transMatrix" transformation matrix uniform in the shader
GLuint texHead; //head texture
GLuint texFace; //face texture
glm::mat4 matHead(1.0f); //transformation matrix for head object
glm::mat4 matFace(1.0f); //transformation matrix for face object
//parts of the model (see modelpart.hpp)
ModelPart parts[2];
void display () {
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(head);
glBindTexture(GL_TEXTURE_2D, texHead);
glUniformMatrix4fv(transUniform, 1, GL_FALSE, glm::value_ptr(matHead));
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(face);
glBindTexture(GL_TEXTURE_2D, texFace);
glUniformMatrix4fv(transUniform, 1, GL_FALSE, glm::value_ptr(matFace));
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
for (int i = 0; i < sizeof(parts)/sizeof(ModelPart); i++) {
parts[i].bindAndDraw();
}
glutSwapBuffers();
}
@ -77,8 +64,6 @@ void initBuffers (GLuint* vaoNum) {
GLuint uvAttr = glGetAttribLocation(shader, "texcoord");
glVertexAttribPointer(uvAttr, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void*)(2*sizeof(GLfloat)));
glEnableVertexAttribArray(uvAttr);
}
void initGraphics () {
@ -88,24 +73,24 @@ void initGraphics () {
glutInit(&argc, argv);
glutCreateWindow("FaceCam2D");
glutInitWindowSize(512, 512);
glutDisplayFunc(display);
glewInit();
initShader();
initBuffers(&head);
initBuffers(&face);
parts[0] = ModelPart("head-base.png", transUniform);
parts[1] = ModelPart("head-face.png", transUniform);
initTexture(&texHead, "head-base.png");
initTexture(&texFace, "head-face.png");
//enable blending for alpha textures
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//set background color
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glutDisplayFunc(display);
std::cout << "graphics init complete" << std::endl;
}
void initTexture (GLuint* texNum, const char* path) {
@ -190,8 +175,8 @@ void initModel () {
void updateModel(glm::vec2 headPos, glm::vec2 facePos) {
//calculate transforms
matHead = glm::translate(glm::mat4(1.0f), glm::vec3(headPos.x, -headPos.y, 0.0f));
matFace = glm::translate(glm::mat4(1.0f), glm::vec3(facePos.x, -facePos.y, 0.0f));
parts[0].setPosition(headPos);
parts[1].setPosition(facePos);
//tell FreeGLUT to schedule a screen update
glutPostRedisplay();

View file

@ -8,6 +8,8 @@ void initGraphics ();
void graphicsFrame ();
void initBuffers (GLuint* vaoNum);
void initTexture (GLuint* texNum, const char* path);
void initShader();

31
src/modelpart.cpp Normal file
View file

@ -0,0 +1,31 @@
#include <GL/glew.h>
#include <glm/mat4x4.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <graphics.hpp>
#include <modelpart.hpp>
#include <iostream>
ModelPart::ModelPart() {
}
ModelPart::ModelPart(const char* texPath, GLuint transUniformNum) {
//create vbo, ebo, vao
initBuffers(&vao);
//create texture
initTexture(&tex, texPath);
transUniform = transUniformNum;
}
void ModelPart::bindAndDraw() {
glBindVertexArray(vao);
glBindTexture(GL_TEXTURE_2D, tex);
glUniformMatrix4fv(transUniform, 1, GL_FALSE, glm::value_ptr(transMatrix));
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}
void ModelPart::setPosition(glm::vec2 position) {
transMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(position.x, position.y, 0.0f));
}

20
src/modelpart.hpp Normal file
View file

@ -0,0 +1,20 @@
#ifndef MODELPART_HPP
#define MODELPART_HPP
#include <glm/ext/matrix_transform.hpp>
class ModelPart {
GLuint vao, tex, transUniform;
glm::mat4 transMatrix = glm::mat4(1.0f);
public:
ModelPart();
ModelPart(const char* texPath, GLuint transUniformNum);
void bindAndDraw();
void setPosition(glm::vec2 position);
};
#endif