glCreateShader和glCreateProgram在SDL 2 IOS中返回0

时间:2014-10-03 18:59:42

标签: c++ ios opengl-es opengl-es-2.0 sdl-2

我正在尝试使用c ++和SDL 2在OpenGL ES 2.x中渲染一个简单的三角形。 但glCreateShader和glCreatProgram返回零。 Bellow是我正在使用的代码

#include "SDL.h"
#include <OpenGLES/ES2/gl.h>
#include <OpenGLES/ES2/glext.h>
#include <iostream>
#include <string>
#include <memory>
using namespace std;
GLuint programObject;

class Graphics
{
private:

    SDL_Window* _window;

public:
    Graphics(SDL_Window* window)
    {
        _window = window;
    }

    void update()
    {
        glClearColor(255.0f, 0.0f, 255.0f, 1);
//        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
        GLfloat vVertices[] = {
            0.0f, 0.5f, 0.0f,
            -0.5f, -0.5f, 0.0f,
            0.5f, -0.5f, 0.0f
        };

        glViewport (0, 0, 320, 480);

        glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
        glClear (GL_COLOR_BUFFER_BIT);

        glUseProgram (programObject);

        glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);

        glEnableVertexAttribArray (0);
        glDrawArrays (GL_TRIANGLES, 0, 3);

        SDL_GL_SwapWindow(_window);
    }
};

void UpdateFrame(void* param)
{
    Graphics* graphics = (Graphics*)param;
    graphics->update();
}
///
// Create a shader object, load the shader source, and
// compile the shader.
//
GLuint LoadShader(GLenum type, const GLchar *shaderSrc)
{
    GLuint shader;
    GLint compiled;

    // Create the shader object
    shader = glCreateShader(type);

    if(shader == 0)
    {
        cerr << "Could not create OpenGL shader " << endl;
        return 0;
    }

    // Load the shader source

    glShaderSource(shader, 1, &shaderSrc, NULL);

    // Compile the shader
    glCompileShader(shader);

    // Check the compile status
    glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);

    if(!compiled)
    {
        GLint infoLen = 0;

        glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);

        if(infoLen > 1)
        {
            char* infoLog = (char*)malloc(sizeof(char) * infoLen);

            glGetShaderInfoLog(shader, infoLen, NULL, infoLog);
            SDL_Log("Error compiling shader:\n%s\n", infoLog);
            free(infoLog);
        }

        glDeleteShader(shader);
        return 0;
    }

    return shader;

}
///
// Initialize the shader and program object
//
int Init()
{

    const char vertexShaderString[] =
    "attribute vec4 vPosition;    \n"
    "void main()                  \n"
    "{                            \n"
    "   gl_Position = vPosition;  \n"
    "}                            \n";

    const char fragmentShaderString[] =
    "precision mediump float;\n"\
    "void main()                                  \n"
    "{                                            \n"
    "  gl_FragColor = vec4 ( 1.0, 0.0, 0.0, 1.0 );\n"
    "}                                            \n";

    GLuint vertexShader;
    GLuint fragmentShader;
    GLint linked;

    vertexShader = LoadShader (GL_VERTEX_SHADER, vertexShaderString);
    fragmentShader = LoadShader (GL_FRAGMENT_SHADER, fragmentShaderString);

    programObject = glCreateProgram ();

    if (programObject == 0) {

        cerr << "Could not create OpenGL program" << endl;
        return 0;

    }

    glAttachShader (programObject, vertexShader);
    glAttachShader (programObject, fragmentShader);
    glBindAttribLocation (programObject, 0, "vPosition");
    glLinkProgram (programObject);

    glGetProgramiv (programObject, GL_LINK_STATUS, &linked);

    if (!linked) {

        GLint infoLen = 0;

        glGetProgramiv (programObject, GL_INFO_LOG_LENGTH, &infoLen);

        if (infoLen > 1) {

            char* infoLog = (char*) malloc (sizeof (char) * infoLen);
            glGetProgramInfoLog (programObject, infoLen, NULL, infoLog);
            cerr << "Error linking program: " << infoLog << endl;
            free (infoLog);

        }

        glDeleteProgram (programObject);
        return 0;

    }

    glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
    return true;
}
int EventFilter(void* userdata, SDL_Event* event)
{
    switch (event->type)
    {
        case SDL_FINGERMOTION:
            SDL_Log("Finger Motion");
            return 0;

        case SDL_FINGERDOWN:
            SDL_Log("Finger Down");
            return 0;

        case SDL_FINGERUP:
            SDL_Log("Finger Up");
            return 0;
    }

    return 1;
}


