crane completed

This commit is contained in:
Hugo Willaume 2018-11-14 21:47:36 +09:00
parent f020c3dd81
commit 0b6282a21b
10 changed files with 259 additions and 19 deletions

View File

@ -53,4 +53,38 @@ public:
{
_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,6 +154,7 @@
<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="MyGLWindow.cpp" />
<ClCompile Include="Source.cpp" />
<ClCompile Include="Viewer.cpp" />
@ -172,6 +173,7 @@
<ClInclude Include="imgui\imstb_rectpack.h" />
<ClInclude Include="imgui\imstb_textedit.h" />
<ClInclude Include="imgui\imstb_truetype.h" />
<ClInclude Include="LineSegment.h" />
<ClInclude Include="Loader.h" />
<ClInclude Include="ModelView.h" />
<ClInclude Include="MyGLWindow.h" />

View File

@ -57,6 +57,9 @@
<ClCompile Include="WireCube.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="LineSegment.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="ColorCube.h">
@ -110,6 +113,9 @@
<ClInclude Include="ADrawable.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="LineSegment.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="simple.frag">

View File

@ -83,7 +83,7 @@ void ColorCube::draw(ShaderProgram *shader, glm::mat4x4 pv)
glBindVertexArray(_vaoHandle);
int size;
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
glm::mat4 mvpMatrix = pv * _model.getMatrix();
glm::mat4 mvpMatrix = pv * _model.getMatrix();
glUniformMatrix4fv(shader->uniform("mvp"), 1, GL_FALSE, glm::value_ptr(mvpMatrix));
glDrawElements(GL_TRIANGLES, size / sizeof(GLushort), GL_UNSIGNED_SHORT, 0);

View File

@ -9,7 +9,8 @@ enum DrawableType
{
COLOR_CUBE,
CHECKERED_FLOOR,
WIRE_CUBE
WIRE_CUBE,
LINE_SEGMENT
};
class IDrawable {
@ -17,6 +18,12 @@ 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 pv) = 0;
virtual DrawableType getType() = 0;

View File

@ -0,0 +1,69 @@
#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 pv)
{
_model.glPushMatrix();
effectTransformations();
glm::mat4 mvpMatrix = pv * _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

@ -0,0 +1,32 @@
#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 pv) override;
DrawableType getType() override;
~LineSegment();
};

View File

@ -61,9 +61,43 @@ void MyGlWindow::setup()
{
_shaderProgram = new ShaderProgram();
_shaderProgram->initFromFiles("simple.vert", "simple.frag");
_drawables.emplace_back(new CheckeredFloor(50, 16));
_drawables.emplace_back(new WireCube());
_drawables.back()->addTranslation(glm::vec4(0, 1, 0, 0));
_static_drawables.emplace_back(new CheckeredFloor(50, 16));
_static_drawables.emplace_back(new WireCube());
_static_drawables.back()->addTranslation(glm::vec4(0, 1.01, 0, 0));
_static_drawables.emplace_back(new WireCube());
_static_drawables.back()->addTranslation(glm::vec4(0, 3.01, 0, 0));
_static_drawables.emplace_back(new WireCube());
_static_drawables.back()->addTranslation(glm::vec4(0, 5.01, 0, 0));
_static_drawables.emplace_back(new WireCube());
_static_drawables.back()->addTranslation(glm::vec4(0, 7.01, 0, 0));
_crane_boom.emplace_back(new WireCube());
_crane_boom.back()->addTranslation(glm::vec4(0, 9.01, 0, 0));
_crane_boom.emplace_back(new WireCube());
_crane_boom.back()->addTranslation(glm::vec4(-2, 9.01, 0, 0));
_crane_boom.emplace_back(new WireCube());
_crane_boom.back()->addTranslation(glm::vec4(2, 9.01, 0, 0));
_crane_boom.emplace_back(new WireCube());
_crane_boom.back()->addTranslation(glm::vec4(4, 9.01, 0, 0));
_crane_boom.emplace_back(new WireCube());
_crane_boom.back()->addTranslation(glm::vec4(6, 9.01, 0, 0));
_crane_boom.push_back(new ColorCube());
_crane_boom.back()->addTranslation(glm::vec4(8, 7.01, 0, 0));
_crane_boom.emplace_back(new WireCube());
_crane_boom.back()->addTranslation(glm::vec4(8, 9.01, 0, 0));
_crane_boom.emplace_back(new LineSegment());
_crane_boom.back()->addTranslation(glm::vec4(8, 8.01, 0, 0));
}
void MyGlWindow::setupCubeBuffer()
@ -122,6 +156,23 @@ void MyGlWindow::setupRectBuffer()
glBindVertexArray(0);
}
void MyGlWindow::setViewFromBox(glm::vec3 &eye, glm::vec3 &look, glm::vec3 &up)
{
glm::vec4 cube_base_position = { 8, 7.01, 0 , 1.0f};
Model model;
cube_base_position.x = 7 - (floatValues["box_horizontal"] * 6);
cube_base_position.y = 7 - (floatValues["box_vertical"] * 6);
model.glPushMatrix();
model.glTranslate(-8, 2, 0);
model.glRotate(floatValues["boom_rotation"], 0, -1, 0);
model.glTranslate(8, -2, 0);
eye = cube_base_position * model.getMatrix();
}
void MyGlWindow::draw()
{
glClearColor(_bgColor[0], _bgColor[1], _bgColor[2], 1);
@ -134,14 +185,41 @@ void MyGlWindow::draw()
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);
if (view_from_box)
setViewFromBox(eye, look, up);
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);
_shaderProgram->addUniform("mvp");
for (ADrawable *drawable : _drawables)
{
for (ADrawable *drawable : _static_drawables)
drawable->draw(_shaderProgram, projection * view);
for (ADrawable *drawable : _crane_boom)
{
glm::vec4 t = drawable->translateToPivot(glm::vec3(0, 9.01, 0));
drawable->addRotation(glm::vec4(0, 1, 0, floatValues["boom_rotation"]));
drawable->addTranslation(t);
if (drawable->getType() == COLOR_CUBE)
drawable->addTranslation(glm::vec4(floatValues["box_horizontal"] * -6.01,
floatValues["box_vertical"] * -6.01, 0 ,0));
if (drawable->getType() == LINE_SEGMENT)
{
drawable->addTranslation(glm::vec4(floatValues["box_horizontal"] * -6.01 , floatValues["box_vertical"] * -4.01, 0, 0));
drawable->addScaling(glm::vec4(0, floatValues["box_vertical"] * 4.01, 0, 0));
}
if (!view_from_box || (drawable->getType() != COLOR_CUBE && drawable->getType() != LINE_SEGMENT))
drawable->draw(_shaderProgram, projection * view);
if (drawable->getType() == COLOR_CUBE)
drawable->removeLastTransformations(1);
else if (drawable->getType() == LINE_SEGMENT)
drawable->removeLastTransformations(2);
drawable->removeLastTransformations(3);
}
_shaderProgram->disable();
@ -149,7 +227,7 @@ void MyGlWindow::draw()
void MyGlWindow::AddDrawable(ADrawable *d)
{
_drawables.push_back(d);
_static_drawables.push_back(d);
}
void MyGlWindow::resize(int w, int h)

View File

@ -11,6 +11,7 @@
#include "Viewer.h"
#include "CheckeredFloor.h"
#include "WireCube.h"
#include "LineSegment.h"
struct vertexAttr {
GLfloat posX, posY, posZ;
@ -26,7 +27,8 @@ public:
void draw();
void setBgColor(float bgColor[3]);
float cubeRotation = .0f;
std::map<std::string, float> floatValues;
bool view_from_box = false;
void AddDrawable(ADrawable *);
void resize(int w, int h);
Viewer viewer;
@ -43,10 +45,12 @@ private:
GLuint _vaoHandle;
GLuint _iboHandle;
std::vector<ADrawable *> _static_drawables;
std::vector<ADrawable *> _drawables;
std::vector<ADrawable *> _crane_boom;
void setup();
void setupRectBuffer();
void setupCubeBuffer();
void setViewFromBox(glm::vec3 &eye, glm::vec3 &look, glm::vec3 &up);
};

View File

@ -173,21 +173,29 @@ int loop(GLFWwindow *window)
if (ImGui::Begin("First Window"))
{
ImGui::SetWindowSize(ImVec2(350, 150));
ImGui::SetWindowPos(ImVec2(20, 20));
static float box_horizontal = .0f;
ImGui::SliderFloat("Box Horizontal", &box_horizontal, 0, 1);
glWin.floatValues["box_horizontal"] = box_horizontal;
ImGui::Text("WannaQuit ?");
if (ImGui::Button("Yes"))
glfwSetWindowShouldClose(window, GLFW_TRUE);
static float box_vertical = .0f;
ImGui::SliderFloat("Box Vertical", &box_vertical, 0, 1);
glWin.floatValues["box_vertical"] = box_vertical;
static float rotation = .0f;
ImGui::SliderFloat("Volume", &rotation, 0.0f, 360.0f);
glWin.cubeRotation = rotation;
static float boom_rotation = .0f;
ImGui::SliderFloat("Boom rotation", &boom_rotation, -180, 180);
glWin.floatValues["boom_rotation"] = boom_rotation;
static float bgColor[3] = { .0f, .0f, .0f };
static int box_view;
ImGui::RadioButton("Default", &box_view, 0); ImGui::SameLine();
ImGui::RadioButton("Box", &box_view, 1);
glWin.view_from_box = box_view;
static float bgColor[3] = { .3f, .3f, .3f };
ImGui::ColorEdit3("Background", bgColor, 0);
glWin.setBgColor(bgColor);
ImGui::End();
}