Учу OpenGL по урокам на хабре. Делаю текстуры. Используется библиотека stb_image. Проблема следующая: отображается среднее значение цвета текстуры. Никак не могу найти причину этого. Вот код:
Mesh.cpp
#define STB_IMAGE_IMPLEMENTATION
#include "Mesh.h"
Mesh::Mesh(vector<Vector3<GLfloat>> vertices, vector<unsigned int> indicles, vector<float> uvs) {
cout<<"creating mesh stated\n";
this->vertices = vertices;
this->indices = indicles;
this->UVs = uvs;
//Shader newShader("StandartShader.vert", "StandartShader.frag");
this->meshShader = Shader ("../src/GameSource/StandartSource/Shaders/StandartShader.vert", "../src/GameSource/StandartSource/Shaders/StandartShader.frag");//src\GameSource\StandartSource\Shaders
InitMesh();
cout<<"creating mesh finished\n";
}
Mesh::Mesh() {
cout << "Warning! This mesh dosn't contain vertices data. Please, set them\n";
}
Mesh::~Mesh() {}
void Mesh::SetVertices(vector<Vector3<GLfloat>> verts) {
Mesh::vertices = verts;
}
void Mesh::SetIndicles(vector<unsigned int> indicles) {
Mesh::indices = indicles;
}
vector<Vector3<GLfloat>> Mesh::GetVertices() {
return Mesh::vertices;
}
vector<unsigned int> Mesh::GetIndicles(){
return Mesh::indices;
}
void Mesh::InitMesh() {
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
std::cout << "vertices size = " << vertices.size() << "\n";
std::cout << "indices size = " << indices.size() << "\n";
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vector3<GLfloat>), &vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
// Position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, vertices.size() * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
// Textures attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, UVs.size() * sizeof(float), (GLvoid*)0);
glEnableVertexAttribArray(1);
// load and create a texture
// -------------------------
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture); // all upcoming GL_TEXTURE_2D operations now have effect on this texture object
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps
int width, height, nrChannels;
// The FileSystem::getPath(...) is part of the GitHub repository so we can find files on any IDE/platform; replace it with your own image path.
unsigned char *data = stbi_load("../src/GameSource/StandartSource/Textures/anypics.ru-56720.jpg", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
std::cout << "\n\nquality is " << width << " * " << height << "\n\n";
stbi_image_free(data);
std::cout << " \n\n this is uvs length - " << UVs.size() << "\n";
glBindVertexArray(0);
}
void Mesh::Draw() {
// bind Texture
glBindTexture(GL_TEXTURE_2D, texture);
// create transformations
glm::mat4 transform = glm::mat4(1.0f); // make sure to initialize matrix to identity matrix first
transform = glm::translate(transform, glm::vec3(0.5f, -0.5f, 0.0f));
transform = glm::rotate(transform, (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));
// get matrix's uniform location and set matrix
meshShader.Use();
unsigned int transformLoc = glGetUniformLocation(meshShader.Program, "transform");
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform));
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
glUseProgram(0);
}
void Mesh::Update() {
Mesh::Draw();
}
StandartShader.vert
#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 = vec2(aTexCoord.x, aTexCoord.y);
}
StandartShader.frag
#version 330 core
out vec4 FragColor;
in vec3 ourColor;
in vec2 TexCoord;
// texture sampler
uniform sampler2D texture1;
void main()
{
FragColor = texture(texture1, TexCoord);
}
Весь проект сразу: Ссылка на архив в Google диск
glVertexAttribPointer
не видно bind-а соответствующего VBO, и передпоследний параметр выглядит странно.