.gif image display using OpenGl - opengl

Can I display *.gif image in openGL?
I want to use it like texture of glQuad display list.
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
I m using yhis libraries
#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>

I recommend you to use SFML - it supports lot of image formats and image loading/processing is made simple with it. Here's an example application with textured cube using 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;
}
Note: And do not use glaux - it is terribly lost in time.
Note: demo uses .jpg image format. This one is better than .gif, because it is more lightweight and, afaik, widespreaded and easier to implement in application.

It's kind of overkill if you just want to support GIF, but DevIL supports GIF (among many others). There are lighter solutions around as well. Alternatively, you can get the GIF spec, and write it yourself (makes a nice, relaxing afternoon project as I recall).

Related

Weird glitch involving my code

So today, I was messing around with some examples and decided I wanted to make an app. So I attempted. When I click once, the screen becomes black, another click then white, and after some spamming the block appears, clicking again makes the background go black, another click, then white. After some more spamming there is another block inside the block, pictures are attached.
CODE:
#include "SFML/Graphics.hpp"
#include "SFML/OpenGL.hpp"
#include <GL\GLU.h>
#include <iostream>
#include "cube.cpp"
void createblock() {
gluPerspective(90.f, 1.f, 1.f, 300.0f);
glClearColor(0.3f, 0.3f, 0.3f, 0.f);
glMatrixMode(GL_PROJECTION);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glBegin(GL_QUADS);
glVertex3f(-50.f, -50.f, -50.f);
glVertex3f(-50.f, 50.f, -50.f);
glVertex3f(50.f, 50.f, -50.f);
glVertex3f(50.f, -50.f, -50.f);
glVertex3f(-50.f, -50.f, 50.f);
glVertex3f(-50.f, 50.f, 50.f);
glVertex3f(50.f, 50.f, 50.f);
glVertex3f(50.f, -50.f, 50.f);
glColor3f(1, 0, 1);
glVertex3f(-50.f, -50.f, -50.f);
glVertex3f(-50.f, 50.f, -50.f);
glVertex3f(-50.f, 50.f, 50.f);
glVertex3f(-50.f, -50.f, 50.f);
glColor3f(0, 1, 0);
glVertex3f(50.f, -50.f, -50.f);
glVertex3f(50.f, 50.f, -50.f);
glVertex3f(50.f, 50.f, 50.f);
glVertex3f(50.f, -50.f, 50.f);
glColor3f(1, 1, 0);
glVertex3f(-50.f, -50.f, 50.f);
glVertex3f(-50.f, -50.f, -50.f);
glVertex3f(50.f, -50.f, -50.f);
glVertex3f(50.f, -50.f, 50.f);
glColor3f(1, 0, 0);
glVertex3f(-50.f, 50.f, 50.f);
glVertex3f(-50.f, 50.f, -50.f);
glVertex3f(50.f, 50.f, -50.f);
glVertex3f(50.f, 50.f, 50.f);
glEnd();
}
sf::RenderWindow window(sf::VideoMode(800, 600), "SimpleBlocks");
sf::Event maine;
int main() {
while (window.isOpen()) {
// load defaults here
while (window.pollEvent(maine)) {
// events here
if (maine.type == sf::Event::Closed) {
window.close();
}
if (maine.type == sf::Event::MouseButtonPressed) {
createblock();
window.display();
// make block later
}
}
}
}
IMAGES:
imgur album here
You never call glClear(GL_COLOR_BUFFER_BIT) before you draw, so there are always leftover fragments or some overlap in depth information. Adding a clear call should fix your issue.
Call glPushMatrix before gluPerspective and glPopMatrix at the end of createblock

OpenGL, SOIL, Textures and Transparent backgrounds

I can't seem to get the transparent background on my image to load as transparent. It's always white instead. I'm still fairly new to OpenGL/SOIL so if I did something stupid don't be mad :/ Does it have to do with my flag on the load being RGB instead of RGBA?
void Scene::loadTexShips()
{
texShips[0] = SOIL_load_OGL_texture(
"Textures/Carrier.png",
SOIL_LOAD_LA,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_NTSC_SAFE_RGB
);
if (texShips[0] == 0)
{
printf("SOIL loading error: '%s'\n", SOIL_last_result());
}
}
void Scene::drawShips()
{
glColor4ub(255, 255, 255, 255);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glLoadIdentity();
glTranslatef(6.0f, 2.0f, -12.0f);
glBindTexture(GL_TEXTURE_2D, texShips[0]);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3f(-4.0f, -3.0f, 2.0);
glTexCoord2f(0, 1);
glVertex3f(-4.0f, -2.3, 2.0);
glTexCoord2f(1, 1);
glVertex3f(-1.0, -2.3, 2.0);
glTexCoord2f(1, 0);
glVertex3f(-1.0, -3.0, 2.0);
glEnd();
glDisable(GL_TEXTURE_2D);
}

