简体   繁体   English

OpenGL 3.3无法渲染任何纹理(黑色纹理,C ++,GLFW / SOIL)

[英]OpenGL 3.3 no texture gets rendered (black texture, C++, GLFW/SOIL)

I'm currently learning OpenGL 3.3 using C++ and GLFW (and SOIL for loading image files to create textures). 我目前正在使用C ++和GLFW(以及用于加载图像文件以创建纹理的SOIL)学习OpenGL 3.3。 I've implemented a batch rendering system that takes vertex information, maps the VBO data to a buffer variable, writes submitted vertex information to that buffer every frame and renders said frame. 我已经实现了一个批处理渲染系统,该系统采用顶点信息,将VBO数据映射到缓冲区变量,每帧将提交的顶点信息写入该缓冲区并渲染该帧。

The renderer.cpp looks like this: http://pastebin.com/N6nWdew6 while the VertexData struct is defined in the renderer.hpp as: renderer.cpp看起来像这样: http ://pastebin.com/N6nWdew6,而在renderer.hpp中将VertexData结构定义为:

struct VertexData {
    VertexData()
        :   vertex(glm::vec3(0.0f, 0.0f, 0.0f)),
            color(glm::vec4(0.0f, 0.0f, 0.0f, 0.0f)),
            texCoord(glm::vec2(0.0f, 0.0f)),
            tid(0) {};

    VertexData(glm::vec3 vertex, glm::vec4 color, glm::vec2 texCoord, GLuint tid)
        :   vertex(vertex),
            color(color),
            texCoord(texCoord),
            tid(tid) {};

    ~VertexData() {};

    glm::vec3   vertex;
    glm::vec4   color;
    glm::vec2   texCoord;
    GLuint      tid;
};

Now, submitting and rendering actual vertices, color information, texture coordinates etc. seems to work, but the texture still doesn't get rendered, all I get is a black square. 现在,提交和渲染实际的顶点,颜色信息,纹理坐标等似乎可以正常工作,但是纹理仍然无法渲染,我得到的只是一个黑色正方形。 The most important bits of the actual submission in the main loop look like this (warning: looks bad, just for testing purposes; I tried to shorten it the best I could): 主循环中实际提交的最重要部分如下所示(警告:看起来很糟糕,仅出于测试目的;我已尽力将其缩短)。

[...]

/*Static variables in the window class, just for testing purposes*/
Shader*                 Window::_exampleShader = nullptr;
Texture*                Window::_exampleTexture = nullptr;
Renderer::VertexData    Window::vA;
Renderer::VertexData    Window::vB;
Renderer::VertexData    Window::vC;
Renderer::VertexData    Window::vD;

[...]

/*Initialization in the window constructor*/
_exampleShader  = new Shader("../src/glsl/basicVertex.glsl", "../src/glsl/basicFragment.glsl");
_exampleTexture = new Texture("../res/test.png");

/*TOP LEFT*/
vA.vertex       = glm::vec3(-0.5f, 0.5f, 0.0f);
vA.color        = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
vA.texCoord     = glm::vec2(0.0f, 1.0f);
vA.tid          = _exampleTexture->getTextureID();

/*TOP RIGHT*/
vB.vertex       = glm::vec3(0.5f, 0.5f, 0.0f);
vB.color        = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
vB.texCoord     = glm::vec2(1.0f, 1.0f);
vB.tid          = _exampleTexture->getTextureID();

/*BOTTOM LEFT*/
vC.vertex       = glm::vec3(-0.5f, -0.5f, 0.0f);
vC.color        = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
vC.texCoord     = glm::vec2(0.0f, 0.0f);
vC.tid          = _exampleTexture->getTextureID();

/*BOTTOM RIGHT*/
vD.vertex       = glm::vec3(0.5f, -0.5f, 0.0f);
vD.color        = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
vD.texCoord     = glm::vec2(1.0f, 0.0f);
vD.tid          = _exampleTexture->getTextureID();

[...]

/*Submission in the main loop*/
_exampleShader->bind();
_exampleTexture->bind();

_exampleShader->setUniformMat4("model_matrix", glm::mat4(1.0f));
_exampleShader->setUniformMat4("view_matrix", glm::lookAt(glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 1.0f, 0.0f)));
_exampleShader->setUniformMat4("projection_matrix", glm::perspective(ConversionUtils::convertFOV(90.0f), getAspectRatio(), 0.0001f, 100.0f));

Renderer::begin();      
    Renderer::submit(vC);
    Renderer::submit(vA);
    Renderer::submit(vB);

    Renderer::submit(vC);
    Renderer::submit(vD);
    Renderer::submit(vB);
Renderer::end();

glfwSwapBuffers(_window);

[...]

As you can see, I create some example vertex data and submit that every frame. 如您所见,我创建了一些示例顶点数据并将其提交给每一帧。 Now, the last bits of code are my vertex and fragment shaders: 现在,最后的代码是我的顶点和片段着色器:

