我正在尝试使用着色器和VBO绘制三角形。为什么我的程序不起作用?

时间:2015-06-19 12:34:18

标签: 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 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;
}

这是我的着色器:

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\
    ";

}

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

0 个答案:

没有答案