未绘制每个顶点的C ++ OpenGL颜色

时间:2015-04-03 22:37:02

标签: c++ opengl

我是OpenGL的新手,目前我正在努力了解VAO和VBO。

VAO只是VBO的集合。

每个VBO是一个对象的属性,对象顶点的坐标,每个对象顶点的颜色等。

在下面的代码中,Vertex结构定义了顶点的2个属性,即顶点的位置和颜色。因此,需要2个VBO来存储这些信息。

我的问题是:我能够在屏幕上绘制三角形,但似乎并没有绘制每个顶点的颜色。为什么会这样?

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>

// create vertex buffer
GLuint vertexbuffer;
struct Vertex{
    GLdouble position[3];
    GLfloat color[3];
};
static const GLfloat vertex_data[] = {
    -0.7f, -0.7f, 0.0f,
    0.7f, -0.7f, 0.0f,
    0.0f, 1.0f, 0.0f
};

void SetupGeometry(){
    const struct Vertex triangle[3] = {
        {{-0.7, -0.7, 0.0}, {1.0f, 0.0f, 0.0f}},
        {{0.7, -0.7, 0.0}, {0.0f, 1.0f, 0.0f}},
        {{0.0, 1.0, 0.0}, {0.0f, 0.0f, 1.0f}}
    };

    //GLuint VertexArrayID;
    //glGenVertexArrays(1, &VertexArrayID);
    //glBindVertexArray(VertexArrayID);

    //generate 1 buffer, put the resulting identifier in vertex buffer
    glGenBuffers(1, &vertexbuffer);

    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

    // give our vertices to OpenGL
    glBufferData(GL_ARRAY_BUFFER, 3*sizeof(struct Vertex), triangle, GL_STATIC_DRAW);

    // GLuint index, GLuint size, GLenum type, GLboolean normalized, GLsizei stride, const void *offset
    glVertexAttribPointer(0, 3, GL_DOUBLE, GL_FALSE, sizeof(struct Vertex), (void*) offsetof(struct Vertex, position));

    // any newly created VAO disables array access for all attributes
    // array access is enabled by binding the VAO in SetupGeometry and calling:
    glEnableVertexAttribArray(0);

    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), (void*) offsetof(struct Vertex, color));
    glEnableVertexAttribArray(1);
}

void SetupShaders(void){

}

void Render(int i){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glDrawArrays(GL_LINE_LOOP, 0, 3);
}

static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods){
    if((key == GLFW_KEY_ESCAPE || key == GLFW_KEY_Q) && action != GLFW_PRESS){
        glfwSetWindowShouldClose(window, GL_TRUE);
    }
}

int main( void ) {
    /* Create a windowed mode window and its OpenGL context */
    GLFWwindow* window;
    if( !glfwInit() ) {
            printf("Failed to start GLFW\n");
            exit( EXIT_FAILURE );
    }
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window) {
        glfwTerminate();
        printf("GLFW Failed to start\n");
        return -1;
    }
    /* Make the window's context current */
    glfwMakeContextCurrent(window); // IMPORTANT: Must be done so glew recognises OpenGL
    glfwWindowHint(GLFW_SAMPLES, 4);
    int err = glewInit();
    if (glewInit() != GLEW_OK) {
        /* Problem: glewInit failed, something is seriously wrong. */
        fprintf(stderr, "Error initializing GLEW: %s\n", glewGetErrorString(err));
    }
    fprintf(stderr, "Glew done\n");
    fprintf(stderr, "GL VERSION INFO %s\n", glGetString(GL_VERSION));
    glfwSetKeyCallback(window, key_callback);

    SetupGeometry();
    while(!glfwWindowShouldClose(window)){
        Render(0);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
}

1 个答案:

答案 0 :(得分:1)

您似乎没有包含任何着色器。您需要使用片段着色器设置三角形的颜色值。 This link可以帮助你。