Vertex Shader: 顶点着色器:

#version 330 core

layout(location = 0) in vec3 v_vertex;
layout(location = 1) in vec4 v_color;
layout(location = 2) in vec2 v_texCoord;
layout(location = 3) in uint v_tid;

out vec4 f_color;
out vec2 f_texCoord;
flat out uint f_tid;

uniform mat4 model_matrix;
uniform mat4 view_matrix;
uniform mat4 projection_matrix;

void main() {
    f_color     = v_color;
    f_texCoord  = v_texCoord;
    f_tid       = v_tid;

    mat4 mvp = projection_matrix * view_matrix * model_matrix;
    gl_Position = mvp * vec4(v_vertex, 1.0);
}

Fragment shader: 片段着色器:

#version 330 core

in vec4 f_color;
in vec2 f_texCoord;
flat in uint f_tid;

out vec4 color;

uniform sampler2D texSampler;

void main() {
    color = texture(texSampler, f_texCoord);
}

That's what I'm working with. 这就是我正在使用的。 Now, I already did a bunch of googling and even asked on the OpenGL channel @ freenode, but no one could help me with this. 现在,我已经进行了一堆谷歌搜索,甚至在OpenGL频道@ freenode上询问了,但是没有人可以帮我这个忙。 I removed deprecated functionality by using the core 3.3 profile, even the forward compatible profile, despite the fact that I should not be doing this. 尽管我不应该这样做,但我通过使用核心3.3配置文件(甚至是向前兼容的配置文件)删除了不推荐使用的功能。 Other things I already checked: 我已经检查过的其他内容:

  • I'm only using one thread with a valid GL context, so there's probably no problems regarding "packaging an OGL object in a C++ class" 我只使用一个具有有效GL上下文的线程,因此“在C ++类中打包OGL对象”可能没有问题
  • Both the shader and the texture are bound; 着色器和纹理都已绑定。 since the texture sampler and the active texture are 0 by default, that shouldn't cause any problems either 由于纹理采样器和活动纹理默认情况下为0,因此也不会引起任何问题
  • I am actually loading test.png properly (texture.cpp if you're interested: http://pastebin.com/hWGTCe5C ) 我实际上正在正确加载test.png(如果您感兴趣,请加载texture.cpp: http ://pastebin.com/hWGTCe5C)
  • I am not using deprecated functionality afaik, even the SOIL functions that use deprecated functionality aren't called; 我没有使用过时的功能afaik,甚至没有调用使用过时功能的SOIL函数; loading the image via SDL2 und SDL2_image, which I did before switching libraries, caused the same problem 在切换库之前通过SDL2和SDL2_image加载图像导致了相同的问题
  • The shader gets valid texture coordinates (tested by outputting them as colors to the square I'm rendering) 着色器获取有效的纹理坐标(通过将其作为颜色输出到我正在渲染的正方形进行测试)
  • Neither the shader nor the compiler give me any warnings/errors, despite the -Wall compiler flag being set, glGetError() doesn't help either 尽管设置了-Wall编译器标志,但是无论着色器还是编译器都不会向我发出任何警告/错误,glGetError()也不起作用

Other information that might be useful: I've tested this on both Intel and Nvidia GPUs using their most recent drivers on Antergos (Arch) Linux (one on Wayland, one on X11). 其他可能有用的信息:我已经使用Intel和Nvidia GPU在Antergos(Arch)Linux上的最新驱动程序(在Wayland上使用一个,在X11上使用一个)对它们进行了测试。 64-bit architecture. 64位架构。 Most recent versions of the libraries (GLFW3, SOIL, GLEW and GLM). 库的最新版本(GLFW3,SOIL,GLEW和GLM)。 Compiled using Clang++. 使用Clang ++编译。 Valgrind doesn't give me any useful output either. Valgrind也没有给我任何有用的输出。

Me and the IRC chatroom are running out of ideas - what could cause this? 我和IRC聊天室的想法都用光了-是什么原因引起的?

As @RetoKoradi said in his comment, you are not actually passing any parameters to your texture, therefore your texture is not complete. 正如@RetoKoradi在他的评论中所说,您实际上并没有将任何参数传递给纹理,因此您的纹理并不完整。

Try to add in texture.cpp something like : 尝试在texture.cpp中添加如下内容:

glBindTexture(GL_TEXTURE_2D, texture_id);
glTexImage2D(GL_TEXTURE_2D, 0, your_internal_format, width, height, 0, your_format, your_type, texture_pointer);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, your_behavior);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, your_behavior);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);

or 要么

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);

or 要么

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);

you can find more informations about texture completness here : OpenGL Common Mistakes . 您可以在此处找到有关纹理完成度的更多信息: OpenGL常见错误 Note that the problem @RetoKoradi is refering about is addressed in this link. 请注意,此链接中已解决@RetoKoradi所涉及的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM