OpenGL + SDL2黑屏

时间:2013-09-12 00:21:07

标签: c++ opengl sdl-2

我目前正在学习OpenGL,并且一直在使用SDL2,当我尝试运行一个简单的程序时,我得到一个黑屏。任何帮助,将不胜感激。我正在使用OpenGL 2.1和vc编译器。

这是我的代码

#include <iostream>
#include <SDL.h>
#include <Windows.h>
#include <gl/GL.h>
#include <gl/GLU.h>

using namespace std;

int main(int argc, char* argv[]) {
    int width, height;

    width = 640;
    height = 480;

    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_Window* win;
    win = SDL_CreateWindow("SDL Application", 100, 100, width, height, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);

    SDL_GLContext context;
    context = SDL_GL_CreateContext(win);

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

    height = (height <= 0) ? height = height : height = 1;
    glViewport(0, 0, width, height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluPerspective(45.0f, (GLfloat) width / (GLfloat) height, 0.1f, 100.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glShadeModel(GL_SMOOTH);

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);

    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    SDL_GL_SwapWindow(win);

    glColor3f(1.0f, 1.0f, 1.0f);

    glTranslatef(-1.5f, 0.0f, -6.0f);

    glBegin(GL_TRIANGLES);
        glVertex3f(0.0f, 1.0f, 0.0f);
        glVertex3f(-1.0f, -1.0f, 0.0f);
        glVertex3f(1.0f, -1.0f, 0.0f);
    glEnd();

    glTranslatef(3.0f, 0.0f, 0.0f);

    glBegin(GL_QUADS);
        glVertex3f(-1.0f, 1.0f, 0.0f);
        glVertex3f(1.0f, 1.0f, 0.0f);
        glVertex3f(1.0f, -1.0f, 0.0f);
        glVertex3f(-1.0f, -1.0f, 0.0f);
    glEnd();

    SDL_Delay(5000);

    SDL_GL_DeleteContext(context);
    SDL_DestroyWindow(win);

    SDL_Quit(); 
    return 0;
}

1 个答案:

答案 0 :(得分:1)

这些顺序是错误的:

SDL_GLContext context;
context = SDL_GL_CreateContext(win);

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

必须在创建上下文之前设置OpenGL上下文属性(它们是控制上下文创建过程的状态变量)。

这没有任何意义:首先你清除,然后交换,然后你绘制(进入一个未定义的后缓冲区,因为后交换缓冲区的内容在交换后未定义)然后你不< / em>交换。

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

SDL_GL_SwapWindow(win);

glColor3f(1.0f, 1.0f, 1.0f);

/* this translate will move the triangle out
 * of the NDC space i.e. it gets clipped or
 * won't be visible at all. */
glTranslatef(-1.5f, 0.0f, -6.0f);

glBegin(GL_TRIANGLES);
  glVertex3f(0.0f, 1.0f, 0.0f);

这应该是

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 1.0f);

glBegin(GL_TRIANGLES);
    glVertex3f(0.0f, 1.0f, 0.0f);
    glVertex3f(-1.0f, -1.0f, 0.0f);
    glVertex3f(1.0f, -1.0f, 0.0f);
glEnd();

glTranslatef(3.0f, 0.0f, 0.0f);

glBegin(GL_QUADS);
    glVertex3f(-1.0f, 1.0f, 0.0f);
    glVertex3f(1.0f, 1.0f, 0.0f);
    glVertex3f(1.0f, -1.0f, 0.0f);
    glVertex3f(-1.0f, -1.0f, 0.0f);
glEnd();

SDL_GL_SwapWindow(win);
SDL_Delay(5000);

完全没有任何意义。其余的代码仍然存在很多问题,但如果你改变它,你至少应该在黑色地面上看到一些白色三角形。