wip camera type switch, fix incorrect slice calculation

This commit is contained in:
2025-11-23 19:13:43 +03:00
parent 8b84258c1a
commit ef2a366e8e
6 changed files with 93 additions and 33 deletions

View File

@@ -1,13 +1,17 @@
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <camera/camera.hpp> #include <camera/camera.hpp>
#include <glm/geometric.hpp>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
Camera::Camera(glm::vec3 pos, glm::vec3 up, float yaw, float pitch, float speed, Camera::Camera(glm::vec3 pos, glm::vec3 up, float yaw, float pitch, float radialAngle, float speed, float sensitivity)
float sensitivity, CameraType type) : pos(pos), worldUp(up), yaw(yaw), pitch(pitch), radialAngle(radialAngle), speed(speed), sensitivity(sensitivity), type(CameraType::FREE) {
: pos(pos), worldUp(up), yaw(yaw), pitch(pitch), speed(speed), sensitivity(sensitivity), type(type) {
updateCameraVectors();
} }
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); } glm::mat4 Camera::getViewMatrix() { return glm::lookAt(pos, pos + front, up); }
void Camera::processKeyboardInput(CameraMovement direction) { void Camera::processKeyboardInput(CameraMovement direction) {
@@ -16,10 +20,13 @@ void Camera::processKeyboardInput(CameraMovement direction) {
} }
float deltaTime = glfwGetTime() - lastTime; float deltaTime = glfwGetTime() - lastTime;
float velocity = speed * deltaTime; float velocity = speed * deltaTime;
float angularVelocity = angularSpeed * deltaTime;
switch (type) { switch (type) {
case CameraType::FREE: { case CameraType::FREE: {
switch (direction) { switch (direction) {
case CameraMovement::STOP:
break;
case CameraMovement::FORWARD: case CameraMovement::FORWARD:
pos += front * velocity; pos += front * velocity;
break; break;
@@ -33,14 +40,25 @@ void Camera::processKeyboardInput(CameraMovement direction) {
pos += right * velocity; pos += right * velocity;
break; break;
} }
break;
} }
case CameraType::SPINNING: { case CameraType::SPINNING: {
switch (direction) {
case CameraMovement::LEFT:
radialAngle += angularVelocity;
break;
case CameraMovement::RIGHT:
radialAngle -= angularVelocity;
break;
default:
break;
}
} }
} }
lastTime = glfwGetTime(); lastTime = glfwGetTime();
// if (type == CameraType::SPINNING)
updateCameraVectors();
} }
void Camera::processMouseInput(GLFWwindow *window, double mouseX, double mouseY) { 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() { void Camera::updateCameraVectors() {
glm::vec3 front; 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)); right = glm::normalize(glm::cross(front, worldUp));
up = glm::normalize(glm::cross(right, front)); up = glm::normalize(glm::cross(right, front));
} }

View File

