Anonymous
OpenGL - объект показывает только один цвет из текстуры
Сообщение
Anonymous » 23 фев 2025, 22:43
Как говорится в названии, мой объект OpenGL должен иметь текстуру, и, хотя он действительно имеет текстуру, проблема в том, что он показывает только один цвет из этой текстуры. Вот как выглядит текстура.
Это то, что происходит. < /P>
Вот мой основной файл: < /p>
Код: Выделить всё
#include
#include
#include
#include
#include
#include "shader.h"
#include "VAO.h"
#include "VBO.h"
#include "EBO.h"
#include "Extra.h"
#include "tex_class.h"
bool fullscreen = false;
extern const char* vertexShaderSource;
extern const char* fragmentShaderSource;
namespace filesys = std::filesystem;
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
extern GLfloat vertices[32];
extern GLuint indices[6];
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
int W_WIDTH = glfwGetVideoMode(monitor)->width;
int W_HEIGHT = glfwGetVideoMode(monitor)->height;
GLFWwindow* window = glfwCreateWindow(W_WIDTH, W_HEIGHT, "Client", nullptr, nullptr);
if (fullscreen)
glfwSetWindowMonitor(window, monitor, 0, 0, W_WIDTH, W_HEIGHT, GLFW_DONT_CARE);
else
{
glfwMaximizeWindow(window);
glfwWindowHint(GLFW_RESIZABLE, 0);
}
glfwMakeContextCurrent(window);
gladLoadGL();
Shader shaderProgram("default.vert", "default.frag");
VAO VAO;
VAO.Bind();
VBO VBO(vertices, sizeof(vertices));
EBO EBO(indices, sizeof(indices));
std::string parentDir = (filesys::current_path().filesys::path::parent_path()).string();
VAO.LinkVBO(VBO, 0);
VAO.Unbind();
VBO.Unbind();
EBO.Unbind();
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glViewport(0, 0, W_WIDTH, W_HEIGHT);
Texture texture(512, 512, 3, "D:\\dev\\3D\\tile.jpg", GL_TEXTURE_2D, GL_TEXTURE0, GL_RGB, GL_UNSIGNED_BYTE);
texture.texUnit(shaderProgram, "ourTexture", 0);
while (!glfwWindowShouldClose(window))
{
glClearColor(0, 0.656f, 0.75f, 1);
glClear(GL_COLOR_BUFFER_BIT);
shaderProgram.Activate();
texture.Bind();
VAO.Bind();
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window);
glfwPollEvents();
}
texture.Terminate();
VAO.Terminate();
VBO.Terminate();
EBO.Terminate();
shaderProgram.Terminate();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
< /code>
Вот мой метод Linkvbo: < /p>
void VAO::LinkVBO(VBO VBO, GLuint Layout)
{
VBO.Bind();
glVertexAttribPointer(Layout, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0); //position
glEnableVertexAttribArray(Layout);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float))); //color
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float))); //texturing
glEnableVertexAttribArray(2);
VBO.Unbind();
}
< /code>
Вот мой класс текстур: < /p>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include "glad/glad.h"
#include
#include "shader.h"
class Texture {
public:
GLuint ID;
GLenum type;
Texture(int width, int height, int nrChannels, const char* texture, GLenum texType, GLenum slot, GLenum format, GLenum pixelType)
{
type = texType;
stbi_set_flip_vertically_on_load(true);
glGenTextures(1, &ID);
glActiveTexture(slot);
glBindTexture(texType, ID);
glTexParameteri(texType, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
glTexParameteri(texType, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(texType, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(texType, GL_TEXTURE_WRAP_T, GL_REPEAT);
unsigned char* data = stbi_load(texture, &width, &height, &nrChannels, 0);
glTexImage2D(texType, 0, format, width, height, 0, format, pixelType, data);
glGenerateMipmap(texType);
stbi_image_free(data);
glBindTexture(texType, 0);
}
void texUnit(Shader &shaderProgram, const char* uniform, GLuint unit)
{
GLuint texUni = glGetUniformLocation(shaderProgram.ID, uniform);
shaderProgram.Activate();
glUniform1i(texUni, unit);
}
void Bind()
{
glBindTexture(type, ID);
}
void Unbind()
{
glBindTexture(type, 0);
}
void Terminate()
{
glDeleteTextures(1, &ID);
}
};
< /code>
Вот массивы для моих вершин и индексов: < /p>
#pragma once
#include
float vertices[32] = {
// positions // colors // texture coords
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
};
GLuint indices[6] = {
0, 2, 1,
0, 3, 2
};
< /code>
И, наконец, вот мои файлы шейдеров: < /p>
1.
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;
out vec3 ourColor;
out vec2 texCoord;
void main()
{
gl_Position = vec4(aPos, 1.0);
ourColor = aColor;
texCoord = aTexCoord;
}
< /code>
[list]
[*] < /li>
< /ol>
#version 330 core
out vec4 FragColor;
in vec3 ourColor;
in vec2 TexCoord;
uniform sampler2D ourTexture;
void main()
{
FragColor = texture(ourTexture, TexCoord);
}
< /code>
может быть чрезвычайно очевидное решение для этого, но я действительно новичок в OpenGL, и до сих пор я не нашел решения. Может быть много людей, которые видят это, которые имеют опыт в OpenGL, поэтому, если вы один из этих людей, пожалуйста, помогите. < /P>
Вот файлы для Vao, Ebo, и классы VBO, если они также помогают: < /p>
vao: < /p>
#include "VAO.h"
VAO::VAO()
{
glGenVertexArrays(1, &ID);
}
void VAO::LinkVBO(VBO VBO, GLuint Layout)
{
VBO.Bind();
glVertexAttribPointer(Layout, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0); //position
glEnableVertexAttribArray(Layout);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float))); //color
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float))); //texturing
glEnableVertexAttribArray(2);
VBO.Unbind();
}
void VAO::Bind()
{
glBindVertexArray(ID);
}
void VAO::Unbind()
{
glBindVertexArray(0);
}
void VAO::Terminate()
{
glDeleteVertexArrays(1, &ID);
}
< /code>
vbo: < /p>
#include "VBO.h"
VBO::VBO(GLfloat* vertices, GLsizeiptr size)
{
glGenBuffers(1, &ID);
glBindBuffer(GL_ARRAY_BUFFER, ID);
glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
}
void VBO::Bind()
{
glBindBuffer(GL_ARRAY_BUFFER, ID);
}
void VBO::Unbind()
{
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void VBO::Terminate()
{
glDeleteBuffers(1, &ID);
}
< /code>
ebo: < /p>
#include "EBO.h"
EBO::EBO(GLuint* indices, GLsizeiptr size)
{
glGenBuffers(1, &ID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, indices, GL_STATIC_DRAW);
}
void EBO::Bind()
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ID);
}
void EBO::Unbind()
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void EBO::Terminate()
{
glDeleteBuffers(1, &ID);
}
< /code>
(попытка) Решения: < /p>
Я попытался изменить параметры ширины и высоты для текстуры Текстура (512, 512, 3, "D: \\ dev \\ 3d \\ tile.jpg", gl_texture_2d, gl_texture0, gl_rgb, Gl_unsigned_byte);
(они являются первыми и вторыми параметрами), но до сих пор не сработало.
[*] i ' В.Е. попытался заменить типы данных для вершин и индексов, но это фактически ухудшило ситуацию.
[/list]
Подробнее здесь:
https://stackoverflow.com/questions/794 ... om-texture
1740339780
Anonymous
Как говорится в названии, мой объект OpenGL должен иметь текстуру, и, хотя он действительно имеет текстуру, проблема в том, что он показывает только один цвет из этой текстуры. Вот как выглядит текстура. Это то, что происходит. < /P> Вот мой основной файл: < /p> [code]#include #include #include #include #include #include "shader.h" #include "VAO.h" #include "VBO.h" #include "EBO.h" #include "Extra.h" #include "tex_class.h" bool fullscreen = false; extern const char* vertexShaderSource; extern const char* fragmentShaderSource; namespace filesys = std::filesystem; int main() { glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); extern GLfloat vertices[32]; extern GLuint indices[6]; GLFWmonitor* monitor = glfwGetPrimaryMonitor(); int W_WIDTH = glfwGetVideoMode(monitor)->width; int W_HEIGHT = glfwGetVideoMode(monitor)->height; GLFWwindow* window = glfwCreateWindow(W_WIDTH, W_HEIGHT, "Client", nullptr, nullptr); if (fullscreen) glfwSetWindowMonitor(window, monitor, 0, 0, W_WIDTH, W_HEIGHT, GLFW_DONT_CARE); else { glfwMaximizeWindow(window); glfwWindowHint(GLFW_RESIZABLE, 0); } glfwMakeContextCurrent(window); gladLoadGL(); Shader shaderProgram("default.vert", "default.frag"); VAO VAO; VAO.Bind(); VBO VBO(vertices, sizeof(vertices)); EBO EBO(indices, sizeof(indices)); std::string parentDir = (filesys::current_path().filesys::path::parent_path()).string(); VAO.LinkVBO(VBO, 0); VAO.Unbind(); VBO.Unbind(); EBO.Unbind(); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glViewport(0, 0, W_WIDTH, W_HEIGHT); Texture texture(512, 512, 3, "D:\\dev\\3D\\tile.jpg", GL_TEXTURE_2D, GL_TEXTURE0, GL_RGB, GL_UNSIGNED_BYTE); texture.texUnit(shaderProgram, "ourTexture", 0); while (!glfwWindowShouldClose(window)) { glClearColor(0, 0.656f, 0.75f, 1); glClear(GL_COLOR_BUFFER_BIT); shaderProgram.Activate(); texture.Bind(); VAO.Bind(); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); glfwSwapBuffers(window); glfwPollEvents(); } texture.Terminate(); VAO.Terminate(); VBO.Terminate(); EBO.Terminate(); shaderProgram.Terminate(); glfwDestroyWindow(window); glfwTerminate(); return 0; } < /code> Вот мой метод Linkvbo: < /p> void VAO::LinkVBO(VBO VBO, GLuint Layout) { VBO.Bind(); glVertexAttribPointer(Layout, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0); //position glEnableVertexAttribArray(Layout); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float))); //color glEnableVertexAttribArray(1); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float))); //texturing glEnableVertexAttribArray(2); VBO.Unbind(); } < /code> Вот мой класс текстур: < /p> #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" #include "glad/glad.h" #include #include "shader.h" class Texture { public: GLuint ID; GLenum type; Texture(int width, int height, int nrChannels, const char* texture, GLenum texType, GLenum slot, GLenum format, GLenum pixelType) { type = texType; stbi_set_flip_vertically_on_load(true); glGenTextures(1, &ID); glActiveTexture(slot); glBindTexture(texType, ID); glTexParameteri(texType, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR); glTexParameteri(texType, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(texType, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(texType, GL_TEXTURE_WRAP_T, GL_REPEAT); unsigned char* data = stbi_load(texture, &width, &height, &nrChannels, 0); glTexImage2D(texType, 0, format, width, height, 0, format, pixelType, data); glGenerateMipmap(texType); stbi_image_free(data); glBindTexture(texType, 0); } void texUnit(Shader &shaderProgram, const char* uniform, GLuint unit) { GLuint texUni = glGetUniformLocation(shaderProgram.ID, uniform); shaderProgram.Activate(); glUniform1i(texUni, unit); } void Bind() { glBindTexture(type, ID); } void Unbind() { glBindTexture(type, 0); } void Terminate() { glDeleteTextures(1, &ID); } }; < /code> Вот массивы для моих вершин и индексов: < /p> #pragma once #include float vertices[32] = { // positions // colors // texture coords 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left }; GLuint indices[6] = { 0, 2, 1, 0, 3, 2 }; < /code> И, наконец, вот мои файлы шейдеров: < /p> 1. #version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec3 aColor; layout (location = 2) in vec2 aTexCoord; out vec3 ourColor; out vec2 texCoord; void main() { gl_Position = vec4(aPos, 1.0); ourColor = aColor; texCoord = aTexCoord; } < /code> [list] [*] < /li> < /ol> #version 330 core out vec4 FragColor; in vec3 ourColor; in vec2 TexCoord; uniform sampler2D ourTexture; void main() { FragColor = texture(ourTexture, TexCoord); } < /code> может быть чрезвычайно очевидное решение для этого, но я действительно новичок в OpenGL, и до сих пор я не нашел решения. Может быть много людей, которые видят это, которые имеют опыт в OpenGL, поэтому, если вы один из этих людей, пожалуйста, помогите. < /P> Вот файлы для Vao, Ebo, и классы VBO, если они также помогают: < /p> vao: < /p> #include "VAO.h" VAO::VAO() { glGenVertexArrays(1, &ID); } void VAO::LinkVBO(VBO VBO, GLuint Layout) { VBO.Bind(); glVertexAttribPointer(Layout, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0); //position glEnableVertexAttribArray(Layout); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float))); //color glEnableVertexAttribArray(1); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float))); //texturing glEnableVertexAttribArray(2); VBO.Unbind(); } void VAO::Bind() { glBindVertexArray(ID); } void VAO::Unbind() { glBindVertexArray(0); } void VAO::Terminate() { glDeleteVertexArrays(1, &ID); } < /code> vbo: < /p> #include "VBO.h" VBO::VBO(GLfloat* vertices, GLsizeiptr size) { glGenBuffers(1, &ID); glBindBuffer(GL_ARRAY_BUFFER, ID); glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW); } void VBO::Bind() { glBindBuffer(GL_ARRAY_BUFFER, ID); } void VBO::Unbind() { glBindBuffer(GL_ARRAY_BUFFER, 0); } void VBO::Terminate() { glDeleteBuffers(1, &ID); } < /code> ebo: < /p> #include "EBO.h" EBO::EBO(GLuint* indices, GLsizeiptr size) { glGenBuffers(1, &ID); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ID); glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, indices, GL_STATIC_DRAW); } void EBO::Bind() { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ID); } void EBO::Unbind() { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } void EBO::Terminate() { glDeleteBuffers(1, &ID); } < /code> (попытка) Решения: < /p> Я попытался изменить параметры ширины и высоты для текстуры Текстура (512, 512, 3, "D: \\ dev \\ 3d \\ tile.jpg", gl_texture_2d, gl_texture0, gl_rgb, Gl_unsigned_byte); [/code] (они являются первыми и вторыми параметрами), но до сих пор не сработало. [*] i ' В.Е. попытался заменить типы данных для вершин и индексов, но это фактически ухудшило ситуацию. [/list] Подробнее здесь: [url]https://stackoverflow.com/questions/79461806/opengl-object-only-shows-one-color-from-texture[/url]