Anonymous
Почему эта программа не правильно текстурировать модели?
Сообщение
Anonymous » 03 июл 2025, 23:20
Я пытаюсь использовать 3D рендеринг, работая с использованием OpenGL 1.1: < /p>
Код: Выделить всё
#define GLFW_INCLUDE_GLU
#include
#include
#include
#include
#include
#include
#define TINYOBJLOADER_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#include "tiny_obj_loader.h"
#include "stb_image.h"
using namespace std;
int main() {
if (!glfwInit())
{
const char* error;
glfwGetError(&error);
printf("GLFW failed to intialize: %s", error);
return 1;
}
GLFWwindow* window = glfwCreateWindow(640, 480, "MRE", NULL, NULL);
glfwMakeContextCurrent(window);
tinyobj::attrib_t attrib;
vector shape;
vector material;
vector textures;
tinyobj::LoadObj(&attrib, &shape, &material, nullptr, nullptr, (filesystem::current_path() / "model" / "animegirl.obj").generic_string().c_str(), (filesystem::current_path() / "model" / "").generic_string().c_str());
for (int i = 0; i < material.size(); i++) {
if (material[i].diffuse_texname != "") {
stbi_set_flip_vertically_on_load(true);
int w;
int h;
int n;
unsigned char* tex = stbi_load((filesystem::current_path() / "model" / material[i].diffuse_texname).generic_string().c_str(), &w, &h, &n, 4);
GLuint glTex;
glGenTextures(1, &glTex);
glBindTexture(GL_TEXTURE_2D, glTex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex);
textures.push_back(glTex);
}
else {
textures.push_back(0);
}
}
glClearColor(0.2, 0.2, 0.2, 0.2);
glViewport(0, 0, 640, 480);
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
gluPerspective(60, (float)(640.0 / 480.0), 2, 500);
gluLookAt(0, 0, 200, 0, 0, 0, 0, 1, 0);
while (true) {
glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (int i = 0; i < shape.size(); i++) {
glBegin(GL_TRIANGLES);
int offset = 0;
int lastMat = 0;
for (int j = 0; j < shape[i].mesh.num_face_vertices.size(); j++) {
int mati = shape[i].mesh.material_ids[j];
if (mati >= 0) {
tinyobj::material_t mat = material[mati];
if (textures[mati] != 0 && mati != lastMat) {
glEnd();
glBindTexture(GL_TEXTURE_2D, textures[mati]);
glBegin(GL_TRIANGLES);
lastMat = mati;
}
}
for (int k = 0; k < shape[i].mesh.num_face_vertices[j]; k++) {
tinyobj::index_t id = shape[i].mesh.indices[offset + k];
glVertex3f(attrib.vertices[3 * size_t(id.vertex_index)], attrib.vertices[3 * size_t(id.vertex_index) + 1], attrib.vertices[3 * size_t(id.vertex_index) + 2]);
if (id.normal_index >= 0) {
glNormal3f(attrib.normals[3 * size_t(id.normal_index)], attrib.normals[3 * size_t(id.normal_index) + 1], attrib.normals[3 * size_t(id.normal_index) + 2]);
}
if (id.texcoord_index >= 0) {
glTexCoord2f(attrib.texcoords[2 * size_t(id.texcoord_index)], attrib.texcoords[2 * size_t(id.texcoord_index) + 1]);
}
}
offset += shape[i].mesh.num_face_vertices[j];
}
glEnd();
}
glfwSwapBuffers(window);
}
}
Однако я не могу получить отображение текстуры для правильной работы. Та же модель, что и в моем двигателе:
Подробнее здесь:
https://stackoverflow.com/questions/796 ... ing-models
1751574037
Anonymous
Я пытаюсь использовать 3D рендеринг, работая с использованием OpenGL 1.1: < /p> [code]#define GLFW_INCLUDE_GLU #include #include #include #include #include #include #define TINYOBJLOADER_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION #include "tiny_obj_loader.h" #include "stb_image.h" using namespace std; int main() { if (!glfwInit()) { const char* error; glfwGetError(&error); printf("GLFW failed to intialize: %s", error); return 1; } GLFWwindow* window = glfwCreateWindow(640, 480, "MRE", NULL, NULL); glfwMakeContextCurrent(window); tinyobj::attrib_t attrib; vector shape; vector material; vector textures; tinyobj::LoadObj(&attrib, &shape, &material, nullptr, nullptr, (filesystem::current_path() / "model" / "animegirl.obj").generic_string().c_str(), (filesystem::current_path() / "model" / "").generic_string().c_str()); for (int i = 0; i < material.size(); i++) { if (material[i].diffuse_texname != "") { stbi_set_flip_vertically_on_load(true); int w; int h; int n; unsigned char* tex = stbi_load((filesystem::current_path() / "model" / material[i].diffuse_texname).generic_string().c_str(), &w, &h, &n, 4); GLuint glTex; glGenTextures(1, &glTex); glBindTexture(GL_TEXTURE_2D, glTex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex); textures.push_back(glTex); } else { textures.push_back(0); } } glClearColor(0.2, 0.2, 0.2, 0.2); glViewport(0, 0, 640, 480); glEnable(GL_TEXTURE_2D); glEnable(GL_DEPTH_TEST); glMatrixMode(GL_PROJECTION); gluPerspective(60, (float)(640.0 / 480.0), 2, 500); gluLookAt(0, 0, 200, 0, 0, 0, 0, 1, 0); while (true) { glfwPollEvents(); glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); for (int i = 0; i < shape.size(); i++) { glBegin(GL_TRIANGLES); int offset = 0; int lastMat = 0; for (int j = 0; j < shape[i].mesh.num_face_vertices.size(); j++) { int mati = shape[i].mesh.material_ids[j]; if (mati >= 0) { tinyobj::material_t mat = material[mati]; if (textures[mati] != 0 && mati != lastMat) { glEnd(); glBindTexture(GL_TEXTURE_2D, textures[mati]); glBegin(GL_TRIANGLES); lastMat = mati; } } for (int k = 0; k < shape[i].mesh.num_face_vertices[j]; k++) { tinyobj::index_t id = shape[i].mesh.indices[offset + k]; glVertex3f(attrib.vertices[3 * size_t(id.vertex_index)], attrib.vertices[3 * size_t(id.vertex_index) + 1], attrib.vertices[3 * size_t(id.vertex_index) + 2]); if (id.normal_index >= 0) { glNormal3f(attrib.normals[3 * size_t(id.normal_index)], attrib.normals[3 * size_t(id.normal_index) + 1], attrib.normals[3 * size_t(id.normal_index) + 2]); } if (id.texcoord_index >= 0) { glTexCoord2f(attrib.texcoords[2 * size_t(id.texcoord_index)], attrib.texcoords[2 * size_t(id.texcoord_index) + 1]); } } offset += shape[i].mesh.num_face_vertices[j]; } glEnd(); } glfwSwapBuffers(window); } } [/code] Однако я не могу получить отображение текстуры для правильной работы. Та же модель, что и в моем двигателе: Подробнее здесь: [url]https://stackoverflow.com/questions/79689344/why-isnt-this-program-correctly-texturing-models[/url]