First step of the rework is done : now for manual MeshCreation from code

This commit is contained in:
Hurlu 2019-03-08 20:00:13 +09:00
parent 1bf24da18d
commit 8f595cab38
34 changed files with 763 additions and 1936 deletions

View File

@ -1,90 +0,0 @@
#pragma once
#include <queue>
#include "IDrawable.h"
enum Transformation
{
Rotation,
Translation,
Scaling
};
class ADrawable : IDrawable {
protected:
std::vector<std::pair<glm::vec4, Transformation>> _transformations;
Model _model;
void effectTransformations()
{
for (auto pair : _transformations)
{
switch (pair.second)
{
case(Rotation):
_model.glRotate(pair.first.w, pair.first.x, pair.first.y, pair.first.z);
break;
case(Translation):
_model.glTranslate(pair.first.x, pair.first.y, pair.first.z);
break;
case(Scaling):
_model.glScale(pair.first.x, pair.first.y, pair.first.z);
break;
}
}
}
public:
virtual void draw(ShaderProgram *shader, glm::mat4x4 proj_matrix, glm::mat4x4 view_matrix) = 0;
virtual DrawableType getType() = 0;
void addRotation(glm::vec4 vec) override
{
_transformations.emplace_back(vec, Rotation);
}
void addTranslation(glm::vec4 vec) override
{
_transformations.emplace_back(vec, Translation);
}
void addScaling(glm::vec4 vec) override
{
_transformations.emplace_back(vec, Scaling);
}
glm::vec4 translateToPivot(glm::vec3 pivot) override
{
glm::vec4 curr_pos = { 0, 0, 0, 0 };
glm::vec4 h_pivot = { pivot.x, pivot.y, pivot.z, 0 };
for (auto pair : _transformations)
{
if (pair.second == Translation)
curr_pos += pair.first;
}
addTranslation(h_pivot - curr_pos);
return curr_pos - h_pivot;
}
void removeLastTransformations(int n = 1) override
{
for (int i = 0; i < n; i++)
_transformations.pop_back();
}
glm::vec3 getPosition() override
{
glm::vec4 curr_pos = { 0, 0, 0, 0 };
for (auto pair : _transformations)
{
if (pair.second == Translation)
curr_pos += pair.first;
}
return curr_pos;
}
};

View File

@ -154,22 +154,17 @@
<ClCompile Include="imgui\imgui_impl_glfw.cpp" />
<ClCompile Include="imgui\imgui_impl_opengl3.cpp" />
<ClCompile Include="imgui\imgui_widgets.cpp" />
<ClCompile Include="LineSegment.cpp" />
<ClCompile Include="Light.cpp" />
<ClCompile Include="Models\Mesh.cpp" />
<ClCompile Include="MyGLWindow.cpp" />
<ClCompile Include="Plane.cpp" />
<ClCompile Include="Shader.cpp" />
<ClCompile Include="Source.cpp" />
<ClCompile Include="Sphere.cpp" />
<ClCompile Include="vboteapot.cpp" />
<ClCompile Include="Viewer.cpp" />
<ClCompile Include="WireCube.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="ADrawable.h" />
<ClInclude Include="CheckeredFloor.h" />
<ClInclude Include="ColorCube.h" />
<ClInclude Include="Global.h" />
<ClInclude Include="IDrawable.h" />
<ClInclude Include="Dataset.h" />
<ClInclude Include="imgui\imconfig.h" />
<ClInclude Include="imgui\imgui.h" />
<ClInclude Include="imgui\imgui_impl_glfw.h" />
@ -178,17 +173,15 @@
<ClInclude Include="imgui\imstb_rectpack.h" />
<ClInclude Include="imgui\imstb_textedit.h" />
<ClInclude Include="imgui\imstb_truetype.h" />
<ClInclude Include="LineSegment.h" />
<ClInclude Include="Light.h" />
<ClInclude Include="Loader.h" />
<ClInclude Include="Material.h" />
<ClInclude Include="Models\Mesh.h" />
<ClInclude Include="ModelView.h" />
<ClInclude Include="MyGLWindow.h" />
<ClInclude Include="Plane.h" />
<ClInclude Include="Sphere.h" />
<ClInclude Include="teapotdata.h" />
<ClInclude Include="vboteapot.h" />
<ClInclude Include="SceneContext.h" />
<ClInclude Include="Shader.h" />
<ClInclude Include="Viewer.h" />
<ClInclude Include="WireCube.h" />
</ItemGroup>
<ItemGroup>
<None Include="light.frag" />

View File

@ -57,35 +57,20 @@
<ClCompile Include="imgui\imgui_widgets.cpp">
<Filter>imgui</Filter>
</ClCompile>
<ClCompile Include="WireCube.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="LineSegment.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Sphere.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="vboteapot.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Models\Mesh.cpp">
<Filter>Models</Filter>
</ClCompile>
<ClCompile Include="Plane.cpp">
<ClCompile Include="Light.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Shader.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="ColorCube.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="ModelView.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="IDrawable.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Loader.h">
<Filter>Header Files</Filter>
</ClInclude>
@ -95,9 +80,6 @@
<ClInclude Include="Viewer.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="CheckeredFloor.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="imgui\imconfig.h">
<Filter>imgui</Filter>
</ClInclude>
@ -122,31 +104,28 @@
<ClInclude Include="imgui\imstb_truetype.h">
<Filter>imgui</Filter>
</ClInclude>
<ClInclude Include="WireCube.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="ADrawable.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="LineSegment.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Sphere.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="teapotdata.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="vboteapot.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Models\Mesh.h">
<Filter>Models</Filter>
</ClInclude>
<ClInclude Include="Global.h">
<ClInclude Include="ColorCube.h">
<Filter>Models</Filter>
</ClInclude>
<ClInclude Include="CheckeredFloor.h">
<Filter>Models</Filter>
</ClInclude>
<ClInclude Include="Light.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Plane.h">
<ClInclude Include="Dataset.h">
<Filter>Models</Filter>
</ClInclude>
<ClInclude Include="Shader.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="SceneContext.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Material.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>

View File

@ -1,99 +1,99 @@
#include "CheckeredFloor.h"
void CheckeredFloor::genVertices(std::vector<glm::vec4> &vertices,
std::vector<glm::vec3> &colors)
{
float maxX = _size / 2.f, maxY = _size / 2.f;
float minX = -_size / 2.f, minY = -_size / 2.f;
float side_size = _size / (float)_squares;
int color = 1;
for (float x = minX; x < maxX; x += side_size)
{
for (float y = minY; y < maxY; y += side_size)
{
color *= -1;
glm::vec3 tile_color = (color > 0) ? _light_color : _dark_color;
//gen 1 carré
vertices.emplace_back(x, 0, y, 1); //upleft
vertices.emplace_back(x, 0, y + side_size, 1); //downleft
vertices.emplace_back(x + side_size, 0, y + side_size, 1); //downright
vertices.emplace_back(x + side_size, 0, y + side_size, 1); //downright
vertices.emplace_back(x + side_size, 0, y, 1); //upright
vertices.emplace_back(x, 0, y, 1); //upleft
for (int z = 0; z < 6; z++)
colors.push_back(tile_color);
}
color *= -1;
}
}
void CheckeredFloor::setup()
{
std::vector<glm::vec4> floor_vertices;
std::vector<glm::vec3> floor_colors;
glGenVertexArrays(1, &_vaoHandle);
glBindVertexArray(_vaoHandle);
genVertices(floor_vertices, floor_colors);
glGenBuffers(1, &_vbo_vertices);
glBindBuffer(GL_ARRAY_BUFFER, _vbo_vertices);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * floor_vertices.size() * 4, floor_vertices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(
0, //attr number = 0
4,
GL_FLOAT,
GL_FALSE,
0,
(void*)0);
glEnableVertexAttribArray(0); //attr number = 0
glGenBuffers(1, &_vbo_colors);
glBindBuffer(GL_ARRAY_BUFFER, _vbo_colors);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * floor_colors.size() * 3, floor_colors.data(), GL_STATIC_DRAW);
glVertexAttribPointer(
1,
3,
GL_FLOAT,
GL_FALSE,
0,
(void *)0);
glEnableVertexAttribArray(1);
glBindVertexArray(0);
}
CheckeredFloor::CheckeredFloor(int size, int squares) :
_size(size), _squares(squares)
{
setup();
}
CheckeredFloor::~CheckeredFloor()
{
}
void CheckeredFloor::draw(ShaderProgram * shader, glm::mat4x4 proj_matrix, glm::mat4x4 view_matrix)
{
_model.glPushMatrix();
effectTransformations();
glBindVertexArray(_vaoHandle);
glm::mat4 mvpMatrix = proj_matrix * view_matrix * _model.getMatrix();
glUniformMatrix4fv(shader->uniform("mvp"), 1, GL_FALSE, glm::value_ptr(mvpMatrix));
glDrawArrays(GL_TRIANGLES, 0, _squares * _squares * 6);
_model.glPopMatrix();
}
DrawableType CheckeredFloor::getType()
{
return DrawableType::CHECKERED_FLOOR;
}
//#include "CheckeredFloor.h"
//
//void CheckeredFloor::genVertices(std::vector<glm::vec4> &vertices,
// std::vector<glm::vec3> &colors)
//{
// float maxX = _size / 2.f, maxY = _size / 2.f;
// float minX = -_size / 2.f, minY = -_size / 2.f;
// float side_size = _size / (float)_squares;
// int color = 1;
//
// for (float x = minX; x < maxX; x += side_size)
// {
// for (float y = minY; y < maxY; y += side_size)
// {
// color *= -1;
// glm::vec3 tile_color = (color > 0) ? _light_color : _dark_color;
// //gen 1 carré
// vertices.emplace_back(x, 0, y, 1); //upleft
// vertices.emplace_back(x, 0, y + side_size, 1); //downleft
// vertices.emplace_back(x + side_size, 0, y + side_size, 1); //downright
//
// vertices.emplace_back(x + side_size, 0, y + side_size, 1); //downright
// vertices.emplace_back(x + side_size, 0, y, 1); //upright
// vertices.emplace_back(x, 0, y, 1); //upleft
//
//
//
// for (int z = 0; z < 6; z++)
// colors.push_back(tile_color);
// }
// color *= -1;
// }
//}
//
//void CheckeredFloor::setup()
//{
// std::vector<glm::vec4> floor_vertices;
// std::vector<glm::vec3> floor_colors;
//
// glGenVertexArrays(1, &_vaoHandle);
// glBindVertexArray(_vaoHandle);
//
// genVertices(floor_vertices, floor_colors);
//
// glGenBuffers(1, &_vbo_vertices);
// glBindBuffer(GL_ARRAY_BUFFER, _vbo_vertices);
// glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * floor_vertices.size() * 4, floor_vertices.data(), GL_STATIC_DRAW);
//
// glVertexAttribPointer(
// 0, //attr number = 0
// 4,
// GL_FLOAT,
// GL_FALSE,
// 0,
// (void*)0);
// glEnableVertexAttribArray(0); //attr number = 0
//
//
// glGenBuffers(1, &_vbo_colors);
// glBindBuffer(GL_ARRAY_BUFFER, _vbo_colors);
// glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * floor_colors.size() * 3, floor_colors.data(), GL_STATIC_DRAW);
//
// glVertexAttribPointer(
// 1,
// 3,
// GL_FLOAT,
// GL_FALSE,
// 0,
// (void *)0);
// glEnableVertexAttribArray(1);
//
// glBindVertexArray(0);
//}
//
//CheckeredFloor::CheckeredFloor(int size, int squares) :
// _size(size), _squares(squares)
//{
// setup();
//}
//
//CheckeredFloor::~CheckeredFloor()
//{
//}
//
//void CheckeredFloor::draw(ShaderProgram * shader, glm::mat4x4 proj_matrix, glm::mat4x4 view_matrix)
//{
// _model.glPushMatrix();
// effectTransformations();
// glBindVertexArray(_vaoHandle);
// glm::mat4 mvpMatrix = proj_matrix * view_matrix * _model.getMatrix();
// glUniformMatrix4fv(shader->uniform("mvp"), 1, GL_FALSE, glm::value_ptr(mvpMatrix));
// glDrawArrays(GL_TRIANGLES, 0, _squares * _squares * 6);
// _model.glPopMatrix();
//}
//
//DrawableType CheckeredFloor::getType()
//{
// return DrawableType::CHECKERED_FLOOR;
//}

