C++ I';我正在尝试使用着色器和VBO绘制三角形。为什么我的程序没有';不行?

C++ I';我正在尝试使用着色器和VBO绘制三角形。为什么我的程序没有';不行?,c++,opengl,opengl-es,C++,Opengl,Opengl Es,我曾尝试使用此代码使用VBO和着色器绘制三角形 #include "gl.h" #include <stdlib.h> #include <GLFW/glfw3.h> // utility libraries #include "OGLShader.h" #include "OGLProgram.h" #include "OGLCommon.h" #include "Shaders.h" #include "OGLBuffer.h" using namespace O

我曾尝试使用此代码使用VBO和着色器绘制三角形

#include "gl.h"
#include <stdlib.h>
#include <GLFW/glfw3.h>

// utility libraries
#include "OGLShader.h"
#include "OGLProgram.h"
#include "OGLCommon.h"
#include "Shaders.h"
#include "OGLBuffer.h"

using namespace OpenGL;

GLFWwindow *window;
const int window_width = 800;
const int window_height = 600;

Shader **shaders;
Program *mainProgram;

int verticesCount = 3;
float triangle[] = {
    0.0f, 0.5f, 0.0f,
    0.5f, -0.5f, 0.0f,
    -0.5f, -0.5f, 0.0f,
};
int indicesCount = 3;
int indices[] = {
    0, 1, 2
};
Buffer *verticesBuffer;

void init() {
    glViewport(0, 0, window_width, window_height);
    glClearColor(0, 0, 0, 1);

    shaders = new OpenGL::Shader*[2];
     // this code creates and compiles shaders, links shader program
    shaders[0] = new OpenGL::Shader(GL_VERTEX_SHADER, OpenGL::vertexShaderSource);
    shaders[1] = new OpenGL::Shader(GL_FRAGMENT_SHADER, OpenGL::fragmentShaderSource);
    mainProgram = new OpenGL::Program(2, shaders);
    mainProgram->use(); // use program

    vertices_attrib_location = 0;
    colors_attrib_location = 1;
    glBindAttribLocation(mainProgram->getId(), vertices_attrib_location, "iVertex");
    glBindAttribLocation(mainProgram->getId(), colors_attrib_location, "iColor");

    verticesBuffer = new Buffer(GL_ARRAY_BUFFER);
    verticesBuffer->data(verticesCount*3*sizeof(float), triangle, GL_STATIC_DRAW);
}

void deinit() {
    delete verticesBuffer;

    delete shaders[0];
    delete shaders[1];
    delete []shaders;
    delete mainProgram;
}

void display() {
    glClear(GL_COLOR_BUFFER_BIT);

    glVertexAttrib3f(colors_attrib_location, 1, 0, 1);
    verticesBuffer->bind();
    glEnableVertexAttribArray(vertices_attrib_location);
    glVertexAttribPointer(vertices_attrib_location, 3, GL_FLOAT, GL_FALSE, 0, nullptr);

    glDrawArrays(GL_TRIANGLES, 0, 3);
}

void update(double deltaTime) {

}

int main(int argc, char **argv) {
    if (!glfwInit()) {
        exit(1);
    }
    glfwWindowHint(GLFW_SAMPLES, 1);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
    window = glfwCreateWindow(window_width, window_height, "Test", nullptr, nullptr);
    if (!window) {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }
    glfwMakeContextCurrent(window);
    glfwSwapInterval(1);
    init();

    double lastTime = 0.0;
    while (!glfwWindowShouldClose(window)) {
        display();
        double curTime = glfwGetTime();
        update(curTime - lastTime);
        lastTime = curTime;

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    deinit();
    glfwDestroyWindow(window);
    glfwTerminate();

    return 0;
}

我尝试在Intel和ATI显卡上运行此代码,但我在屏幕上看到的所有内容都是空的黑色窗口。我做错了什么?

首先尝试使用glGetError查看是否有错误。一个问题是调用
GLBindAttriblLocation()
太晚了。例如,请参见我答案的第一部分:@MaticOblak no,我使用glGetError和OpenGL Profilers没有发现错误。好的,太好了。接下来,尝试使用不同的清晰颜色,如果屏幕颜色发生变化,将确认问题实际上存在于图形本身中。同时尝试将碎片着色器更改为使用gl_FragColor=vec4(1,1,1,1);因为你如何使用这种颜色似乎很可疑。如果是这样的话,应该画一个白色三角形。
namespace OpenGL {

std::string vertexShaderSource =
    "#version 120\n\
    attribute vec3 iVertex;\
    attribute vec3 iColor;\
    varying vec3 oColor;\
    void main(void)\n\
    {\n\
        oColor = iColor;\n\
        \n\
        gl_Position = vec4(iVertex, 1);\n\
    }\n\
    ";

    std::string fragmentShaderSource =
    "#version 120\n\
    varying vec3 oColor;\
    varying vec2 oTexCoord;\n\
    void main(void)\n\
    {\n\
        gl_FragColor = vec4(oColor, 1);\n\
    }\n\
    ";

}