diff --git a/src/camera/camera.cpp b/src/camera/camera.cpp index 0932e91..3d8d909 100644 --- a/src/camera/camera.cpp +++ b/src/camera/camera.cpp @@ -1,13 +1,17 @@ #include #include +#include #include -Camera::Camera(glm::vec3 pos, glm::vec3 up, float yaw, float pitch, float speed, - float sensitivity, CameraType type) - : pos(pos), worldUp(up), yaw(yaw), pitch(pitch), speed(speed), sensitivity(sensitivity), type(type) { - updateCameraVectors(); +Camera::Camera(glm::vec3 pos, glm::vec3 up, float yaw, float pitch, float radialAngle, float speed, float sensitivity) + : pos(pos), worldUp(up), yaw(yaw), pitch(pitch), radialAngle(radialAngle), speed(speed), sensitivity(sensitivity), type(CameraType::FREE) { } +Camera::Camera(glm::vec3 up, float coilCenter, float angularSpeed, float radialAngle, float sensitivity) + : worldUp(up), coilCenter(coilCenter), angularSpeed(angularSpeed), radialAngle(radialAngle), sensitivity(sensitivity), type(CameraType::SPINNING) { + updateCameraVectors(); + } + glm::mat4 Camera::getViewMatrix() { return glm::lookAt(pos, pos + front, up); } void Camera::processKeyboardInput(CameraMovement direction) { @@ -16,10 +20,13 @@ void Camera::processKeyboardInput(CameraMovement direction) { } float deltaTime = glfwGetTime() - lastTime; float velocity = speed * deltaTime; + float angularVelocity = angularSpeed * deltaTime; switch (type) { case CameraType::FREE: { switch (direction) { + case CameraMovement::STOP: + break; case CameraMovement::FORWARD: pos += front * velocity; break; @@ -33,14 +40,25 @@ void Camera::processKeyboardInput(CameraMovement direction) { pos += right * velocity; break; } - break; } case CameraType::SPINNING: { - + switch (direction) { + case CameraMovement::LEFT: + radialAngle += angularVelocity; + break; + case CameraMovement::RIGHT: + radialAngle -= angularVelocity; + break; + default: + break; + } } } lastTime = glfwGetTime(); + + // if (type == CameraType::SPINNING) + updateCameraVectors(); } void Camera::processMouseInput(GLFWwindow *window, double mouseX, double mouseY) { @@ -69,14 +87,33 @@ void Camera::processMouseInput(GLFWwindow *window, double mouseX, double mouseY) } +void Camera::setCameraType(CameraType type) { + this->type = type; + updateCameraVectors(); +} + +CameraType Camera::getCameraType() { return type; } + void Camera::updateCameraVectors() { glm::vec3 front; - front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch)); - front.y = sin(glm::radians(pitch)); - front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch)); - // front = glm::vec3(0, 0, 10); - this->front = glm::normalize(front); + switch (type) { + case CameraType::FREE: { + front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch)); + front.y = sin(glm::radians(pitch)); + front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch)); + break; + } + case CameraType::SPINNING: { + float x = (coilCenter + 10) * cos(radialAngle); + float z = (coilCenter + 10) * sin(radialAngle); + + pos = glm::vec3(x, 0, z); + front = -pos; + break; + } + } + this->front = glm::normalize(front); right = glm::normalize(glm::cross(front, worldUp)); up = glm::normalize(glm::cross(right, front)); } \ No newline at end of file diff --git a/src/camera/camera.hpp b/src/camera/camera.hpp index f552005..94db0b1 100644 --- a/src/camera/camera.hpp +++ b/src/camera/camera.hpp @@ -4,9 +4,15 @@ #include #include -enum CameraType { FREE, SPINNING }; +enum CameraType { FREE = 0, SPINNING }; + +inline CameraType& operator++ (CameraType& type) { + type = static_cast(type + 1 > 1? 0 : type + 1); + return type; +} enum CameraMovement { + STOP, FORWARD, BACKWARD, LEFT, @@ -14,21 +20,26 @@ enum CameraMovement { }; class Camera { - float pitch, yaw; + float pitch, yaw, radialAngle; float prevX = -1, prevY = -1; glm::vec3 pos, front, up, right, worldUp; CameraType type; float lastTime = -1; - float speed, sensitivity; + float speed, angularSpeed, sensitivity; + float coilCenter; public: - Camera(glm::vec3 pos, glm::vec3 up, float yaw, float pitch, float speed, float sensitivity, CameraType type); + Camera(glm::vec3 pos, glm::vec3 up, float yaw, float pitch, float radialAngle, float speed, float sensitivity); + Camera(glm::vec3 up, float coilCenter, float angularSpeed, float radialAngle, float sensitivity); glm::mat4 getViewMatrix(); - void processKeyboardInput(CameraMovement direction); + void processKeyboardInput(CameraMovement direction = CameraMovement::STOP); void processMouseInput(GLFWwindow *window, double offsetX, double offsetY); + void setCameraType(CameraType type); + CameraType getCameraType(); + private: void updateCameraVectors(); }; diff --git a/src/coil/coil.cpp b/src/coil/coil.cpp index b112950..a2a1107 100644 --- a/src/coil/coil.cpp +++ b/src/coil/coil.cpp @@ -35,9 +35,6 @@ void Coil::initGLBuffers() { glBindBuffer(GL_ARRAY_BUFFER, VBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); - // glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, - // GL_STATIC_DRAW); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0); glEnableVertexAttribArray(0); @@ -60,13 +57,12 @@ void Coil::render() { std::cerr << "Couldn't allocate memory for vertices." << std::endl; } fillVBO(vertices); + free(vertices); uint32_t indicesAmount = fillEBO(); glDrawElements(GL_TRIANGLES, indicesAmount, GL_UNSIGNED_INT, 0); glBindVertexArray(0); - - free(vertices); } void Coil::fillVBO(float *vertices) { uint64_t verticesSize = 0; @@ -115,12 +111,10 @@ uint32_t Coil::fillEBO() { currentIndex++; indices[currentIndex] = currentPointIndex + 1; currentIndex++; - std::cout << segment.getSlicePointsAmount() << " " << pointOfSlice << std::endl; // Second triangle indices[currentIndex] = currentPointIndex + 1; currentIndex++; - if (pointOfSlice == segment.getSlicePointsAmount() - 1) { indices[currentIndex] = currentPointIndex - (slicePointsAmount - 1); currentIndex++; diff --git a/src/coil/segment.cpp b/src/coil/segment.cpp index 7eeaef4..b9d37af 100644 --- a/src/coil/segment.cpp +++ b/src/coil/segment.cpp @@ -85,7 +85,7 @@ float *CoilSegment::calculateSlice() { float degreeByPoint = -180. / (sliceDetailization + 1); for (uint32_t i = 1; i <= sliceDetailization; i ++) { - uint32_t offset = FIELDS_IN_POINT * i; + uint32_t offset = 3 * i; slice[offset + 0] = round_to_precision(cos(radians(degreeByPoint * i)), 5); slice[offset + 1] = round_to_precision(sin(radians(degreeByPoint * i)), 5); @@ -93,7 +93,7 @@ float *CoilSegment::calculateSlice() { } // End of a slice - uint32_t endIndex = (size / sizeof(float) - FIELDS_IN_POINT); + uint32_t endIndex = (size / sizeof(float) - 3); slice[endIndex + 0] = round_to_precision(cos(radians(180.)), 5); slice[endIndex + 1] = round_to_precision(sin(radians(180.)), 5); slice[endIndex + 2] = 0; @@ -108,7 +108,7 @@ void CoilSegment::constructSegment(float *slice) { float daysDegree = day * degreesPerDay; vec3 daysPosition = vec3( cos(radians(daysDegree)) * coil->getRadius(), - height * daysDegree/360. + shift * height, + height * (float)day/getDaysAmount() + shift * height, sin(radians(daysDegree)) * coil->getRadius() ); diff --git a/src/main.cpp b/src/main.cpp index c62256d..0cc4418 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include @@ -30,10 +29,23 @@ void framebufferSizeCallback(GLFWwindow *window, int width, int height) { glViewport(10, 10, width-10, height-10); } -Camera cam = Camera(glm::vec3(0., 0., 0), glm::vec3(0., 1.,0.),-89, 0, 20, 0.1, CameraType::FREE); +float coilRadius = 50; +float coilWidth = 10; +// Camera cam = Camera(glm::vec3(0., 0., 70), glm::vec3(0., 1.,0.),-89, 0, 0, 20, 0.1); +Camera cam = Camera(glm::vec3(0, 1, 0), coilRadius + (coilWidth / 2), 1, 0, 0.1); -void processInput(GLFWwindow *window) { +void processClickInput(GLFWwindow* window, int key, int scancode, int action, int mods) +{ + if (key == GLFW_KEY_R && action == GLFW_PRESS) { + CameraType type = cam.getCameraType(); + ++type; + std::cout << type << std::endl; + cam.setCameraType(type); + } +} + +void processPressInput(GLFWwindow *window) { if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { glfwSetWindowShouldClose(window, true); return; @@ -57,7 +69,7 @@ void processMousePosCallback(GLFWwindow *window, double deltaX, double deltaY) { } int main () { - Coil coil = Coil(2024, 3, 10, 1, 50, 10); + Coil coil = Coil(2024, 1, 10, 2, coilRadius, coilWidth); srand(time(0)); glfwInit(); @@ -77,7 +89,8 @@ int main () { glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebufferSizeCallback); glfwSetCursorPosCallback(window, processMousePosCallback); - // glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_CAPTURED); + glfwSetKeyCallback(window, processClickInput); + // glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { cerr << "Failed to initialize GLAD." << endl; @@ -97,7 +110,7 @@ int main () { coil.initGLBuffers(); glEnable(GL_DEPTH_TEST); while(!glfwWindowShouldClose(window)) { - processInput(window); + processPressInput(window); glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); diff --git a/src/ui/ui.hpp b/src/ui/ui.hpp index 23302f0..60addee 100644 --- a/src/ui/ui.hpp +++ b/src/ui/ui.hpp @@ -1,4 +1,9 @@ +#ifndef COIL_UI_H +#define COIL_UI_H + class UI { void draw(); -}; \ No newline at end of file +}; + +#endif // COIL_UI_H \ No newline at end of file