View File

@ -1,31 +1,31 @@
#pragma once
#define GLM_SWIZZLE
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>
#include <glm/gtx/string_cast.hpp>
#include <vector>
#include "ADrawable.h"
class CheckeredFloor : public ADrawable
{
private :
GLuint _vaoHandle;
GLuint _vbo_vertices, _vbo_colors;
void genVertices(std::vector<glm::vec4> &vertices,
std::vector<glm::vec3> &colors);
void setup();
glm::vec3 _light_color = {.7f, .7f, .7f};
glm::vec3 _dark_color = {.3f, .3f, .3f};
int _size;
int _squares;
public :
CheckeredFloor(int size, int squares);
~CheckeredFloor();
void draw(ShaderProgram *shader, glm::mat4x4 proj_matrix, glm::mat4x4 view_matrix) override;
DrawableType getType() override;
};
//#pragma once
//#define GLM_SWIZZLE
//#define GLM_ENABLE_EXPERIMENTAL
//#include <glm/glm.hpp>
//#include <glm/gtx/string_cast.hpp>
//#include <vector>
//#include "ADrawable.h"
//
//class CheckeredFloor : public ADrawable
//{
//private :
// GLuint _vaoHandle;
// GLuint _vbo_vertices, _vbo_colors;
//
// void genVertices(std::vector<glm::vec4> &vertices,
// std::vector<glm::vec3> &colors);
// void setup();
//
// glm::vec3 _light_color = {.7f, .7f, .7f};
// glm::vec3 _dark_color = {.3f, .3f, .3f};
//
// int _size;
// int _squares;
//
//public :
// CheckeredFloor(int size, int squares);
// ~CheckeredFloor();
// void draw(ShaderProgram *shader, glm::mat4x4 proj_matrix, glm::mat4x4 view_matrix) override;
// DrawableType getType() override;
//
//};

View File

@ -1,211 +1,211 @@
#include "ColorCube.h"
glm::vec3 computeNormal(glm::vec3 p1, glm::vec3 p2, glm::vec3 p3)
{
glm::vec3 v1 = p2 - p1;
glm::vec3 v2 = p3 - p1;
return glm::normalize(glm::cross(v1, v2));
}
void ColorCube::iboSetup()
{
std::vector<glm::vec3> cube_vertices =
{ { -1.0, -1.0, 1.0},
{ 1.0, -1.0, 1.0},
{ 1.0, 1.0, 1.0},
{ -1.0, 1.0, 1.0},
{ -1.0, -1.0, -1.0},
{ 1.0, -1.0, -1.0},
{ 1.0, 1.0, -1.0},
{ -1.0, 1.0, -1.0} };
GLfloat cube_colors[] = {
// front colors
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
1.0, 1.0, 1.0,
// back colors
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
1.0, 1.0, 1.0,
};
GLushort cube_elements[] = {
0, 1, 2, 2, 3, 0, 1, 5, 6,
6, 2, 1, 7, 6, 5, 5, 4, 7,
4, 0, 3, 3, 7, 4, 4, 5, 1,
1, 0, 4, 3, 2, 6, 6, 7, 3,
};
glGenVertexArrays(1, &_vaoHandle);
glBindVertexArray(_vaoHandle);
glGenBuffers(1, &_vbo_vertices);
glBindBuffer(GL_ARRAY_BUFFER, _vbo_vertices);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * cube_vertices.size() * 3, cube_vertices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(
0, //attr number = 0
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0);
glEnableVertexAttribArray(0); //attr number = 0
glGenBuffers(1, &_vbo_colors);
glBindBuffer(GL_ARRAY_BUFFER, _vbo_colors);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 24, cube_colors, GL_STATIC_DRAW);
glVertexAttribPointer(
1,
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0);
glEnableVertexAttribArray(1);
glGenBuffers(1, &_iboHandle);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _iboHandle);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cube_elements), cube_elements, GL_STATIC_DRAW);
glBindVertexArray(0);
}
void ColorCube::vaoSetup()
{
std::vector<glm::vec3> cube_vertices =
{ { -1.0, -1.0, 1.0}, { 1.0, -1.0, 1.0}, { 1.0, 1.0, 1.0},
{ 1.0, 1.0, 1.0}, { -1.0, 1.0, 1.0}, { -1.0, -1.0, 1.0},
{ 1.0, -1.0, 1.0}, { 1.0, -1.0, -1.0}, { 1.0, 1.0, -1.0},
{ 1.0, 1.0, -1.0}, { 1.0, 1.0, 1.0}, { 1.0, -1.0, 1.0},
{ -1.0, 1.0, -1.0}, { 1.0, 1.0, -1.0}, { 1.0, -1.0, -1.0},
{ 1.0, -1.0, -1.0}, { -1.0, -1.0, -1.0}, { -1.0, 1.0, -1.0},
{ -1.0, -1.0, -1.0}, { -1.0, -1.0, 1.0}, { -1.0, 1.0, 1.0},
{ -1.0, 1.0, 1.0}, { -1.0, 1.0, -1.0}, { -1.0, -1.0, -1.0},
{ -1.0, -1.0, -1.0}, { 1.0, -1.0, -1.0}, { 1.0, -1.0, 1.0},
{ 1.0, -1.0, 1.0}, { -1.0, -1.0, 1.0}, { -1.0, -1.0, -1.0},
{ -1.0, 1.0, 1.0}, { 1.0, 1.0, 1.0}, { 1.0, 1.0, -1.0},
{ 1.0, 1.0, -1.0}, { -1.0, 1.0, -1.0}, { -1.0, 1.0, 1.0} };
std::vector<GLfloat> cube_colors = {
1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0,
0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0,
0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0,
1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0,
0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0,
1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0,
0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0
};
std::vector<glm::vec3> cube_normals;
for (int i = 0; i < cube_vertices.size() - 2; i += 3)
{
glm::vec3 norm = computeNormal(
cube_vertices[i], cube_vertices[i + 1], cube_vertices[i + 2]);
cube_normals.push_back(norm); cube_normals.push_back(norm); cube_normals.push_back(norm);
}
glGenVertexArrays(1, &_vaoHandle);
glBindVertexArray(_vaoHandle);
glGenBuffers(1, &_vbo_vertices);
glBindBuffer(GL_ARRAY_BUFFER, _vbo_vertices);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * cube_vertices.size() * 3, cube_vertices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(
0, //attr number = 0
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0);
glEnableVertexAttribArray(0); //attr number = 0
glGenBuffers(1, &_vbo_normals);
glBindBuffer(GL_ARRAY_BUFFER, _vbo_normals);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * cube_normals.size() * 3, cube_normals.data(), GL_STATIC_DRAW);
glVertexAttribPointer(
1,
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0);
glEnableVertexAttribArray(1);
glGenBuffers(1, &_vbo_colors);
glBindBuffer(GL_ARRAY_BUFFER, _vbo_colors);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * cube_colors.size(), cube_colors.data(), GL_STATIC_DRAW);
glVertexAttribPointer(
2,
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0);
glEnableVertexAttribArray(2);
glBindVertexArray(0);
}
ColorCube::ColorCube(bool usingIBO)
{
ibo_used = usingIBO;
if (usingIBO)
iboSetup();
else
vaoSetup();
}
void ColorCube::draw(ShaderProgram *shader, glm::mat4x4 proj_matrix, glm::mat4x4 view_matrix)
{
_model.glPushMatrix();
effectTransformations();
glBindVertexArray(_vaoHandle);
glm::mat4 mvpMatrix = proj_matrix * view_matrix * _model.getMatrix();
glUniformMatrix4fv(shader->uniform("mvp"), 1, GL_FALSE, glm::value_ptr(mvpMatrix));
glm::mat4 modelview = view_matrix * _model.getMatrix();
_model.glPopMatrix();
glUniform4fv(shader->uniform("LightLocation"), 1, glm::value_ptr(view_matrix * glm::vec4(50, 50, 50, 1)));
glUniformMatrix4fv(shader->uniform("ModelViewMatrix"), 1, GL_FALSE, glm::value_ptr(modelview));
glm::mat4 inverseModelView = glm::inverse(modelview);
glm::mat3 normalMatrix = glm::mat3(glm::transpose(inverseModelView));
glUniformMatrix3fv(shader->uniform("NormalMatrix"), 1, GL_FALSE, glm::value_ptr(normalMatrix));
if (ibo_used)
{
int size;
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
glDrawElements(GL_TRIANGLES, size / sizeof(GLushort), GL_UNSIGNED_SHORT, 0);
}
else
glDrawArrays(GL_TRIANGLES, 0, 36);
}
DrawableType ColorCube::getType()
{
return DrawableType::COLOR_CUBE;
}
ColorCube::~ColorCube()
{
}
//#include "ColorCube.h"
//
//glm::vec3 computeNormal(glm::vec3 p1, glm::vec3 p2, glm::vec3 p3)
//{
// glm::vec3 v1 = p2 - p1;
// glm::vec3 v2 = p3 - p1;
//
// return glm::normalize(glm::cross(v1, v2));
//}
//
//void ColorCube::iboSetup()
//{
// std::vector<glm::vec3> cube_vertices =
// { { -1.0, -1.0, 1.0},
// { 1.0, -1.0, 1.0},
// { 1.0, 1.0, 1.0},
// { -1.0, 1.0, 1.0},
// { -1.0, -1.0, -1.0},
// { 1.0, -1.0, -1.0},
// { 1.0, 1.0, -1.0},
// { -1.0, 1.0, -1.0} };
//
// GLfloat cube_colors[] = {
// // front colors
// 1.0, 0.0, 0.0,
// 0.0, 1.0, 0.0,
// 0.0, 0.0, 1.0,
// 1.0, 1.0, 1.0,
// // back colors
// 1.0, 0.0, 0.0,
// 0.0, 1.0, 0.0,
// 0.0, 0.0, 1.0,
// 1.0, 1.0, 1.0,
// };
//
// GLushort cube_elements[] = {
// 0, 1, 2, 2, 3, 0, 1, 5, 6,
// 6, 2, 1, 7, 6, 5, 5, 4, 7,
// 4, 0, 3, 3, 7, 4, 4, 5, 1,
// 1, 0, 4, 3, 2, 6, 6, 7, 3,
// };
//
//
// glGenVertexArrays(1, &_vaoHandle);
// glBindVertexArray(_vaoHandle);
//
// glGenBuffers(1, &_vbo_vertices);
// glBindBuffer(GL_ARRAY_BUFFER, _vbo_vertices);
// glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * cube_vertices.size() * 3, cube_vertices.data(), GL_STATIC_DRAW);
//
// glVertexAttribPointer(
// 0, //attr number = 0
// 3,
// GL_FLOAT,
// GL_FALSE,
// 0,
// (void*)0);
// glEnableVertexAttribArray(0); //attr number = 0
//
// glGenBuffers(1, &_vbo_colors);
// glBindBuffer(GL_ARRAY_BUFFER, _vbo_colors);
// glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 24, cube_colors, GL_STATIC_DRAW);
//
// glVertexAttribPointer(
// 1,
// 3,
// GL_FLOAT,
// GL_FALSE,
// 0,
// (void*)0);
// glEnableVertexAttribArray(1);
//
// glGenBuffers(1, &_iboHandle);
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _iboHandle);
// glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cube_elements), cube_elements, GL_STATIC_DRAW);
//
// glBindVertexArray(0);
//}
//
//void ColorCube::vaoSetup()
//{
// std::vector<glm::vec3> cube_vertices =
// { { -1.0, -1.0, 1.0}, { 1.0, -1.0, 1.0}, { 1.0, 1.0, 1.0},
// { 1.0, 1.0, 1.0}, { -1.0, 1.0, 1.0}, { -1.0, -1.0, 1.0},
// { 1.0, -1.0, 1.0}, { 1.0, -1.0, -1.0}, { 1.0, 1.0, -1.0},
// { 1.0, 1.0, -1.0}, { 1.0, 1.0, 1.0}, { 1.0, -1.0, 1.0},
// { -1.0, 1.0, -1.0}, { 1.0, 1.0, -1.0}, { 1.0, -1.0, -1.0},
// { 1.0, -1.0, -1.0}, { -1.0, -1.0, -1.0}, { -1.0, 1.0, -1.0},
// { -1.0, -1.0, -1.0}, { -1.0, -1.0, 1.0}, { -1.0, 1.0, 1.0},
// { -1.0, 1.0, 1.0}, { -1.0, 1.0, -1.0}, { -1.0, -1.0, -1.0},
// { -1.0, -1.0, -1.0}, { 1.0, -1.0, -1.0}, { 1.0, -1.0, 1.0},
// { 1.0, -1.0, 1.0}, { -1.0, -1.0, 1.0}, { -1.0, -1.0, -1.0},
// { -1.0, 1.0, 1.0}, { 1.0, 1.0, 1.0}, { 1.0, 1.0, -1.0},
// { 1.0, 1.0, -1.0}, { -1.0, 1.0, -1.0}, { -1.0, 1.0, 1.0} };
//
// std::vector<GLfloat> cube_colors = {
// 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0,
// 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
// 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0,
// 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0,
// 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0,
// 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0,
// 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0,
// 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
// 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
// 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
// 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0,
// 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0
// };
//
// std::vector<glm::vec3> cube_normals;
//
// for (int i = 0; i < cube_vertices.size() - 2; i += 3)
// {
// glm::vec3 norm = computeNormal(
// cube_vertices[i], cube_vertices[i + 1], cube_vertices[i + 2]);
// cube_normals.push_back(norm); cube_normals.push_back(norm); cube_normals.push_back(norm);
// }
//
//
// glGenVertexArrays(1, &_vaoHandle);
// glBindVertexArray(_vaoHandle);
//
// glGenBuffers(1, &_vbo_vertices);
// glBindBuffer(GL_ARRAY_BUFFER, _vbo_vertices);
// glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * cube_vertices.size() * 3, cube_vertices.data(), GL_STATIC_DRAW);
//
// glVertexAttribPointer(
// 0, //attr number = 0
// 3,
// GL_FLOAT,
// GL_FALSE,
// 0,
// (void*)0);
// glEnableVertexAttribArray(0); //attr number = 0
//
// glGenBuffers(1, &_vbo_normals);
// glBindBuffer(GL_ARRAY_BUFFER, _vbo_normals);
// glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * cube_normals.size() * 3, cube_normals.data(), GL_STATIC_DRAW);
//
// glVertexAttribPointer(
// 1,
// 3,
// GL_FLOAT,
// GL_FALSE,
// 0,
// (void*)0);
// glEnableVertexAttribArray(1);
//
// glGenBuffers(1, &_vbo_colors);
// glBindBuffer(GL_ARRAY_BUFFER, _vbo_colors);
// glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * cube_colors.size(), cube_colors.data(), GL_STATIC_DRAW);
//
// glVertexAttribPointer(
// 2,
// 3,
// GL_FLOAT,
// GL_FALSE,
// 0,
// (void*)0);
// glEnableVertexAttribArray(2);
//
// glBindVertexArray(0);
//}
//
//ColorCube::ColorCube(bool usingIBO)
//{
// ibo_used = usingIBO;
// if (usingIBO)
// iboSetup();
// else
// vaoSetup();
//}
//
//
//void ColorCube::draw(ShaderProgram *shader, glm::mat4x4 proj_matrix, glm::mat4x4 view_matrix)
//{
// _model.glPushMatrix();
// effectTransformations();
// glBindVertexArray(_vaoHandle);
// glm::mat4 mvpMatrix = proj_matrix * view_matrix * _model.getMatrix();
// glUniformMatrix4fv(shader->uniform("mvp"), 1, GL_FALSE, glm::value_ptr(mvpMatrix));
// glm::mat4 modelview = view_matrix * _model.getMatrix();
// _model.glPopMatrix();
// glUniform4fv(shader->uniform("LightLocation"), 1, glm::value_ptr(view_matrix * glm::vec4(50, 50, 50, 1)));
// glUniformMatrix4fv(shader->uniform("ModelViewMatrix"), 1, GL_FALSE, glm::value_ptr(modelview));
// glm::mat4 inverseModelView = glm::inverse(modelview);
// glm::mat3 normalMatrix = glm::mat3(glm::transpose(inverseModelView));
// glUniformMatrix3fv(shader->uniform("NormalMatrix"), 1, GL_FALSE, glm::value_ptr(normalMatrix));
//
// if (ibo_used)
// {
// int size;
// glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
// glDrawElements(GL_TRIANGLES, size / sizeof(GLushort), GL_UNSIGNED_SHORT, 0);
// }
// else
// glDrawArrays(GL_TRIANGLES, 0, 36);
//
//
//
//}
//
//DrawableType ColorCube::getType()
//{
// return DrawableType::COLOR_CUBE;
//}
//
//ColorCube::~ColorCube()
//{
//}

