C++ OpenGL GLSL纹理函数始终返回vec4(1,1,1,1),白色三角形

C++ OpenGL GLSL纹理函数始终返回vec4(1,1,1,1),白色三角形,c++,opengl,glsl,textures,texture2d,C++,Opengl,Glsl,Textures,Texture2d,我正在尝试将2D纹理映射到三角形。但目前,我只能得到一个没有纹理的渐变色三角形。也就是说,glsl中的纹理函数始终返回vec4(1,1,1,1),我的textCoords正在工作。我该怎么修?任何建议都有助于尝试 顺便说一下,我已经做了三天了 在纹理类中: 建造商: Texture::Texture(const std::string& fileName){ int width, height, numComponents; //float* imageData = stb

我正在尝试将2D纹理映射到三角形。但目前,我只能得到一个没有纹理的渐变色三角形。也就是说,glsl中的纹理函数始终返回vec4(1,1,1,1),我的textCoords正在工作。我该怎么修?任何建议都有助于尝试

顺便说一下,我已经做了三天了

在纹理类中:

建造商:

Texture::Texture(const std::string& fileName){

   int width, height, numComponents;
   //float* imageData = stbi_loadf(fileName.c_str(), &width, &height, &numComponents, 4);
   unsigned char* imageData = stbi_load(fileName.c_str(), &width, &height, &numComponents, 4);
   if(imageData == NULL){
      cerr << "Texture loading failed for "<<fileName<< endl;
   }

   glGenTextures(1, &m_texture);
   glBindTexture(GL_TEXTURE_2D, m_texture);

   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

   glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);


   stbi_image_free(imageData);
 }
在my main.cpp中:

m_shader.generateProgramObject();
m_shader.attachVertexShader( getAssetFilePath("VertexShader.vs").c_str() );
m_shader.attachFragmentShader( getAssetFilePath("FragmentShader.fs").c_str()   );
m_shader.link();

// texture created here
texture = Texture("Assets/bricks.jpg");


// enable vertex attribute indices
glGenVertexArrays(1, &m_vao_triangle);
glBindVertexArray(m_vao_triangle);

// Enable the attribute index location for "position" when rendering.
GLint positionAttribLocation = m_shader.getAttribLocation( "position" );
glEnableVertexAttribArray(positionAttribLocation);

GLint textCoordLocation = m_shader.getAttribLocation( "atextCoord" );
glEnableVertexAttribArray(textCoordLocation);


// Restore defaults
glBindVertexArray(0);


CHECK_GL_ERRORS;
将三角形数据上载到缓冲区

vec3 triangleVertices[] = {
    // Construct equalaterial triangle
    vec3(0.0f, 0.0f, 0.0f),
    vec3(0.25f, 1.0f, 0.0),
    vec3(0.5f, 0.0f, 0.0f)
};

vec2 textCoords[] = {
         vec2(1.0f, 0.0f), 
         vec2(0.25f, 1.0f), 
         vec2(0.5f, 0.0f)};



// Generate a vertex buffer object to hold the triangle's vertex data.
glGenBuffers(1, &m_vbo_triangle);

//-- Upload triangle vertex data to the vertex buffer:
glBindBuffer(GL_ARRAY_BUFFER, m_vbo_triangle);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangleVertices), triangleVertices,
        GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, 0);
CHECK_GL_ERRORS;




//====generate buffer for holding texture coordinates====
glGenBuffers(1, &m_uv_triangle);
glBindBuffer(GL_ARRAY_BUFFER, m_uv_triangle);
glBufferData(GL_ARRAY_BUFFER, sizeof(textCoords), textCoords,
             GL_STATIC_DRAW);

// Unbind the target GL_ARRAY_BUFFER, now that we are finished using it.
glBindBuffer(GL_ARRAY_BUFFER, 0);

CHECK_GL_ERRORS;
将缓冲区数据映射到着色器

 glBindVertexArray(m_vao_triangle);

 glBindBuffer(GL_ARRAY_BUFFER, m_vbo_triangle);
 GLint positionAttribLocation = m_shader.getAttribLocation( "position" );
 glVertexAttribPointer(positionAttribLocation, 3, GL_FLOAT, GL_FALSE, 0, nullptr);



 glBindBuffer(GL_ARRAY_BUFFER, m_uv_triangle);
 GLint textCoordLocation = m_shader.getAttribLocation( "atextCoord" );
 glVertexAttribPointer(textCoordLocation,2, GL_FLOAT, GL_FALSE, 0, nullptr);


 //-- Unbind target, and restore default values:
 glBindBuffer(GL_ARRAY_BUFFER, 0);
 glBindVertexArray(0);

 CHECK_GL_ERRORS;
    m_shader.enable();

    ...

    GLint uniformLocation_diffuse = m_shader.getUniformLocation("diffuse");
    glUniform1i(uniformLocation_diffuse, 0);
    CHECK_GL_ERRORS;

    m_shader.disable();

    CHECK_GL_ERRORS;
