Программы на C++. Форум разработчиков
Anonymous
OpenGL импортирует текстуру как один цвет [закрыто]
Сообщение
Anonymous » 26 ноя 2025, 22:18
У меня есть простая программа OpenGL, которая рисует куб в центре экрана. Я также добавил несколько функций для придания кубу текстуры:
Shader.vs
Код: Выделить всё
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;
out vec2 TexCoord;
void main()
{
gl_Position = vec4(aPos, 1.0);
TexCoord = aTexCoord;
}
Shader.fs
Код: Выделить всё
#version 330 core
in vec2 TexCoord;
out vec4 FragColor;
uniform sampler2D tex1;
void main()
{
FragColor = texture(tex1, TexCoord);
}
Shaders.h
Код: Выделить всё
#ifndef SHADER_H
#define SHADER_H
#include
#include
#include
#include
#include
class Shader
{
public:
unsigned int ID;
// constructor generates the shader on the fly
// ------------------------------------------------------------------------
Shader(const char* vertexPath, const char* fragmentPath)
{
printf("%s", vertexPath);
// 1. retrieve the vertex/fragment source code from filePath
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
// ensure ifstream objects can throw exceptions:
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try
{
// open files
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
std::stringstream vShaderStream, fShaderStream;
// read file's buffer contents into streams
vShaderStream
Подробнее здесь: [url]https://stackoverflow.com/questions/79831070/opengl-imports-texture-as-single-color[/url]
1764184713
Anonymous
У меня есть простая программа OpenGL, которая рисует куб в центре экрана. Я также добавил несколько функций для придания кубу текстуры: Shader.vs [code]#version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec2 aTexCoord; out vec2 TexCoord; void main() { gl_Position = vec4(aPos, 1.0); TexCoord = aTexCoord; } [/code] Shader.fs [code]#version 330 core in vec2 TexCoord; out vec4 FragColor; uniform sampler2D tex1; void main() { FragColor = texture(tex1, TexCoord); } [/code] Shaders.h [code]#ifndef SHADER_H #define SHADER_H #include #include #include #include #include class Shader { public: unsigned int ID; // constructor generates the shader on the fly // ------------------------------------------------------------------------ Shader(const char* vertexPath, const char* fragmentPath) { printf("%s", vertexPath); // 1. retrieve the vertex/fragment source code from filePath std::string vertexCode; std::string fragmentCode; std::ifstream vShaderFile; std::ifstream fShaderFile; // ensure ifstream objects can throw exceptions: vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); try { // open files vShaderFile.open(vertexPath); fShaderFile.open(fragmentPath); std::stringstream vShaderStream, fShaderStream; // read file's buffer contents into streams vShaderStream Подробнее здесь: [url]https://stackoverflow.com/questions/79831070/opengl-imports-texture-as-single-color[/url]