我正在尝试在mac osx yosemite上使用SDL2和OpenGL,我已经在SDL页面上跟随了示例以及Lazy Foo的页面http://lazyfoo.net/tutorials/SDL/50_SDL_and_opengl_2/index.php中的示例,但每次运行代码时,我得到相同的结果,一个黑色背景的空白窗口,我已经在谷歌搜索了一段时间了,但我还没有得到任何解决方案,我很感激任何帮助
以下是我的代码
/*
* 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();
}
答案 0 :(得分:3)
正如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,现在我不得不重新学习所有东西......
答案 1 :(得分:0)
我忘了提到我正在使用eclipse,我在eclipse上尝试了完全相同的代码并且它工作得非常好,但我仍然担心,为什么它适用于xcode但不适用于eclipse,谢谢无论如何