View File

@ -1,27 +1,27 @@
#include <iostream>
#include <string>
#include <vector>
#include <glm/vec4.hpp>
#include "ADrawable.h"
#pragma once
class ColorCube : public ADrawable
{
private:
GLuint _vaoHandle;
GLuint _iboHandle;
GLuint _vbo_vertices, _vbo_colors, _vbo_normals;
bool ibo_used;
void iboSetup();
void vaoSetup();
public:
ColorCube(bool usingIBO);
void draw(ShaderProgram *shader, glm::mat4x4 proj_matrix, glm::mat4x4 view_matrix) override;
DrawableType getType() override;
~ColorCube();
};
//#include <iostream>
//#include <string>
//#include <vector>
//#include <glm/vec4.hpp>
// #include "ADrawable.h"
//
//#pragma once
//class ColorCube : public ADrawable
//{
//private:
// GLuint _vaoHandle;
// GLuint _iboHandle;
// GLuint _vbo_vertices, _vbo_colors, _vbo_normals;
//
// bool ibo_used;
//
// void iboSetup();
// void vaoSetup();
//
//public:
// ColorCube(bool usingIBO);
//
// void draw(ShaderProgram *shader, glm::mat4x4 proj_matrix, glm::mat4x4 view_matrix) override;
// DrawableType getType() override;
// ~ColorCube();
//};
//

8
BaseGLProject/Dataset.h Normal file
View File

@ -0,0 +1,8 @@
#pragma once
class Dataset
{
public:
Dataset();
~Dataset();
};

View File

@ -1,8 +0,0 @@
#pragma once
#include "glm/glm.hpp"
namespace global
{
static glm::vec4 LightLocation(50, 50, 50, 1);
static glm::vec3 LightIntensity(0.9, 0.9, 0.9);
}

View File

@ -1,34 +0,0 @@
#pragma once
#include "GL/glew.h"
#include "glm/gtc/type_ptr.hpp"
#include "Loader.h"
#include "ModelView.h"
enum DrawableType
{
COLOR_CUBE,
CHECKERED_FLOOR,
WIRE_CUBE,
LINE_SEGMENT,
SPHERE,
TEAPOT,
LOADED_MESH,
LIGHTED_PLANE
};
class IDrawable {
public:
virtual void addRotation(glm::vec4 vec) = 0;
virtual void addTranslation(glm::vec4 vec) = 0;
virtual void addScaling(glm::vec4 vec) = 0;
virtual void removeLastTransformations(int n) = 0;
virtual glm::vec3 getPosition() = 0;
virtual glm::vec4 translateToPivot(glm::vec3) = 0;
virtual void draw(ShaderProgram *shader, glm::mat4x4 proj_matrix, glm::mat4x4 view_matrix) = 0;
virtual DrawableType getType() = 0;
};

10
BaseGLProject/Light.cpp Normal file
View File

@ -0,0 +1,10 @@
#include "Light.h"
Light::Light(glm::vec3 intensity_, glm::vec4 location_) : location(location_), intensity(intensity_)
{
}
Light::~Light()
{
}

16
BaseGLProject/Light.h Normal file
View File

@ -0,0 +1,16 @@
#pragma once
#include "glm/vec3.hpp"
#include "glm/vec4.hpp"
class Light
{
public:
Light(glm::vec3 intensity, glm::vec4 location);
~Light();
bool activated = true;
glm::vec3 intensity;
glm::vec4 location;
};

View File

@ -1,69 +0,0 @@
#include "LineSegment.h"
#define GLM_ENABLE_EXPERIMENTAL
#include "glm/gtx/string_cast.hpp"
void LineSegment::setup()
{
std::vector<glm::vec3> seg_vertices =
{ { .0, -1.0f, .0}, {.0, 1.0f, .0} };
std::vector<glm::vec3> seg_colors =
{ { 1.f, .0, .0}, {1.f, .0, .0} };
glGenVertexArrays(1, &_vaoHandle);
glBindVertexArray(_vaoHandle);
glGenBuffers(1, &_vbo_vertices);
glBindBuffer(GL_ARRAY_BUFFER, _vbo_vertices);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * seg_vertices.size() * 3, seg_vertices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(
0, //attr number = 0
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0);
glEnableVertexAttribArray(0); //attr number = 0
glGenBuffers(1, &_vbo_colors);
glBindBuffer(GL_ARRAY_BUFFER, _vbo_colors);
glBufferData(GL_ARRAY_BUFFER, seg_colors.size() * sizeof(GLfloat) * 3, seg_colors.data(), GL_STATIC_DRAW);
glVertexAttribPointer(
1,
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0);
glEnableVertexAttribArray(1);
}
LineSegment::LineSegment()
{
setup();
}
void LineSegment::draw(ShaderProgram *shader, glm::mat4x4 proj_matrix, glm::mat4x4 view_matrix)
{
_model.glPushMatrix();
effectTransformations();
glm::mat4 mvpMatrix = proj_matrix * view_matrix * _model.getMatrix();
glUniformMatrix4fv(shader->uniform("mvp"), 1, GL_FALSE, glm::value_ptr(mvpMatrix));
glLineWidth(4);
glBindVertexArray(_vaoHandle);
glDrawArrays(GL_LINES, 0, 2);
_model.glPopMatrix();
}
DrawableType LineSegment::getType()
{
return DrawableType::LINE_SEGMENT;
}
LineSegment::~LineSegment()
{
}

View File

@ -1,32 +0,0 @@
#pragma once
#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <glm/vec4.hpp>
#include "ADrawable.h"
#pragma once
class LineSegment : public ADrawable
{
private:
ADrawable *_start;
ADrawable *_end;
GLuint _vaoHandle;
GLuint _iboHandle;
GLuint _vbo_vertices, _vbo_colors;
void setup();
public:
LineSegment();
void draw(ShaderProgram *shader, glm::mat4x4 proj_matrix, glm::mat4x4 view_matrix) override;
DrawableType getType() override;
~LineSegment();
};

View File

@ -38,7 +38,7 @@ private:
GLuint fragmentShaderId;
// How many shaders are attached to the shader program
GLuint shaderCount;
GLuint shaderCount;
// Map of attributes and their binding locations
std::map<std::string, int> attributeMap;

11
BaseGLProject/Material.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include <glm/vec3.hpp>
struct Material
{
glm::vec3 ka;
glm::vec3 kd;
glm::vec3 ks;
float shininess;
};

View File

