Problem with learning texturizing please Help

Hi!!

I’m trying to load a picture to a box in opengl and im having some difficulties :frowning: 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!

Did you notice that you load your texture in your myDisplay function? That’s not a good idea.
Load it after glutCreateWindow in your main() function.

I wouldn’t know why it is black. It could be the texture itself is all black.

For file formats, I prefer png. Good compression and it support alpha component (RGBA8). DDS is nice to for other types of internal formats.

does my code support .png? i would certainly prefer .png to .raw, basically i would prefer anything to .raw! :smiley:

PNG will not support it self. All you’re doing is taking a file and blasting its contents into memory. The reason you can only use RAW is because RAW is defined as just the raw image data. Every other format has, at the very least, a file header, if not actual decompression to work with.

:slight_smile: as you can probably tell i am quite new to this, so… can u give me some pointers on how to actually get png or bmp or jpg to work? i would appreciate it :slight_smile:

Try using any one of these.

If you are not going to use a 3rd party image library and want to code things yourself, the best bet is to support TGA files and then DDS files.
TGA files are great because they only have a 12 byte header followed by the actual image data. TGA does have a number of ‘options’ in the header which could mean the image is flipped in X or Y, compressed or ABGR format - however in the simplest case for BGR(A) non-compressed formats it’s extreemly easy.