【问题标题】:Do not render anything (think its bcs of shaders)不要渲染任何东西(想想它的着色器)
【发布时间】:2015-10-29 12:09:21
【问题描述】:

我的 opengl 程序什么也不渲染。我认为问题出在我的着色器上。谁能告诉我哪里出了问题,或者如果没有问题,请告诉我问题出在哪里?

片段着色器:

#version 330 core
in vec2 TexCoords;
out vec4 color;

uniform sampler2D image;
uniform vec3 spriteColor;

void main()
{  
    color = vec4(spriteColor, 1.0) * texture(image, TexCoords);
}  

顶点着色器

#version 330 core
layout (location = 0) in vec3 vertex;
layout (location = 2) in vec2 text;

out vec2 TexCoords;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
    TexCoords = text;
    gl_Position = projection * view * model * vec4(vertex, 1.0);
}

我用来向着色器发送数据的函数:

void SpriteRenderer::initRenderData()
{

GLfloat taille = 1.0f;
taille /= 2;

int m_tailleCoordTextureBytes = 72 * sizeof(float);
int m_tailleVerticesBytes = 108 * sizeof(float);

    GLuint VBO;
    GLfloat verticesTmp[] = {-taille, -taille, -taille,   taille, -taille, -taille,   taille, taille, -taille,     // Face 1
                       -taille, -taille, -taille,   -taille, taille, -taille,   taille, taille, -taille,     // Face 1

                       taille, -taille, taille,   taille, -taille, -taille,   taille, taille, -taille,       // Face 2
                       taille, -taille, taille,   taille, taille, taille,   taille, taille, -taille,         // Face 2

                       -taille, -taille, taille,   taille, -taille, taille,   taille, -taille, -taille,      // Face 3
                       -taille, -taille, taille,   -taille, -taille, -taille,   taille, -taille, -taille,    // Face 3

                       -taille, -taille, taille,   taille, -taille, taille,   taille, taille, taille,        // Face 4
                       -taille, -taille, taille,   -taille, taille, taille,   taille, taille, taille,        // Face 4

                       -taille, -taille, -taille,   -taille, -taille, taille,   -taille, taille, taille,     // Face 5
                       -taille, -taille, -taille,   -taille, taille, -taille,   -taille, taille, taille,     // Face 5

                       -taille, taille, taille,   taille, taille, taille,   taille, taille, -taille,         // Face 6
                       -taille, taille, taille,   -taille, taille, -taille,   taille, taille, -taille};      // Face 6


    GLfloat coordtextures[] = {0, 0,   1, 0,   1, 1,     // Face 1
                           0, 0,   0, 1,   1, 1,     // Face 1

                           0, 0,   1, 0,   1, 1,     // Face 2
                           0, 0,   0, 1,   1, 1,     // Face 2

                           0, 0,   1, 0,   1, 1,     // Face 3
                           0, 0,   0, 1,   1, 1,     // Face 3

                           0, 0,   1, 0,   1, 1,     // Face 4
                           0, 0,   0, 1,   1, 1,     // Face 4

                           0, 0,   1, 0,   1, 1,     // Face 5
                           0, 0,   0, 1,   1, 1,     // Face 5

                           0, 0,   1, 0,   1, 1,     // Face 6
                           0, 0,   0, 1,   1, 1};    // Face 6

    for (int i = 0; i < 72; i++)
    {
        m_coord_texture[i] = coordtextures[i];
    }

    for (int j = 0; j < 108; j++)
    {
        m_vertices[j] = verticesTmp[j];
    }



    glGenBuffers(1, &VBO);

        glBindBuffer(GL_ARRAY_BUFFER, VBO);
        glBufferData(GL_ARRAY_BUFFER, sizeof(m_vertices)+sizeof(m_coord_texture), 0, GL_STATIC_DRAW);

        glBufferSubData(GL_ARRAY_BUFFER, 0, m_tailleVerticesBytes, m_vertices);
        glBufferSubData(GL_ARRAY_BUFFER, m_tailleVerticesBytes, m_tailleCoordTextureBytes, m_coord_texture);


    glBindBuffer(GL_ARRAY_BUFFER, 0);



    glGenVertexArrays(1, &this->quadVAO);

        glBindVertexArray(this->quadVAO);

        glBindBuffer(GL_ARRAY_BUFFER, VBO);

            // Accès aux vertices
            glEnableVertexAttribArray(0);
            glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), BUFFER_OFFSET(0));

            // Accès aux coord des textures
            glEnableVertexAttribArray(2);
            glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), BUFFER_OFFSET(m_tailleVerticesBytes));


        glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);

【问题讨论】:

    标签: opengl rendering


    【解决方案1】:

    从你的数据来看,好像第二个顶点指针是错误的:

    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat),
                          BUFFER_OFFSET(m_tailleVerticesBytes));
    

    uv 坐标之间的偏移量设置为 3 个浮点数,但由于您只提供其中两个,因此应该是 2 * sizeof(GLfloat)

    【讨论】:

    • 修复了!谢谢 !但不幸的是,我的问题仍然存在。但是我重新阅读了我的代码,发现我无法解释“uniform sampler2D image”的使用;在我的片段着色器中。我将其值设置为 0 但我不知道为什么...我认为该值本身应该具有纹理但我不知道如何传递它...也许您可以解释原因...“ResourceManager:: GetShader("sprite").Use().SetInteger("image", 0);"
    • 纹理采样器必须设置为纹理绑定的纹理单元。见here
    • 谢谢我现在明白了!但我钢铁看不出有什么问题......有人已经遇到过这个问题吗?因为它什么都没有渲染没有错误问题没有......不知道从哪里开始
    • 我本可以找到线索的!我用 glclearcolor 将颜色更改为蓝色,我看到的是我的黑色立方体,没有纹理,对于立方体的每个面,我只有两个三角形中的一个......但我不知道为什么......跨度>
    • 您看到的只有一半的面可能来自背面剔除。您的网格中有不同的缠绕顺序。
    猜你喜欢
    • 2011-07-19
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    • 1970-01-01
    相关资源
    最近更新 更多