@ -4,7 +4,6 @@
#include <vector>
#include <iostream>
#include "Mesh.h"
#include "../Global.h"
#include <glm/vec3.hpp>
#include <glm/gtx/string_cast.hpp>
@ -19,9 +18,6 @@
* Constructor, loading the specified aiMesh
**/
Mesh::MeshEntry::MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m)
{
parent = m;
@ -38,7 +34,7 @@ Mesh::MeshEntry::MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m)
if (mesh->HasPositions()) {
float *vertices = new float[mesh->mNumVertices * 3];
for (int i = 0; i < mesh->mNumVertices; ++i) {
for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
vertices[i * 3] = mesh->mVertices[i].x;
vertices[i * 3 + 1] = mesh->mVertices[i].y;
vertices[i * 3 + 2] = mesh->mVertices[i].z;
@ -57,7 +53,7 @@ Mesh::MeshEntry::MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m)
if (mesh->HasTextureCoords(0)) {
float *texCoords = new float[mesh->mNumVertices * 2];
for (int i = 0; i < mesh->mNumVertices; ++i) {
for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
texCoords[i * 2] = mesh->mTextureCoords[0][i].x;
texCoords[i * 2 + 1] = mesh->mTextureCoords[0][i].y;
}
@ -75,7 +71,7 @@ Mesh::MeshEntry::MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m)
if (mesh->HasNormals()) {
float *normals = new float[mesh->mNumVertices * 3];
for (int i = 0; i < mesh->mNumVertices; ++i) {
for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
normals[i * 3] = mesh->mNormals[i].x;
normals[i * 3 + 1] = mesh->mNormals[i].y;
normals[i * 3 + 2] = mesh->mNormals[i].z;
@ -94,7 +90,7 @@ Mesh::MeshEntry::MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m)
if (mesh->HasFaces()) {
unsigned int *indices = new unsigned int[mesh->mNumFaces * 3];
for (int i = 0; i < mesh->mNumFaces; ++i) {
for (unsigned int i = 0; i < mesh->mNumFaces; ++i) {
indices[i * 3] = mesh->mFaces[i].mIndices[0];
indices[i * 3 + 1] = mesh->mFaces[i].mIndices[1];
indices[i * 3 + 2] = mesh->mFaces[i].mIndices[2];
@ -157,18 +153,17 @@ Mesh::MeshEntry::~MeshEntry() {
**/
void Mesh::MeshEntry::render() {
glBindVertexArray(vao);
int size;
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
glDrawElements(GL_TRIANGLES, size/ sizeof(unsigned int), GL_UNSIGNED_INT, NULL);
int size;
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
glDrawElements(GL_TRIANGLES, size/ sizeof(unsigned int), GL_UNSIGNED_INT, NULL);
glBindVertexArray(0);
}
/**
* Mesh constructor, loads the specified filename if supported by Assimp
**/
Mesh::Mesh(const char *filename, ShaderProgram * sh)
Mesh::Mesh(const char *filename, Shader *sh)
{
shader = sh;
@ -185,11 +180,10 @@ Mesh::Mesh(const char *filename, ShaderProgram * sh)
return;
}
for (int i = 0; i < scene->mNumMeshes; ++i) {
for (unsigned int i = 0; i < scene->mNumMeshes; ++i) {
meshEntries.push_back(new Mesh::MeshEntry(scene->mMeshes[i], scene, this));
}
}
/**
@ -197,73 +191,28 @@ Mesh::Mesh(const char *filename, ShaderProgram * sh)
**/
Mesh::~Mesh(void)
{
for (int i = 0; i < meshEntries.size(); ++i) {
for (unsigned int i = 0; i < meshEntries.size(); ++i) {
delete meshEntries.at(i);
}
meshEntries.clear();
}
/**
* Renders all loaded MeshEntries
**/
DrawableType Mesh::getType()
{
return DrawableType::LOADED_MESH;
}
void Mesh::draw(SceneContext ctx) {
void Mesh::draw(ShaderProgram *, glm::mat4x4 proj_matrix, glm::mat4x4 view_matrix) {
shader->use();
for (int i = 0; i < meshEntries.size(); ++i) {
shader->enable();
for (unsigned int i = 0; i < meshEntries.size(); ++i) {
MeshEntry * m = meshEntries[i];
// Moving the object to his set position
float shiness = meshEntries.at(i)->shininessStrength;
_model.glPushMatrix();
effectTransformations();
shader->addUniform("mvp");
shader->addUniform("NormalMatrix"); //Refer next slide : mat3
shader->addUniform("ModelViewMatrix"); //View*Model : mat4
glm::vec4 light_center(0, 10, 0, 0);
float radius = 10.0f;
float angle = 0;
static bool first_draw = true;
for (int i = 0; i < 5; i++)
{
std::string name = "Light[" + std::to_string(i) + "].Position";
shader->addUniform(name);
glm::vec4 position = glm::vec4(radius * cos(glm::radians(angle)), 10 ,radius * sin(glm::radians(angle)), 1);
glUniform4fv(shader->uniform(name), 1, glm::value_ptr(view_matrix * position));
angle += 360 / 5;
}
first_draw = false;
shader->addUniform("Light[0].Intensity");
glUniform3fv(shader->uniform("Light[0].Intensity"), 1, glm::value_ptr(glm::vec3(0.0f, 0.5f, 0.5f)));
shader->addUniform("Light[1].Intensity");
glUniform3fv(shader->uniform("Light[1].Intensity"), 1, glm::value_ptr(glm::vec3(0.0f, 0.0f, 0.5f)));
shader->addUniform("Light[2].Intensity");
glUniform3fv(shader->uniform("Light[2].Intensity"), 1, glm::value_ptr(glm::vec3(0.5f, 0.0f, 0.0f)));
shader->addUniform("Light[3].Intensity");
glUniform3fv(shader->uniform("Light[3].Intensity"), 1, glm::value_ptr(glm::vec3(0.0f, 0.5f, 0.0f)));
shader->addUniform("Light[4].Intensity");
glUniform3fv(shader->uniform("Light[4].Intensity"), 1, glm::value_ptr(glm::vec3(0.5f, 0.5f, 0.5f)));
glm::mat4 mvpMatrix = proj_matrix * view_matrix * _model.getMatrix();
glUniformMatrix4fv(shader->uniform("mvp"), 1, GL_FALSE, glm::value_ptr(mvpMatrix));
glm::mat4 modelview = view_matrix * _model.getMatrix();
glUniformMatrix4fv(shader->uniform("ModelViewMatrix"), 1, GL_FALSE, glm::value_ptr(modelview));
glm::mat4 inverseModelView = glm::inverse(modelview);
glm::mat3 normalMatrix = glm::mat3(glm::transpose(inverseModelView));
glUniformMatrix3fv(shader->uniform("NormalMatrix"), 1, GL_FALSE, glm::value_ptr(normalMatrix));
model.glPushMatrix();
effectTransformations();
//Retrieving material data from the vertex
float shininess = meshEntries.at(i)->shininessStrength;
glm::vec3 diffuse = glm::vec3(meshEntries.at(i)->dcolor.r, meshEntries.at(i)->dcolor.g, meshEntries.at(i)->dcolor.b);
glm::vec3 specular = glm::vec3(meshEntries.at(i)->scolor.r, meshEntries.at(i)->scolor.g, meshEntries.at(i)->scolor.b);
glm::vec3 ambient = glm::vec3(meshEntries.at(i)->acolor.r, meshEntries.at(i)->acolor.g, meshEntries.at(i)->acolor.b);
@ -280,17 +229,92 @@ void Mesh::draw(ShaderProgram *, glm::mat4x4 proj_matrix, glm::mat4x4 view_matri
specular = glm::vec3(0.4, 0.4, 0.4);
}
if (shiness == 0)
shiness = 150.0f;
if (shininess == 0)
shininess = 150.0f;
glUniform3fv(shader->uniform("Ka"), 1, glm::value_ptr(ambient));
glUniform3fv(shader->uniform("Kd"), 1, glm::value_ptr(diffuse));
glUniform3fv(shader->uniform("Ks"), 1, glm::value_ptr(specular));
glUniform1f(shader->uniform("Shininess"), shiness);
shader->mat.ka = ambient;
shader->mat.kd = diffuse;
shader->mat.ks = specular;
shader->mat.shininess = shininess;
// Setting the space matrixes uniques to the object
ctx.mvpMatrix = ctx.projectionMatrix * ctx.viewMatrix * model.getMatrix();
ctx.modelViewMatrix = ctx.viewMatrix * model.getMatrix();
ctx.normalMatrix = glm::mat3(glm::transpose(glm::inverse(ctx.modelViewMatrix)));
//Sending the uniform data to the shader
shader->setUniforms(ctx);
meshEntries.at(i)->render();
_model.glPopMatrix();
}
model.glPopMatrix();
}
shader->disable();
}
void Mesh::effectTransformations()
{
for (auto pair : _transformations)
{
switch (pair.second)
{
case(Rotation):
model.glRotate(pair.first.w, pair.first.x, pair.first.y, pair.first.z);
break;
case(Translation):
model.glTranslate(pair.first.x, pair.first.y, pair.first.z);
break;
case(Scaling):
model.glScale(pair.first.x, pair.first.y, pair.first.z);
break;
}
}
}
void Mesh::addRotation(glm::vec4 vec)
{
_transformations.emplace_back(vec, Rotation);
}
void Mesh::addTranslation(glm::vec4 vec)
{
_transformations.emplace_back(vec, Translation);
}
void Mesh::addScaling(glm::vec4 vec)
{
_transformations.emplace_back(vec, Scaling);
}
glm::vec4 Mesh::translateToPivot(glm::vec3 pivot)
{
glm::vec4 curr_pos = { 0, 0, 0, 0 };
glm::vec4 h_pivot = { pivot.x, pivot.y, pivot.z, 0 };
for (auto pair : _transformations)
{
if (pair.second == Translation)
curr_pos += pair.first;
}
addTranslation(h_pivot - curr_pos);
return curr_pos - h_pivot;
}
void Mesh::removeLastTransformations(int n = 1)
{
for (int i = 0; i < n; i++)
_transformations.pop_back();
}
glm::vec3 Mesh::getPosition()
{
glm::vec4 curr_pos = { 0, 0, 0, 0 };
for (auto pair : _transformations)
{
if (pair.second == Translation)
curr_pos += pair.first;
}
return curr_pos;
}

View File

@ -7,15 +7,24 @@
#include "assimp\scene.h"
#include "assimp\mesh.h"
#include "../Loader.h"
#include "../ADrawable.h"
#include "../Shader.h"
#include "../SceneContext.h"
#include "../Dataset.h"
#include "../ModelView.h"
class Mesh : public ADrawable
enum Transformation
{
Rotation,
Translation,
Scaling
};
class Mesh
{
public:
struct MeshEntry {
static enum BUFFERS {
enum BUFFERS {
VERTEX_BUFFER, TEXCOORD_BUFFER, NORMAL_BUFFER, INDEX_BUFFER
};
GLuint vao;
@ -33,13 +42,24 @@ public:
};
public:
Mesh(const char *filename, ShaderProgram * sh);
~Mesh(void);
Mesh(const char *filename, Shader *sh);
Mesh(Dataset dataset, Shader *sh);
~Mesh(void);
std::vector<MeshEntry*> meshEntries;
ShaderProgram * shader;
void draw(ShaderProgram *shader, glm::mat4x4 proj_matrix, glm::mat4x4 view_matrix);
DrawableType getType();
private:
std::vector<std::pair<glm::vec4, Transformation>> _transformations;
public:
Model model;
void effectTransformations();
void addRotation(glm::vec4 vec);
void addTranslation(glm::vec4 vec);
void addScaling(glm::vec4 vec);
glm::vec4 translateToPivot(glm::vec3);
void removeLastTransformations(int n);
glm::vec3 getPosition();
Shader *shader;
void draw(SceneContext ctx);
};

View File

@ -57,29 +57,26 @@ void MyGlWindow::setBgColor(float bgColor[3])
void MyGlWindow::setup()
{
_staticShader = new ShaderProgram();
_staticShader->initFromFiles("light.vert", "onelight.frag");
_lightShader = new ShaderProgram();
_lightShader->initFromFiles("light.vert", "toon.frag");
_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)));
_static_drawables.push_back(new Plane(50, 16));
//shaders["ToonShader"] = Shader("light.vert", "toon.frag");
//// Removing useless specular component
//shaders["ToonShader"].uniformFlags &= ~ShaderFlags::KS_FLAG;
//shaders["ToonShader"].uniformFlags &= ~ShaderFlags::SHINE_FLAG;
/*_static_drawables.emplace_back(new CheckeredFloor(50, 16));
_light_drawables.push_back(new ColorCube(false));
_light_drawables.back()->addTranslation(glm::vec4(2, 1.1f, 0, 0));
_light_drawables.push_back(new Sphere(1, 50, 50));
_light_drawables.back()->addTranslation(glm::vec4(-2, 1.1f, 0, 0));
_light_drawables.push_back(new VBOTeapot(64, glm::identity<glm::mat4x4>()));
_light_drawables.back()->addTranslation(glm::vec4(2, 0, -7, 0));
_light_drawables.back()->addRotation(glm::vec4(1, 0, 0, -90));
_light_drawables.push_back(new Mesh("bunny.obj", _lightShader));
_light_drawables.push_back(new Mesh("buddha.obj", _lightShader));
_light_drawables.back()->addTranslation(glm::vec4(0, 13, -15, 0));
_light_drawables.back()->addRotation(glm::vec4(0, 1, 0, -180));
_light_drawables.back()->addScaling(glm::vec4(30, 30, 30, 0));*/
_light_drawables.push_back(new Mesh("teapot.obj", _lightShader));
_light_drawables.back()->addTranslation(glm::vec4(0, 1, 0, 0));
_light_drawables.back()->addScaling(glm::vec4(1, 1, 1, 1));
//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()
@ -88,85 +85,24 @@ void MyGlWindow::draw()
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 paramters of m_viewer
glm::mat4 projection = perspective(45.0f, m_width / m_height, 0.1f, 500.0f);
_staticShader->use();
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
_staticShader->addUniform("mvp");
_staticShader->addUniform("Ka"); //Ambient Object Color :vec3
glUniform3fv(_staticShader->uniform("Ka"), 1, glm::value_ptr(glm::vec3(0.19225, 0.19225, 0.19225)));
_staticShader->addUniform("Kd"); //Diffuse Object Color :vec3
glUniform3fv(_staticShader->uniform("Kd"), 1, glm::value_ptr(glm::vec3(0.50754, 0.50754, 0.50754)));
_staticShader->addUniform("Ks"); //Specular Object Color :vec3
glUniform3fv(_staticShader->uniform("Ks"), 1, glm::value_ptr(glm::vec3(0.508273, 0.508273, 0.508273)));
_staticShader->addUniform("Shininess");
glUniform1f(_staticShader->uniform("Shininess"), 51.2);
_staticShader->addUniform("NormalMatrix"); //Refer next slide : mat3
_staticShader->addUniform("ModelViewMatrix"); //View*Model : mat4
/* Variable set for spot lighting */
_staticShader->addUniform("LightDirection");
_staticShader->addUniform("Exponent");
glUniform1f(_staticShader->uniform("Exponent"), 10);
_staticShader->addUniform("Cutoff");
glUniform1f(_staticShader->uniform("Cutoff"), 30);
_staticShader->addUniform("InnerCutoff");
glUniform1f(_staticShader->uniform("InnerCutoff"), 25);
/* END set */
for (ADrawable *drawable : _static_drawables)
drawable->draw(_staticShader, projection, view);
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);
_staticShader->disable();
_lightShader->use();
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
_lightShader->addUniform("mvp");
_lightShader->addUniform("Ka"); //Ambient Object Color :vec3
glUniform3fv(_lightShader->uniform("Ka"), 1, glm::value_ptr(glm::vec3(0.19225, 0.19225, 0.19225)));
_lightShader->addUniform("Kd"); //Diffuse Object Color :vec3
glUniform3fv(_lightShader->uniform("Kd"), 1, glm::value_ptr(glm::vec3(0.50754, 0.50754, 0.50754)));
_lightShader->addUniform("Ks"); //Specular Object Color :vec3
glUniform3fv(_lightShader->uniform("Ks"), 1, glm::value_ptr(glm::vec3(0.508273, 0.508273, 0.508273)));
_lightShader->addUniform("Shininess");
glUniform1f(_lightShader->uniform("Shininess"), 51.2);
_lightShader->addUniform("NormalMatrix"); //Refer next slide : mat3
_lightShader->addUniform("ModelViewMatrix"); //View*Model : mat4
for (ADrawable *drawable : _light_drawables)
drawable->draw(_lightShader, projection, view);
_lightShader->disable();
}
void MyGlWindow::AddDrawable(ADrawable *d)
{
_static_drawables.push_back(d);
_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)

View File

@ -4,17 +4,11 @@
#include "GL/glew.h"
#include <string>
#include <vector>
#include "IDrawable.h"
#include "ColorCube.h"
#include "Loader.h"
#include "Shader.h"
#include "ModelView.h"
#include "Viewer.h"
#include "CheckeredFloor.h"
#include "WireCube.h"
#include "LineSegment.h"
#include "Sphere.h"
#include "vboteapot.h"
#include "Plane.h"
#include "Light.h"
#include "SceneContext.h"
#include "Models/Mesh.h"
struct vertexAttr {
@ -24,21 +18,21 @@ struct vertexAttr {
class MyGlWindow {
public:
MyGlWindow(int w, int h);
MyGlWindow();
MyGlWindow(int w, int h);
~MyGlWindow();
void draw();
void setBgColor(float bgColor[3]);
std::map<std::string, Shader> shaders;
std::vector<Mesh> meshes;
std::map<std::string, float> floatValues;
void AddDrawable(ADrawable *);
void resize(int w, int h);
Viewer viewer;
Viewer viewer;
private:
ShaderProgram *_staticShader;
ShaderProgram *_lightShader;
Shader *ToonShader;
SceneContext _scnctx;
int m_width;
int m_height;
@ -46,10 +40,8 @@ private:
GLuint _vaoHandle;
GLuint _iboHandle;
std::vector<Mesh> _drawables;
std::vector<ADrawable *> _static_drawables;
std::vector<ADrawable *> _light_drawables;
void setup();
void setViewFromBox(glm::vec3 &eye, glm::vec3 &look, glm::vec3 &up);
void setup();
};

View File

@ -1,129 +0,0 @@
#include "Plane.h"
Plane::Plane(int size, int numcell)
{
this->size = size;
this->ncell = numcell;
setup(size, numcell);
}
int nvert2;
void Plane::setup(float size, int nSquares)
{
std::vector<glm::vec3> vlists;
std::vector<glm::vec3> clists;
// parameters:
float maxX = size / 2, maxY = size / 2;
float minX = -size / 2, minY = -size / 2;
int x, y, v[3], i;
float xp, yp, xd, yd;
v[2] = 0;
xd = (maxX - minX) / ((float)nSquares);
yd = (maxY - minY) / ((float)nSquares);
for (x = 0, xp = minX; x < nSquares; x++, xp += xd) {
for (y = 0, yp = minY, i = x; y < nSquares; y++, i++, yp += yd) {
clists.push_back(glm::vec3(0, 1, 0));
clists.push_back(glm::vec3(0, 1, 0));
clists.push_back(glm::vec3(0, 1, 0));
clists.push_back(glm::vec3(0, 1, 0));
clists.push_back(glm::vec3(0, 1, 0));
clists.push_back(glm::vec3(0, 1, 0));
vlists.push_back(glm::vec3(xp, -0.1, yp));
vlists.push_back(glm::vec3(xp, -0.1, yp + yd));
vlists.push_back(glm::vec3(xp + xd, -0.1, yp + yd));
vlists.push_back(glm::vec3(xp, -0.1, yp));
vlists.push_back(glm::vec3(xp + xd, -0.1, yp + yd));
vlists.push_back(glm::vec3(xp + xd, -0.1, yp));
} // end of for j
}// end of for i
nvert2 = vlists.size();
//create vao
glGenVertexArrays(1, &vaoHandle);
glBindVertexArray(vaoHandle);
//create vbo for vertices
glGenBuffers(1, &vbo_cube_vertices);
glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_vertices);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*vlists.size() * 3, vlists.data(), GL_STATIC_DRAW);
glVertexAttribPointer(
0, // attribute
3, // number of elements per vertex, here (x,y,z,1)
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
0, // no extra data between each position
0 // offset of first element
);
glEnableVertexAttribArray(0);
//create vbo for colors
glGenBuffers(1, &vbo_cube_colors);
glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_colors);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*clists.size() * 3, clists.data(), GL_STATIC_DRAW);
glVertexAttribPointer(
1, // attribute
3, // number of elements per vertex, here (R,G,B)
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
0, // no extra data between each position
0 // offset of first element
);
glEnableVertexAttribArray(1);
vlists.clear();
clists.clear();
glBindVertexArray(0);
}
void Plane::resize(int size, int numcell)
{
setup(size, numcell);
}
void Plane::draw(ShaderProgram * shader, glm::mat4x4 proj_matrix, glm::mat4x4 view_matrix)
{
_model.glPushMatrix();
effectTransformations();
glBindVertexArray(vaoHandle);
glm::mat4 mvpMatrix = proj_matrix * view_matrix * _model.getMatrix();
glUniformMatrix4fv(shader->uniform("mvp"), 1, GL_FALSE, glm::value_ptr(mvpMatrix));
glm::mat4 modelview = view_matrix * _model.getMatrix();
shader->addUniform("LightLocation");
glUniform4fv(shader->uniform("LightLocation"), 1, glm::value_ptr(view_matrix * glm::vec4(0, 15, 0, 1)));
shader->addUniform("LightIntensity");
glUniform3fv(shader->uniform("LightIntensity"), 1, glm::value_ptr(glm::vec3(0.7, 0.7, 0.7)));
glUniformMatrix4fv(shader->uniform("ModelViewMatrix"), 1, GL_FALSE, glm::value_ptr(modelview));
glUniform4fv(shader->uniform("LightDirection"), 1, glm::value_ptr(view_matrix * glm::vec4(0, -15, 0, 1)));
glm::mat4 inverseModelView = glm::inverse(modelview);
glm::mat3 normalMatrix = glm::mat3(glm::transpose(inverseModelView));
glUniformMatrix3fv(shader->uniform("NormalMatrix"), 1, GL_FALSE, glm::value_ptr(normalMatrix));
glDrawArrays(GL_TRIANGLES, 0, nvert2 * 3);
glBindVertexArray(0);
_model.glPopMatrix();
}
DrawableType Plane::getType()
{
return DrawableType::LIGHTED_PLANE;
}

View File

@ -1,25 +0,0 @@
#pragma once
#include <GL/glew.h>
#include <GL/gl.h>
#include <glm/glm.hpp>
#include <vector>
#include "ADrawable.h"
#include "Loader.h"
class Plane : public ADrawable
{
public:
Plane(int size, int numcell);
virtual void draw(ShaderProgram * shader, glm::mat4x4 proj_matrix, glm::mat4x4 view_matrix) override;
virtual DrawableType getType() override;
void setup(float size, int nSquares);
void resize(int size, int numcell);
GLuint vaoHandle;
GLuint vbo_cube_vertices, vbo_cube_colors;
int size;
int ncell;
};

View File

@ -0,0 +1,18 @@
#pragma once
#include <map>
#include <glm/mat3x3.hpp>
#include <glm/mat4x4.hpp>
struct SceneContext
{
std::map<std::string, Light> lights;
std::map<std::string, float> floatValues;
glm::mat4x4 viewMatrix;
glm::mat4x4 projectionMatrix;
glm::mat4x4 mvpMatrix;
glm::mat4x4 modelViewMatrix;
glm::mat3x3 normalMatrix;
};

70
BaseGLProject/Shader.cpp Normal file
View File

@ -0,0 +1,70 @@
#include "Shader.h"
Shader::Shader(const std::string vtx_name, const std::string frag_name)
{
_program.initFromFiles(vtx_name, frag_name);
}
Shader::~Shader()
{
}
void Shader::enable()
{
_program.use();
}
void Shader::disable()
{
_program.disable();
}
void Shader::setMaterial(SceneContext ctx)
{
if ((uniformFlags & ShaderFlags::KA_FLAG) == ShaderFlags::KA_FLAG)
glUniform3fv(_program.addUniform("Ka"), 1, glm::value_ptr(mat.ka));
if ((uniformFlags & ShaderFlags::KD_FLAG) == ShaderFlags::KD_FLAG)
glUniform3fv(_program.addUniform("Kd"), 1, glm::value_ptr(mat.kd));
if ((uniformFlags & ShaderFlags::KS_FLAG) == ShaderFlags::KS_FLAG)
glUniform3fv(_program.addUniform("Ks"), 1, glm::value_ptr(mat.ks));
if ((uniformFlags & ShaderFlags::SHINE_FLAG) == ShaderFlags::SHINE_FLAG)
glUniform1f(_program.addUniform("Shininess"), mat.shininess);
}
void Shader::setCamera(SceneContext ctx)
{
if ((uniformFlags & ShaderFlags::MVP_FLAG) == ShaderFlags::MVP_FLAG)
glUniformMatrix4fv(_program.addUniform("mvp"), 1, GL_FALSE, glm::value_ptr(ctx.mvpMatrix));
if ((uniformFlags & ShaderFlags::NORMAL_MAT_FLAG) == ShaderFlags::NORMAL_MAT_FLAG)
glUniformMatrix3fv(_program.addUniform("NormalMatrix"), 1, GL_FALSE, glm::value_ptr(ctx.normalMatrix));
if ((uniformFlags & ShaderFlags::MODELVIEW_FLAG) == ShaderFlags::MODELVIEW_FLAG)
glUniformMatrix4fv(_program.addUniform("ModelViewMatrix"), 1, GL_FALSE, glm::value_ptr(ctx.modelViewMatrix));
}
void Shader::setLights(SceneContext ctx)
{
int i = 0;
if ((uniformFlags & ShaderFlags::LIGHTS_FLAG) != ShaderFlags::LIGHTS_FLAG)
return;
for (auto light: ctx.lights)
{
if (light.second.activated)
{
std::string lightname = "Light[" + std::to_string(i) + "].";
_program.addUniform(lightname + "Position");
glUniform4fv(_program.uniform(lightname + "Position"), 1, glm::value_ptr(ctx.viewMatrix * light.second.location));
_program.addUniform(lightname + "Intensity");
glUniform3fv(_program.uniform(lightname + "Intensity"), 1, glm::value_ptr(light.second.intensity));
i++;
}
}
_program.addUniform("LightCount");
glUniform1i(_program.uniform("LightCount"), i);
}
void Shader::setUniforms(SceneContext ctx)
{
setCamera(ctx);
setLights(ctx);
setMaterial(ctx);
}

50
BaseGLProject/Shader.h Normal file
View File

@ -0,0 +1,50 @@
#pragma once
#include <string>
#include <vector>
#include <memory>
#include <GL/glew.h>
#include "glm/gtc/type_ptr.hpp"
#include "Material.h"
#include "Light.h"
#include "SceneContext.h"
#include "Loader.h"
enum ShaderFlags
{
NO_UNIFORM = 0,
KD_FLAG = 1,
KA_FLAG = 2,
KS_FLAG = 4,
SHINE_FLAG = 8,
MVP_FLAG = 16,
NORMAL_MAT_FLAG = 32,
MODELVIEW_FLAG = 64,
LIGHTS_FLAG = 128,
};
class Shader
{
public:
Shader() {};
Shader(const std::string vtx_name, const std::string frag_name);
~Shader();
int uniformFlags = KD_FLAG | KA_FLAG | KS_FLAG | SHINE_FLAG | MVP_FLAG | NORMAL_MAT_FLAG | MODELVIEW_FLAG | LIGHTS_FLAG;
Material mat;
void enable();
void disable();
public:
void setUniforms(SceneContext ctx);
private:
void setMaterial(SceneContext ctx);
void setCamera(SceneContext ctx);
void setLights(SceneContext ctx);
private:
ShaderProgram _program;
};

View File

@ -1,191 +0,0 @@
#include "Sphere.h"
#include <glm//gtc/constants.hpp>
#include <glm/vec3.hpp>
#include <glm/mat3x3.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/matrix_inverse.hpp>
Sphere::Sphere()
{
}
Sphere::~Sphere()
{
}
Sphere::Sphere(float rad, GLuint sl, GLuint st) :
radius(rad), slices(sl), stacks(st)
{
nVerts = (slices + 1) * (stacks + 1);
elements = (slices * 2 * (stacks - 1)) * 3;
// Verts
float * v = new float[3 * nVerts];
// Normals
float * n = new float[3 * nVerts];
// Tex coords
float * tex = new float[2 * nVerts]; //we don't use it now
// Index
unsigned int * el = new unsigned int[elements]; //index
// Generate the vertex data
generateVerts(v, n, tex, el);
//create vao, vbo and ibo here... (We didn't use std::vector here...)
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &VBO_position);
glBindBuffer(GL_ARRAY_BUFFER, VBO_position);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * nVerts, v, GL_STATIC_DRAW);
glVertexAttribPointer(
0, //attr number = 0
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0);
glEnableVertexAttribArray(0); //attr number = 0
glGenBuffers(1, &VBO_normal);
glBindBuffer(GL_ARRAY_BUFFER, VBO_normal);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * nVerts, n, GL_STATIC_DRAW);
glVertexAttribPointer(
1,
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0);
glEnableVertexAttribArray(1);
glGenBuffers(1, &VBO_tex);
glBindBuffer(GL_ARRAY_BUFFER, VBO_tex);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 2 * nVerts, tex, GL_STATIC_DRAW);
glVertexAttribPointer(
2,
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0);
glEnableVertexAttribArray(2);
glGenBuffers(1, &IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, elements * sizeof(GLuint), el, GL_STATIC_DRAW);
glBindVertexArray(0);
delete[] v;
delete[] n;
delete[] el;
delete[] tex;
}
void Sphere::draw(ShaderProgram *shader, glm::mat4x4 proj_matrix, glm::mat4x4 view_matrix)
{
_model.glPushMatrix();
effectTransformations();
glBindVertexArray(VAO);
glm::mat4 mvpMatrix = proj_matrix * view_matrix * _model.getMatrix();
glUniformMatrix4fv(shader->uniform("mvp"), 1, GL_FALSE, glm::value_ptr(mvpMatrix));
glm::mat4 modelview = view_matrix * _model.getMatrix();
glUniform4fv(shader->uniform("LightLocation"), 1, glm::value_ptr(view_matrix * glm::vec4(50, 50, 50, 1)));
glUniformMatrix4fv(shader->uniform("ModelViewMatrix"), 1, GL_FALSE, glm::value_ptr(modelview));
glm::mat4 inverseModelView = glm::inverse(modelview);
glm::mat3 normalMatrix = glm::mat3(glm::transpose(inverseModelView));
glUniformMatrix3fv(shader->uniform("NormalMatrix"), 1, GL_FALSE, glm::value_ptr(normalMatrix));
int size;
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
glDrawElements(GL_TRIANGLES, size / sizeof(GLuint), GL_UNSIGNED_INT, 0);
_model.glPopMatrix();
}
void Sphere::generateVerts(float * verts, float * norms, float * tex,
unsigned int * el)
{
// Generate positions and normals
GLfloat theta, phi;
GLfloat thetaFac = glm::two_pi<float>() / slices;
GLfloat phiFac = glm::pi<float>() / stacks;
GLfloat nx, ny, nz, s, t;
GLuint idx = 0, tIdx = 0;
for (GLuint i = 0; i <= slices; i++) {
theta = i * thetaFac;
s = (GLfloat)i / slices;
for (GLuint j = 0; j <= stacks; j++) {
phi = j * phiFac;
t = (GLfloat)j / stacks;
nx = sinf(phi) * cosf(theta);
ny = sinf(phi) * sinf(theta);
nz = cosf(phi);
verts[idx] = radius * nx; verts[idx + 1] = radius * ny; verts[idx + 2] = radius * nz;
norms[idx] = nx; norms[idx + 1] = ny; norms[idx + 2] = nz;
idx += 3;
tex[tIdx] = s;
tex[tIdx + 1] = t;
tIdx += 2;
}
}
// Generate the element list
idx = 0;
for (GLuint i = 0; i < slices; i++) {
GLuint stackStart = i * (stacks + 1);
GLuint nextStackStart = (i + 1) * (stacks + 1);
for (GLuint j = 0; j < stacks; j++) {
if (j == 0) {
el[idx] = stackStart;
el[idx + 1] = stackStart + 1;
el[idx + 2] = nextStackStart + 1;
idx += 3;
}
else if (j == stacks - 1) {
el[idx] = stackStart + j;
el[idx + 1] = stackStart + j + 1;
el[idx + 2] = nextStackStart + j;
idx += 3;
}
else {
el[idx] = stackStart + j;
el[idx + 1] = stackStart + j + 1;
el[idx + 2] = nextStackStart + j + 1;
el[idx + 3] = nextStackStart + j;
el[idx + 4] = stackStart + j;
el[idx + 5] = nextStackStart + j + 1;
idx += 6;
}
}
}
}
DrawableType Sphere::getType()
{
return DrawableType::SPHERE;
}
int Sphere::getVertexArrayHandle()
{
return this->VAO;
}

View File

@ -1,45 +0,0 @@
#ifndef ___SPHERE_H
#define ___SPHERE_H
#include "GL/glew.h"
#include <GL/GL.h>
#include <glm/mat4x4.hpp>
#include <vector>
#include "ADrawable.h"
#include "Loader.h"
class Sphere : public ADrawable
{
public:
Sphere();
Sphere(float rad, GLuint sl, GLuint st);
~Sphere();
void draw(ShaderProgram *shader, glm::mat4x4 proj_matrix, glm::mat4x4 view_matrix);
int getVertexArrayHandle();
GLuint VAO, VBO_position, VBO_normal, VBO_tex, IBO;
private:
GLuint nVerts, elements;
float radius;
GLuint slices, stacks;
void generateVerts(float *, float *, float *, unsigned int *);
// Inherited via ADrawable
virtual DrawableType getType() override;
};
#endif

View File

@ -200,6 +200,6 @@ void Viewer::getFrustrumInfo() {
m_imagePlaneHorizDir = glm::normalize(m_imagePlaneHorizDir);
// Get the view plane width and height at the view center.
m_displayHeight = 2.0 * glm::length(m_viewCenter-m_viewPoint) * tan(0.5*m_fieldOfView);
m_displayHeight = float(2.0 * glm::length(m_viewCenter-m_viewPoint) * tan(0.5*m_fieldOfView));
m_displayWidth = m_displayHeight * m_aspectRatio;
}

