Mac 上的 SDL2 + Opengl 黑色 Window

SDL2 + Opengl Black Window on Mac

我正在尝试在 mac osx yosemite 上使用 SDL2 和 OpenGL,我遵循了 SDL 页面上的示例以及 Lazy Foo 页面中的示例 http://lazyfoo.net/tutorials/SDL/50_SDL_and_opengl_2/index.php 但每次我 运行 代码,我得到相同的结果,一个空白的 window 和黑色背景,我已经在 google 上搜索了一段时间,但我仍然没有得到任何解决方案,我将不胜感激

下面是我的代码

/*
 * Main.cpp
 *
 *  Created on: Sep 15, 2015
 *      Author: Damian-Machine
 */
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include<SDL.h>
#include<SDL_opengl.h>
#include <stdio.h>
#include <string>
#include<iostream>
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480

bool init();

bool initGL();

void handleKeys(unsigned char key, int x, int y);

void update();

void render();

void close();

SDL_Window *gWindow = NULL;

SDL_GLContext gContext;

bool gRenderQuad = true;

int main(){
    SDL_Event e;
    bool quit = false;
    if(!init()){
        printf("Failed to initialize!\n");
        return 0;
    }
    SDL_StartTextInput();


    while(!quit){
        while(SDL_PollEvent(&e) !=0){
            if(e.type == SDL_QUIT){
                quit = true;
            }else if (e.type == SDL_TEXTINPUT){
                int x = 0, y = 0;
                SDL_GetMouseState(&x, &y);
                handleKeys(e.text.text[0], x, y);
            }

        }

        render();
    }

    SDL_StopTextInput();

    close();
}

bool init(){
    bool success = true;
    if(SDL_Init(SDL_INIT_VIDEO) < 0){
        printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
        success = false;
    }else{
        printf("SDL Opengl context created successfully\n");
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);


        printf("Checking failed status: %s\n", SDL_GetError());

        //Create window
        gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED,
                SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL);

        //std::cout << "OpenGL version " << glGetString​(GL_VERSION​) << std::endl;
      //std::cout << "GLSL version " << glGetString​(GL_SHADING_LANGUAGE_VERSION​​) << std::endendl;
        if( gWindow == NULL )
        {
            printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
            success = false;
        }else { //Create context
            gContext = SDL_GL_CreateContext( gWindow );
            if( gContext == NULL ) {
                printf( "OpenGL context could not be created! SDL Error: %s\n", SDL_GetError() );
                success = false;
            } else { //Use Vsync
                if( SDL_GL_SetSwapInterval( 1 ) < 0 ) {
                    printf( "Warning: Unable to set VSync! SDL Error: %s\n", SDL_GetError() );
                } //Initialize OpenGL
                if( !initGL() ) {
                    printf( "Unable to initialize OpenGL!\n" ); success = false;
                }
            }
        }
    }
    return success;
}

bool initGL(){
    bool success = true;

    GLenum error = GL_NO_ERROR;
    glViewport(0, 0, SCREEN_WIDTH,SCREEN_HEIGHT);
    glEnable(GL_BLEND);
    glEnable(GL_DEPTH_TEST);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    error = glGetError();
    if(error != GL_NO_ERROR){
        printf("Error initializing OpenGL! %s\n", gluErrorString(error));
        success = false;
    }

    glShadeModel(GL_SMOOTH);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();


    error = glGetError();
    if(error != GL_NO_ERROR){
        printf("Error initializing OpenGL %s\n", gluErrorString(error));
        success = false;
    }

    glClearColor(1.f, 0.f, 0.f, 1.f);

    error = glGetError();
    if(error != GL_NO_ERROR){
        printf("Error initializing OpenGL %s\n", gluErrorString(error));
        success = false;
    }
    return success;
}

void handleKeys( unsigned char key, int x, int y ) {
    //Toggle quad
    if( key == 'q' )
    {
        gRenderQuad = !gRenderQuad;
    }
}

void update(){

}

void render(){
    glClear(GL_COLOR_BUFFER_BIT);

    if(gRenderQuad){
        glRotatef(0.4f,0.0f,1.0f,0.0f);    // Rotate The cube around the Y axis
        glRotatef(0.2f,1.0f,1.0f,1.0f);
        glColor3f(0.0f,1.0f,0.0f);
        glBegin(GL_QUADS);
            glVertex2f( -0.5f, -0.5f );
            glVertex2f( 0.5f, -0.5f );
            glVertex2f( 0.5f, 0.5f );
            glVertex2f( -0.5f, 0.5f );
        glEnd();
    }
    SDL_GL_SwapWindow(gWindow);
}

void close(){
    SDL_GL_DeleteContext(gContext);
    SDL_DestroyWindow(gWindow);
    gWindow = NULL;
    SDL_Quit();
}

正如Reto Koradi所说,这是版本不匹配。几十年来,OpenGL 经历了很多变化,“核心”配置文件禁用了您和 LazyFoo 正在使用的旧功能。

LazyFoo 第 50 页使用 OpenGL 2.1 功能,但您的代码指定您将仅使用核心 4.1 功能。删除这些行:

SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);

使您的代码 运行。然而,它只会产生一个空白的红色屏幕。删除此行:

glEnable(GL_DEPTH_TEST);

让绿色四边形显示在屏幕上:

或者,如果您想使用深度缓冲区,请保留 glEnable 行,并将 glClear 更改为:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

查看 LazyFoo page 51 以了解在更现代的 OpenGL 中是如何编写的。完全不一样!你必须编写着色器,自己实现旋转,将所有顶点放入缓冲区。我用旧功能重新学习了 OpenGL,现在必须重新学习所有内容……

我忘了说我正在使用 eclipse,我在 eclipse 上尝试了完全相同的代码,它运行得非常好,但我仍然担心,为什么它在 xcode 上运行而不在 eclipse 上运行,无论如何谢谢