SDL OpenGL问题

时间:2014-01-06 03:19:07

标签: opengl sdl

我正在关注youtube上的教程,但问题是本教程的制作者正在使用SDL 1.2和Im使用2.00所以我不得不在SDL迁移指南之后稍微改变代码。但是我的代码没有做它应该绘制三角形的东西。就那么简单。我得到的只是一个白色的屏幕。

==主============

#include "stdafx.h"


void init()
{
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45, 640.0 / 480.0, 1.0, 500.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glBegin(GL_TRIANGLES);
    glVertex3f(0.0, 2.0, -5.0);
    glVertex3f(-2.0, -2.0, -5.0);
    glVertex3f(2.0, -2.0, -5.0);
    glEnd();
}

int main(int argc, char* argv[]){
SDL_Init(SDL_INIT_EVERYTHING); 
    SDL_Window * window;
    window = SDL_CreateWindow("OpenGLTest", 300, 300, 640, 480, SDL_WINDOW_SHOWN |       SDL_WINDOW_OPENGL);
    init();
    SDL_Event * mainEvent;
    mainEvent = new SDL_Event;

    while (true){
        SDL_PollEvent(mainEvent);
        display();
        SDL_GL_SwapWindow(window);

        if (mainEvent->type == SDL_QUIT){
            break;
        }
    }

return 0;
}

===== stdafx ======

#pragma once

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

#define PI 3.14159265

using namespace std;

任何想法我做错了什么?

1 个答案:

答案 0 :(得分:3)

仅{p> SDL_WINDOW_OPENGL是必要的,但还不够。

使用SDL_GL_CreateContext()创建一个总帐上下文,并通过SDL_GL_MakeCurrent()

使其成为当前状态
#include <GL/glew.h>
#include <SDL/SDL.h>

int main( int argc, char *argv[] )
{
    SDL_Init(SDL_INIT_EVERYTHING); 
    SDL_Window * window = SDL_CreateWindow
        (
        "OpenGLTest", 
        300, 300, 
        640, 480, 
        SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL
        );
    SDL_GLContext context = SDL_GL_CreateContext( window );
    SDL_GL_MakeCurrent( window, context );

    glewInit();

    bool running = true;
    while( running )
    {
        SDL_Event ev;
        while( SDL_PollEvent( &ev ) )
        {
            if ( ev.type == SDL_QUIT)
                running = false;
        }

        glClearColor(0.0, 0.0, 0.0, 1.0);
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45, 640.0 / 480.0, 1.0, 500.0);

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        glBegin(GL_TRIANGLES);
        glVertex3f(0.0, 2.0, -5.0);
        glVertex3f(-2.0, -2.0, -5.0);
        glVertex3f(2.0, -2.0, -5.0);
        glEnd();

        SDL_GL_SwapWindow(window);
    }

    SDL_GL_DeleteContext( context );
    SDL_DestroyWindow( window );
    SDL_Quit();

    return 0;
}