View File

@ -1,67 +0,0 @@
#include "WireCube.h"
void WireCube::setup()
{
_cube_vertices =
{ { -1.0, -1.0, 1.0}, { 1.0, -1.0, 1.0}, { 1.0, 1.0, 1.0},
{ -1.0, 1.0, 1.0}, { 1.0, 1.0, 1.0}, { -1.0, -1.0, 1.0},
{ -1.0, -1.0, -1.0}, { 1.0, -1.0, -1.0}, { 1.0, 1.0, -1.0},
{ -1.0, 1.0, -1.0}, { 1.0, 1.0, -1.0}, { -1.0, -1.0, -1.0},
{ -1.0, 1.0, -1.0}, { 1.0, 1.0, -1.0}, { 1.0, 1.0, 1.0},
{ -1.0, 1.0, 1.0}, { -1.0, 1.0, -1.0}, { 1.0, 1.0, -1.0},
{ -1.0, -1.0, -1.0}, { 1.0, -1.0, -1.0}, { 1.0, -1.0, 1.0},
{ -1.0, -1.0, 1.0}, { -1.0, -1.0, -1.0}, { 1.0, -1.0, -1.0},
{ -1.0, -1.0, -1.0}, { -1.0, -1.0, 1.0}, { -1.0, 1.0, 1.0},
{ -1.0, 1.0, -1.0}, { -1.0, -1.0, -1.0}, { -1.0, -1.0, 1.0},
{ 1.0, -1.0, -1.0}, { 1.0, -1.0, 1.0}, { 1.0, 1.0, 1.0},
{ 1.0, 1.0, -1.0}, { 1.0, -1.0, -1.0}, { 1.0, -1.0, 1.0}};
glGenVertexArrays(1, &_vaoHandle);
glBindVertexArray(_vaoHandle);
glGenBuffers(1, &_vbo_vertices);
glBindBuffer(GL_ARRAY_BUFFER, _vbo_vertices);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * _cube_vertices.size() * 3, _cube_vertices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(
0, //attr number = 0
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0);
glEnableVertexAttribArray(0); //attr number = 0
}
WireCube::WireCube()
{
setup();
}
void WireCube::draw(ShaderProgram * shader, glm::mat4x4 proj_matrix, glm::mat4x4 view_matrix)
{
_model.glPushMatrix();
effectTransformations();
glm::mat4 mvpMatrix = proj_matrix * view_matrix * _model.getMatrix();
glUniformMatrix4fv(shader->uniform("mvp"), 1, GL_FALSE, glm::value_ptr(mvpMatrix));
glLineWidth(2);
glBindVertexArray(_vaoHandle);
for (int i = 0; i < _cube_vertices.size() * 3; i += 3)
glDrawArrays(GL_LINE_LOOP, i, 3);
_model.glPopMatrix();
}
DrawableType WireCube::getType()
{
return DrawableType::WIRE_CUBE;
}
WireCube::~WireCube()
{
}