@@ -4,9 +4,15 @@
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <glm/glm.hpp> #include <glm/glm.hpp>
enum CameraType { FREE, SPINNING }; enum CameraType { FREE = 0, SPINNING };
inline CameraType& operator++ (CameraType& type) {
type = static_cast<CameraType>(type + 1 > 1? 0 : type + 1);
return type;
}
enum CameraMovement { enum CameraMovement {
STOP,
FORWARD, FORWARD,
BACKWARD, BACKWARD,
LEFT, LEFT,
@@ -14,21 +20,26 @@ enum CameraMovement {
}; };
class Camera { class Camera {
float pitch, yaw; float pitch, yaw, radialAngle;
float prevX = -1, prevY = -1; float prevX = -1, prevY = -1;
glm::vec3 pos, front, up, right, worldUp; glm::vec3 pos, front, up, right, worldUp;
CameraType type; CameraType type;
float lastTime = -1; float lastTime = -1;
float speed, sensitivity; float speed, angularSpeed, sensitivity;
float coilCenter;
public: 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(); glm::mat4 getViewMatrix();
void processKeyboardInput(CameraMovement direction); void processKeyboardInput(CameraMovement direction = CameraMovement::STOP);
void processMouseInput(GLFWwindow *window, double offsetX, double offsetY); void processMouseInput(GLFWwindow *window, double offsetX, double offsetY);
void setCameraType(CameraType type);
CameraType getCameraType();
private: private:
void updateCameraVectors(); void updateCameraVectors();
}; };

View File

@@ -35,9 +35,6 @@ void Coil::initGLBuffers() {
glBindBuffer(GL_ARRAY_BUFFER, VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); 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); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
@@ -60,13 +57,12 @@ void Coil::render() {
std::cerr << "Couldn't allocate memory for vertices." << std::endl; std::cerr << "Couldn't allocate memory for vertices." << std::endl;
} }
fillVBO(vertices); fillVBO(vertices);
free(vertices);
uint32_t indicesAmount = fillEBO(); uint32_t indicesAmount = fillEBO();
glDrawElements(GL_TRIANGLES, indicesAmount, glDrawElements(GL_TRIANGLES, indicesAmount,
GL_UNSIGNED_INT, 0); GL_UNSIGNED_INT, 0);
glBindVertexArray(0); glBindVertexArray(0);
free(vertices);
} }
void Coil::fillVBO(float *vertices) { void Coil::fillVBO(float *vertices) {
uint64_t verticesSize = 0; uint64_t verticesSize = 0;
@@ -115,12 +111,10 @@ uint32_t Coil::fillEBO() {
currentIndex++; currentIndex++;
indices[currentIndex] = currentPointIndex + 1; indices[currentIndex] = currentPointIndex + 1;
currentIndex++; currentIndex++;
std::cout << segment.getSlicePointsAmount() << " " << pointOfSlice << std::endl;
// Second triangle // Second triangle
indices[currentIndex] = currentPointIndex + 1; indices[currentIndex] = currentPointIndex + 1;
currentIndex++; currentIndex++;
if (pointOfSlice == segment.getSlicePointsAmount() - 1) { if (pointOfSlice == segment.getSlicePointsAmount() - 1) {
indices[currentIndex] = currentPointIndex - (slicePointsAmount - 1); indices[currentIndex] = currentPointIndex - (slicePointsAmount - 1);
currentIndex++; currentIndex++;

View File

@@ -85,7 +85,7 @@ float *CoilSegment::calculateSlice() {
float degreeByPoint = -180. / (sliceDetailization + 1); float degreeByPoint = -180. / (sliceDetailization + 1);
for (uint32_t i = 1; i <= sliceDetailization; i ++) { 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 + 0] = round_to_precision(cos(radians(degreeByPoint * i)), 5);
slice[offset + 1] = round_to_precision(sin(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 // 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 + 0] = round_to_precision(cos(radians(180.)), 5);
slice[endIndex + 1] = round_to_precision(sin(radians(180.)), 5); slice[endIndex + 1] = round_to_precision(sin(radians(180.)), 5);
slice[endIndex + 2] = 0; slice[endIndex + 2] = 0;
@@ -108,7 +108,7 @@ void CoilSegment::constructSegment(float *slice) {
float daysDegree = day * degreesPerDay; float daysDegree = day * degreesPerDay;
vec3 daysPosition = vec3( vec3 daysPosition = vec3(
cos(radians(daysDegree)) * coil->getRadius(), cos(radians(daysDegree)) * coil->getRadius(),
height * daysDegree/360. + shift * height, height * (float)day/getDaysAmount() + shift * height,
sin(radians(daysDegree)) * coil->getRadius() sin(radians(daysDegree)) * coil->getRadius()
); );

View File

@@ -1,7 +1,6 @@
#include <glad/glad.h> #include <glad/glad.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <cstring> #include <cstring>
#include <cmath>
#include <cstdlib> #include <cstdlib>
#include <ctime> #include <ctime>
#include <iostream> #include <iostream>
@@ -30,10 +29,23 @@ void framebufferSizeCallback(GLFWwindow *window, int width, int height) {
glViewport(10, 10, width-10, height-10); 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) { if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true); glfwSetWindowShouldClose(window, true);
return; return;
@@ -57,7 +69,7 @@ void processMousePosCallback(GLFWwindow *window, double deltaX, double deltaY) {
} }
int main () { int main () {
Coil coil = Coil(2024, 3, 10, 1, 50, 10); Coil coil = Coil(2024, 1, 10, 2, coilRadius, coilWidth);
srand(time(0)); srand(time(0));
glfwInit(); glfwInit();
@@ -77,7 +89,8 @@ int main () {
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebufferSizeCallback); glfwSetFramebufferSizeCallback(window, framebufferSizeCallback);
glfwSetCursorPosCallback(window, processMousePosCallback); glfwSetCursorPosCallback(window, processMousePosCallback);
// glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_CAPTURED); glfwSetKeyCallback(window, processClickInput);
// glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
cerr << "Failed to initialize GLAD." << endl; cerr << "Failed to initialize GLAD." << endl;
@@ -97,7 +110,7 @@ int main () {
coil.initGLBuffers(); coil.initGLBuffers();
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
while(!glfwWindowShouldClose(window)) { while(!glfwWindowShouldClose(window)) {
processInput(window); processPressInput(window);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

View File

@@ -1,4 +1,9 @@
#ifndef COIL_UI_H
#define COIL_UI_H
class UI { class UI {
void draw(); void draw();
}; };
#endif // COIL_UI_H