int main(int argc, char** argv)
{

    /* initialize SDL */
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf("Could not initialize SDL\n");
        return 1;
    }

    SDL_DisplayMode displayMode;
    SDL_GetDesktopDisplayMode(0, &displayMode);

    /* create window and renderer */
    SDL_Window* window = SDL_CreateWindow(NULL, 0, 0, displayMode.w, displayMode.h, SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN | SDL_WINDOW_RESIZABLE);

    if (!window) {
        printf("Could not initialize Window\n");
        return 1;
    }

    auto gl = SDL_GL_CreateContext(window);
    if (!Init()) {
        cerr << "Error initializing OpenGL" << endl;
        return 1;
    }

    unique_ptr<Graphics> graphics = unique_ptr<Graphics>(new Graphics(window));
    SDL_iPhoneSetAnimationCallback(window, 1, UpdateFrame, graphics.get());
    SDL_AddEventWatch(EventFilter, NULL);


    //Game Loop
    SDL_Event event;
    auto done = false;
    while (!done)
    {
        SDL_PumpEvents();
        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
                case SDL_QUIT:
                    done = true;
                    break;

                case SDL_APP_DIDENTERFOREGROUND:
                    SDL_Log("SDL_APP_DIDENTERFOREGROUND");
                    break;

                case SDL_APP_DIDENTERBACKGROUND:
                    SDL_Log("SDL_APP_DIDENTERBACKGROUND");
                    break;

                case SDL_APP_LOWMEMORY:
                    SDL_Log("SDL_APP_LOWMEMORY");
                    break;

                case SDL_APP_TERMINATING:
                    SDL_Log("SDL_APP_TERMINATING");
                    break;

                case SDL_APP_WILLENTERBACKGROUND:
                    SDL_Log("SDL_APP_WILLENTERBACKGROUND");
                    break;

                case SDL_APP_WILLENTERFOREGROUND:
                    SDL_Log("SDL_APP_WILLENTERFOREGROUND");
                    break;

                case SDL_WINDOWEVENT:
                {
                    switch (event.window.event)
                    {
                        case SDL_WINDOWEVENT_RESIZED:
                        {
                            SDL_Log("Window %d resized to %dx%d", event.window.windowID, event.window.data1, event.window.data2);

                            break;
                        }
                    }
                }
            }
        }
    }

    SDL_GL_DeleteContext(gl);

    // Done! Close the window, clean-up and exit the program.
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}

也许我在这里遗漏了一些东西,任何帮助都会受到赞赏。 谢谢, 物语

1 个答案:

答案 0 :(得分:2)

这很可能是由SDL创建的上下文问题引起的,特别是没有MAJOR / MINOR和MASK属性的属性,这些属性对iOS,OS X等更为重要

需要做的事情如下。在创建SDL_Window之前,请执行以下操作:

/* Set the correct attributes for MASK and MAJOR version */
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);

/* create window and renderer */
SDL_Window* window = SDL_CreateWindow(NULL, 0, 0, displayMode.w, displayMode.h, SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN | SDL_WINDOW_RESIZABLE);

另一种可能导致相同错误输出的情况是在glBegin / glEnd块内调用glCreateShader或glCreateProgram。