View File

@ -1,27 +0,0 @@
#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <glm/vec4.hpp>
#include "ADrawable.h"
#pragma once
class WireCube : public ADrawable
{
private:
GLuint _vaoHandle;
GLuint _vbo_vertices, _vbo_colors;
std::vector<glm::vec3> _cube_vertices;
void setup();
public:
WireCube();
void draw(ShaderProgram *shader, glm::mat4x4 proj_matrix, glm::mat4x4 view_matrix) override;
DrawableType getType() override;
~WireCube();
};

View File

@ -1,206 +0,0 @@
#ifndef TEAPOTDATA_H
#define TEAPOTDATA_H
/* Copyright (c) Mark J. Kilgard, 1994. */
/**
(c) Copyright 1993, Silicon Graphics, Inc.
ALL RIGHTS RESERVED
Permission to use, copy, modify, and distribute this software
for any purpose and without fee is hereby granted, provided
that the above copyright notice appear in all copies and that
both the copyright notice and this permission notice appear in
supporting documentation, and that the name of Silicon
Graphics, Inc. not be used in advertising or publicity
pertaining to distribution of the software without specific,
written prior permission.
THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU
"AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR
OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN NO
EVENT SHALL SILICON GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE
ELSE FOR ANY DIRECT, SPECIAL, INCIDENTAL, INDIRECT OR
CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER,
INCLUDING WITHOUT LIMITATION, LOSS OF PROFIT, LOSS OF USE,
SAVINGS OR REVENUE, OR THE CLAIMS OF THIRD PARTIES, WHETHER OR
NOT SILICON GRAPHICS, INC. HAS BEEN ADVISED OF THE POSSIBILITY
OF SUCH LOSS, HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
ARISING OUT OF OR IN CONNECTION WITH THE POSSESSION, USE OR
PERFORMANCE OF THIS SOFTWARE.
US Government Users Restricted Rights
Use, duplication, or disclosure by the Government is subject to
restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
(c)(1)(ii) of the Rights in Technical Data and Computer
Software clause at DFARS 252.227-7013 and/or in similar or
successor clauses in the FAR or the DOD or NASA FAR
Supplement. Unpublished-- rights reserved under the copyright
laws of the United States. Contractor/manufacturer is Silicon
Graphics, Inc., 2011 N. Shoreline Blvd., Mountain View, CA
94039-7311.
OpenGL(TM) is a trademark of Silicon Graphics, Inc.
*/
/* Rim, body, lid, and bottom data must be reflected in x and
y; handle and spout data across the y axis only. */
namespace Teapot {
static int patchdata[][16] =
{
/* rim */
{102, 103, 104, 105, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
/* body */
{12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27},
{24, 25, 26, 27, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40},
/* lid */
{96, 96, 96, 96, 97, 98, 99, 100, 101, 101, 101, 101, 0, 1, 2, 3,},
{0, 1, 2, 3, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117},
/* bottom */
{118, 118, 118, 118, 124, 122, 119, 121, 123, 126, 125, 120, 40, 39, 38, 37},
/* handle */
{41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56},
{53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 28, 65, 66, 67},
/* spout */
{68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83},
{80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95}
};
static float cpdata[][3] =
{
{0.2f, 0.f, 2.7f},
{0.2f, -0.112f, 2.7f},
{0.112f, -0.2f, 2.7f},
{0.f, -0.2f, 2.7f},
{1.3375f, 0.f, 2.53125f},
{1.3375f, -0.749f, 2.53125f},
{0.749f, -1.3375f, 2.53125f},
{0.f, -1.3375f, 2.53125f},
{1.4375f, 0.f, 2.53125f},
{1.4375f, -0.805f, 2.53125f},
{0.805f, -1.4375f, 2.53125f},
{0.f, -1.4375f, 2.53125f},
{1.5f, 0.f, 2.4f},
{1.5f, -0.84f, 2.4f},
{0.84f, -1.5f, 2.4f},
{0.f, -1.5f, 2.4f},
{1.75f, 0.f, 1.875f},
{1.75f, -0.98f, 1.875f},
{0.98f, -1.75f, 1.875f},
{0.f, -1.75f, 1.875f},
{2.f, 0.f, 1.35f},
{2.f, -1.12f, 1.35f},
{1.12f, -2.f, 1.35f},
{0.f, -2.f, 1.35f},
{2.f, 0.f, 0.9f},
{2.f, -1.12f, 0.9f},
{1.12f, -2.f, 0.9f},
{0.f, -2.f, 0.9f},
{-2.f, 0.f, 0.9f},
{2.f, 0.f, 0.45f},
{2.f, -1.12f, 0.45f},
{1.12f, -2.f, 0.45f},
{0.f, -2.f, 0.45f},
{1.5f, 0.f, 0.225f},
{1.5f, -0.84f, 0.225f},
{0.84f, -1.5f, 0.225f},
{0.f, -1.5f, 0.225f},
{1.5f, 0.f, 0.15f},
{1.5f, -0.84f, 0.15f},
{0.84f, -1.5f, 0.15f},
{0.f, -1.5f, 0.15f},
{-1.6f, 0.f, 2.025f},
{-1.6f, -0.3f, 2.025f},
{-1.5f, -0.3f, 2.25f},
{-1.5f, 0.f, 2.25f},
{-2.3f, 0.f, 2.025f},
{-2.3f, -0.3f, 2.025f},
{-2.5f, -0.3f, 2.25f},
{-2.5f, 0.f, 2.25f},
{-2.7f, 0.f, 2.025f},
{-2.7f, -0.3f, 2.025f},
{-3.f, -0.3f, 2.25f},
{-3.f, 0.f, 2.25f},
{-2.7f, 0.f, 1.8f},
{-2.7f, -0.3f, 1.8f},
{-3.f, -0.3f, 1.8f},
{-3.f, 0.f, 1.8f},
{-2.7f, 0.f, 1.575f},
{-2.7f, -0.3f, 1.575f},
{-3.f, -0.3f, 1.35f},
{-3.f, 0.f, 1.35f},
{-2.5f, 0.f, 1.125f},
{-2.5f, -0.3f, 1.125f},
{-2.65f, -0.3f, 0.9375f},
{-2.65f, 0.f, 0.9375f},
{-2.f, -0.3f, 0.9f},
{-1.9f, -0.3f, 0.6f},
{-1.9f, 0.f, 0.6f},
{1.7f, 0.f, 1.425f},
{1.7f, -0.66f, 1.425f},
{1.7f, -0.66f, 0.6f},
{1.7f, 0.f, 0.6f},
{2.6f, 0.f, 1.425f},
{2.6f, -0.66f, 1.425f},
{3.1f, -0.66f, 0.825f},
{3.1f, 0.f, 0.825f},
{2.3f, 0.f, 2.1f},
{2.3f, -0.25f, 2.1f},
{2.4f, -0.25f, 2.025f},
{2.4f, 0.f, 2.025f},
{2.7f, 0.f, 2.4f},
{2.7f, -0.25f, 2.4f},
{3.3f, -0.25f, 2.4f},
{3.3f, 0.f, 2.4f},
{2.8f, 0.f, 2.475f},
{2.8f, -0.25f, 2.475f},
{3.525f, -0.25f, 2.49375f},
{3.525f, 0.f, 2.49375f},
{2.9f, 0.f, 2.475f},
{2.9f, -0.15f, 2.475f},
{3.45f, -0.15f, 2.5125f},
{3.45f, 0.f, 2.5125f},
{2.8f, 0.f, 2.4f},
{2.8f, -0.15f, 2.4f},
{3.2f, -0.15f, 2.4f},
{3.2f, 0.f, 2.4f},
{0.f, 0.f, 3.15f},
{0.8f, 0.f, 3.15f},
{0.8f, -0.45f, 3.15f},
{0.45f, -0.8f, 3.15f},
{0.f, -0.8f, 3.15f},
{0.f, 0.f, 2.85f},
{1.4f, 0.f, 2.4f},
{1.4f, -0.784f, 2.4f},
{0.784f, -1.4f, 2.4f},
{0.f, -1.4f, 2.4f},
{0.4f, 0.f, 2.55f},
{0.4f, -0.224f, 2.55f},
{0.224f, -0.4f, 2.55f},
{0.f, -0.4f, 2.55f},
{1.3f, 0.f, 2.55f},
{1.3f, -0.728f, 2.55f},
{0.728f, -1.3f, 2.55f},
{0.f, -1.3f, 2.55f},
{1.3f, 0.f, 2.4f},
{1.3f, -0.728f, 2.4f},
{0.728f, -1.3f, 2.4f},
{0.f, -1.3f, 2.4f},
{0.f, 0.f, 0.f},
{1.425f, -0.798f, 0.f},
{1.5f, 0.f, 0.075f},
{1.425f, 0.f, 0.f},
{0.798f, -1.425f, 0.f},
{0.f, -1.5f, 0.075f},
{0.f, -1.425f, 0.f},
{1.5f, -0.84f, 0.075f},
{0.84f, -1.5f, 0.075f}
};
}
#endif // TEAPOTDATA_H

View File

@ -13,8 +13,8 @@ struct LightInfo {
vec3 Intensity;
};
uniform LightInfo Light[5];
uniform LightInfo Light[64];
uniform int LightCount;
in vec3 fNormal;
in vec3 pos;
@ -26,7 +26,7 @@ void main()
vec3 ambient;
ambient = Ka * Light[0].Intensity;
for (int i=0; i<5; i++)
for (int i=0; i< LightCount; i++)
{
vec3 L = normalize(Light[i].Position.xyz - pos);
vec3 N = fNormal;
@ -44,8 +44,9 @@ void main()
vec3 diffuse = Kd * Light[i].Intensity * max(dot(L, N), 0.0) * value;
// Not needed for cartoon shader
vec3 specular = Ks * Light[i].Intensity * pow(max(dot(H, N), 0.0), Shininess);
//vec3 specular = Ks * Light[i].Intensity * pow(max(dot(H, N), 0.0), Shininess);
//dirty trick to stay with the same shader params
finalColor = finalColor + diffuse;
}

View File

@ -1,326 +0,0 @@
#include "vboteapot.h"
#include "teapotdata.h"
#include <GL/glew.h>
#include <GL/gl.h>
#include <cstdio>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/matrix_inverse.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/constants.hpp>
//set grid = 64, lidTransform = 4x4 identity matrix
VBOTeapot::VBOTeapot(int grid, mat4 lidTransform)
{
int verts = 32 * (grid + 1) * (grid + 1);
faces = grid * grid * 32;
float * v = new float[ verts * 3 ]; //vertex positions : vec3
float * n = new float[ verts * 3 ]; //vertex normals : vec3
float * tc = new float[ verts * 2 ]; //texture coordinates : vec2 (we don't use it at this point)
unsigned int * el = new unsigned int[faces * 6]; //indices for IBO
generatePatches(v, n, tc, el, grid);
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &VBO_position);
glBindBuffer(GL_ARRAY_BUFFER, VBO_position);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * verts, v, GL_STATIC_DRAW);
glVertexAttribPointer(
0, //attr number = 0
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0);
glEnableVertexAttribArray(0); //attr number = 0
glGenBuffers(1, &VBO_normal);
glBindBuffer(GL_ARRAY_BUFFER, VBO_normal);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * verts, n, GL_STATIC_DRAW);
glVertexAttribPointer(
1,
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0);
glEnableVertexAttribArray(1);
glGenBuffers(1, &VBO_tex);
glBindBuffer(GL_ARRAY_BUFFER, VBO_tex);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 2 * verts, tc, GL_STATIC_DRAW);
glVertexAttribPointer(
2,
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0);
glEnableVertexAttribArray(2);
glGenBuffers(1, &IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, faces * 6 * sizeof(GLuint), el, GL_STATIC_DRAW);
glBindVertexArray(0);
delete [] v;
delete [] n;
delete [] el;
delete [] tc;
}
void VBOTeapot::draw(ShaderProgram * shader, glm::mat4x4 proj_matrix, glm::mat4x4 view_matrix)
{
_model.glPushMatrix();
effectTransformations();
glBindVertexArray(VAO);
glm::mat4 mvpMatrix = proj_matrix * view_matrix * _model.getMatrix();
glUniformMatrix4fv(shader->uniform("mvp"), 1, GL_FALSE, glm::value_ptr(mvpMatrix));
glm::mat4 modelview = view_matrix * _model.getMatrix();
glUniform4fv(shader->uniform("LightLocation"), 1, glm::value_ptr(view_matrix * glm::vec4(50, 50, 50, 1)));
glUniformMatrix4fv(shader->uniform("ModelViewMatrix"), 1, GL_FALSE, glm::value_ptr(modelview));
glm::mat4 inverseModelView = glm::inverse(modelview);
glm::mat3 normalMatrix = glm::mat3(glm::transpose(inverseModelView));
glUniformMatrix3fv(shader->uniform("NormalMatrix"), 1, GL_FALSE, glm::value_ptr(normalMatrix));
int size;
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
glDrawElements(GL_TRIANGLES, size / sizeof(GLuint), GL_UNSIGNED_INT, 0);
_model.glPopMatrix();
}
DrawableType VBOTeapot::getType()
{
return DrawableType::TEAPOT;
}
void VBOTeapot::generatePatches(float * v, float * n, float * tc, unsigned int* el, int grid) {
float * B = new float[4*(grid+1)]; // Pre-computed Bernstein basis functions
float * dB = new float[4*(grid+1)]; // Pre-computed derivitives of basis functions
int idx = 0, elIndex = 0, tcIndex = 0;
// Pre-compute the basis functions (Bernstein polynomials)
// and their derivatives
computeBasisFunctions(B, dB, grid);
// Build each patch
// The rim
buildPatchReflect(0, B, dB, v, n, tc, el, idx, elIndex, tcIndex, grid, true, true);
// The body
buildPatchReflect(1, B, dB, v, n, tc, el, idx, elIndex, tcIndex, grid, true, true);
buildPatchReflect(2, B, dB, v, n, tc, el, idx, elIndex, tcIndex, grid, true, true);
// The lid
buildPatchReflect(3, B, dB, v, n, tc, el, idx, elIndex, tcIndex, grid, true, true);
buildPatchReflect(4, B, dB, v, n, tc, el, idx, elIndex, tcIndex, grid, true, true);
// The bottom
buildPatchReflect(5, B, dB, v, n, tc, el, idx, elIndex, tcIndex, grid, true, true);
// The handle
buildPatchReflect(6, B, dB, v, n, tc, el, idx, elIndex, tcIndex, grid, false, true);
buildPatchReflect(7, B, dB, v, n, tc, el, idx, elIndex, tcIndex, grid, false, true);
// The spout
buildPatchReflect(8, B, dB, v, n, tc, el, idx, elIndex, tcIndex, grid, false, true);
buildPatchReflect(9, B, dB, v, n, tc, el, idx, elIndex, tcIndex, grid, false, true);
delete [] B;
delete [] dB;
}
void VBOTeapot::moveLid(int grid, float *v, mat4 lidTransform) {
int start = 3 * 12 * (grid+1) * (grid+1);
int end = 3 * 20 * (grid+1) * (grid+1);
for( int i = start; i < end; i+=3 )
{
vec4 vert = vec4(v[i], v[i+1], v[i+2], 1.0f );
vert = lidTransform * vert;
v[i] = vert.x;
v[i+1] = vert.y;
v[i+2] = vert.z;
}
}
void VBOTeapot::buildPatchReflect(int patchNum,
float *B, float *dB,
float *v, float *n,
float *tc, unsigned int *el,
int &index, int &elIndex, int &tcIndex, int grid,
bool reflectX, bool reflectY)
{
vec3 patch[4][4];
vec3 patchRevV[4][4];
getPatch(patchNum, patch, false);
getPatch(patchNum, patchRevV, true);
// Patch without modification
buildPatch(patch, B, dB, v, n, tc, el,
index, elIndex, tcIndex, grid, mat3(1.0f), true);
// Patch reflected in x
if( reflectX ) {
buildPatch(patchRevV, B, dB, v, n, tc, el,
index, elIndex, tcIndex, grid, mat3(vec3(-1.0f, 0.0f, 0.0f),
vec3(0.0f, 1.0f, 0.0f),
vec3(0.0f, 0.0f, 1.0f) ), false );
}
// Patch reflected in y
if( reflectY ) {
buildPatch(patchRevV, B, dB, v, n, tc, el,
index, elIndex, tcIndex, grid, mat3(vec3(1.0f, 0.0f, 0.0f),
vec3(0.0f, -1.0f, 0.0f),
vec3(0.0f, 0.0f, 1.0f) ), false );
}
// Patch reflected in x and y
if( reflectX && reflectY ) {
buildPatch(patch, B, dB, v, n, tc, el,
index, elIndex, tcIndex, grid, mat3(vec3(-1.0f, 0.0f, 0.0f),
vec3(0.0f, -1.0f, 0.0f),
vec3(0.0f, 0.0f, 1.0f) ), true );
}
}
void VBOTeapot::buildPatch(vec3 patch[][4],
float *B, float *dB,
float *v, float *n, float *tc,
unsigned int *el,
int &index, int &elIndex, int &tcIndex, int grid, mat3 reflect,
bool invertNormal)
{
int startIndex = index / 3;
float tcFactor = 1.0f / grid;
for( int i = 0; i <= grid; i++ )
{
for( int j = 0 ; j <= grid; j++)
{
vec3 pt = reflect * evaluate(i,j,B,patch);
vec3 norm = reflect * evaluateNormal(i,j,B,dB,patch);
if( invertNormal )
norm = -norm;
v[index] = pt.x;
v[index+1] = pt.y;
v[index+2] = pt.z;
n[index] = norm.x;
n[index+1] = norm.y;
n[index+2] = norm.z;
tc[tcIndex] = i * tcFactor;
tc[tcIndex+1] = j * tcFactor;
index += 3;
tcIndex += 2;
}
}
for( int i = 0; i < grid; i++ )
{
int iStart = i * (grid+1) + startIndex;
int nextiStart = (i+1) * (grid+1) + startIndex;
for( int j = 0; j < grid; j++)
{
el[elIndex] = iStart + j;
el[elIndex+1] = nextiStart + j + 1;
el[elIndex+2] = nextiStart + j;
el[elIndex+3] = iStart + j;
el[elIndex+4] = iStart + j + 1;
el[elIndex+5] = nextiStart + j + 1;
elIndex += 6;
}
}
}
void VBOTeapot::getPatch( int patchNum, vec3 patch[][4], bool reverseV )
{
for( int u = 0; u < 4; u++) { // Loop in u direction
for( int v = 0; v < 4; v++ ) { // Loop in v direction
if( reverseV ) {
patch[u][v] = vec3(
Teapot::cpdata[Teapot::patchdata[patchNum][u*4+(3-v)]][0],
Teapot::cpdata[Teapot::patchdata[patchNum][u*4+(3-v)]][1],
Teapot::cpdata[Teapot::patchdata[patchNum][u*4+(3-v)]][2]
);
} else {
patch[u][v] = vec3(
Teapot::cpdata[Teapot::patchdata[patchNum][u*4+v]][0],
Teapot::cpdata[Teapot::patchdata[patchNum][u*4+v]][1],
Teapot::cpdata[Teapot::patchdata[patchNum][u*4+v]][2]
);
}
}
}
}
void VBOTeapot::computeBasisFunctions( float * B, float * dB, int grid ) {
float inc = 1.0f / grid;
for( int i = 0; i <= grid; i++ )
{
float t = i * inc;
float tSqr = t * t;
float oneMinusT = (1.0f - t);
float oneMinusT2 = oneMinusT * oneMinusT;
B[i*4 + 0] = oneMinusT * oneMinusT2;
B[i*4 + 1] = 3.0f * oneMinusT2 * t;
B[i*4 + 2] = 3.0f * oneMinusT * tSqr;
B[i*4 + 3] = t * tSqr;
dB[i*4 + 0] = -3.0f * oneMinusT2;
dB[i*4 + 1] = -6.0f * t * oneMinusT + 3.0f * oneMinusT2;
dB[i*4 + 2] = -3.0f * tSqr + 6.0f * t * oneMinusT;
dB[i*4 + 3] = 3.0f * tSqr;
}
}
vec3 VBOTeapot::evaluate( int gridU, int gridV, float *B, vec3 patch[][4] )
{
vec3 p(0.0f,0.0f,0.0f);
for( int i = 0; i < 4; i++) {
for( int j = 0; j < 4; j++) {
p += patch[i][j] * B[gridU*4+i] * B[gridV*4+j];
}
}
return p;
}
vec3 VBOTeapot::evaluateNormal( int gridU, int gridV, float *B, float *dB, vec3 patch[][4] )
{
vec3 du(0.0f,0.0f,0.0f);
vec3 dv(0.0f,0.0f,0.0f);
for( int i = 0; i < 4; i++) {
for( int j = 0; j < 4; j++) {
du += patch[i][j] * dB[gridU*4+i] * B[gridV*4+j];
dv += patch[i][j] * B[gridU*4+i] * dB[gridV*4+j];
}
}
return glm::normalize( glm::cross( du, dv ) );
}