上载到着色器

 glBindVertexArray(m_vao_triangle);

 glBindBuffer(GL_ARRAY_BUFFER, m_vbo_triangle);
 GLint positionAttribLocation = m_shader.getAttribLocation( "position" );
 glVertexAttribPointer(positionAttribLocation, 3, GL_FLOAT, GL_FALSE, 0, nullptr);



 glBindBuffer(GL_ARRAY_BUFFER, m_uv_triangle);
 GLint textCoordLocation = m_shader.getAttribLocation( "atextCoord" );
 glVertexAttribPointer(textCoordLocation,2, GL_FLOAT, GL_FALSE, 0, nullptr);


 //-- Unbind target, and restore default values:
 glBindBuffer(GL_ARRAY_BUFFER, 0);
 glBindVertexArray(0);

 CHECK_GL_ERRORS;
    m_shader.enable();

    ...

    GLint uniformLocation_diffuse = m_shader.getUniformLocation("diffuse");
    glUniform1i(uniformLocation_diffuse, 0);
    CHECK_GL_ERRORS;

    m_shader.disable();

    CHECK_GL_ERRORS;
在我的绘图功能中:

void Texture::Bind(){
  glActiveTexture(GL_TEXTURE0);
  glBindTexture(GL_TEXTURE_2D, m_texture);
}
glBindVertexArray(m_vao_triangle);
//   below I tried, but didn't work
//   glClear(GL_STENCIL_BUFFER_BIT);
//   glEnable(GL_BLEND);
//   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//   glEnable(GL_DEPTH_TEST);

texture.Bind();
//   do these below: 
//   glActiveTexture(GL_TEXTURE0);
//   glBindTexture(GL_TEXTURE_2D, texture.m_texture);

m_shader.enable();

glDrawArrays(GL_TRIANGLES, 0, 3);


m_shader.disable();


// Restore defaults
glBindVertexArray(0);

CHECK_GL_ERRORS;
在这里,我还将附加我的着色器 顶点着色器:

#version 330

in vec3 position;
in vec2 atextCoord;

uniform mat4 transform;
out vec2 textCoord;

void main() {
   gl_Position = transform * vec4(position, 1.0);
   textCoord = atextCoord;
}
和我的片段着色器:

#version 330

uniform sampler2D diffuse;

out vec4 fragColor;
in vec2 textCoord;

void main() {

   fragColor = vec4(texture(diffuse, textCoord).rgb,1.0) *    vec4(textCoord,0.0,1.0); 
   // texture(...) shows vec4(1,1,1,1)
   // radiant colour can only prove my textCoord is working
   // fragColor = texture(diffuse, textCoord);   <- only shows a default texture
}
#版本330
均匀二维漫反射;
out vec4 fragColor;
在vec2中,textCoord;
void main(){
fragColor=vec4(纹理(漫反射,textCoord).rgb,1.0)*vec4(textCoord,0.0,1.0);
//纹理(…)显示vec4(1,1,1,1)
//发光的颜色只能证明我的textCoord在工作

//fragColor=纹理(漫反射,textCoord);我找到了一种方法让它工作

我复制纹理构造函数中的每一行并将其粘贴到draw(),而不是调用Texture.Bind()。 看起来我在绘制几何体之前制作了一个纹理,这种方法很有效

但我仍然需要知道为什么会这样。对于编码风格,我仍然需要将我的代码放在纹理类中。您介意提供一个以前发生过的解决方案吗


将所有片段粘在一起并编辑结果。您好,刚刚尝试过,但没有成功@Rabbid76@Rabbid76,您好,我的lodepng在编译时没有显示错误消息。这很奇怪。我还尝试了另一个函数:unsigned char*imageData=stbi_load(fileName.c_str())、宽度、高度和数字组件,4);这一个也没有打印出任何错误消息。但似乎仍然没有成功地将图像上载到GL_TEXTURE_2D。@genpfault,我想将它们全部粘贴,但它们实际上位于不同的函数中。@Rabbi76我可能不确定什么是真实数据。不是null?等等。这里发生了一些神奇的事情。我很奇怪。另一个解决方案是:I c将我的构造函数更改为一个名为setUp(fileName)的函数。现在我的构造函数是空的。我没有在上面的代码中初始化实例,而是调用了texture.setUp(“bricks.jpg”),而texture仍然存在,我甚至不需要在绘图中调用texture.Bind()???