SDL2 + OpenGL在着色器上崩溃

时间:2015-03-14 13:36:16

标签: c++ opengl sdl-2

我正在尝试学习SDL2,现代OpenGL等... 下面是我从教程改编的代码,用于SDL2。 我已经设法让其他一切工作,但由于某种原因,着色器部分崩溃了程序。 ..

...我无法让任何控制台输出工作(关于它的任何提示?)。 我正在使用Eclipse。

此外,如果您在代码中找到任何其他可修复的位置,请随时 指出他们。

程序编译时没有任何错误或警告。

#include <windows.h>
#include <GL/glew.h>
#include <GL/gl.h>
#include <SDL2/SDL.h>

#include <fstream>
#include <string>
#include <cmath>
#include <vector>

const int WINDOW_W = 1280;
const int WINDOW_H = 720;
const int WINDOW_X = 200;
const int WINDOW_Y = 200;


// FUNCTIONS

/* Shader loader:
 * Handles loading GLSL vertex and fragment shaders
 */
GLuint ShaderLoad(const char * vertex_file_path, const char * fragment_file_path)
{
    // Vertex shader ID
    GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);

    //Read the vertex shader files
    std::string VertexShaderCode;
    std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);

    if (VertexShaderStream.is_open())
    {
        std::string Line = "";

        while (getline(VertexShaderStream, Line))
        {
            VertexShaderCode += "\n" + Line;
        }

        VertexShaderStream.close();
    }

    GLint Result = GL_FALSE;
    int InfoLogLength;

    // Compile vertex shader
    char const * VertexSourcePointer = VertexShaderCode.c_str();
    glShaderSource(VertexShaderID, 1, &VertexSourcePointer, NULL);
    glCompileShader(VertexShaderID);

    // Check vertex shader
    glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
    glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
    std::vector<char> VertexShaderErrorMessage(InfoLogLength);
    glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);

    // Fragment shader ID
    GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

    std::string FragmentShaderCode;
    std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
    if(FragmentShaderStream.is_open()){
        std::string Line = "";
        while(getline(FragmentShaderStream, Line))
            FragmentShaderCode += "\n" + Line;
        FragmentShaderStream.close();
    }

    // Compile Fragment Shader
    printf("Compiling shader : %s\n", fragment_file_path);
    char const * FragmentSourcePointer = FragmentShaderCode.c_str();
    glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
    glCompileShader(FragmentShaderID);

    // Check Fragment Shader
    glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
    glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
    std::vector<char> FragmentShaderErrorMessage(InfoLogLength);
    glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
    fprintf(stdout, "%s\n", &FragmentShaderErrorMessage[0]);

    // Link the program
    fprintf(stdout, "Linking program\n");
    GLuint ProgramID = glCreateProgram();
    glAttachShader(ProgramID, VertexShaderID);
    glAttachShader(ProgramID, FragmentShaderID);
    glLinkProgram(ProgramID);

    // Check the program
    glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
    glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
    std::vector<char> ProgramErrorMessage(InfoLogLength);
    glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
    fprintf(stdout, "%s\n", &ProgramErrorMessage[0]);

    glDeleteShader(VertexShaderID);
    glDeleteShader(FragmentShaderID);

    return ProgramID;
}



//GLuint programID = ShaderLoad("vshader.glsl", "fshader.glsl");



// Main
int main(int argc, char **argv)
{
    //Variables
    SDL_Window * SDLWindow = NULL;
    SDL_GLContext GLContext;
    SDL_Event event;

    static const GLfloat g_vertex_buffer_data[] = {
       -1.0f, -1.0f, 0.0f,
       1.0f, -1.0f, 0.0f,
       0.0f,  1.0f, 0.0f,
    };

    SDL_Init(SDL_INIT_EVERYTHING);

    SDLWindow = SDL_CreateWindow("ELSA",
                                 WINDOW_X, WINDOW_Y,
                                 WINDOW_W, WINDOW_H,
                                 SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);

    GLContext = SDL_GL_CreateContext(SDLWindow);
    SDL_GL_MakeCurrent(SDLWindow, GLContext);

    SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 4);
    SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 3);
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 24);
    SDL_GL_SetSwapInterval(1);

    // OPENGL INIT

    glewExperimental = GL_TRUE;
    glewInit();

    GLuint vertexbuffer;
    GLuint VertexArrayID;

    glGenVertexArrays(1, &VertexArrayID);
    glBindVertexArray(VertexArrayID);

    // Generate 1 buffer, put the resulting identifier in vertexbuffer
    glGenBuffers(1, &vertexbuffer);

    // The following commands will talk about our 'vertexbuffer' buffer
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

    // Give our vertices to OpenGL.
    glBufferData(GL_ARRAY_BUFFER,
                 sizeof(g_vertex_buffer_data),
                 g_vertex_buffer_data, GL_STATIC_DRAW);




    while(true)
    {
        while (SDL_PollEvent(&event)) // Get all events per frame
        {
            if (event.type == SDL_QUIT)
            {
                return false;
            }
            if (event.type == SDL_WINDOWEVENT_RESIZED)
            {
                glViewport(0, 0,
                           SDL_GetWindowSurface(SDLWindow)->w,
                           SDL_GetWindowSurface(SDLWindow)->h);
            }
        }

        //OpenGL code
        glClearColor(0.15, 0.15, 0.30, 1.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // Use shader
        //glUseProgram(programID);

        glEnableVertexAttribArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

        glDrawArrays(GL_TRIANGLES, 0, 3);

        SDL_GL_SwapWindow(SDLWindow);
        glDisableVertexAttribArray(0);
    }

    SDL_GL_DeleteContext(GLContext);
    SDL_DestroyWindow(SDLWindow);
    SDL_Quit();
    return 0;
}

0 个答案:

没有答案