View File

@ -1,52 +0,0 @@
#ifndef VBOTEAPOT_H
#define VBOTEAPOT_H
#include <GL/glew.h>
#include <GL/gl.h>
#include <glm/glm.hpp>
#include "ADrawable.h"
#include "Loader.h"
using glm::vec4;
using glm::vec3;
using glm::mat3;
using glm::mat4;
class VBOTeapot : public ADrawable
{
private:
GLuint VAO, VBO_position, VBO_normal, VBO_tex, IBO;
unsigned int faces;
void generatePatches(float * v, float * n, float *tc, unsigned int* el, int grid);
void buildPatchReflect(int patchNum,
float *B, float *dB,
float *v, float *n, float *, unsigned int *el,
int &index, int &elIndex, int &, int grid,
bool reflectX, bool reflectY);
void buildPatch(vec3 patch[][4],
float *B, float *dB,
float *v, float *n,float *, unsigned int *el,
int &index, int &elIndex, int &, int grid, mat3 reflect, bool invertNormal);
void getPatch( int patchNum, vec3 patch[][4], bool reverseV );
void computeBasisFunctions( float * B, float * dB, int grid );
vec3 evaluate( int gridU, int gridV, float *B, vec3 patch[][4] );
vec3 evaluateNormal( int gridU, int gridV, float *B, float *dB, vec3 patch[][4] );
void moveLid(int,float *,mat4);
public:
VBOTeapot(int grid, mat4 lidTransform);
// Inherited via ADrawable
virtual void draw(ShaderProgram * shader, glm::mat4x4 proj_matrix, glm::mat4x4 view_matrix) override;
virtual DrawableType getType() override;
};
#endif // VBOTEAPOT_H