Texture problems in LWJGL (OpenGL) using slick-util library

I’m just starting out with 3D graphics programming and I’ve got a nicely encapsulated Cube object. This class has a render() method, which proceeds to push a new matrix, perform transformations, glBegin, specify all the vertices and texture coordinates, glEnd, and pop the matrix. Now, I have written this Cube class with methods for using different textures on different faces of the cube, but that isn't happening at runtime. I understood textures to be like colors, which I can easily change per face of the cube by making a call to glColor before the appropriate vertices, but using texture.bind() (from the slick-util library) seems to do nothing.
This is my render method:
public void render(){
glPushMatrix();
top.bind();
top.setTextureFilter(GL_NEAREST);
glColor4f(1, 1, 1, 0);
glTranslatef(x, y, z);
glRotatef(yaw, 0, 1, 0);
glBegin(GL_QUADS);
//Top
glTexCoord2f(0, 0); glVertex3f(0, size, 0);
glTexCoord2f(0, 1); glVertex3f(0, size, size);
glTexCoord2f(1, 1); glVertex3f(size, size, size);
glTexCoord2f(1, 0); glVertex3f(size, size, 0);
//Front
top.release();
side.bind();
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(0, 1); glVertex3f(0, size, 0);
glTexCoord2f(1, 1); glVertex3f(size, size, 0);
glTexCoord2f(1, 0); glVertex3f(size, 0, 0);
//Left
glTexCoord2f(0, 0); glVertex3f(0, 0, size);
glTexCoord2f(0, 1); glVertex3f(0, size, size);
glTexCoord2f(1, 1); glVertex3f(0, size, 0);
glTexCoord2f(1, 0); glVertex3f(0, 0, 0);
//Right
glTexCoord2f(0, 0); glVertex3f(size, 0, size);
glTexCoord2f(0, 1); glVertex3f(size, size, size);
glTexCoord2f(1, 1); glVertex3f(size, size, 0);
glTexCoord2f(1, 0); glVertex3f(size, 0, 0);
//Back
glTexCoord2f(0, 0); glVertex3f(0, 0, size);
glTexCoord2f(0, 1); glVertex3f(0, size, size);
glTexCoord2f(1, 1); glVertex3f(size, size, size);
glTexCoord2f(1, 0); glVertex3f(size, 0, size);
//Bottom
side.release();
bottom.bind();
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(0, 1); glVertex3f(0, 0, size);
glTexCoord2f(1, 1); glVertex3f(size, 0, size);
glTexCoord2f(1, 0); glVertex3f(size, 0, 0);
bottom.release();
glEnd();
glPopMatrix();
}
Variables top, side, and bottom are the textures I want to put on the faces of the cube.
The result is that every face of the cube has the “top” texture.
The result is the same whether I call texture.release() or not.
You can't bind textures between glBegin() and glEnd(). Only a limited set of GL calls can be made between glBegin() and glEnd() (see https://www.opengl.org/sdk/docs/man2/xhtml/glBegin.xml for the full list), and glBindTexture() is not one of them.
To bind a different texture for each side, you need to start a new begin/end pair for each side:
//Top
top.bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, size, 0);
glTexCoord2f(0, 1); glVertex3f(0, size, size);
glTexCoord2f(1, 1); glVertex3f(size, size, size);
glTexCoord2f(1, 0); glVertex3f(size, size, 0);
glEnd();
//Front
side.bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
...
glEnd();
//Bottom
bottom.bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
...
glEnd();

Transparency Opengl+SDL not working C++

