This commit is contained in:
Michael Zhang 2023-04-08 23:39:07 -05:00
parent d327791de9
commit cdf01052da
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
2 changed files with 10047 additions and 27 deletions

File diff suppressed because it is too large Load diff

View file

@ -5,14 +5,17 @@
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <x86intrin.h>
#include <xmmintrin.h>
#include <fstream>
#include <iostream>
#include <istream>
#include <optional>
#include <sstream>
#include <string>
#include <vector>
// Unfortunately this must be included first because it does some annoying check
// that vomits if GLFW has already been included (dumb)
@ -97,6 +100,12 @@ GLdouble TRANSLATE_DELTA_X = 2.0 / window_width;
GLdouble TRANSLATE_DELTA_Y = -2.0 / window_height;
GLdouble translate_x = 0.0, translate_y = 0.0;
vector<ColorType3D> colors;
vector<FloatType2D> vertices;
bool read_file = false;
string file_path;
// Matrices
GLfloat rotation_matrix[16], translation_matrix[16], scale_matrix[16];
@ -181,37 +190,68 @@ static void cursor_pos_callback(GLFWwindow *window, double xpos, double ypos) {
//----------------------------------------------------------------------------
void init(void) {
ColorType3D colors[NVERTICES];
FloatType2D vertices[NVERTICES];
GLuint vao[1], buffer, program, location1, location2;
zeroInitGlfloats(rotation_matrix, 16);
zeroInitGlfloats(translation_matrix, 16);
zeroInitGlfloats(scale_matrix, 16);
// clang-format off
// set up some hard-coded colors and geometry
// this part can be customized to read in an object description from a file
colors[0].r = 1; colors[0].g = 1; colors[0].b = 1; // white
colors[1].r = 1; colors[1].g = 0; colors[1].b = 0; // red
colors[2].r = 1; colors[2].g = 0; colors[2].b = 0; // red
colors[3].r = 1; colors[3].g = 1; colors[3].b = 1; // white
colors[4].r = 0; colors[4].g = 0; colors[4].b = 1; // blue
colors[5].r = 0; colors[5].g = 0; colors[5].b = 1; // blue
colors[6].r = 1; colors[6].g = 1; colors[6].b = 1; // white
colors[7].r = 0; colors[7].g = 1; colors[7].b = 1; // cyan
colors[8].r = 0; colors[8].g = 1; colors[8].b = 1; // cyan
if (read_file) {
// C++ is the most disgusting language ever invented and we should get rid
// of it as fast as possible. Just look at this mess:
vertices[0].x = 0; vertices[0].y = 0.25; // center
vertices[1].x = 0.25; vertices[1].y = 0.5; // upper right
vertices[2].x = -0.25; vertices[2].y = 0.5; // upper left
vertices[3].x = 0; vertices[3].y = 0.25; // center (again)
vertices[4].x = 0.25; vertices[4].y = -0.5; // low-lower right
vertices[5].x = 0.5; vertices[5].y = -0.25; // mid-lower right
vertices[6].x = 0; vertices[6].y = 0.25; // center (again)
vertices[7].x = -0.5; vertices[7].y = -0.25; // low-lower left
vertices[8].x = -0.25; vertices[8].y = -0.5; // mid-lower left
// clang-format on
ifstream f(file_path);
if (!f.is_open()) {
printf("Could not open file %s\n", file_path.c_str());
exit(1);
}
string contents, line;
f >> contents;
f.close();
istringstream iss(contents);
for (std::string line; std::getline(iss, line);) {
printf("line: '%s'\n", line.c_str());
if (line.find_first_not_of("\t\n ") == string::npos) {
continue;
}
istringstream iss2(line);
string a;
std::getline(iss2, a, ',');
if (a == "v") {
printf("hello %s\n", line.c_str());
}
}
} else {
// clang-format off
// set up some hard-coded colors and geometry
// this part can be customized to read in an object description from a file
colors.push_back({ .r = 1, .g = 1, .b = 1 }); // white
colors.push_back({ .r = 1, .g = 0, .b = 0 }); // red
colors.push_back({ .r = 1, .g = 0, .b = 0 }); // red
colors.push_back({ .r = 1, .g = 1, .b = 1 }); // white
colors.push_back({ .r = 0, .g = 0, .b = 1 }); // blue
colors.push_back({ .r = 0, .g = 0, .b = 1 }); // blue
colors.push_back({ .r = 1, .g = 1, .b = 1 }); // white
colors.push_back({ .r = 0, .g = 1, .b = 1 }); // cyan
colors.push_back({ .r = 0, .g = 1, .b = 1 }); // cyan
vertices.push_back({ .x = 0, .y = 0.25 }); // center
vertices.push_back({ .x = 0.25, .y = 0.5 }); // upper right
vertices.push_back({ .x = -0.25, .y = 0.5 }); // upper left
vertices.push_back({ .x = 0, .y = 0.25 }); // center (again)
vertices.push_back({ .x = 0.25, .y = -0.5 }); // low-lower right
vertices.push_back({ .x = 0.5, .y = -0.25 }); // mid-lower right
vertices.push_back({ .x = 0, .y = 0.25 }); // center (again)
vertices.push_back({ .x = -0.5, .y = -0.25 }); // low-lower left
vertices.push_back({ .x = -0.25, .y = -0.5 }); // mid-lower left
// clang-format on
}
// Create and bind a vertex array object
glGenVertexArrays(1, vao);
@ -221,12 +261,15 @@ void init(void) {
// position and color data
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
cout << vertices.size() << " " << colors.size() << endl;
glBufferData(
GL_ARRAY_BUFFER, sizeof(vertices) + sizeof(colors), vertices,
GL_ARRAY_BUFFER, vertices.size() + colors.size(), vertices.data(),
GL_STATIC_DRAW
);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices), sizeof(colors), colors);
glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size(), vertices.data());
glBufferSubData(
GL_ARRAY_BUFFER, vertices.size(), colors.size(), colors.data()
);
// Define the names of the shader files
std::stringstream vshader, fshader;
@ -261,9 +304,21 @@ void init(void) {
//----------------------------------------------------------------------------
int main(int argc, char **argv) {
char c;
int i;
GLFWwindow *window;
while ((c = getopt(argc, argv, "abc:")) != -1)
switch (c) {
default:
exit(1);
}
if (argc > optind) {
read_file = true;
file_path = argv[optind];
}
// Define the error callback function
glfwSetErrorCallback(error_callback);