Open GL ES 2.0为什么我的VBO不工作?

问题描述 投票:0回答:1

嘿那里我试图通过vbo在OpenGL中绘制一个带有2个三角形的简单四边形。但是我多次看了我的代码,我看不到我错过了什么。我不是没有Open GL专家,代码工作正常,没有任何缓冲区但是当我切换到VBO我不再看到任何东西了。 OpenGL也不提供任何有用的错误。

Image::Image(Graphics * GFX)
{

    glm::vec2 corners[2];
    corners[0] = glm::vec2(0, 0);
    corners[1] = glm::vec2(1, 1);

    vertices = new GLfloat[30]
    {
        //Vertices XYZ                      TexCoord X,Y
        corners[0].x, corners[0].y, 0.0f,   0,0,
        corners[0].x, corners[1].y, 0.0f,   0,1,
        corners[1].x, corners[0].y, 0.0f,   1,0,

        corners[1].x,corners[1].y,0.0f,     1,1,
        corners[0].x, corners[1].y,0.0f,    0,1,
        corners[1].x, corners[0].y, 0.0f,   1,0
    };

    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    //Setting up some stuff
    const char *vertexShaderSource =
        "attribute vec4 vPosition; \n"
        "attribute vec2 vTexCoord; \n"
        "void main() \n"
        "{ \n"
        " gl_Position = vPosition; \n"
        "} \n";
    const char *fragmentShaderSource =
        "precision mediump float; \n"
        "void main() \n"
        "{ \n"
        " gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); \n"
        "} \n";


    // Load and compile the vertex/fragment shaders
    vertexShader = GFX->LoadShader(GL_VERTEX_SHADER, (const char*)vertexShaderSource);
    fragmentShader = GFX->LoadShader(GL_FRAGMENT_SHADER, (const char*)fragmentShaderSource);

    // Create the program object    
    programObject = glCreateProgram();


    // now we have the V and F shaders  attach them to the progam object
    glAttachShader(programObject, vertexShader);
    glAttachShader(programObject, fragmentShader);

    // Link the program
    glLinkProgram(programObject);
    // Check the link status

    // Link the program
    GLint AreTheylinked;
    glGetProgramiv(programObject, GL_LINK_STATUS, &AreTheylinked);
    if (!AreTheylinked)
    {
        GLint RetinfoLen = 0;
        // check and report any errors
        glGetProgramiv(programObject, GL_INFO_LOG_LENGTH, &RetinfoLen);
        if (RetinfoLen > 1)
        {
            GLchar* infoLog = (GLchar*)malloc(sizeof(char) * RetinfoLen);
            glGetProgramInfoLog(programObject, RetinfoLen, NULL, infoLog);
            fprintf(stderr, "Error linking program:\n%s\n", infoLog);
            free(infoLog);
        }
        glDeleteProgram(programObject);
    }

    positionLocation = glGetAttribLocation(programObject, "vPosition");
    textureCoordLocation = glGetAttribLocation(programObject, "vTexCoord");

    if (glGetError() == GL_NO_ERROR) {}
    else
        printf("Init failed!\n");
    //End setting up

}

Image::~Image()
{
}

void Image::Draw()
{
    std::cout << "Calling Draw on Image" << std::endl;
    glUseProgram(programObject);
    glEnable(GL_DEPTH_TEST);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);

    GLsizei stride = (5) * sizeof(GLfloat);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, 0);
    glEnableVertexAttribArray(0);
    glDrawArrays(GL_TRIANGLES, 0, 6);
    if (glGetError() != GL_NO_ERROR)
    {
        printf("UI Draw error\n");
    }
    glDisableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

}

以及我如何加载着色器:

GLuint Graphics::LoadShader(GLenum type, const char *shaderSrc)
{
    // 1st create the shader object
    GLuint TheShader = glCreateShader(type);

    if (TheShader == 0) return FALSE; // can't allocate so stop.

// pass the shader source then compile it
    glShaderSource(TheShader, 1, &shaderSrc, NULL);
    glCompileShader(TheShader);

    GLint  IsItCompiled;

    // After the compile we need to check the status and report any errors
    glGetShaderiv(TheShader, GL_COMPILE_STATUS, &IsItCompiled);
    if (!IsItCompiled)
    {
        GLint RetinfoLen = 0;
        glGetShaderiv(TheShader, GL_INFO_LOG_LENGTH, &RetinfoLen);
        if (RetinfoLen > 1)
        { // standard output for errors
            char* infoLog = (char*)malloc(sizeof(char) * RetinfoLen);
            glGetShaderInfoLog(TheShader, RetinfoLen, NULL, infoLog);
            fprintf(stderr, "Error compiling this shader:\n%s\n", infoLog);
            free(infoLog);
        }
        glDeleteShader(TheShader);
        return FALSE;
    }
    return TheShader;
}

它以前没有缓冲区工作正常,并通过使用以下方式看到一个白色方块:

glVertexAttribPointer(0, 6, GL_FLOAT, GL_FALSE, 0, vertices);

但是现在我想通过VBO将纹理坐标添加到我的四边形但是没有任何显示。

Smoothy101

c++ opengl-es drawing opengl-es-2.0 vbo
1个回答
0
投票

你的缓冲区创建看起来很好 - 问题是你的属性指针设置。您添加了纹理坐标,这些坐标已更改每个顶点的数据布局,但您没有在代码中处理它。

你需要这样的东西:

glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, stride, 0);
glVertexAttribPointer(textureCoordLocation, 2, GL_FLOAT, GL_FALSE, stride, 8);

... 不是这个:

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, 0);
© www.soinside.com 2019 - 2024. All rights reserved.