forkkmuopengl/BaseGLProject/MyGLWindow.cpp

114 lines
3.4 KiB
C++

#include <algorithm>
#include "MyGlWindow.h"
//Getting the projection matrix
glm::mat4x4 perspective(float fovy, float aspect, float near, float far)
{
float fovy2 = glm::tan(fovy / 2);
glm::mat4x4 pmat{ { 1 / (aspect * fovy2), 0, 0, 0},
{ 0, 1 / fovy2, 0, 0},
{ 0, 0, -((far + near) / (far - near)), -1},
{ 0, 0, -((2 * far * near) / (far - near)), 0} };
return pmat;
}
// Getting the view matrix
glm::mat4x4 lookAt(glm::vec3 campos, glm::vec3 look, glm::vec3 up)
{
glm::vec3 ZCam(glm::normalize(campos - look));
glm::vec3 XCam(glm::normalize(glm::cross(up, ZCam)));
glm::vec3 YCam(glm::normalize(glm::cross(ZCam, XCam)));
glm::mat4x4 cam_mat{ {XCam.x, YCam.x, ZCam.x, 0},
{XCam.y, YCam.y, ZCam.y, 0},
{XCam.z, YCam.z, ZCam.z, 0},
{0, 0, 0 ,1} };
glm::mat4x4 norm_mat{ {1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{-campos.x, -campos.y, -campos.z, 1} };
return cam_mat * norm_mat;
}
MyGlWindow::MyGlWindow(int w, int h) :
viewer(glm::vec3(5, 5, 5), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0), 45.0f, (w / (float)h))
{
m_width = w;
m_height = h;
setup();
}
MyGlWindow::~MyGlWindow()
{
}
void MyGlWindow::setBgColor(float bgColor[3])
{
_bgColor[0] = bgColor[0];
_bgColor[1] = bgColor[1];
_bgColor[2] = bgColor[2];
}
void MyGlWindow::setup()
{
_scnctx.lights.emplace("Light1", Light(glm::vec3(0.0f, 0.5f, 0.5f), glm::vec4(10, 10, 0, 1)));
_scnctx.lights.emplace("Light2", Light(glm::vec3(0.0f, 0.0f, 0.5f), glm::vec4(3.09, 10, 9.51, 1)));
_scnctx.lights.emplace("Light3", Light(glm::vec3(0.5f, 0.0f, 0.0f), glm::vec4(-8.09, 10, 5.87, 1)));
_scnctx.lights.emplace("Light4", Light(glm::vec3(0.0f, 0.5f, 0.0f), glm::vec4(-8.09, 10, -5.87, 1)));
_scnctx.lights.emplace("Light5", Light(glm::vec3(0.5f, 0.5f, 0.5f), glm::vec4(3.09, 10, -9.51, 1)));
//shaders["ToonShader"] = Shader("light.vert", "toon.frag");
//// Removing useless specular component
//shaders["ToonShader"].uniformFlags &= ~ShaderFlags::KS_FLAG;
//shaders["ToonShader"].uniformFlags &= ~ShaderFlags::SHINE_FLAG;
//meshes.emplace_back("teapot.obj", &shaders["ToonShader"]);
ToonShader = new Shader("light.vert", "toon.frag");
ToonShader->uniformFlags &= ~ShaderFlags::KS_FLAG;
ToonShader->uniformFlags &= ~ShaderFlags::SHINE_FLAG;
meshes.emplace_back("teapot.obj", ToonShader);
}
void MyGlWindow::draw()
{
glClearColor(_bgColor[0], _bgColor[1], _bgColor[2], 1);
glViewport(0, 0, m_width, m_height);
glEnable(GL_DEPTH_TEST);
glEnable(GL_DEPTH_BUFFER);
glEnable(GL_BLEND);
//glEnable(GL_CULL_FACE);
//glCullFace(GL_BACK);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//setting the view data in the scene context
glm::vec3 eye(viewer.getViewPoint().x, viewer.getViewPoint().y, viewer.getViewPoint().z);
glm::vec3 look(viewer.getViewCenter().x, viewer.getViewCenter().y, viewer.getViewCenter().z);
glm::vec3 up(viewer.getUpVector().x, viewer.getUpVector().y, viewer.getUpVector().z);
glm::mat4 view = lookAt(eye, look, up); //Calculate view matrix from parameters of m_viewer
glm::mat4 projection = perspective(45.0f, (float)m_width / (float)m_height, 0.1f, 500.0f);
_scnctx.viewMatrix = view;
_scnctx.projectionMatrix = projection;
static int i = 0;
for (auto it = meshes.begin(); it != meshes.end(); ++it)
it->draw(_scnctx);
}
void MyGlWindow::resize(int w, int h)
{
m_width = w;
m_height = h;
viewer.setAspectRatio(w / float(h));
}