SWBF2-Classic-Msh-Viewer/MshViewer/Source/Object.cpp
2016-09-06 15:15:29 +02:00

146 lines
3.0 KiB
C++

#include <gl\glew.h>
#include <gl\glfw3.h>
#include <glm\gtc\matrix_transform.hpp>
#include "Object.h"
#include "shader.hpp"
#include "import.h"
#include "Texture.h"
#define VERTEX_SHADER "Shader/VertexTextureShader.mv2shdr"
#define FRAGMENT_SHADER "Shader/FragmentTextureShader.mv2shdr"
#define TEXTURE_NAME "Textures/dice.tga"
/////////////////////////////////////////////////////////////////////////
// public constructor/destructor
Object::Object(const char* path) :
fRotationX(0),
fRotationY(0),
fRotationZ(0)
{
m4x4Model = glm::mat4(1.0f);
glGenVertexArrays(1, &gluiVertexArrayID);
glGenBuffers(1, &gluiVertexBufferID);
glGenBuffers(1, &gluiUVBufferID);
gluiShaderPrgmID = LoadShaders(VERTEX_SHADER, FRAGMENT_SHADER);
vfVertices = loadData();
vfUV = loadUV();
processTexture();
loadMesh2OGL();
}
Object::~Object()
{
glDeleteBuffers(1, &gluiUVBufferID);
glDeleteBuffers(1, &gluiVertexBufferID);
glDeleteVertexArrays(1, &gluiVertexArrayID);
glDeleteProgram(gluiShaderPrgmID);
}
/////////////////////////////////////////////////////////////////////////
// private functions
void Object::processTexture()
{
glGenTextures(1, &gluiTextureID);
glBindTexture(GL_TEXTURE_2D, gluiTextureID);
TextureTGA tempTex(TEXTURE_NAME);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tempTex.getWidth(), tempTex.getHeight(), 0, GL_BGR, GL_UNSIGNED_BYTE, tempTex.getData().data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
}
void Object::calcMatrix()
{
m4x4Model = glm::rotate(m4x4Model, fRotationX, glm::vec3(1, 0, 0));
m4x4Model = glm::rotate(m4x4Model, fRotationY, glm::vec3(0, 1, 0));
m4x4Model = glm::rotate(m4x4Model, fRotationZ, glm::vec3(0, 0, 1));
}
void Object::loadMesh2OGL()
{
// load object to OGL
glBindBuffer(GL_ARRAY_BUFFER, gluiVertexBufferID);
glBufferData(
GL_ARRAY_BUFFER,
sizeof(vfVertices) * vfVertices.size(),
vfVertices.data(),
GL_STATIC_DRAW
);
// load UV to OGL
glBindBuffer(GL_ARRAY_BUFFER, gluiUVBufferID);
glBufferData(
GL_ARRAY_BUFFER,
sizeof(vfUV) * vfUV.size(),
vfUV.data(),
GL_STATIC_DRAW
);
}
/////////////////////////////////////////////////////////////////////////
// public getter
glm::mat4 Object::getMatrix()
{
calcMatrix();
return m4x4Model;
}
GLuint Object::getShader() const
{
return gluiShaderPrgmID;
}
GLuint Object::getTextureID() const
{
return gluiTextureID;
}
GLuint Object::getVertexBufferID() const
{
return gluiVertexBufferID;
}
GLuint Object::getUVBufferID() const
{
return gluiUVBufferID;
}
int Object::getVertexNumber() const
{
return 12*3;
}
/////////////////////////////////////////////////////////////////////////
// public functions
void Object::add2x(float value)
{
fRotationX += value;
}
void Object::add2y(float value)
{
fRotationY += value;
}
void Object::add2z(float value)
{
fRotationZ += value;
}