Basic segment settings and generation
This commit is contained in:
17
src/coil/coil.cpp
Normal file
17
src/coil/coil.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "coil.hpp"
|
||||
|
||||
Coil::Coil(uint32_t currentYear, uint32_t amountOfSegments)
|
||||
: 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));
|
||||
}
|
||||
};
|
||||
|
||||
std::vector<CoilSegment> &Coil::getSegments() {
|
||||
return segments;
|
||||
}
|
||||
|
||||
double Coil::getWidth() {
|
||||
return width;
|
||||
}
|
||||
25
src/coil/coil.hpp
Normal file
25
src/coil/coil.hpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <vector>
|
||||
#include "segment.hpp"
|
||||
|
||||
#ifndef COIL_HPP
|
||||
#define COIL_HPP
|
||||
|
||||
// Coil segment is a one turn of a coil
|
||||
// Coil slice is a set of points that define slice of coil
|
||||
|
||||
class Coil {
|
||||
// distance between coil center and nearest to it coil edge
|
||||
double radius;
|
||||
// distance between coil slices edges
|
||||
double width;
|
||||
double segmentHeight;
|
||||
uint32_t currentYear;
|
||||
std::vector<CoilSegment> segments;
|
||||
|
||||
public:
|
||||
Coil(uint32_t currentYear, uint32_t amountOfSegments);
|
||||
std::vector<CoilSegment> &getSegments();
|
||||
double getWidth();
|
||||
};
|
||||
|
||||
#endif // COIL_HPP
|
||||
225
src/coil/segment.cpp
Normal file
225
src/coil/segment.cpp
Normal file
@@ -0,0 +1,225 @@
|
||||
#include "segment.hpp"
|
||||
#include "coil.hpp"
|
||||
#include "glm/ext/matrix_float4x4.hpp"
|
||||
#include "glm/ext/matrix_transform.hpp"
|
||||
#include "glm/trigonometric.hpp"
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <glm/glm.hpp>
|
||||
#include <cstdint>
|
||||
|
||||
// x, y, z, r, g, b
|
||||
#define FIELDS_IN_POINT 6
|
||||
#define DAYS_IN_YEAR (isLeap? 366.0f : 365.0f)
|
||||
|
||||
using glm::vec4;
|
||||
using glm::vec3;
|
||||
using glm::mat4;
|
||||
using glm::radians;
|
||||
using glm::rotate;
|
||||
using glm::sin;
|
||||
using glm::cos;
|
||||
using glm::translate;
|
||||
|
||||
using std::endl;
|
||||
using std::cout;
|
||||
using std::cerr;
|
||||
|
||||
CoilSegment::CoilSegment(
|
||||
Coil* coil,
|
||||
uint16_t year,
|
||||
uint16_t height,
|
||||
int shift,
|
||||
uint32_t sliceDetalization,
|
||||
bool isLeap
|
||||
) :
|
||||
coil(coil), year(year),
|
||||
height(height), shift(shift),
|
||||
sliceDetalization(sliceDetalization), isLeap(isLeap) {
|
||||
init();
|
||||
calculate();
|
||||
}
|
||||
|
||||
void CoilSegment::init () {
|
||||
verticesLength = DAYS_IN_YEAR * FIELDS_IN_POINT * (sliceDetalization+2);
|
||||
vertices = (double *) calloc(verticesLength, sizeof(double));
|
||||
if (vertices == NULL) {
|
||||
cerr << "Allocation failed" << endl;
|
||||
exit(-1);
|
||||
}
|
||||
// cout << "Initialized CoilSegment"
|
||||
// << "(year: " << year
|
||||
// << ", height: " << height
|
||||
// << ", shift: " << shift
|
||||
// << ", isLeap: " << isLeap <<")" << endl;
|
||||
}
|
||||
|
||||
void CoilSegment::printVertices() {
|
||||
for (uint16_t day = 0; day < DAYS_IN_YEAR; day ++) {
|
||||
cout << "Day " << day << ", " <<
|
||||
"x: " << vertices[FIELDS_IN_POINT * day + 0] << ", " <<
|
||||
"y: " << vertices[FIELDS_IN_POINT * day + 1] << ", " <<
|
||||
"z: " << vertices[FIELDS_IN_POINT * day + 2] << ", " <<
|
||||
"r: " << vertices[FIELDS_IN_POINT * day + 3] << ", " <<
|
||||
"g: " << vertices[FIELDS_IN_POINT * day + 4] << ", " <<
|
||||
"b: " << vertices[FIELDS_IN_POINT * day + 5] << endl;
|
||||
}
|
||||
}
|
||||
|
||||
double round_to_presicion( double value, int precision ) {
|
||||
const int adjustment = pow(10,precision);
|
||||
return floor( value*(adjustment) + 0.5 )/adjustment;
|
||||
}
|
||||
|
||||
double *CoilSegment::calculateSlice() {
|
||||
uint32_t size = (/*start and end of a slize*/2 + sliceDetalization) * FIELDS_IN_POINT * sizeof(double);
|
||||
double *slice = (double *)malloc(size);
|
||||
// Start of a slice
|
||||
slice[0] = cos(0);
|
||||
slice[1] = sin(0);
|
||||
slice[2] = 0;
|
||||
slice[3] = 1;
|
||||
slice[4] = 1;
|
||||
slice[5] = 1;
|
||||
|
||||
double degreeByPoint = -180. / (sliceDetalization + 1);
|
||||
|
||||
for (uint32_t i = 1; i <= sliceDetalization; i ++) {
|
||||
uint32_t offset = FIELDS_IN_POINT * i;
|
||||
|
||||
slice[offset + 0] = round_to_presicion(cos(radians(degreeByPoint * i)), 5);
|
||||
slice[offset + 1] = round_to_presicion(sin(radians(degreeByPoint * i)), 5);
|
||||
slice[offset + 2] = 0;
|
||||
slice[offset + 3] = 1;
|
||||
slice[offset + 4] = 1;
|
||||
slice[offset + 5] = 1;
|
||||
}
|
||||
|
||||
// End of a slice
|
||||
uint32_t endIndex = (size / sizeof(double) - 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;
|
||||
slice[endIndex + 3] = 1;
|
||||
slice[endIndex + 4] = 1;
|
||||
slice[endIndex + 5] = 1;
|
||||
|
||||
return slice;
|
||||
}
|
||||
|
||||
void CoilSegment::constructSegment(double *slice) {
|
||||
|
||||
double degreesPerDay = 360.f / DAYS_IN_YEAR;
|
||||
|
||||
for (uint32_t day = 0; day < DAYS_IN_YEAR; day ++) {
|
||||
float daysDegree = day * degreesPerDay;
|
||||
// TODO: replace 10 to coil->getWidth();
|
||||
vec3 daysPosition = vec3(
|
||||
cos(radians(daysDegree)) * 200,
|
||||
10* daysDegree/360.,
|
||||
sin(radians(daysDegree)) * 200
|
||||
);
|
||||
|
||||
mat4 transform = mat4(1.);
|
||||
// For some reason I cannot combine transform and rotation to one singe matrix.
|
||||
// TODO: investigate and fix
|
||||
// transform = translate(
|
||||
// transform,
|
||||
// daysPosition
|
||||
// );
|
||||
|
||||
transform = rotate(
|
||||
transform,
|
||||
radians(daysDegree),
|
||||
vec3(
|
||||
0.0,
|
||||
1.0,
|
||||
0.0
|
||||
)
|
||||
);
|
||||
|
||||
for (uint32_t slicePoint = 0; slicePoint < (2 + sliceDetalization); slicePoint ++) {
|
||||
uint32_t slicePointOffset = FIELDS_IN_POINT * slicePoint;
|
||||
vec4 point(
|
||||
slice[slicePointOffset + 0],
|
||||
slice[slicePointOffset + 1],
|
||||
slice[slicePointOffset + 2],
|
||||
1
|
||||
);
|
||||
|
||||
point = point*transform ;
|
||||
uint64_t currentPointOffset = day * (2+sliceDetalization) * FIELDS_IN_POINT + slicePointOffset;
|
||||
vertices[currentPointOffset + 0] = point.x + daysPosition.x;
|
||||
vertices[currentPointOffset + 1] = point.y + daysPosition.y;
|
||||
vertices[currentPointOffset + 2] = point.z + daysPosition.z;
|
||||
|
||||
memcpy(
|
||||
vertices + currentPointOffset + 3,
|
||||
slice + slicePointOffset + 3,
|
||||
3
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 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
|
||||
// | point | point | point | point | point | point |
|
||||
// | slice | slice |
|
||||
//
|
||||
void CoilSegment::calculate() {
|
||||
double *slice = calculateSlice();
|
||||
|
||||
constructSegment(slice);
|
||||
exportSegmentToCsv();
|
||||
|
||||
}
|
||||
|
||||
double *CoilSegment::getVertices() {
|
||||
return vertices;
|
||||
}
|
||||
|
||||
uint16_t CoilSegment::getVerticesLength() {
|
||||
return verticesLength;
|
||||
}
|
||||
|
||||
void CoilSegment::printSlice(double *slice) {
|
||||
for (uint32_t i = 0; i < sliceDetalization+2; i ++) {
|
||||
uint32_t offset = FIELDS_IN_POINT * i;
|
||||
cout << "Point ("
|
||||
<< slice[offset + 0] << ", "
|
||||
<< slice[offset + 1] << ", "
|
||||
<< slice[offset + 2] << ", "
|
||||
<< ") rgb("
|
||||
<< slice[offset + 3] << ", "
|
||||
<< slice[offset + 4] << ", "
|
||||
<< slice[offset + 5] << ")"
|
||||
<< endl;
|
||||
}
|
||||
}
|
||||
|
||||
void CoilSegment::exportSliceToCSV(double *slice) {
|
||||
cout << "x,y,z" << endl;
|
||||
|
||||
for (uint32_t i = 0; i < sliceDetalization+2; i ++) {
|
||||
uint32_t offset = FIELDS_IN_POINT * i;
|
||||
cout
|
||||
<< slice[offset + 0] << ","
|
||||
<< slice[offset + 1] << ",0" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
void CoilSegment::exportSegmentToCsv() {
|
||||
cout << "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] << ","
|
||||
<< vertices[dayOffset + sliceOffset + 1] << ","
|
||||
<< vertices[dayOffset + sliceOffset + 2] << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
34
src/coil/segment.hpp
Normal file
34
src/coil/segment.hpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef COIL_SEGMENT_HPP
|
||||
#define COIL_SEGMENT_HPP
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
class Coil;
|
||||
|
||||
class CoilSegment {
|
||||
Coil *coil;
|
||||
uint16_t year;
|
||||
uint16_t height;
|
||||
// Number of a segments, counting from the central (which is 0)
|
||||
int shift;
|
||||
uint32_t sliceDetalization;
|
||||
bool isLeap;
|
||||
double *vertices;
|
||||
uint32_t verticesLength;
|
||||
|
||||
void init();
|
||||
double* calculateSlice();
|
||||
void constructSegment(double *slice);
|
||||
|
||||
public:
|
||||
CoilSegment(Coil* coil, uint16_t year, uint16_t height, int shift, uint32_t sliceDetalization, bool isLeap);
|
||||
void calculate();
|
||||
void printVertices();
|
||||
double *getVertices();
|
||||
uint16_t getVerticesLength();
|
||||
void printSlice(double *slice);
|
||||
void exportSliceToCSV(double *slice);
|
||||
void exportSegmentToCsv();
|
||||
};
|
||||
|
||||
#endif // COIL_SEGMENT_HPP
|
||||
150
src/main.cpp
150
src/main.cpp
@@ -6,7 +6,10 @@
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
|
||||
#define RAND_0_1 ((float)rand()) / RAND_MAX
|
||||
#include "coil/coil.hpp"
|
||||
#include "coil/segment.hpp"
|
||||
|
||||
#define RAND_0_1 ((double)rand()) / RAND_MAX
|
||||
|
||||
void framebufferSizeCallback(GLFWwindow *window, int width, int height) {
|
||||
glViewport(10, 10, width-10, height-10);
|
||||
@@ -44,9 +47,9 @@ std::string readFile(std::string path) {
|
||||
return content;
|
||||
}
|
||||
|
||||
void updateVertices(float (&vertices)[24]) {
|
||||
void updateVertices(double (&vertices)[24]) {
|
||||
// if (RAND_0_1 < 0.99f) return;
|
||||
float a[] = {
|
||||
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
|
||||
@@ -107,89 +110,102 @@ unsigned int prepareShaders() {
|
||||
}
|
||||
|
||||
int main () {
|
||||
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
|
||||
Coil c = Coil(2025, 1);
|
||||
|
||||
GLFWwindow *window = glfwCreateWindow(800, 600, "TimeCoil", NULL, NULL);
|
||||
if (window == NULL) {
|
||||
std::cerr << "Failed to create GLFW window." << std::endl;
|
||||
gracefulExit(-1);
|
||||
// CoilSegment cs = CoilSegment(10, 0, false);
|
||||
for (auto segment : c.getSegments()) {
|
||||
// segment.printVertices();
|
||||
}
|
||||
// cs.printVertices();
|
||||
return 0;
|
||||
// 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
|
||||
|
||||
glfwMakeContextCurrent(window);
|
||||
glfwSetFramebufferSizeCallback(window, framebufferSizeCallback);
|
||||
// GLFWwindow *window = glfwCreateWindow(800, 600, "TimeCoil", NULL, NULL);
|
||||
// if (window == NULL) {
|
||||
// std::cerr << "Failed to create GLFW window." << std::endl;
|
||||
// gracefulExit(-1);
|
||||
// }
|
||||
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
std::cerr << "Failed to initialize GLAD." << std::endl;
|
||||
return -1;
|
||||
}
|
||||
// glfwMakeContextCurrent(window);
|
||||
// glfwSetFramebufferSizeCallback(window, framebufferSizeCallback);
|
||||
|
||||
unsigned int shaderProgram = prepareShaders();
|
||||
// if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
// std::cerr << "Failed to initialize GLAD." << std::endl;
|
||||
// return -1;
|
||||
// }
|
||||
|
||||
float vertices[] = {
|
||||
-1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, // upper left
|
||||
-1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left
|
||||
1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, // upper right
|
||||
1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f // bottom right
|
||||
};
|
||||
unsigned int indices[] = {
|
||||
1, 2, 3,
|
||||
1, 2, 0
|
||||
};
|
||||
unsigned int VBO, VAO, EBO;
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glGenBuffers(1, &VBO);
|
||||
glGenBuffers(1, &EBO);
|
||||
// unsigned int shaderProgram = prepareShaders();
|
||||
|
||||
glBindVertexArray(VAO);
|
||||
// double *vertices = cs.getVertices();
|
||||
// unsigned int amountOfVerticies = cs.getVerticesLength();
|
||||
// std::cout << amountOfVerticies << std::endl;
|
||||
// // double 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
|
||||
// // 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f // bottom right
|
||||
// // };
|
||||
// // unsigned int indices[] = {
|
||||
// // 1, 2, 3,
|
||||
// // 1, 2, 0
|
||||
// // };
|
||||
// unsigned int VBO, VAO, EBO;
|
||||
// glGenVertexArrays(1, &VAO);
|
||||
// glGenBuffers(1, &VBO);
|
||||
// // glGenBuffers(1, &EBO);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
// glBindVertexArray(VAO);
|
||||
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
||||
// glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
// // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
// glBufferData(GL_ARRAY_BUFFER, amountOfVerticies, vertices, GL_STATIC_DRAW);
|
||||
// // glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*) (3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
// glVertexAttribPointer(0, 3, GL_DOUBLE, GL_FALSE, 6 * sizeof(double), (void*)0);
|
||||
// glEnableVertexAttribArray(0);
|
||||
|
||||
glBindVertexArray(0);
|
||||
// glVertexAttribPointer(1, 3, GL_DOUBLE, GL_FALSE, 6 * sizeof(double), (void*) (3 * sizeof(double)));
|
||||
// glEnableVertexAttribArray(1);
|
||||
|
||||
// glBindVertexArray(0);
|
||||
|
||||
while(!glfwWindowShouldClose(window)) {
|
||||
processInput(window);
|
||||
// while(!glfwWindowShouldClose(window)) {
|
||||
// processInput(window);
|
||||
|
||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
// glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||
// glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glUseProgram(shaderProgram);
|
||||
// glUseProgram(shaderProgram);
|
||||
|
||||
// float time = glfwGetTime();
|
||||
// float greenValue = sin(time);
|
||||
// // double time = glfwGetTime();
|
||||
// // double greenValue = sin(time);
|
||||
|
||||
|
||||
// int vertexColorLocation = glGetUniformLocation(shaderProgram, "MyColor");
|
||||
// glUniform4f(vertexColorLocation, 0, greenValue, 0, 1);
|
||||
glBindVertexArray(VAO);
|
||||
// // int vertexColorLocation = glGetUniformLocation(shaderProgram, "MyColor");
|
||||
// // glUniform4f(vertexColorLocation, 0, greenValue, 0, 1);
|
||||
// glBindVertexArray(VAO);
|
||||
|
||||
updateVertices(vertices);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
// // updateVertices(vertices);
|
||||
// glBufferData(GL_ARRAY_BUFFER, amountOfVerticies, vertices, GL_DYNAMIC_DRAW);
|
||||
// // glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
// glDrawArrays(GL_LINE_STRIP_ADJACENCY, -30, amountOfVerticies);
|
||||
|
||||
|
||||
glBindVertexArray(0);
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
// glBindVertexArray(0);
|
||||
// glfwSwapBuffers(window);
|
||||
// glfwPollEvents();
|
||||
// }
|
||||
|
||||
glDeleteVertexArrays(1, &VAO);
|
||||
glDeleteBuffers(1, &VBO);
|
||||
glDeleteProgram(shaderProgram);
|
||||
gracefulExit(0);
|
||||
// glDeleteVertexArrays(1, &VAO);
|
||||
// glDeleteBuffers(1, &VBO);
|
||||
// glDeleteProgram(shaderProgram);
|
||||
// gracefulExit(0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user