From 0b6282a21bc69285fc88bffc587273d7229ff03d Mon Sep 17 00:00:00 2001 From: Hugo Willaume Date: Wed, 14 Nov 2018 21:47:36 +0900 Subject: [PATCH] crane completed --- BaseGLProject/ADrawable.h | 34 ++++++++ BaseGLProject/BaseGLProject.vcxproj | 2 + BaseGLProject/BaseGLProject.vcxproj.filters | 6 ++ BaseGLProject/ColorCube.cpp | 2 +- BaseGLProject/IDrawable.h | 9 ++- BaseGLProject/LineSegment.cpp | 69 ++++++++++++++++ BaseGLProject/LineSegment.h | 32 ++++++++ BaseGLProject/MyGLWindow.cpp | 90 +++++++++++++++++++-- BaseGLProject/MyGLWindow.h | 8 +- BaseGLProject/Source.cpp | 26 +++--- 10 files changed, 259 insertions(+), 19 deletions(-) create mode 100644 BaseGLProject/LineSegment.cpp create mode 100644 BaseGLProject/LineSegment.h diff --git a/BaseGLProject/ADrawable.h b/BaseGLProject/ADrawable.h index 6aaf2d0..a2038d6 100644 --- a/BaseGLProject/ADrawable.h +++ b/BaseGLProject/ADrawable.h @@ -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; + } + }; \ No newline at end of file diff --git a/BaseGLProject/BaseGLProject.vcxproj b/BaseGLProject/BaseGLProject.vcxproj index ad07ebf..a5dbd77 100644 --- a/BaseGLProject/BaseGLProject.vcxproj +++ b/BaseGLProject/BaseGLProject.vcxproj @@ -154,6 +154,7 @@ + @@ -172,6 +173,7 @@ + diff --git a/BaseGLProject/BaseGLProject.vcxproj.filters b/BaseGLProject/BaseGLProject.vcxproj.filters index 9bded70..cefa8ea 100644 --- a/BaseGLProject/BaseGLProject.vcxproj.filters +++ b/BaseGLProject/BaseGLProject.vcxproj.filters @@ -57,6 +57,9 @@ Source Files + + Source Files + @@ -110,6 +113,9 @@ Header Files + + Header Files + diff --git a/BaseGLProject/ColorCube.cpp b/BaseGLProject/ColorCube.cpp index 14dc411..0872d18 100644 --- a/BaseGLProject/ColorCube.cpp +++ b/BaseGLProject/ColorCube.cpp @@ -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); diff --git a/BaseGLProject/IDrawable.h b/BaseGLProject/IDrawable.h index 7628c55..254f661 100644 --- a/BaseGLProject/IDrawable.h +++ b/BaseGLProject/IDrawable.h @@ -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; diff --git a/BaseGLProject/LineSegment.cpp b/BaseGLProject/LineSegment.cpp new file mode 100644 index 0000000..2b1989f --- /dev/null +++ b/BaseGLProject/LineSegment.cpp @@ -0,0 +1,69 @@ +#include "LineSegment.h" +#define GLM_ENABLE_EXPERIMENTAL +#include "glm/gtx/string_cast.hpp" + +void LineSegment::setup() +{ + std::vector seg_vertices = + { { .0, -1.0f, .0}, {.0, 1.0f, .0} }; + + std::vector 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() +{ +} diff --git a/BaseGLProject/LineSegment.h b/BaseGLProject/LineSegment.h new file mode 100644 index 0000000..5b61d26 --- /dev/null +++ b/BaseGLProject/LineSegment.h @@ -0,0 +1,32 @@ +#pragma once + +#pragma once + +#include +#include +#include +#include +#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(); +}; \ No newline at end of file diff --git a/BaseGLProject/MyGLWindow.cpp b/BaseGLProject/MyGLWindow.cpp index 0e5cc96..2036037 100644 --- a/BaseGLProject/MyGLWindow.cpp +++ b/BaseGLProject/MyGLWindow.cpp @@ -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) diff --git a/BaseGLProject/MyGLWindow.h b/BaseGLProject/MyGLWindow.h index 6fa8de2..63b069c 100644 --- a/BaseGLProject/MyGLWindow.h +++ b/BaseGLProject/MyGLWindow.h @@ -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 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 _static_drawables; - std::vector _drawables; + std::vector _crane_boom; void setup(); void setupRectBuffer(); void setupCubeBuffer(); + void setViewFromBox(glm::vec3 &eye, glm::vec3 &look, glm::vec3 &up); }; diff --git a/BaseGLProject/Source.cpp b/BaseGLProject/Source.cpp index 5edb3ff..4f8c3ac 100644 --- a/BaseGLProject/Source.cpp +++ b/BaseGLProject/Source.cpp @@ -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(); }