add class for model parts
This commit is contained in:
parent
62779b6f78
commit
f1b6f5b80a
|
@ -9,6 +9,6 @@ find_package( glm REQUIRED )
|
||||||
find_package( FreeGLUT REQUIRED )
|
find_package( FreeGLUT REQUIRED )
|
||||||
include_directories( ${OpenCV_INCLUDE_DIRS} )
|
include_directories( ${OpenCV_INCLUDE_DIRS} )
|
||||||
include_directories( ${PROJECT_SOURCE_DIR}/src )
|
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 )
|
target_link_libraries( fc2d ${OpenCV_LIBS} -lOpenGL -lglut -lGLEW )
|
||||||
set( CMAKE_BUILD_TYPE Debug )
|
set( CMAKE_BUILD_TYPE Debug )
|
||||||
|
|
|
@ -10,33 +10,20 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include <graphics.hpp>
|
#include <graphics.hpp>
|
||||||
|
#include <modelpart.hpp>
|
||||||
//VAOs for different parts
|
|
||||||
//TODO: make this more modular
|
|
||||||
GLuint head; //head object
|
|
||||||
GLuint face; //face object
|
|
||||||
|
|
||||||
GLuint shader; //standard shader program used for all elements
|
GLuint shader; //standard shader program used for all elements
|
||||||
GLuint transUniform; //location of the "transMatrix" transformation matrix uniform in the shader
|
GLuint transUniform; //location of the "transMatrix" transformation matrix uniform in the shader
|
||||||
|
|
||||||
GLuint texHead; //head texture
|
//parts of the model (see modelpart.hpp)
|
||||||
GLuint texFace; //face texture
|
ModelPart parts[2];
|
||||||
|
|
||||||
glm::mat4 matHead(1.0f); //transformation matrix for head object
|
|
||||||
glm::mat4 matFace(1.0f); //transformation matrix for face object
|
|
||||||
|
|
||||||
void display () {
|
void display () {
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
glBindVertexArray(head);
|
for (int i = 0; i < sizeof(parts)/sizeof(ModelPart); i++) {
|
||||||
glBindTexture(GL_TEXTURE_2D, texHead);
|
parts[i].bindAndDraw();
|
||||||
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);
|
|
||||||
|
|
||||||
glutSwapBuffers();
|
glutSwapBuffers();
|
||||||
}
|
}
|
||||||
|
@ -77,8 +64,6 @@ void initBuffers (GLuint* vaoNum) {
|
||||||
GLuint uvAttr = glGetAttribLocation(shader, "texcoord");
|
GLuint uvAttr = glGetAttribLocation(shader, "texcoord");
|
||||||
glVertexAttribPointer(uvAttr, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void*)(2*sizeof(GLfloat)));
|
glVertexAttribPointer(uvAttr, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void*)(2*sizeof(GLfloat)));
|
||||||
glEnableVertexAttribArray(uvAttr);
|
glEnableVertexAttribArray(uvAttr);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void initGraphics () {
|
void initGraphics () {
|
||||||
|
@ -88,24 +73,24 @@ void initGraphics () {
|
||||||
glutInit(&argc, argv);
|
glutInit(&argc, argv);
|
||||||
glutCreateWindow("FaceCam2D");
|
glutCreateWindow("FaceCam2D");
|
||||||
glutInitWindowSize(512, 512);
|
glutInitWindowSize(512, 512);
|
||||||
glutDisplayFunc(display);
|
|
||||||
|
|
||||||
glewInit();
|
glewInit();
|
||||||
|
|
||||||
initShader();
|
initShader();
|
||||||
|
|
||||||
initBuffers(&head);
|
parts[0] = ModelPart("head-base.png", transUniform);
|
||||||
initBuffers(&face);
|
parts[1] = ModelPart("head-face.png", transUniform);
|
||||||
|
|
||||||
initTexture(&texHead, "head-base.png");
|
|
||||||
initTexture(&texFace, "head-face.png");
|
|
||||||
|
|
||||||
//enable blending for alpha textures
|
//enable blending for alpha textures
|
||||||
glEnable(GL_BLEND);
|
glEnable(GL_BLEND);
|
||||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
|
||||||
//set background color
|
//set background color
|
||||||
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
|
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) {
|
void initTexture (GLuint* texNum, const char* path) {
|
||||||
|
@ -190,8 +175,8 @@ void initModel () {
|
||||||
|
|
||||||
void updateModel(glm::vec2 headPos, glm::vec2 facePos) {
|
void updateModel(glm::vec2 headPos, glm::vec2 facePos) {
|
||||||
//calculate transforms
|
//calculate transforms
|
||||||
matHead = glm::translate(glm::mat4(1.0f), glm::vec3(headPos.x, -headPos.y, 0.0f));
|
parts[0].setPosition(headPos);
|
||||||
matFace = glm::translate(glm::mat4(1.0f), glm::vec3(facePos.x, -facePos.y, 0.0f));
|
parts[1].setPosition(facePos);
|
||||||
|
|
||||||
//tell FreeGLUT to schedule a screen update
|
//tell FreeGLUT to schedule a screen update
|
||||||
glutPostRedisplay();
|
glutPostRedisplay();
|
||||||
|
|
|
@ -8,6 +8,8 @@ void initGraphics ();
|
||||||
|
|
||||||
void graphicsFrame ();
|
void graphicsFrame ();
|
||||||
|
|
||||||
|
void initBuffers (GLuint* vaoNum);
|
||||||
|
|
||||||
void initTexture (GLuint* texNum, const char* path);
|
void initTexture (GLuint* texNum, const char* path);
|
||||||
|
|
||||||
void initShader();
|
void initShader();
|
||||||
|
|
31
src/modelpart.cpp
Normal file
31
src/modelpart.cpp
Normal 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
20
src/modelpart.hpp
Normal 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
|
Loading…
Reference in a new issue