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

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();

Related

Stroke is not drawn correctly (OpenGL)

I am trying to make a stroke for chests in a minecraft game and I almost succeeded, but for some reason it is not drawn correctly.
ScreenShot
The screenshot shows how part of the line has disappeared. This happens when the line is on the chest. Maybe it's a texture mapping or something else. In this case, the stroke is drawn through the blocks normally.
ScreenShot2
Also, when the chest is in the dark, the outline becomes dark, although I turned off GL_LIGHTING
ScreenShot3
Here is a piece of code with which I draw the stroke
glPushAttrib(GL_ALL_ATTRIB_BITS);
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_CULL_FACE);
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glLineWidth(2);
glColor3ub(255, 255, 255);
glBegin(GL_LINES);
glVertex3f(1, 0, 0);
glVertex3f(1, -1, 0);
glVertex3f(0, 0, 0);
glVertex3f(1, 0, 0);
glVertex3f(0, 0, 0);
glVertex3f(0, -1, 0);
glVertex3f(0, -1, 0);
glVertex3f(1, -1, 0);
glVertex3f(1, -1, 0);
glVertex3f(1, -1, -1);
glVertex3f(1, -1, -1);
glVertex3f(1, 0, -1);
glVertex3f(1, -1, -1);
glVertex3f(0, -1, -1);
glVertex3f(0, -1, -1);
glVertex3f(0, -1, 0);
glVertex3f(0, -1, -1);
glVertex3f(0, 0, -1);
glVertex3f(0, 0, -1);
glVertex3f(1, 0, -1);
glVertex3f(0, 0, -1);
glVertex3f(0, 0, 0);
glVertex3f(1, 0, -1);
glVertex3f(1, 0, 0);
glEnd();
glPopAttrib();
What can be wrong?
From your screenshots it seems that the order of rendering is the following:
terrain
outline
chest
Now since you have disabled GL_DEPTH_TEST in step 2, the fragments (pixels) that turned white (those on which the outline is drawn), won't get their depth information updated. That's because disabling GL_DEPTH_TEST not only disables depth testing, but also disables updating the depth information of fragments.
So when you finally draw the chest (with depth testing re-enabled), it will be drawn over all the fragments whose depth value indicates that they are behind it. That means, also over the fragments where the outline is drawn.
The black color of the outline on the third screenshot may be caused by your call to glBindTexture(GL_TEXTURE_2D, 0);. But that's just a hunch.

OpenGL transparent object behind another transparent object

