使用OpenGl显示.gif图像
我可以在openGL中显示*.gif图像吗? 我想使用它像glQuad显示列表的纹理使用OpenGl显示.gif图像,opengl,Opengl,我可以在openGL中显示*.gif图像吗? 我想使用它像glQuad显示列表的纹理 glNewList(base+loop,GL_COMPILE); // Start Building A List glBegin(GL_QUADS); // Use A Quad For Each Character glTexCoord2f(cx,1-cy-0.0625f)
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>
#包含//Windows的头文件
#包含//标准输入/输出的头文件
#包括
#包括
#包括
#包括
//#包括“glut.h”
#包含//OpenGL32库的头文件
#包含//GLu32库的头文件
#包括
#包括
如果您只想支持GIF,但支持GIF(以及其他许多功能),那就有点过分了。附近也有。或者,您也可以自己编写(我记得这是一个愉快、轻松的下午项目)。我建议您使用-它支持多种图像格式,并且图像加载/处理变得简单。下面是一个使用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;
}
#包括
#包括
int main()
{
//创建主窗口
sf::RenderWindow应用程序(sf::VideoMode(800600),“SFML OpenGL”);
App.PreserveOpenGLStates(true);
//为背景创建一个精灵
图像背景图像;
如果(!BackgroundImage.LoadFromFile(“datas/opengl/background.jpg”))
返回退出失败;
sf::精灵背景(背景图像);
//加载OpenGL纹理。
//我们可以直接使用sf::Image作为OpenGL纹理(使用其Bind()成员函数),
//但在这里,我们需要更多的控制(生成mipmap,…),所以我们创建了一个新的
胶合纹理=0;
{
sf::图像;
如果(!Image.LoadFromFile(“datas/opengl/texture.jpg”))
返回退出失败;
glGenTextures(1,&纹理);
glBindTexture(GL_TEXTURE_2D,纹理);
gluBuild2DMipmaps(GL_纹理_2D,GL_RGBA,Image.GetWidth(),Image.GetHeight(),GL_RGBA,GL_无符号字节,Image.GetPixelsPtr());
glTexParameteri(GL_纹理2D、GL_纹理MAG_过滤器、GL_线性);
glTexParameteri(GL\u纹理\u 2D、GL\u纹理\u最小\u过滤器、GL\u线性\u MIPMAP\u线性);
}
//启用Z缓冲区读写
glEnable(GLU深度试验);
glDepthMask(GL_TRUE);
GLC清除深度(1.f);
//设置透视投影
glMatrixMode(GL_投影);
glLoadIdentity();
(90.f,1.f,1.f,500.f);
//绑定我们的纹理
glEnable(GL_纹理_2D);
glBindTexture(GL_TEXTURE_2D,纹理);
GL4F(1.f,1.f,1.f,1.f);
//创建一个时钟来测量经过的时间
sf:时钟;
//开始游戏循环
而(App.IsOpened())
{
//处理事件
sf::事件;
while(App.GetEvent(Event))
{
//关闭窗口:退出
如果(Event.Type==sf::Event::Closed)
App.Close();
//退出键:退出
if((Event.Type==sf::Event::KeyPressed)&&&(Event.Key.Code==sf::Key::Escape))
App.Close();
//调整窗口大小时调整视口
if(Event.Type==sf::Event::Resized)
glViewport(0,0,Event.Size.Width,Event.Size.Height);
}
//画背景
应用绘图(背景);
//清除深度缓冲区
glClear(GLU深度缓冲位);
//应用一些转换
glMatrixMode(GLU模型视图);
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);
//画一个立方体
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);
格伦德();
//在OpenGL对象的顶部绘制一些文本
字符串文本(“这是一个旋转立方体”);
文本设置位置(250.f,300.f);
SetColor(sf::Color(128,0,128));
应用图(文本);
//最后,在屏幕上显示渲染帧
App.Display();
}
//别忘了破坏我们的纹理
glDeleteTextures(1和纹理);
返回退出成功;
}
注意:并且不要使用glaux
-它会严重地浪费时间
注意:演示使用
.jpg
图像格式。这个比.gif
好,因为它更轻量级,而且更广泛,更易于在应用程序中实现。删除了我的答案,因为我想不出任何轻量级gif解码库。好的,谢谢