I've been trying to get transparency working with SDL+ OpenGL.
Here are my functions that initialize OpenGL and SDL, draw an image, and create a texture
void initGL()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho( 0.0, Screen_Width,Screen_Height, 0.0, 1.0, -1.0 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, Screen_Width, Screen_Height);
}
void initSDL()
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
Bob=SDL_SetVideoMode(0, 0, 32, SDL_OPENGL);
Screen_Width= Bob->w;
Screen_Height= Bob->h;
SDL_WM_SetCaption("Project", NULL);;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
}
void Texture::draw(int x1, int y1,int x2, int y2,std::string filename)
{
glClear( GL_COLOR_BUFFER_BIT );
CreateTexture(filename.c_str());
glEnable( GL_TEXTURE_2D );
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, Tex);
glBegin(GL_QUADS);
glTexCoord2f(0.f, 0.f);
glVertex2f( 0.f, 0.f );
glTexCoord2f(1.f, 0.f);
glVertex2f( 1920.f, 0.f );
glTexCoord2f(1.f, 1.f);
glVertex2f( 1920.f, 1080.f );
glTexCoord2f(0.f, 1.f);
glVertex2f( 0.f, 1080.f );
glEnd();
glDisable(GL_TEXTURE_2D);
SDL_GL_SwapBuffers();
}
void Texture::CreateTexture(std::string filename)
{
SDL_Surface* image =NULL;
image=IMG_Load( filename.c_str() );
glClearColor( 0, 0, 0, 0 );
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glGenTextures(1, &Tex);
glBindTexture(GL_TEXTURE_2D, Tex);
int mode = GL_RGB;
if(image->format->BytesPerPixel == 4)
mode = GL_RGBA;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 4, image->w, image->h, 0, mode, GL_UNSIGNED_BYTE, image- >pixels);
SDL_FreeSurface(image);
}
You can check the texture format is should be RGBA and you can test a white texture(RGB(255,255,255)) that has alpha channel of 128, so the whole texture is just white with alpha half white. Drawing this on a black background should result a half white texture being drawn.
Also the vertex positions might not be right, you could try something like this (with identity matrix for projection and world transform) to see if everything is in order with opengl setup
glTexCoord2f(0.f, 0.f);
glVertex2f( 0.25f, 0.25f );
glTexCoord2f(1.f, 0.f);
glVertex2f( 0.25f,0.5f );
glTexCoord2f(1.f, 1.f);
glVertex2f( 0.5f,0.5f );
glTexCoord2f(0.f, 1.f);
glVertex2f( 0.5f,0.25f );
Hope this helps.
Razvan.
You're drawing exactly one quad, with one texture. What exactly do you expect to be visible in the translucent parts? The background color?
Or your desktop, or the windows beneath? That takes quite some additional work.
First you need a compositor running (on Windows this requires Windows Vista with Aero or later).
And the window must be configured in a way, that its alpha channel is actually considered when compositing; AFAIK SDL doesn't take those preparations.

OpenGL Lighting Changes Brightness With Camera

So I've just started playing around with OpenGL, and decided to make a little voxel render thing. I'm trying to light it, but weird effects happen when I rotate the camera around the y axis. For example, when I first spawn in, the light looks like this:
http://i.stack.imgur.com/zQ49y.png
But when I rotate around a bit I get this:
http://i.stack.imgur.com/PWvVo.png
Here's the code that does this stuff:
glRotatef(xrot, 1.0f, 0.0f, 0.0);
glRotatef(yrot, 0.0f, 1.0f, 0.0);
glTranslatef(-3.5f-xcam, ycam, -3.5f-zcam);
glEnable (GL_DEPTH_TEST); //enable the depth testing
glEnable (GL_LIGHTING); //enable the lighting
glEnable (GL_LIGHT0);
GLfloat specular[] = {1.0f-xcam, ycam+1.0, 1.0f-zcam, 1.0};
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, grnd);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBegin(GL_QUADS);
glTexCoord2i(0, 0);
glVertex3f(0, 0, 0);
glTexCoord2i(0, 1);
glVertex3f(0, 0, 7.0f);
glTexCoord2i(1, 1);
glVertex3f(7.0f, 0, 7.0f);
glTexCoord2i(1, 0);
glVertex3f(7.0f, 0, 0);
glEnd();
glDisable(GL_TEXTURE_2D);
glLoadIdentity();
drawVoxel(2.f, 0.f, 2.f, tex);
drawVoxel(1.f, 0.f, 1.f, tex);
drawVoxel(2.f, 0.f, 2.f, tex);
drawVoxel(3.f, 0.f, 3.f, tex);
drawVoxel(4.f, 0.f, 4.f, tree);
drawVoxel(4.f, 1.f, 4.f, tree);
drawVoxel(4.f, 2.f, 4.f, tree);
drawVoxel(4.f, 3.f, 4.f, tree);
test.Flip();
Does anybody have any idea what's going on?
You also need to use glNormal3f().
.
.
.
glBegin(GL_QUADS);
glTexCoord2i(0, 0);
glNormal3f(0,1,0);
glVertex3f(0, 0, 0);
glTexCoord2i(0, 1);
glNormal3f(0,1,0);
glVertex3f(0, 0, 7.0f);
glTexCoord2i(1, 1);
glNormal3f(0,1,0);
glVertex3f(7.0f, 0, 7.0f);
glTexCoord2i(1, 0);
glNormal3f(0,1,0);
glVertex3f(7.0f, 0, 0);
glEnd();