From 273ae90ea38eb004564e31294dfbaa8c8ef2cdc2 Mon Sep 17 00:00:00 2001 From: vlad Date: Thu, 23 Mar 2023 19:31:48 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BF=D0=BE=D0=B4=D0=B3=D1=80=D1=83=D0=B7=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=BC=D0=B0=D1=82=D0=B5=D1=80=D0=B8=D0=B0=D0=BB=D0=BE?= =?UTF-8?q?=D0=B2=20(=D1=81=D0=B5=D0=B9=D1=87=D0=B0=D1=81=20=D1=82=D0=BE?= =?UTF-8?q?=D0=BB=D1=8C=D0=BA=D0=BE=20=D1=86=D0=B2=D0=B5=D1=82)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lab2/res/main.vsh | 3 +- lab2/src/graphics/MeshLoader.cpp | 58 +++++++++++++++++++++++++++++--- lab2/src/main.cpp | 16 ++++----- 3 files changed, 64 insertions(+), 13 deletions(-) diff --git a/lab2/res/main.vsh b/lab2/res/main.vsh index 378109b..749688f 100644 --- a/lab2/res/main.vsh +++ b/lab2/res/main.vsh @@ -2,6 +2,7 @@ layout (location = 0) in vec3 v_position; layout (location = 1) in vec3 v_normal; +layout (location = 2) in vec3 v_color; out vec4 a_color; uniform mat4 projview; @@ -17,6 +18,6 @@ void main(){ // потом [0.5 ... 1] l = (l + 2) / 3; - a_color = vec4(vec3(0.5, 0, 1) * l, 1); + a_color = vec4(v_color * l, 1); } diff --git a/lab2/src/graphics/MeshLoader.cpp b/lab2/src/graphics/MeshLoader.cpp index f8de1f8..15f9194 100644 --- a/lab2/src/graphics/MeshLoader.cpp +++ b/lab2/src/graphics/MeshLoader.cpp @@ -20,13 +20,45 @@ enum ParserState { struct MeshPoint { glm::vec3 point; glm::vec3 normal; + glm::vec3 color; }; +struct Material { + std::string name; + glm::vec3 color; +}; + +static void loadMaterials(std::vector& materials, const std::string &filename) { + std::ifstream file(filename, std::ios::in); + + std::string line; + std::string mtl_name; + while (std::getline(file, line)) { + if (line.find("newmtl ") == 0) { + mtl_name = line.substr(7); + } else if (line.find("Kd ") == 0) { + // цвет + char* end = nullptr; + + Material m{}; + m.name = mtl_name; + + m.color.x = std::strtof(line.c_str() + 3, &end); + m.color.y = std::strtof(end, &end); + m.color.z = std::strtof(end, &end); + + materials.push_back(m); + } + } +} + Mesh *MeshLoader::loadMesh(const std::string &filename) { std::vector points; std::vector normals; std::vector mesh_points; + std::vector materials; + loadMaterials(materials, filename + ".mtl"); std::ifstream file(filename + ".obj", std::ios::in); @@ -35,6 +67,7 @@ Mesh *MeshLoader::loadMesh(const std::string &filename) { char tmp_buff[128]; int tmp_buff_index = 0; + glm::vec3 color(0.5f, 0.0f, 1.0f); // дефолтный цвет while (true) { char in; @@ -63,7 +96,7 @@ Mesh *MeshLoader::loadMesh(const std::string &filename) { if (in == '#' || in == 'o' || in == 's') { // коммент если это информация о имени объекта или о сглаживании state = COMMENT; - } else if (in == 'v' || in == 'f') { + } else if (in == 'v' || in == 'f' || in == 'u') { state = READ_STR; } break; @@ -126,8 +159,20 @@ Mesh *MeshLoader::loadMesh(const std::string &filename) { p.normal = normals[tmp]; } + p.color = color; + mesh_points.push_back(p); } + } else if (source.find("usemtl ") == 0) { + // материал + std::string mtl_name = source.substr(7); + + for (const auto& m: materials) { + if (m.name == mtl_name) { + color = m.color; + break; + } + } } } else { tmp_buff[tmp_buff_index++] = in; @@ -137,9 +182,9 @@ Mesh *MeshLoader::loadMesh(const std::string &filename) { } // тут имеем готовый массив вершин - // размер меша = кол-во вершин * (3 + 3) + // размер меша = кол-во вершин * (3 + 3 + 3) const int vertices = (int)mesh_points.size(); - auto* out_mesh = new float[vertices * (3 + 3)]; + auto* out_mesh = new float[vertices * (3 + 3 + 3)]; int curr_index = 0; for (const auto& p : mesh_points) { @@ -152,9 +197,14 @@ Mesh *MeshLoader::loadMesh(const std::string &filename) { out_mesh[curr_index++] = p.normal.x; out_mesh[curr_index++] = p.normal.y; out_mesh[curr_index++] = p.normal.z; + + // цвет + out_mesh[curr_index++] = p.color.x; + out_mesh[curr_index++] = p.color.y; + out_mesh[curr_index++] = p.color.z; } - const int attrs[] = {3, 3, 0}; + const int attrs[] = {3, 3, 3, 0}; Mesh* m = new Mesh(out_mesh, vertices, attrs); delete[] out_mesh; diff --git a/lab2/src/main.cpp b/lab2/src/main.cpp index a1030f8..fea21cf 100644 --- a/lab2/src/main.cpp +++ b/lab2/src/main.cpp @@ -145,17 +145,17 @@ void mainloop(Camera& camera) { void loadResources() { const float buffer[] = { // треугольник - // X Y Z nX nY nZ - -1, -1, 0.5, 0, 0, 1, - 1, -1, 0.5, 0, 0, 1, - 0, 1, 0.5, 0, 0, 1, + // X Y Z nX nY nZ R G B + -1, -1, 0.5, 0, 0, 1, 0, 0, 1, + 1, -1, 0.5, 0, 0, 1, 0, 0, 1, + 0, 1, 0.5, 0, 0, 1, 0, 0, 1, - -1, -1, -0.5, 0, 0, -1, - 1, -1, -0.5, 0, 0, -1, - 0, 1, -0.5, 0, 0, -1, + -1, -1, -0.5, 0, 0, -1, 0, 1, 1, + 1, -1, -0.5, 0, 0, -1, 0, 1, 1, + 0, 1, -0.5, 0, 0, -1, 0, 1, 1, }; const int mesh_attrs[] { - 3, 3, 0 + 3, 3, 3, 0 }; world = new Mesh(buffer, 6, mesh_attrs);