This commit is contained in:
Michael Zhang 2023-04-09 00:21:23 -05:00
parent cdf01052da
commit e2a692a912
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
6 changed files with 98 additions and 9988 deletions

View file

@ -7,7 +7,7 @@ SOURCES := $(shell find -name "*.cpp")
all: $(HANDIN) all: $(HANDIN)
$(HANDIN): src/HW2a.cpp README.md $(HANDIN): src/HW2a.cpp examples README.md
$(ZIP) -r $@ $^ $(ZIP) -r $@ $^
clean: clean:

View file

@ -1,3 +1,13 @@
# Assignment 2A # Assignment 2A
Should compile on CSE labs machines. Compiles but does not run on CSE labs machines due to OpenGL 3.2 missing.
Try with `examples/test.obj`, run the program with
cmake -B build
make -C build
./build/HW2a examples/test.obj
This test has a few triangles in it. Other obj files _should_ work in theory
![](examples/test.png)

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,11 @@
v 2 3 0
v 1.5 4 0
v 3 2 0
v 1.5 4 0
v 3 2 0
v 5 3.4 0
v 3 2 0
v 5 3.4 0
v 6 5.9 0

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

View file

@ -25,7 +25,7 @@
// repetitive of the debug flag in the shader loading code, included here // repetitive of the debug flag in the shader loading code, included here
// for clarity only // for clarity only
#define DEBUG_ON 1 #define DEBUG_ON 0
// This file contains the code that reads the shaders from their files and // This file contains the code that reads the shaders from their files and
// compiles them // compiles them
@ -207,26 +207,79 @@ void init(void) {
exit(1); exit(1);
} }
string contents, line; float max_x = -INFINITY;
f >> contents; float min_x = INFINITY;
f.close(); float max_y = -INFINITY;
float min_y = INFINITY;
istringstream iss(contents); string line;
for (std::string line; std::getline(iss, line);) { while (std::getline(f, line)) {
printf("line: '%s'\n", line.c_str()); std::istringstream iss(line);
if (line.find_first_not_of("\t\n ") == string::npos) { string tmp;
continue;
}
istringstream iss2(line); std::getline(iss, tmp, ' ');
string a; if (tmp == "v") {
std::getline(iss2, a, ','); float x, y, z;
if (a == "v") { std::getline(iss, tmp, ' ');
printf("hello %s\n", line.c_str()); x = std::stof(tmp);
std::getline(iss, tmp, ' ');
y = std::stof(tmp);
std::getline(iss, tmp, ' ');
z = std::stof(tmp);
// printf("(%f, %f, %f)\n", x, y, z);
max_x = max(x, max_x);
max_y = max(y, max_y);
min_x = min(x, min_x);
min_y = min(y, min_y);
vertices.push_back({.x = x, .y = y});
} }
} }
// Scale it a little bit so it fits in frame
float obj_scale_x = 1.0 / (max_x - min_x);
float obj_scale_y = 1.0 / (max_x - min_x);
for (int i = 0; i < vertices.size(); ++i) {
vertices[i].x = vertices[i].x * obj_scale_x - 1.0;
vertices[i].y = vertices[i].y * obj_scale_y - 1.0;
}
// Assign some random colors to it
const float delta_r = 0.4, delta_g = 0.7, delta_b = -0.5;
float r = 0.5, g = 0.7, b = 0.8;
for (auto _ : vertices) {
colors.push_back({.r = r, .g = g, .b = b});
r += delta_r;
g += delta_g;
b += delta_b;
while (r > 1.0)
r -= 1.0;
while (r < 0.0)
r += 1.0;
while (g > 1.0)
g -= 1.0;
while (g < 0.0)
g += 1.0;
while (b > 1.0)
b -= 1.0;
while (b < 0.0)
b += 1.0;
}
for (int i = 0; i < vertices.size(); ++i) {
auto &vertex = vertices[i];
auto &color = colors[i];
printf(
"vertex (%f, %f) is color (%f, %f, %f)\n", vertex.x, vertex.y,
color.r, color.g, color.b
);
}
} else { } else {
// clang-format off // clang-format off
// set up some hard-coded colors and geometry // set up some hard-coded colors and geometry
@ -257,19 +310,20 @@ void init(void) {
glGenVertexArrays(1, vao); glGenVertexArrays(1, vao);
glBindVertexArray(vao[0]); glBindVertexArray(vao[0]);
const uint32_t vertices_size = vertices.size() * sizeof(FloatType2D);
const uint32_t colors_size = colors.size() * sizeof(ColorType3D);
// printf("%d , %d\n", vertices_size, colors_size);
// Create and initialize a buffer object large enough to hold both vertex // Create and initialize a buffer object large enough to hold both vertex
// position and color data // position and color data
glGenBuffers(1, &buffer); glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer);
cout << vertices.size() << " " << colors.size() << endl;
glBufferData( glBufferData(
GL_ARRAY_BUFFER, vertices.size() + colors.size(), vertices.data(), GL_ARRAY_BUFFER, vertices_size + colors_size, vertices.data(),
GL_STATIC_DRAW GL_STATIC_DRAW
); );
glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size(), vertices.data()); glBufferSubData(GL_ARRAY_BUFFER, 0, vertices_size, vertices.data());
glBufferSubData( glBufferSubData(GL_ARRAY_BUFFER, vertices_size, colors_size, colors.data());
GL_ARRAY_BUFFER, vertices.size(), colors.size(), colors.data()
);
// Define the names of the shader files // Define the names of the shader files
std::stringstream vshader, fshader; std::stringstream vshader, fshader;