C++ 使用vertexShader中的OpenGl未声明标识符,我在顶点着色器中绘制三角形时遇到问题

C++ 使用vertexShader中的OpenGl未声明标识符,我在顶点着色器中绘制三角形时遇到问题,c++,opengl,glsl,glfw,C++,Opengl,Glsl,Glfw,以及: 当我编译这个脚本时,我得到了上面的错误,我真的不明白为什么它在VertexShader中给出了这个错误 "assign" : cannot convert from "attribute 4-component vector of highp float" to "highp float" #包括 #包括 #包括 静态无符号整数编译器标头(无符号整数类型,常量std::string和source) { unsigned int

以及:

当我编译这个脚本时,我得到了上面的错误,我真的不明白为什么它在VertexShader中给出了这个错误

"assign" : cannot convert from "attribute 4-component vector of highp float" to "highp float"
#包括
#包括
#包括
静态无符号整数编译器标头(无符号整数类型,常量std::string和source)
{
unsigned int id=glCreateShader(类型);
const char*src=source.c_str();
glShaderSource(id,1,&src,nullptr);
glCompileShader(id);
int结果;
glGetShaderiv(id、GL\u编译状态和结果);
如果(结果==GL\u假)
{
整数长度;
glGetShaderiv(标识、总账信息、日志长度和长度);
char*message=(char*)alloca(长度*sizeof(char));
glGetShaderInfoLog(id、长度和长度、消息);

std::coutGLSL区分大小写,您应该使用gl\u Position而不是gl\u Position。不幸的是,我无法再现第二个错误。

在顶点着色器中,将
gl\u Position
替换为
gl\u Position
不是OpenGL着色语言中的内置变量(GLSL),因为GLSL区分大小写。对于GLSL中的内置变量列表。是的,它可以工作!感谢您的帮助和额外信息!噢,它可以工作!使用大写,两个错误都会消失,谢谢!
"assign" : cannot convert from "attribute 4-component vector of highp float" to "highp float"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>

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 == GL_FALSE)
    {
        int length;
        glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
        char* message = (char*)alloca(length * sizeof(char));
        glGetShaderInfoLog(id, length, &length, message);
        std::cout << "falló al compilar el shader!" << (type == GL_VERTEX_SHADER ? "vertex" : 
        "fragment") << "shader!" <<
         std::endl;
    std::cout << message << std::endl;
    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;

    /* Initialize the library */
    if (!glfwInit())
        return -1;



    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    if (glewInit() != GLEW_OK)
        std::cout << "error" << std::endl;

    std::cout << glGetString(GL_VERSION) << std::endl;

    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, sizeof(float) * 2, 0);


    std::string vertexShader =
        "#version 330 core\n"
        "\n"
        "layout(location = 0) in vec4 position;\n"
        "\n"
        "void main()\n"
        "{\n"
        "gl_position = position;\n"
        "}\n";
    std::string fragmentShader =
        "#version 330 core\n"
        "\n"
        "layout(location = 0) out vec4 color;\n"
        "\n"
        "void main()\n"
        "{\n"
        "   color = vec4(1.0,0.0,0.0,1.0); \n"
        "}\n";
    unsigned int shader = CreateShader(vertexShader,fragmentShader);
    glUseProgram(shader);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        glDrawArrays(GL_TRIANGLES, 0, 3);
   

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glDeleteProgram(shader);
    glfwTerminate();
    return 0;
}