Hi!!
I’m trying to load a picture to a box in opengl and im having some difficulties this is what i have so far:
#include <stdlib.h>
#include <windows.h>
#include <math.h>
#include <iostream>
#include <gl/Gl.h>
#include <gl/Glu.h>
#include <gl/Glut.h>
using namespace std;
//<<<<<<<<<<<<<<<<<<<<<<< LoadTexture >>>>>>>>>>>>>>>>>>>>
GLuint LoadTexture( const char * filename, int width, int height )
{
GLuint texture;
unsigned char * data;
FILE * file;
file = fopen( filename, “rb” ); // We need to open our file
if ( file == NULL ) return 0; //If our file is empty, set our texture to empty
data = (unsigned char *)malloc( width * height * 3 ); //assign the nessecary memory for the texture
fread( data, width * height * 3, 1, file ); //read in our file
fclose( file ); //close our file, no point leaving it open
glGenTextures( 1, &texture ); //then we need to tell OpenGL that we are generating a texture
glBindTexture( GL_TEXTURE_2D, texture ); //now we bind the texture that we are working with
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); //set texture environment parameters
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
free( data ); //free the texture
return texture; //return the texture data
}
//<<<<<<<<<<<<<<<<<<<<<<< Free Texture >>>>>>>>>>>>>>>>>>>>
void FreeTexture( GLuint texture )
{
glDeleteTextures( 1, &texture ); // Delete our texture, simple enough.
}
//<<<<<<<<<<<<<<<<<<<<<<< myInit >>>>>>>>>>>>>>>>>>>>
void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0); // the background color is white
glColor3f(0.0f, 0.0f, 0.0f); // the drawing color is black
glPointSize(2.0); // a ‘dot’ is 2 by 2 pixels
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-2.0, 2.0, -2.0, 2.0);
}
//<<<<<<<<<<<<<<<<<<<<<<<< myDisplay >>>>>>>>>>>>>>>>>
void myDisplay(void)
{
// glEnable( GL_TEXTURE_2D );
// glBindTexture( GL_TEXTURE_2D, texture );
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
glColor3d(0,0,0);
GLint texture = LoadTexture( "texture.raw", 256, 256 );
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, texture );
glBegin(GL_QUADS);
glVertex2d(-1.0,-1.0);
glTexCoord2d(0.0,0.0);
glVertex2d(+1.0,-1.0);
glTexCoord2d(1.0,0.0);
glVertex2d(+1.0,+1.0);
glTexCoord2d(1.0,1.0);
glVertex2d(-1.0,+1.0);
glTexCoord2d(0.0,1.0);
glEnd();
/*glEnd();*/
glFlush(); // send all output to display
}
//<<<<<<<<<<<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>>
void main(int argc, char** argv)
{
glutInit(&argc, argv); // initialize the toolkit
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set the display mode
glutInitWindowSize( 640, 420); // set the window size
glutInitWindowPosition(100, 150); // set the window position on screen
glutCreateWindow(“Texture”); // open the screen window
glutDisplayFunc(myDisplay); // register the redraw function
myInit();
glutMainLoop(); // go into a perpetual loop
}
All my program does is give me a black box instead of a texture… i was wondering if there is a better way… and if there is a better file format then .raw to use as a texture…
*note: i did add the file to my program im pretty sure thats not my problem
THANKS!