works
This commit is contained in:
parent
cdf01052da
commit
e2a692a912
6 changed files with 98 additions and 9988 deletions
|
@ -7,7 +7,7 @@ SOURCES := $(shell find -name "*.cpp")
|
|||
|
||||
all: $(HANDIN)
|
||||
|
||||
$(HANDIN): src/HW2a.cpp README.md
|
||||
$(HANDIN): src/HW2a.cpp examples README.md
|
||||
$(ZIP) -r $@ $^
|
||||
|
||||
clean:
|
||||
|
|
|
@ -1,3 +1,13 @@
|
|||
# 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
11
assignment-2a/examples/test.obj
Normal file
11
assignment-2a/examples/test.obj
Normal 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
|
BIN
assignment-2a/examples/test.png
Normal file
BIN
assignment-2a/examples/test.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 30 KiB |
|
@ -25,7 +25,7 @@
|
|||
|
||||
// repetitive of the debug flag in the shader loading code, included here
|
||||
// for clarity only
|
||||
#define DEBUG_ON 1
|
||||
#define DEBUG_ON 0
|
||||
|
||||
// This file contains the code that reads the shaders from their files and
|
||||
// compiles them
|
||||
|
@ -207,26 +207,79 @@ void init(void) {
|
|||
exit(1);
|
||||
}
|
||||
|
||||
string contents, line;
|
||||
f >> contents;
|
||||
f.close();
|
||||
float max_x = -INFINITY;
|
||||
float min_x = INFINITY;
|
||||
float max_y = -INFINITY;
|
||||
float min_y = INFINITY;
|
||||
|
||||
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;
|
||||
}
|
||||
string line;
|
||||
while (std::getline(f, line)) {
|
||||
std::istringstream iss(line);
|
||||
string tmp;
|
||||
|
||||
istringstream iss2(line);
|
||||
string a;
|
||||
std::getline(iss2, a, ',');
|
||||
std::getline(iss, tmp, ' ');
|
||||
if (tmp == "v") {
|
||||
float x, y, z;
|
||||
|
||||
if (a == "v") {
|
||||
printf("hello %s\n", line.c_str());
|
||||
std::getline(iss, tmp, ' ');
|
||||
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 {
|
||||
// clang-format off
|
||||
// set up some hard-coded colors and geometry
|
||||
|
@ -257,19 +310,20 @@ void init(void) {
|
|||
glGenVertexArrays(1, vao);
|
||||
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
|
||||
// position and color data
|
||||
glGenBuffers(1, &buffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, buffer);
|
||||
cout << vertices.size() << " " << colors.size() << endl;
|
||||
glBufferData(
|
||||
GL_ARRAY_BUFFER, vertices.size() + colors.size(), vertices.data(),
|
||||
GL_ARRAY_BUFFER, vertices_size + colors_size, vertices.data(),
|
||||
GL_STATIC_DRAW
|
||||
);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size(), vertices.data());
|
||||
glBufferSubData(
|
||||
GL_ARRAY_BUFFER, vertices.size(), colors.size(), colors.data()
|
||||
);
|
||||
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;
|
||||
|
|
Loading…
Reference in a new issue