build basic coil segment with some problems ;)

This commit is contained in:
leca 2025-03-02 21:57:05 +03:00
parent e7bfc4af89
commit dd3ee57fcb
18 changed files with 316 additions and 86 deletions

16
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/<executable file>",
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

View File

@ -3,6 +3,7 @@ project(TimeCoil)
include(FetchContent)
SET(CMAKE_BUILD_TYPE Debug)
SET(PROJECT_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp src/glad.c
${CMAKE_CURRENT_SOURCE_DIR}/src/coil/coil.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/coil/segment.cpp

View File

@ -1,2 +1,2 @@
#!/usr/bin/env bash
cd build && cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_C_FLAGS="-g -v -da -Q -O0" .. && make && ./TimeCoil > ../points.csv && python ../tools/build_3d.py ../points.csv
cd build && cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_C_FLAGS="-g -v -da -Q -O0" .. && make && ./TimeCoil # > ../points.csv && python ../tools/build_3d.py ../points.csv

View File

@ -1,10 +1,15 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 col;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
out vec3 frag_col;
void main()
{
frag_col = col;
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
gl_Position = projection * view * model * vec4(aPos, 1.0);
}

View File

@ -1,15 +1,18 @@
#include <cstdint>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <coil/coil.hpp>
#include <coil/segment.hpp>
#include <utils/utils.hpp>
Coil::Coil(uint32_t currentYear, uint32_t amountOfSegments)
: currentYear(currentYear) {
Coil::Coil(uint32_t currentYear, uint32_t amountOfSegments, uint32_t sliceDetalization, float radius, float width)
: radius(radius), width(width), currentYear(currentYear) {
for (short i = amountOfSegments / -2.; i < amountOfSegments / 2.; i++) {
uint32_t year = this->currentYear + i;
segments.push_back(CoilSegment(this, year, segmentHeight, i, 3,false));
segments.push_back(CoilSegment(this, year, segmentHeight, i, sliceDetalization,false));
}
};
@ -17,20 +20,47 @@ std::vector<CoilSegment> &Coil::getSegments() {
return segments;
}
double Coil::getWidth() {
float Coil::getWidth() {
return width;
}
void Coil::render() {
prepareShaders();
uint64_t VBO;
glGenBuffers(1, (unsigned int*) &(VBO));
glBindBuffer(GL_ARRAY_BUFFER, VBO);
for (CoilSegment segment : segments) {
double* vertices = segment.getVertices();
uint64_t verticesLength = segment.getVerticesLength();
glBufferData(GL_ARRAY_BUFFER, verticesLength, vertices, GL_STATIC_DRAW);
float Coil::getRadius() {
return radius;
}
void Coil::initGLBuffers() {
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
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);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*) (3 * sizeof(float)));
glEnableVertexAttribArray(1);
glBindVertexArray(0);
}
void Coil::render() {
glBindVertexArray(VAO);
std::cout << "Rendering a coil." << std::endl;
for (CoilSegment segment : segments) {
std::cout << "Rendering " << segment.getYear() << std::endl;
segment.fillVBO();
uint64_t indicesCount = segment.fillEBO(EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// glDrawArrays(GL_POINTS, 0, segment.getVerticesLength());
glDrawElements(GL_TRIANGLES_ADJACENCY, sizeof(unsigned int)*indicesCount, GL_UNSIGNED_INT, 0);
}
glBindVertexArray(0);
}

View File

@ -1,5 +1,6 @@
#include <vector>
#include "segment.hpp"
#include <glad/glad.h>
#ifndef COIL_HPP
#define COIL_HPP
@ -9,18 +10,22 @@
class Coil {
// distance between coil center and nearest to it coil edge
double radius;
float radius;
// distance between coil slices edges
double width;
double segmentHeight;
float width;
float segmentHeight;
uint32_t currentYear;
std::vector<CoilSegment> segments;
GLuint VBO, VAO, EBO;
public:
Coil(uint32_t currentYear, uint32_t amountOfSegments);
Coil(uint32_t currentYear, uint32_t amountOfSegments, uint32_t sliceDetalization, float radius, float width);
std::vector<CoilSegment> &getSegments();
double getWidth();
float getWidth();
float getRadius();
void render();
void initGLBuffers();
};
#endif // COIL_HPP

View File

@ -1,8 +1,10 @@
#include <glm/common.hpp>
#include <glm/ext/matrix_float4x4.hpp>
#include <glm/ext/matrix_transform.hpp>
#include <glm/trigonometric.hpp>
#include <cstring>
#include <iostream>
#include <fstream>
#include <glm/glm.hpp>
#include <cstdint>
@ -44,11 +46,12 @@ CoilSegment::CoilSegment(
void CoilSegment::init () {
verticesLength = DAYS_IN_YEAR * FIELDS_IN_POINT * (sliceDetalization+2);
vertices = (double *) calloc(verticesLength, sizeof(double));
vertices = (float *) calloc(verticesLength, sizeof(float));
if (vertices == NULL) {
cerr << "Allocation failed" << endl;
exit(-1);
}
calculate();
// cout << "Initialized CoilSegment"
// << "(year: " << year
// << ", height: " << height
@ -69,9 +72,19 @@ void CoilSegment::printVertices() {
}
double *CoilSegment::calculateSlice() {
uint32_t size = (/*start and end of a slize*/2 + sliceDetalization) * FIELDS_IN_POINT * sizeof(double);
double *slice = (double *)malloc(size);
float *CoilSegment::calculateSlice() {
uint32_t size = (/*start and end of a slize*/2 + sliceDetalization) * FIELDS_IN_POINT * sizeof(float);
float *slice = (float *)malloc(size);
mat4 transform = mat4(1.);
transform = glm::scale(
transform,
vec3(
coil->getWidth(),
10,
1
)
);
vec3 point = vec3(cos(0), sin(0), 0);
// Start of a slice
slice[0] = cos(0);
slice[1] = sin(0);
@ -80,7 +93,7 @@ double *CoilSegment::calculateSlice() {
slice[4] = 1;
slice[5] = 1;
double degreeByPoint = -180. / (sliceDetalization + 1);
float degreeByPoint = -180. / (sliceDetalization + 1);
for (uint32_t i = 1; i <= sliceDetalization; i ++) {
uint32_t offset = FIELDS_IN_POINT * i;
@ -94,7 +107,7 @@ double *CoilSegment::calculateSlice() {
}
// End of a slice
uint32_t endIndex = (size / sizeof(double) - FIELDS_IN_POINT);
uint32_t endIndex = (size / sizeof(float) - FIELDS_IN_POINT);
slice[endIndex + 0] = round_to_presicion(cos(radians(180.)), 5);
slice[endIndex + 1] = round_to_presicion(sin(radians(180.)), 5);
slice[endIndex + 2] = 0;
@ -105,17 +118,15 @@ double *CoilSegment::calculateSlice() {
return slice;
}
void CoilSegment::constructSegment(double *slice) {
double degreesPerDay = 360.f / DAYS_IN_YEAR;
void CoilSegment::constructSegment(float *slice) {
float degreesPerDay = 360.f / DAYS_IN_YEAR;
for (uint32_t day = 0; day < DAYS_IN_YEAR; day ++) {
float daysDegree = day * degreesPerDay;
// TODO: replace hardcoded value to coil->getWidth();
vec3 daysPosition = vec3(
cos(radians(daysDegree)) * 200,
cos(radians(daysDegree)) * coil->getRadius(),
10* daysDegree/360.,
sin(radians(daysDegree)) * 200
sin(radians(daysDegree)) * coil->getRadius()
);
mat4 transform = mat4(1.);
@ -150,14 +161,78 @@ void CoilSegment::constructSegment(double *slice) {
vertices[currentPointOffset + 1] = point.y;
vertices[currentPointOffset + 2] = point.z;
memcpy(
vertices + currentPointOffset + 3,
slice + slicePointOffset + 3,
3
);
vertices[currentPointOffset + 3] = 0.5;
vertices[currentPointOffset + 4] = 0.5;
vertices[currentPointOffset + 5] = 0.5;
// memcpy(
// vertices + currentPointOffset + 3,
// slice + slicePointOffset + 3,
// 3
// );
}
}
}
void CoilSegment::fillVBO() {
// printVertices();
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * verticesLength, vertices, GL_STATIC_DRAW);
}
uint64_t CoilSegment::fillEBO(uint64_t EBO) {
uint64_t indicesLength = 6 * (sliceDetalization+2) * DAYS_IN_YEAR;
uint64_t currentIndex = 0;
uint64_t* indices = (uint64_t*) calloc(indicesLength, sizeof(uint64_t));
for (uint64_t day = 0; day < DAYS_IN_YEAR; day ++) {
for (uint64_t pointOfSlice = 0; pointOfSlice < (sliceDetalization+2); pointOfSlice ++) {
// cout << currentIndex << endl;
uint64_t dayOffset = day * (sliceDetalization+2);
uint64_t offset = dayOffset + pointOfSlice;
//First triangle
indices[currentIndex] = offset;
currentIndex ++;
indices[currentIndex] = offset + (sliceDetalization+2);
currentIndex ++;
indices[currentIndex] = offset + 1;
currentIndex++;
//Second triangle
if (pointOfSlice % (sliceDetalization+1) == 0 && pointOfSlice != 0) {
indices[currentIndex] = offset - (sliceDetalization+2);
currentIndex++;
indices[currentIndex] = offset + 1;
currentIndex++;
indices[currentIndex] = offset;
} else {
indices[currentIndex] = offset + 1;
currentIndex++;
indices[currentIndex] = offset + (sliceDetalization+3);
currentIndex ++;
indices[currentIndex] = offset + (sliceDetalization+2);
}
currentIndex ++;
}
}
// for (uint64_t i = 0; i < indicesLength; i ++) {
// cout << indices[i] << ",";
// }
// cout << endl;
// cout << indicesLength << endl;
if (currentIndex != indicesLength) {
cerr << "currentIndex == indicesLength assertion failed" << endl;
cerr << currentIndex << " " << indicesLength << endl;
}
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned long) * indicesLength, indices, GL_STATIC_DRAW);
free(indices);
return indicesLength;
}
// void testEBO(uint64_t *EBO, uint64_t length) {
// for (uint64_t i = 0; i < length; i ++) {
// }
// }
// Sample layout of 2 slices with 3 points.
// |x |y |z |r |g |b | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
@ -165,45 +240,48 @@ void CoilSegment::constructSegment(double *slice) {
// | slice | slice |
//
void CoilSegment::calculate() {
double *slice = calculateSlice();
float *slice = calculateSlice();
constructSegment(slice);
exportSegmentToCsv();
}
double *CoilSegment::getVertices() {
return vertices;
}
float *CoilSegment::getVertices() { return vertices; }
uint64_t CoilSegment::getVerticesLength() {
return verticesLength;
}
uint64_t CoilSegment::getVerticesLength() { return verticesLength; }
uint16_t CoilSegment::getYear() { return year; }
void CoilSegment::render() {
}
void CoilSegment::exportSliceToCSV(float *slice) {
cout << "Exporting slice to csv" << endl;
std::ofstream outFile("./slice_export.csv");
void CoilSegment::exportSliceToCSV(double *slice) {
cout << "x,y,z" << endl;
outFile << "x,y,z" << endl;
for (uint32_t i = 0; i < sliceDetalization+2; i ++) {
uint32_t offset = FIELDS_IN_POINT * i;
cout
outFile
<< slice[offset + 0] << ","
<< slice[offset + 1] << ",0" << endl;
}
}
void CoilSegment::exportSegmentToCsv() {
cout << "x,y,z" << endl;
cout << "Exporting segment to csv" << endl;
std::ofstream outFile("./segment_export.csv");
outFile << "x,y,z" << endl;
for (uint16_t day = 0; day < DAYS_IN_YEAR; day ++) {
uint64_t dayOffset = day * (2+sliceDetalization) * FIELDS_IN_POINT;
for (uint32_t point_in_slice = 0; point_in_slice < (2+sliceDetalization); point_in_slice ++) {
uint32_t sliceOffset = point_in_slice * FIELDS_IN_POINT;
cout << vertices[dayOffset + sliceOffset + 0] << ","
outFile << vertices[dayOffset + sliceOffset + 0] << ","
<< vertices[dayOffset + sliceOffset + 1] << ","
<< vertices[dayOffset + sliceOffset + 2] << endl;
}

View File

@ -13,23 +13,26 @@ class CoilSegment {
int shift;
uint32_t sliceDetalization;
bool isLeap;
double *vertices;
float *vertices;
uint64_t verticesLength;
void init();
double* calculateSlice();
void constructSegment(double *slice);
float* calculateSlice();
void constructSegment(float *slice);
public:
CoilSegment(Coil* coil, uint16_t year, uint16_t height, int shift, uint32_t sliceDetalization, bool isLeap);
void calculate();
void printVertices();
double *getVertices();
float *getVertices();
uint64_t getVerticesLength();
void printSlice(double *slice);
void exportSliceToCSV(double *slice);
void printSlice(float *slice);
void exportSliceToCSV(float *slice);
void exportSegmentToCsv();
void render();
void fillVBO();
uint64_t fillEBO(uint64_t EBO);
uint16_t getYear();
};
#endif // COIL_SEGMENT_HPP

View File

@ -3,11 +3,23 @@
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "coil/coil.hpp"
#include "coil/segment.hpp"
#include <coil/coil.hpp>
#include <coil/segment.hpp>
#include <utils/utils.hpp>
#define RAND_0_1 ((double)rand()) / RAND_MAX
using std::cerr;
using std::cout;
using std::endl;
#define RAND_0_1 ((float)rand()) / RAND_MAX
#define DISPLAY_WIDTH 800
#define DISPLAY_HEIGHT 600
void framebufferSizeCallback(GLFWwindow *window, int width, int height) {
glViewport(10, 10, width-10, height-10);
@ -19,28 +31,108 @@ void processInput(GLFWwindow *window) {
}
}
void updateVertices(double (&vertices)[24]) {
// if (RAND_0_1 < 0.99f) return;
double a[] = {
-1.0f, 1.0f, 0.0f, RAND_0_1, RAND_0_1, RAND_0_1, // upper left
-1.0f, -1.0f, 0.0f, RAND_0_1, RAND_0_1, RAND_0_1, // bottom left
1.0f, 1.0f, 0.0f, RAND_0_1, RAND_0_1, RAND_0_1, // upper right
1.0f, -1.0f, 0.0f, RAND_0_1, RAND_0_1, RAND_0_1 // bottom right
};
// vertices = a;
memcpy(vertices, a, sizeof(a));
}
int main () {
Coil c = Coil(2025, 1);
Coil c = Coil(2025, 1, 5, 50, 10);
srand(time(0));
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
GLFWwindow *window = glfwCreateWindow(DISPLAY_WIDTH, DISPLAY_HEIGHT, "TimeCoil", NULL, NULL);
if (window == NULL) {
cerr << "Failed to create GLFW window." << endl;
gracefulExit(-1);
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebufferSizeCallback);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
cerr << "Failed to initialize GLAD." << endl;
gracefulExit(-1);
}
uint64_t shaderProgram = prepareShaders();
glUseProgram(shaderProgram);
glm::mat4 projection = glm::mat4(1.0f);
projection = glm::perspective(glm::radians(50.0f), (float)DISPLAY_WIDTH / (float)DISPLAY_HEIGHT, 0.001f, 2000.0f);
unsigned int projectionLoc = glGetUniformLocation(shaderProgram, "projection");
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
c.initGLBuffers();
// float vertices[] = {
// // positions // texture coords
// 05.5f, 0.5f, 0.0f, 1.0f, 1.0f, // top right
// 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // bottom right
// -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, // bottom left
// -0.5f, 0.5f, 0.0f, 0.0f, 1.0f // top left
// };
// unsigned int indices[] = {
// 0, 1, 3, // first triangle
// 1, 2, 3 // second triangle
// };
// unsigned int VBO, VAO, EBO;
// glGenVertexArrays(1, &VAO);
// glGenBuffers(1, &VBO);
// glGenBuffers(1, &EBO);
// glBindVertexArray(VAO);
// glBindBuffer(GL_ARRAY_BUFFER, VBO);
// glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
// glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// // position attribute
// glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
// glEnableVertexAttribArray(0);
// // texture coord attribute
// glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
// glEnableVertexAttribArray(1);
while(!glfwWindowShouldClose(window)) {
processInput(window);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glm::mat4 model = glm::mat4(1.0f);
glm::mat4 view = glm::mat4(1.0f);
model = glm::rotate(model, glm::radians(5.0f), glm::vec3(1.0f, 0.0f, 0.0f));
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -89.0f));
unsigned int modelLoc = glGetUniformLocation(shaderProgram, "model");
unsigned int viewLoc = glGetUniformLocation(shaderProgram, "view");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
c.render();
// glBindVertexArray(VAO);
// glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window);
glfwPollEvents();
}
// CoilSegment cs = CoilSegment(10, 0, false);
for (auto segment : c.getSegments()) {
// segment.printVertices();
}
// for (auto segment : c.getSegments()) {
// // segment.printVertices();
// }
// cs.printVertices();
return 0;
gracefulExit(0);
// srand(time(0));
// glfwInit();
// glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
@ -66,10 +158,10 @@ int main () {
// unsigned int shaderProgram = prepareShaders();
// double *vertices = cs.getVertices();
// float *vertices = cs.getVertices();
// unsigned int amountOfVerticies = cs.getVerticesLength();
// std::cout << amountOfVerticies << std::endl;
// // double vertices[] = {
// // float vertices[] = {
// // -1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, // upper left
// // -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left
// // 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // upper right
@ -92,10 +184,10 @@ int main () {
// glBufferData(GL_ARRAY_BUFFER, amountOfVerticies, vertices, GL_STATIC_DRAW);
// // glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// glVertexAttribPointer(0, 3, GL_DOUBLE, GL_FALSE, 6 * sizeof(double), (void*)0);
// glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
// glEnableVertexAttribArray(0);
// glVertexAttribPointer(1, 3, GL_DOUBLE, GL_FALSE, 6 * sizeof(double), (void*) (3 * sizeof(double)));
// glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*) (3 * sizeof(float)));
// glEnableVertexAttribArray(1);
// glBindVertexArray(0);
@ -108,8 +200,8 @@ int main () {
// glUseProgram(shaderProgram);
// // double time = glfwGetTime();
// // double greenValue = sin(time);
// // float time = glfwGetTime();
// // float greenValue = sin(time);
// // int vertexColorLocation = glGetUniformLocation(shaderProgram, "MyColor");

View File

@ -5,7 +5,7 @@
#include <fstream>
#include <iostream>
double round_to_presicion( double value, int precision ) {
float round_to_presicion( float value, int precision ) {
const int adjustment = pow(10,precision);
return floor( value*(adjustment) + 0.5 )/adjustment;
}
@ -32,7 +32,7 @@ void gracefulExit(int code) {
exit(code);
}
unsigned int prepareShaders() {
uint64_t prepareShaders() {
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

View File

@ -3,10 +3,10 @@
#include <cmath>
#include <string>
double round_to_presicion( double value, int precision );
float round_to_presicion( float value, int precision );
std::string readFile(std::string path);
void gracefulExit(int code);
unsigned int prepareShaders();
uint64_t prepareShaders();