Hi all.
Sorry if this is a beginner question, but I have not been able to find any solution to this problem, or indeed anyone else who has had the same problem.
I have tried several times to display a texture on a simple polygon using OpenGL, but for some reason, no matter what image library I use, I have problems. The texture does not load, leaving the polygon a default white colour. I have tried with Cocoa image loading, SDL, and GLFW. I'm compiling on a PPC running Mac OS X, and I'm using XCode v2.4.
Here is a sample of my OpenGL code:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "GL/glfw.h"
#define S_WIDTH 640
#define S_HEIGHT 480
// Bad Global Variables:
//int width, height; // Window dimensions
double t; // Time (seconds)
GLuint texture; // Texture reference
// Function prototypes:
void reSizeGLScene( GLsizei width, GLsizei height );
bool initGL();
bool loadTexture( const char* path );
bool drawGLScene();
void cleanUp();
int main( int argc, char** argv )
{
// Initialize variables:
bool status = true; // Is the program okay?
bool done = false; // Is the program done?
// Start up GLFW
glfwInit();
// Open up a window
status = glfwOpenWindow(
S_WIDTH, S_HEIGHT,
8, 8, 8, 8,
24,
0,
GLFW_WINDOW
);
// Exit if there is an error opening the window
if ( !status )
{
glfwTerminate();
fprintf( stderr, "ERROR OPENING GLFW WINDOW\n" );
return 1;
}
// Set the window title
glfwSetWindowTitle( "My OpenGL Program" );
// Enable sticky keys
glfwEnable( GLFW_STICKY_KEYS );
initGL();
loadTexture( "data/test.tga" );
// Game loop:
while ( !done )
{
reSizeGLScene( S_WIDTH, S_HEIGHT );
// Call the render function:
status = drawGLScene();
// Flip buffers:
glfwSwapBuffers();
done = glfwGetKey( GLFW_KEY_ESC ) || !glfwGetWindowParam( GLFW_OPENED );
}
// Clean up:
cleanUp();
}
/*~~~~~~~~~~~ reSizeGLScene() ~~~~~~~~~~~~*/
/* - Resize and initialize the GL window
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void reSizeGLScene( GLsizei width, GLsizei height )
{
// Make sure the height is non-zero
if ( height < 1 )
{
height = 1;
}
// Set the viewport:
glViewport( 0, 0, width, height );
// Now set up the projection matrix:
glMatrixMode( GL_PROJECTION ); // Change the matrix mode to projection
glLoadIdentity(); // Identity Matrix
gluPerspective( 65.0, (double)width/(double)height, 1.0, 100.0 ); // Set the perspective
// Set up the modelview matrix:
glMatrixMode( GL_MODELVIEW ); // Modelview matrix
glLoadIdentity(); // Identity matrix
}
/*~~~~~~~~~~~~~~~ initGL() ~~~~~~~~~~~~~~~*/
/* - Initializes OpenGL
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
bool initGL()
{
glEnable( GL_TEXTURE_2D ); // Enable texture mapping
glShadeModel( GL_SMOOTH ); // Use the smooth shader model
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); // Set a black background
glClearDepth( 1.0f ); // Setup the depth buffer
glEnable( GL_DEPTH_TEST ); // Enable depth testing
glDepthFunc( GL_LEQUAL ); // The type of depth test to do
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); // Really Nice perspective calculations
return true; // Return success
};
/*~~~~~~~~~~~~ loadTexture() ~~~~~~~~~~~~~*/
/* - Initializes Textures
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
bool loadTexture( const char* path )
{
glGenTextures( 0, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glfwLoadTexture2D( path, 0 );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
return true;
}
/*~~~~~~~~~~~~ drawGLScene() ~~~~~~~~~~~~~*/
/* - Main OpenGL draw function
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
bool drawGLScene()
{
// Now let's get the system time
t = glfwGetTime();
// Clear colour and depth buffers:
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // Clear the screen and depth buffer
glLoadIdentity(); // Reset the ModelView Matrix
glTranslatef( 0.0f, 0.0f,-6.0f ); // Set the offset
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, texture ); // Select texture
fprintf( stderr, "Texture id: %d\n", texture );
glBegin( GL_QUADS );
{
glTexCoord2f( 0.0f, 0.0f ); glVertex3f(-2.0f, 2.0f, 0.0f );
glTexCoord2f( 1.0f, 0.0f ); glVertex3f( 2.0f, 2.0f, 0.0f );
glTexCoord2f( 1.0f, 1.0f ); glVertex3f( 2.0f,-2.0f, 0.0f );
glTexCoord2f( 0.0f, 1.0f ); glVertex3f(-2.0f,-2.0f, 0.0f );
}
glEnd();
glDisable( GL_TEXTURE_2D );
glPopMatrix();
return true; // Return success
}
/*~~~~~~~~~~~~~~ cleanUp() ~~~~~~~~~~~~~~~*/
/* - Clean everything up
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void cleanUp()
{
glfwTerminate();
}
I have already looked at several examples and tutorials. I suspect that the problem is probably in the draw function, but I can't seem to pinpoint the exact problem. While I have no error checking on my image file, it is a 256x256 targa file. It also has the correct filepath.
Hopefully I'm not wasting your time with a stupid mistake, and thanks in advance for helping out.