2015-09-16 84 views
0

我想在mac osx yosemite上使用SDL2和OpenGL,我跟隨了SDL頁面上的示例以及Lazy Foo的示例,但每次運行代碼時,同樣的結果,與黑色背景上的空白窗口,我已搜查谷歌上有一段時間了,但香港專業教育學院還沒有得到任何解決方案,我會很感激的任何幫助Mac上的SDL2 + Opengl黑色窗口

下面是我的代碼

/* 
* 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(); 
} 
+2

您正在創建一個核心配置文件上下文('SDL_GL_CONTEXT_PROFILE_CORE'),您使用的OpenGL調用中有一半以上不推薦,並且在覈心配置文件中不受支持。所有矩陣堆棧功能,即時模式繪圖等在覈心配置文件中不可用。 –

回答

2

由於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); 

中獲取綠色四在屏幕上顯示出來:

SDL Tutorial

另外,如果你想使用深度緩衝,留下在glEnable行,改變你的glClear到:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

退房LazyFoo page 51怎麼看的東西都寫在一個更現代的OpenGL。它完全不同!您必須編寫着色器,自己實現旋轉,將所有頂點放入緩衝區。我學會了OpenGL的舊功能,現在不得不重新學習所有東西...

+0

嗨@amitp感謝您的幫助,我已經做出了您建議的更改,但我仍然得到一個空白的黑屏,我仍然無法弄清楚什麼是做錯了 –

0

我忘了提及,我使用的是日食,我試着完全相同的代碼在日食上,它的工作非常好,但我仍然關注,爲什麼它在xcode上工作,但不在eclipse上,謝謝