2011-01-29 143 views
3

我可以在openGL中顯示* .gif圖像嗎? 我想用它像glQuad顯示列表的紋理。.gif使用OpenGl的圖像顯示

glNewList(base+loop,GL_COMPILE);    // Start Building A List 
      glBegin(GL_QUADS);       // Use A Quad For Each Character 
       glTexCoord2f(cx,1-cy-0.0625f);   // Texture Coord (Bottom Left) 
       glVertex2i(0,0);      // Vertex Coord (Bottom Left) 
       glTexCoord2f(cx+0.0625f,1-cy-0.0625f); // Texture Coord (Bottom Right) 
       glVertex2i(16,0);      // Vertex Coord (Bottom Right) 
       glTexCoord2f(cx+0.0625f,1-cy);   // Texture Coord (Top Right) 
       glVertex2i(16,16);      // Vertex Coord (Top Right) 
       glTexCoord2f(cx,1-cy);     // Texture Coord (Top Left) 
       glVertex2i(0,16);      // Vertex Coord (Top Left) 
      glEnd();         // Done Building Our Quad (Character) 
      glTranslated(10,0,0);      // Move To The Right Of The Character 
     glEndList();  

THX

我爲使用yhis庫

#include <windows.h>  // Header File For Windows 
#include <stdio.h>   // Header File For Standard Input/Output 
#include <stdlib.h> 
#include <math.h> 
#include <time.h> 
#include <sstream> 
//#include "glut.h" 
#include <gl\gl.h>   // Header File For The OpenGL32 Library 
#include <gl\glu.h>   // Header File For The GLu32 Library 
#include <gl\glut.h>  
#include <gl\glaux.h> 
+0

刪除我的答案,因爲我想不出任何輕量級的GIF解碼庫。 – 2011-01-29 13:42:33

回答

1

我建議你使用SFML - 它支持很多圖像格式,圖像加載/處理使它變得簡單。這裏的帶紋理的立方體使用SFML一個示例應用程序:

#include <SFML/Graphics.hpp> 
#include <iostream> 