I'm trying to render grass leaves. The simple way is to draw two parallel quads crossing in the middle like this
The problem is that the first quad to be rendered won't be transparent for the next grass leaf to be drawed. If I draw A then B, B won't show behind A
This is how i'm rendering
void drawHighGrass(){
glDisable(GL_LIGHTING);
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor4f(1.0, 1.0, 1.0, 0.95);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texturas[HIGH_GRASS]);
glCullFace(GL_FRONT);
glPushMatrix();
//glTranslatef(1000, 0, 1000);
glScalef(1, 1.5, 1);
glPushMatrix();
glTranslatef(-100 / 2, -10, 0);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(0, 0, 0);
glTexCoord2f(1.0f, 0.0f); glVertex3f(100, 0, 0);
glTexCoord2f(1.0f, 1.0f); glVertex3f(100, 40, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f(0, 40, 0);
glEnd();
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(100, 0, 0);
glTexCoord2f(1.0f, 0.0f); glVertex3f(0, 0, 0);
glTexCoord2f(1.0f, 1.0f); glVertex3f(0, 40, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f(100, 40, 0);
glEnd();
glPopMatrix();
glPushMatrix();
//glTranslatef(1000, 0, 1000);
glTranslatef(0, -10, -100 / 2);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(0, 0, 0);
glTexCoord2f(1.0f, 0.0f); glVertex3f(0, 0, 100);
glTexCoord2f(1.0f, 1.0f); glVertex3f(0, 40, 100);
glTexCoord2f(0.0f, 1.0f); glVertex3f(0, 40, 0);
glEnd();
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(0, 0, 100);
glTexCoord2f(1.0f, 0.0f); glVertex3f(0, 0, 0);
glTexCoord2f(1.0f, 1.0f); glVertex3f(0, 40, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f(0, 40, 100);
glEnd();
glPopMatrix();
glPopMatrix();
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glEnable(GL_LIGHTING);
glCullFace(GL_BACK);
}
I found the solution:
glAlphaFunc(GL_GREATER, 0.5);
glEnable(GL_ALPHA_TEST);

Opengl draw to texture.. recursion?

I' experimenting in draw to texture with opengl.
I've found this simple and clear c code that do that (see in the end).
It draws a cube on a texture, and then a cube on the screen using the previous texture. The generated image is a cube with, in the textures, a lot of cube inside a cube inside a cube etc..: it seems a recursion loop.
But why? I was expecting only 2 cubes. And It seems an infinite recursion loop.. where is the actual recursion? and when does it stops?
/* ============================================================================
**
** Demonstration of rendering to texture
** Copyright (C) 2005 Julien Guertault
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**
** ========================================================================= */
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>
#define SIZE 256
static unsigned char texture[3 * SIZE * SIZE];
static unsigned int texture_id;
static int window_width = 500;
static int window_height = 500;
/*
** Just a textured cube
*/
void Cube(void)
{
glBegin(GL_QUADS);
glTexCoord2i(0, 0); glVertex3f(-1, -1, -1);
glTexCoord2i(0, 1); glVertex3f(-1, -1, 1);
glTexCoord2i(1, 1); glVertex3f(-1, 1, 1);
glTexCoord2i(1, 0); glVertex3f(-1, 1, -1);
glTexCoord2i(0, 0); glVertex3f( 1, -1, -1);
glTexCoord2i(0, 1); glVertex3f( 1, -1, 1);
glTexCoord2i(1, 1); glVertex3f( 1, 1, 1);
glTexCoord2i(1, 0); glVertex3f( 1, 1, -1);
glTexCoord2i(0, 0); glVertex3f(-1, -1, -1);
glTexCoord2i(0, 1); glVertex3f(-1, -1, 1);
glTexCoord2i(1, 1); glVertex3f( 1, -1, 1);
glTexCoord2i(1, 0); glVertex3f( 1, -1, -1);
glTexCoord2i(0, 0); glVertex3f(-1, 1, -1);
glTexCoord2i(0, 1); glVertex3f(-1, 1, 1);
glTexCoord2i(1, 1); glVertex3f( 1, 1, 1);
glTexCoord2i(1, 0); glVertex3f( 1, 1, -1);
glTexCoord2i(0, 0); glVertex3f(-1, -1, -1);
glTexCoord2i(0, 1); glVertex3f(-1, 1, -1);
glTexCoord2i(1, 1); glVertex3f( 1, 1, -1);
glTexCoord2i(1, 0); glVertex3f( 1, -1, -1);
glTexCoord2i(0, 0); glVertex3f(-1, -1, 1);
glTexCoord2i(0, 1); glVertex3f(-1, 1, 1);
glTexCoord2i(1, 1); glVertex3f( 1, 1, 1);
glTexCoord2i(1, 0); glVertex3f( 1, -1, 1);
glEnd();
}
/*
** Function called to update rendering
*/
void DisplayFunc(void)
{
static float alpha = 20;
glLoadIdentity();
glTranslatef(0, 0, -10);
glRotatef(alpha, 1, 0, 0);
glRotatef(20 , 0, 1, 0);
/* Define a view-port adapted to the texture */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(20, 1, 5, 15);
glViewport(0, 0, SIZE, SIZE);
glMatrixMode(GL_MODELVIEW);
/* Render to buffer */
glClearColor(1, 1, 1, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Cube();
glFlush();
/* Copy buffer to texture */
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 5, 5, 0, 0, SIZE - 10, SIZE - 10);
/* Render to screen */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(20, window_width / (float) window_height, 5, 15);
glViewport(0, 0, window_width, window_height);
glMatrixMode(GL_MODELVIEW);
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Cube();
/* End */
glFlush();
glutSwapBuffers();
/* Update again and again */
alpha = alpha + 0.1;
glutPostRedisplay();
}
/*
** Function called when the window is created or resized
*/
void ReshapeFunc(int width, int height)
{
window_width = width;
window_height = height;
glutPostRedisplay();
}
/*
** Function called when a key is hit
*/
void KeyboardFunc(unsigned char key, int x, int y)
{
int foo;
foo = x + y; /* Has no effect: just to avoid a warning */
printf("%d",foo);
if ('q' == key || 'Q' == key || 27 == key)
exit(0);
}
int main(int argc, char **argv)
{
/* Creation of the window */
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("Render to texture");
/* OpenGL settings */
glEnable(GL_DEPTH_TEST);
/* Texture setting */
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &texture_id);
glBindTexture(GL_TEXTURE_2D, texture_id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, SIZE, SIZE, 0, GL_RGB,
GL_UNSIGNED_BYTE, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
/* Declaration of the callbacks */
glutDisplayFunc(&DisplayFunc);
glutReshapeFunc(&ReshapeFunc);
glutKeyboardFunc(&KeyboardFunc);
/* Loop */
glutMainLoop();
/* Never reached */
return 0;
}
/* ========================================================================= */
But why? I was expecting only 2 cubes. And It seems an infinite recursion loop.. where is the actual recursion? and when does it stops?
It's a feedback loop. Small experiment for you: Take a webcam connect it to your computer and let it show a live picture of the webcam. Now point the webcam on the screen so that it looks at the image it produced.
This is kind of what happens here: A cube with a texture is drawn. That picture is now copied to the texture. Then the cube is drawn again, but this time the texture already contains that cube so it's a cube with cubes on it. Also, for your pleasure the same cube is drawn to the screen, but that doesn't feed back.
If you want to have one nesting deep, you must disable texturing when drawing the cube that gets copied to the texture.
Update Code modification that doesn't recurse
void DisplayFunc(void)
{
First render without texture so we don't feed back. Also make the drawing color black, so this will result in a solid black cube on black background.
/* Render to buffer */
glClearColor(1, 1, 1, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_TEXTURE_2D); /* <<<<<< */
glColor3f(0,0,0); /* <<<<<< */
Cube();
On a side note: For this there should really a frame buffer object be used, instead of relying on the back buffer to not be clobbered (overlapping windows may make it fail the pixel ownership test).
/* Copy buffer to texture */
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 5, 5, 0, 0, SIZE-10, SIZE-10);
/* Render to screen */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(20, window_width / (float) window_height, 5, 15);
glViewport(0, 0, window_width, window_height);
glMatrixMode(GL_MODELVIEW);
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Reenable texturing and switch color to white so that we can see the texture (or use GL_REPLACE texture mode).
glEnable(GL_TEXTURE_2D);
glColor3f(1,1,1);
Cube();
/* End */
glFlush();
glutSwapBuffers();
/* Update again and again */
alpha = alpha + 0.1;
glutPostRedisplay();
}

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();

.gif image display using 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).