PDA

View Full Version : Problem with learning texturizing please Help



Ehsan_Liverpool
07-25-2011, 04:49 AM
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, &amp;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, &amp;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(&amp;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!

V-man
07-25-2011, 05:39 AM
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.

Ehsan_Liverpool
07-25-2011, 08:09 PM
does my code support .png? i would certainly prefer .png to .raw, basically i would prefer anything to .raw! :D

Alfonse Reinheart
07-25-2011, 09:38 PM
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.

Ehsan_Liverpool
07-25-2011, 11:40 PM
:) 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 :)

Alfonse Reinheart
07-26-2011, 02:28 AM
Try using any one of these. (http://www.opengl.org/wiki/Related_toolkits_and_APIs#Image_and_Texture_Librar ies)

BionicBytes
07-26-2011, 12:19 PM
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.