opengl 第一个顶点在0 0处渲染,而不是在指定坐标glfw处渲染

s4n0splo  于 2022-12-18  发布在  其他
关注(0)|答案(1)|浏览(115)

我在youtube上跟着The Cherno'sx 1 e1f1x学习着色器,观看并用c++编写实现,但是渲染三角形的第一个顶点出现在(0.0f,0.0f)而不是指定的坐标,指定的坐标为(-0.5华氏度,0.5f),我也不知道为什么,我觉得它看起来不对,因为我没有写着色器来告诉计算机如何处理顶点数据(现在只是坐标)。这是一张三角形的照片:

我正在使用GLFW 4.6.0 - Build 31.0.101.2111。这是正在运行的代码:

#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>

static unsigned int CompileShader(unsigned int type, const std::string& source) {
    unsigned int id = glCreateShader(type);
    const char* src = source.c_str();
    glShaderSource(id, 1, &src, nullptr);
    glCompileShader(id);

    int result;
    glGetShaderiv(id, GL_COMPILE_STATUS, &result);

    if (!result) {
        int length;
        glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
        char* message = (char*) alloca(length * sizeof(char));
        glGetShaderInfoLog(id, length, &length, message);
        std::cerr << "Failed to compile " <<
            (type == GL_VERTEX_SHADER ? "vertex" : type == GL_FRAGMENT_SHADER ? "fragment" : "")
            << " shader!" << '\n';
        std::cerr << message << '\n';
        glDeleteShader(id);
        return 0;
    }

    return id;
}

static unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader) {
    unsigned int program = glCreateProgram();
    unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
    unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);

    glAttachShader(program, vs);
    glAttachShader(program, fs);

    glLinkProgram(program);

    glValidateProgram(program);

    glDeleteShader(vs);
    glDeleteShader(fs);

    return program;
}

int main(void) {
    GLFWwindow* window;

    if (!glfwInit()) return -1;

    window = glfwCreateWindow(840, 680, "Window", NULL, NULL);
    if (!window) {
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);

    if (glewInit() != GLEW_OK) return -1;
    std::cout << glGetString(GL_VERSION) << '\n';

    float positions[6] = {
        -0.5f, 0.5f,
        0.0f, 0.5f,
        0.5f, -0.5f
    };
    unsigned int buffer;
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), 0);

    std::string vertexShader = R"(
        #version 330 core

        layout(location = 0) in vec4 position;

        void main() {
            gl_Position = position;
        }
    )";

    std::string fragmentShader =R"(
        #version 330 core

        layout(location = 0) out vec4 color;

        void main() {
            color = vec4(1.0, 0.0, 0.0, 1.0);
        }
    )";
    unsigned int shader = CreateShader(vertexShader, fragmentShader);
    glUseProgram(shader);

    while (!glfwWindowShouldClose(window)) {
        glClear(GL_COLOR_BUFFER_BIT);

        glDrawArrays(GL_TRIANGLES, 1, 3);

        glfwSwapBuffers(window);

        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

提前感谢您给予的任何帮助!

h9vpoimq

h9vpoimq1#

第一个顶点的起始索引为0,但不是1:
glDrawArrays(GL_TRIANGLES, 1, 3);

glDrawArrays(GL_TRIANGLES, 0, 3);

相关问题