我试图将纹理加载到Modern NVIDIA openGL中的对象
我有一个正方形,我想加载纹理,我使用stb_image作为我的装载机
这是主要代码:
float position1[16]{
0.0f , 0.0f, 0.0f , 0.0f,
0.8f , 0.0f, 1.0f , 0.0f,
0.0f , 0.8f, 0.0f , 1.0f,
0.8f , 0.8f, 1.0f , 1.0f ,
};
unsigned int Indexs[6]{
0 , 1 , 2 ,
2 , 1 , 3
};
VertexBuffer buffer(position1, sizeof(position1));
IndexBuffer Index(Indexs, 6);
Texture Tex("Smile.png");
Tex.Bind();
buffer.Bind();
Index.Bind();
Shader BasicShader("basic.shader");
Renderer renderer;
glfwSwapInterval(0);
//! MAIN LOOP !!!
BasicShader.Uniform1i("u_Texture ", 0);
BasicShader.Uniform4f("u_Color", 0.0f, 1.0f, 1.0f, 1.0f );
while (!glfwWindowShouldClose(window) )
{
//! RENDER
renderer.Clear();
glEnable(GL_TEXTURE_2D);
BasicShader.Uniform1i("u_Texture ", 0);
BasicShader.Uniform4f("u_Color", 0.f, 1.0f, 1.0f, 1.0f);
Tex.Bind();
BasicShader.Bind();
renderer.Draw(buffer, Index, Layout::D4Vertex, BasicShader);
//! FRAME
glfwSwapBuffers(window);
//! EVENTS
glfwPollEvents();
}
glfwTerminate();
return 0;
};
这是我的着色器,当然正如预期的那样正确编译而没有错误:
#shader vertex
#version 410 core
layout(location = 0 ) in vec4 position;
layout(location = 1) in vec2 texCoord;
out vec2 v_TexCoord;
void main()
{
gl_Position = position;
v_TexCoord = texCoord;
};
#shader fragment
#version 410 core
layout(location = 0 ) out vec4 color;
uniform vec4 u_Color;
uniform sampler2D u_Texture;
in vec2 v_TexCoord;
void main()
{
vec4 texColor = texture2D(u_Texture, v_TexCoord);
color = texColor;
};
这是我的平局电话:
void Renderer::Draw(VertexBuffer& vb, IndexBuffer& iv,
Layout layout,Shader& shader)
{
BufferLayout bufferLayout;
shader.Bind();
vb.Bind();
iv.Bind();
bufferLayout.SetLayout(layout);
glDrawElements(GL_TRIANGLES, iv.GetCount(), GL_UNSIGNED_INT, nullptr);
}
这就是我设置缓冲区布局的方式:
void BufferLayout::SetLayout(Layout l)
{
switch (l) {
case Layout::NONE :
glEnableVertexAttribArray(0);
case Layout::D2Vertex :
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) *
2,0);
case Layout::D4Vertex:
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) *
4,0);
}
}
这是我的纹理类(handels加载参数和绑定):
#include "Texture.h"
#include "stb_image.h";
#include "Renderer.h"
Texture::Texture(string path)
: m_RendererID(0) , m_BPP(0) , m_Height (0) , m_Width (0),
m_LocalBuffer(nullptr) , m_FilePath(path)
{
stbi_set_flip_vertically_on_load(1);
m_LocalBuffer = stbi_load(path.c_str(), &m_Width, &m_Height, &m_BPP, 4);
//seems like the image is there
glEnable(GL_TEXTURE_2D);
glGenTextures(1, & m_RendererID);
glBindTexture(GL_TEXTURE_2D, m_RendererID);
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_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
GLenum temp = glGetError();
if (temp != GL_NO_ERROR) throw std::invalid_argument("OPENGL ERROR");
// I Get no erros
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Width, m_Height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, m_LocalBuffer);
}
Texture::~Texture()
{
glDeleteTextures(1, &m_RendererID);
}
void Texture::Bind(unsigned int slot)
{
glActiveTexture(GL_TEXTURE0 + slot);
glBindTexture(GL_TEXTURE_2D, m_RendererID);
}
void Texture::Unbind()
{
glBindTexture(GL_TEXTURE_2D, 0);
}
和我的texture.h文件:
#pragma once
#include "Renderer.h"
class Texture {
private :
unsigned int m_RendererID;
string m_FilePath;
unsigned char * m_LocalBuffer;
int m_Width, m_Height, m_BPP;
public:
Texture(string path);
~Texture();
void Bind(unsigned int slot = 0);
void Unbind();
inline int GetWidth()const { return m_Width; }
inline int GetHeight()const { return m_Height; }
};
基本上我很确定问题必须在我发布的代码中的某处,并且所有其他代码都按预期工作。
有人能发现问题吗? 结果应该是一个带纹理的正方形" Smile.png"在它上面,我得到一个白色的纹理。我有文件" Smile.png"在我的项目目录中,在我看来,像stb_image可以找到并加载它。
* glGetError();
答案 0 :(得分:1)
在代码示例中,缺少attribut 1(texCoord
)的设置代码。对于着色器使用的每个属性,必须绑定缓冲区,必须启用该属性,并且必须指定属性和着色器之间的关系。
如上所述,这是针对属性0而针对属性1完成的。SetLayout
方法中的代码可能看起来像这样:
//Attribute 0: uses first two floats
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, 0);
//Attribute 1: uses 3rd and 4th float
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, (void*)(2 * sizeof(float)));