int main() 
{ 
    // Create main window 
    sf::RenderWindow App(sf::VideoMode(800, 600), "SFML OpenGL"); 
    App.PreserveOpenGLStates(true); 

    // Create a sprite for the background 
    sf::Image BackgroundImage; 
    if (!BackgroundImage.LoadFromFile("datas/opengl/background.jpg")) 
     return EXIT_FAILURE; 
    sf::Sprite Background(BackgroundImage); 

    // Load an OpenGL texture. 
    // We could directly use a sf::Image as an OpenGL texture (with its Bind() member function), 
    // but here we want more control on it (generate mipmaps, ...) so we create a new one 
    GLuint Texture = 0; 
    { 
     sf::Image Image; 
     if (!Image.LoadFromFile("datas/opengl/texture.jpg")) 
      return EXIT_FAILURE; 
     glGenTextures(1, &Texture); 
     glBindTexture(GL_TEXTURE_2D, Texture); 
     gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, Image.GetWidth(), Image.GetHeight(), GL_RGBA, GL_UNSIGNED_BYTE, Image.GetPixelsPtr()); 
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 
    } 

    // Enable Z-buffer read and write 
    glEnable(GL_DEPTH_TEST); 
    glDepthMask(GL_TRUE); 
    glClearDepth(1.f); 

    // Setup a perspective projection 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective(90.f, 1.f, 1.f, 500.f); 

    // Bind our texture 
    glEnable(GL_TEXTURE_2D); 
    glBindTexture(GL_TEXTURE_2D, Texture); 
    glColor4f(1.f, 1.f, 1.f, 1.f); 

    // Create a clock for measuring the time elapsed 
    sf::Clock Clock; 

    // Start game loop 
    while (App.IsOpened()) 
    { 
     // Process events 
     sf::Event Event; 
     while (App.GetEvent(Event)) 
     { 
      // Close window : exit 
      if (Event.Type == sf::Event::Closed) 
       App.Close(); 

      // Escape key : exit 
      if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape)) 
       App.Close(); 

      // Adjust the viewport when the window is resized 
      if (Event.Type == sf::Event::Resized) 
       glViewport(0, 0, Event.Size.Width, Event.Size.Height); 
     } 

     // Draw background 
     App.Draw(Background); 

     // Clear depth buffer 
     glClear(GL_DEPTH_BUFFER_BIT); 

     // Apply some transformations 
     glMatrixMode(GL_MODELVIEW); 
     glLoadIdentity(); 
     glTranslatef(0.f, 0.f, -200.f); 
     glRotatef(Clock.GetElapsedTime() * 50, 1.f, 0.f, 0.f); 
     glRotatef(Clock.GetElapsedTime() * 30, 0.f, 1.f, 0.f); 
     glRotatef(Clock.GetElapsedTime() * 90, 0.f, 0.f, 1.f); 

     // Draw a cube 
     glBegin(GL_QUADS); 

      glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, -50.f); 
      glTexCoord2f(0, 1); glVertex3f(-50.f, 50.f, -50.f); 
      glTexCoord2f(1, 1); glVertex3f(50.f, 50.f, -50.f); 
      glTexCoord2f(1, 0); glVertex3f(50.f, -50.f, -50.f); 

      glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, 50.f); 
      glTexCoord2f(0, 1); glVertex3f(-50.f, 50.f, 50.f); 
      glTexCoord2f(1, 1); glVertex3f(50.f, 50.f, 50.f); 
      glTexCoord2f(1, 0); glVertex3f(50.f, -50.f, 50.f); 

      glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, -50.f); 
      glTexCoord2f(0, 1); glVertex3f(-50.f, 50.f, -50.f); 
      glTexCoord2f(1, 1); glVertex3f(-50.f, 50.f, 50.f); 
      glTexCoord2f(1, 0); glVertex3f(-50.f, -50.f, 50.f); 

      glTexCoord2f(0, 0); glVertex3f(50.f, -50.f, -50.f); 
      glTexCoord2f(0, 1); glVertex3f(50.f, 50.f, -50.f); 
      glTexCoord2f(1, 1); glVertex3f(50.f, 50.f, 50.f); 
      glTexCoord2f(1, 0); glVertex3f(50.f, -50.f, 50.f); 

      glTexCoord2f(0, 1); glVertex3f(-50.f, -50.f, 50.f); 
      glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, -50.f); 
      glTexCoord2f(1, 0); glVertex3f(50.f, -50.f, -50.f); 
      glTexCoord2f(1, 1); glVertex3f(50.f, -50.f, 50.f); 

      glTexCoord2f(0, 1); glVertex3f(-50.f, 50.f, 50.f); 
      glTexCoord2f(0, 0); glVertex3f(-50.f, 50.f, -50.f); 
      glTexCoord2f(1, 0); glVertex3f(50.f, 50.f, -50.f); 
      glTexCoord2f(1, 1); glVertex3f(50.f, 50.f, 50.f); 

     glEnd(); 

     // Draw some text on top of our OpenGL object 
     sf::String Text("This is a rotating cube"); 
     Text.SetPosition(250.f, 300.f); 
     Text.SetColor(sf::Color(128, 0, 128)); 
     App.Draw(Text); 

     // Finally, display the rendered frame on screen 
     App.Display(); 
    } 

    // Don't forget to destroy our texture 
    glDeleteTextures(1, &Texture); 

    return EXIT_SUCCESS; 
} 

注:並且不使用glaux - 它在時間上是非常丟失。

注意:演示使用.jpg圖像格式。這個比.gif更好,因爲它更輕巧,並且在應用程序中更易於實現,更易於實現。

1

這是一種矯枉過正,如果你只是支持GIF,但DevIL支持GIF(以及許多其他)。還有lighter solutions。或者,你可以得到GIF spec,並自己寫(我記得,做一個不錯的,令人放鬆的下午項目)。