从YouTube复制的OpenGl着色器中的语法错误 [英] Syntax error in OpenGl shader that is copied from YouTube

查看:113
本文介绍了从YouTube复制的OpenGl着色器中的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试学习OpenGl,现在我想在黑色背景上制作一个带有顶点和着色器的简单红色三角形.我从该YouTube视频中复制了代码.我改变了看法.那是我的代码:

I trying to learn OpenGl and now I wanted to make a simple red triangle on a black background with vertices and shaders. I copied the code from that YouTube video. I've changed a view things. That's my code:

#include "prec.h"
struct Vector2
{
    float x, y;
};

struct TrianglePos
{
    Vector2 a, b, c;
};

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::cout << "Failed to compile shader, message: " << 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() {

    if (!glfwInit())
        return -1;


    GLFWwindow *window = glfwCreateWindow(640, 480, "Sand Box GL", NULL, NULL);

    if (!window) {
        std::cout << "problems with window" << std::endl;
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);

    glewExperimental = GL_TRUE;
    if (GLEW_OK != glewInit()) {
        std::cout << "something with glew went wrong" << std::endl;
        return -1;
    }


    TrianglePos trianglePos = {
            -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, sizeof(trianglePos), &trianglePos, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vector2), 0);

    std::string vertexShader =
            "#version 400 core\n"
            "\n"
            "layout(location = 0) in vec4 position;\n"
            "\n"
            "void main() \n"
            "{\n"
            "   gl_Position = position;\n"
            "}\n";

    std::string fragmentShader =
            "#version 400 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";

    const char *versionGL;
    versionGL = (char *) (glGetString(GL_VERSION));
    std::cout << "openGl version: " << versionGL << std::endl;

    if(GL_VERSION_4_0)
        std::cout << "opengl 4.0 supported" << std::endl;

    unsigned int shader = createShader(vertexShader, fragmentShader);
    glUseProgram(shader);

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

        glDrawArrays(GL_TRIANGLES, 0, 3);

        glfwSwapBuffers(window);

        glfwPollEvents();
    }
    glfwTerminate();
    return 0;
}

这是预编译的标头(prec.h):#pragma一次

and that's the precompiled header (prec.h): #pragma once

#ifndef GLEWINIT_PREC_H
#define GLEWINIT_PREC_H
#endif //GLEWINIT_PREC_H

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

#include <iostream>

程序将其打印到控制台(支持OpenGL 4.0"表示 GL_VERSION_4_0 == true ):

the program prints that to the console ("OpenGL 4.0 supported" means that GL_VERSION_4_0 == true):

openGl version: 2.1 INTEL-14.5.22 
opengl 4.0 supported

当我尝试运行它时,我从着色器编译器得到了针对我的顶点和片段着色器的以下错误消息:

When I try to run it I get this error message from the shader Compiler for my vertex and fragment shader:

ERROR: 0:1: '' :  version '400' is not supported
ERROR: 0:1: '' : syntax error: #version
ERROR: 0:3: 'layout' : syntax error: syntax error

当我将 #version 400核心更改为 #version 120 时,我只会得到 layout 的语法错误.因此,我想我弄得一团糟.我可以尝试什么?

When I change the #version 400 core to #version 120 I only get the syntax error for the layout. Because of that I think I messed up something with glew. What could I try?

推荐答案

您的系统不支持OpenGL 4.0.它仅支持OpenGL 2.1.查看输出

Your system does not support OpenGL 4.0. It just supports OpenGL 2.1. See the output

openGl version: 2.1 INTEL-14.5.22 

GLSL 1.20 对应于OpenGL 2.1.降级着色器:

GLSL 1.20 corresponds to OpenGL 2.1. Downgrade the shader:

顶点着色器

#version 120

attribute vec4 position;

void main()
{
    gl_Position = position;
}

片段着色器

#version 120

void main()
{
    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}

分别使用原始字符串文字(

这篇关于从YouTube复制的OpenGl着色器中的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