为什么OpenGL着色器无法正确编译?

时间:2018-01-23 22:07:57

标签: c++ opengl sdl sdl-2

这个程序应该创建一个新窗口,将其颜色为蓝色作为背景颜色,然后在屏幕上绘制一个三角形并用橙色填充:

#include <stdio.h>
#include <string>
#include <iostream>

#include <SDL.h>
#include "SDL_CheckError.h"
#include <OpenGL/gl3.h>
#include <OpenGL/gl3ext.h>

const char *vertexShaderSource =
                             "layout(location = 0) in vec3 aPos;\n"
                             "void main()\n"
                             "{\n"
                             "gl_Position = vec4(aPos.x, aPos.y,              aPos.z, 1.0);\n"
                             "}\0";
const char *fragmentShaderSource =
                               "out vec4 FragColor;\n"
                               "void main()\n"
                               "{\n"
                               "FragColor = vec4(1.0, 0.5, 0.2,  1.0);\n"
                               "}\n\0";


int main() {
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize   SDL: %s", SDL_GetError());
    return 3;
}

// Setup the window
SDL_Window *window;
SDL_Renderer *renderer;
// Window mode MUST include SDL_WINDOW_OPENGL for use with OpenGL.
SDL_CreateWindowAndRenderer(720, 480, SDL_WINDOW_OPENGL, &window, &renderer);
SDL_GL_LoadLibrary(NULL);
// Create an OpenGL context associated with the window.
SDL_GLContext glcontext = SDL_GL_CreateContext(window);
SDL_CheckError(__LINE__);
// This makes our buffer swap syncronized with the monitor's vertical refresh
SDL_GL_SetSwapInterval(1);
// Set up window
glViewport(0, 0, 720, 480);

// build and compile our shader program
// ------------------------------------
// vertex shader
int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
// check for shader compile errors
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
    glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
    std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// fragment shader
int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
// check for shader compile errors
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
    glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
    std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// link shaders
int shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
// check for linking errors
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
    glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
    std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);

// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
float vertices[] = {
    0.5f,  0.5f, 0.0f,  // top right
    0.5f, -0.5f, 0.0f,  // bottom right
    -0.5f, -0.5f, 0.0f,  // bottom left
    -0.5f,  0.5f, 0.0f   // top left
};
unsigned int indices[] = {  // note that we start from 0!
    0, 1, 3,  // first Triangle
    1, 2, 3   // second Triangle
};
unsigned int VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(VAO);

glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

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

// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
glBindBuffer(GL_ARRAY_BUFFER, 0);

// remember: do NOT unbind the EBO while a VAO is active as the bound element buffer object IS stored in the VAO; keep the EBO bound.
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

// You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
// VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
glBindVertexArray(0);

// Render loop

SDL_Event event;

while (1) {
    // Checking for quit events.
    SDL_PollEvent(&event);
    if (event.type == SDL_QUIT) {
        break;
    }

    // render
    // ------
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    // draw our first triangle
    glUseProgram(shaderProgram);
    glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
    //glDrawArrays(GL_TRIANGLES, 0, 6);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
    // glBindVertexArray(0); // no need to unbind it every time
    SDL_GL_SwapWindow(window);

}

// De-allocate resources
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);

// Once finished with OpenGL functions, the SDL_GLContext can be deleted.
SDL_GL_DeleteContext(glcontext);
SDL_Quit();

return 0;
}

它正确编译,背景颜色为蓝色,就像我想要的那样。但是,不显示三角形,我收到这些错误:

ERROR::SHADER::VERTEX::COMPILATION_FAILED
ERROR: 0:1: 'layout' : syntax error: syntax error

ERROR::SHADER::FRAGMENT::COMPILATION_FAILED
ERROR: 0:1: Invalid storage qualifiers 'out' in global variable context
ERROR: 0:4: Use of undeclared identifier 'FragColor'

ERROR::SHADER::PROGRAM::LINKING_FAILED
ERROR: One or more attached shaders not successfully compiled

发生了什么事?我似乎无法弄明白。如果它是相关的,我使用Xcode,运行Mac OSX 10.13.2,我使用的是SDL 2.0版

1 个答案:

答案 0 :(得分:3)

在创建窗口时,必须在OSX上请求版本化的Core上下文(它们仅为GL 2.1之后的任何内容提供Core上下文):

SSH_ASKPASS="$(cat /run/secrets/password)"

如果SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 ); SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 3 ); SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE ); SDL_Window* window = SDL_CreateWindow ( "SDL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 720, 480, SDL_WINDOW_OPENGL ); SDL_GLContext glcontext = SDL_GL_CreateContext(window); SDL_CreateWindow()成功,您应该可以在着色器中使用SDL_GL_CreateContext()