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

C++ OpenGL Color at each Vertex not drawn

本文关键字:C++OpenGL 颜色 顶点 绘制      更新时间:2023-10-16

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

VAO只是VBO的集合。

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

在下面的代码中,"顶点"结构定义了顶点的两个属性,即顶点的位置和颜色。因此,需要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 GLFWn");
            exit( EXIT_FAILURE );
    }
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window) {
        glfwTerminate();
        printf("GLFW Failed to startn");
        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: %sn", glewGetErrorString(err));
    }
    fprintf(stderr, "Glew donen");
    fprintf(stderr, "GL VERSION INFO %sn", glGetString(GL_VERSION));
    glfwSetKeyCallback(window, key_callback);
    SetupGeometry();
    while(!glfwWindowShouldClose(window)){
        Render(0);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
}

您似乎没有包含任何着色器。您需要使用片段着色器来设置三角形的颜色值。这个链接应该会对你有所帮助。