⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 guidevideoopengl.html

📁 VC5.6.7的一个扩展库。跟DirectX的功能差不多。
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<HTML><HEAD><TITLE>Using OpenGL With SDL</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="SDL Library Documentation"HREF="index.html"><LINKREL="UP"TITLE="Graphics and Video"HREF="guidevideo.html"><LINKREL="PREVIOUS"TITLE="Graphics and Video"HREF="guidevideo.html"><LINKREL="NEXT"TITLE="Input handling"HREF="guideinput.html"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFF8DC"TEXT="#000000"LINK="#0000ee"VLINK="#551a8b"ALINK="#ff0000"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">SDL Library Documentation</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="guidevideo.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 2. Graphics and Video</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="guideinput.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="GUIDEVIDEOOPENGL"></A>Using OpenGL With SDL</H1><P>SDL has the ability to create and use OpenGL contexts on several platforms(Linux/X11, Win32, BeOS, MacOS Classic/Toolbox, Mac OS X, FreeBSD/X11 and Solaris/X11). This allows you to use SDL's audio, event handling, threads and times in your OpenGL applications (a function often performed by GLUT).</P><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN103"></A>Initialisation</H2><P>Initialising SDL to use OpenGL is not very different to initialising SDL normally. There are three differences; you must pass <TTCLASS="LITERAL">SDL_OPENGL</TT> to <AHREF="sdlsetvideomode.html"><TTCLASS="FUNCTION">SDL_SetVideoMode</TT></A>, you must specify several GL attributes (depth buffer size, framebuffer sizes) using <AHREF="sdlglsetattribute.html"><TTCLASS="FUNCTION">SDL_GL_SetAttribute</TT></A> and finally, if you wish to use double buffering you must specify it as a GL attribute, <SPANCLASS="emphasis"><ICLASS="EMPHASIS">not</I></SPAN> by passing the <TTCLASS="LITERAL">SDL_DOUBLEBUF</TT> flag to <TTCLASS="FUNCTION">SDL_SetVideoMode</TT>.</P><DIVCLASS="EXAMPLE"><ANAME="AEN114"></A><P><B>Example 2-7. Initializing SDL with OpenGL</B></P><PRECLASS="PROGRAMLISTING">    /* Information about the current video settings. */    const SDL_VideoInfo* info = NULL;    /* Dimensions of our window. */    int width = 0;    int height = 0;    /* Color depth in bits of our window. */    int bpp = 0;    /* Flags we will pass into SDL_SetVideoMode. */    int flags = 0;    /* First, initialize SDL's video subsystem. */    if( SDL_Init( SDL_INIT_VIDEO ) &#60; 0 ) {        /* Failed, exit. */        fprintf( stderr, "Video initialization failed: %s\n",             SDL_GetError( ) );        quit_tutorial( 1 );    }    /* Let's get some video information. */    info = SDL_GetVideoInfo( );    if( !info ) {        /* This should probably never happen. */        fprintf( stderr, "Video query failed: %s\n",             SDL_GetError( ) );        quit_tutorial( 1 );    }    /*     * Set our width/height to 640/480 (you would     * of course let the user decide this in a normal     * app). We get the bpp we will request from     * the display. On X11, VidMode can't change     * resolution, so this is probably being overly     * safe. Under Win32, ChangeDisplaySettings     * can change the bpp.     */    width = 640;    height = 480;    bpp = info-&#62;vfmt-&#62;BitsPerPixel;    /*     * Now, we want to setup our requested     * window attributes for our OpenGL window.     * We want *at least* 5 bits of red, green     * and blue. We also want at least a 16-bit     * depth buffer.     *     * The last thing we do is request a double     * buffered window. '1' turns on double     * buffering, '0' turns it off.     *     * Note that we do not use SDL_DOUBLEBUF in     * the flags to SDL_SetVideoMode. That does     * not affect the GL attribute state, only     * the standard 2D blitting setup.     */    SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );    SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );    SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );    SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );    /*     * We want to request that SDL provide us     * with an OpenGL window, in a fullscreen     * video mode.     *     * EXERCISE:     * Make starting windowed an option, and     * handle the resize events properly with     * glViewport.     */    flags = SDL_OPENGL | SDL_FULLSCREEN;    /*     * Set the video mode     */    if( SDL_SetVideoMode( width, height, bpp, flags ) == 0 ) {        /*          * This could happen for a variety of reasons,         * including DISPLAY not being set, the specified         * resolution not being available, etc.         */        fprintf( stderr, "Video mode set failed: %s\n",             SDL_GetError( ) );        quit_tutorial( 1 );    }</PRE></DIV></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN117"></A>Drawing</H2><P>Apart from initialisation, using OpenGL within SDL is the same as using OpenGLwith any other API, e.g. GLUT. You still use all the same function calls anddata types. However if you are using a double-buffered display, then you mustuse<AHREF="sdlglswapbuffers.html"><TTCLASS="FUNCTION">SDL_GL_SwapBuffers()</TT></A>to swap the buffers and update the display. To request double-bufferingwith OpenGL, use<AHREF="sdlglsetattribute.html"><TTCLASS="FUNCTION">SDL_GL_SetAttribute</TT></A>with <TTCLASS="LITERAL">SDL_GL_DOUBLEBUFFER</TT>, and use<AHREF="sdlglgetattribute.html"><TTCLASS="FUNCTION">SDL_GL_GetAttribute</TT></A>to see if you actually got it.</P><P>A full example code listing is now presented below.</P><DIVCLASS="EXAMPLE"><ANAME="AEN128"></A><P><B>Example 2-8. SDL and OpenGL</B></P><PRECLASS="PROGRAMLISTING">/* * SDL OpenGL Tutorial. * (c) Michael Vance, 2000 * briareos@lokigames.com * * Distributed under terms of the LGPL.  */#include &#60;SDL/SDL.h&#62;#include &#60;GL/gl.h&#62;#include &#60;GL/glu.h&#62;#include &#60;stdio.h&#62;#include &#60;stdlib.h&#62;static GLboolean should_rotate = GL_TRUE;static void quit_tutorial( int code ){    /*     * Quit SDL so we can release the fullscreen     * mode and restore the previous video settings,     * etc.     */    SDL_Quit( );    /* Exit program. */    exit( code );}static void handle_key_down( SDL_keysym* keysym ){    /*      * We're only interested if 'Esc' has     * been presssed.     *     * EXERCISE:      * Handle the arrow keys and have that change the     * viewing position/angle.     */    switch( keysym-&#62;sym ) {    case SDLK_ESCAPE:        quit_tutorial( 0 );        break;    case SDLK_SPACE:        should_rotate = !should_rotate;        break;    default:        break;    }}static void process_events( void ){    /* Our SDL event placeholder. */    SDL_Event event;    /* Grab all the events off the queue. */    while( SDL_PollEvent( &#38;event ) ) {        switch( event.type ) {        case SDL_KEYDOWN:            /* Handle key presses. */            handle_key_down( &#38;event.key.keysym );            break;        case SDL_QUIT:            /* Handle quit requests (like Ctrl-c). */            quit_tutorial( 0 );            break;        }    }}static void draw_screen( void ){    /* Our angle of rotation. */    static float angle = 0.0f;    /*     * EXERCISE:     * Replace this awful mess with vertex     * arrays and a call to glDrawElements.     *     * EXERCISE:     * After completing the above, change     * it to use compiled vertex arrays.     *     * EXERCISE:     * Verify my windings are correct here ;).

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -