PDA

View Full Version : glbind prob



ben1441
07-29-2003, 11:04 AM
With this source I'm just trying to bind a 2d texture to a square, but all I get is a blank white square, anyone know what I'm doing wrong?

#include <stdio.h>
#include <windows.h>
#include <gl\glut.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <stdlib.h>

void load_texture ( char *file_name, int width, int height, int depth, GLenum colour_type, GLenum filter_type )
{
GLubyte *raw_bitmap ;
FILE *file;
if ((file = fopen(file_name, "rb"))==NULL)
{
printf("File Not Found : %s\n",file_name);
system("pause");
exit (1);
}
raw_bitmap = (GLubyte *) malloc ( width * height * depth * ( sizeof ( GLubyte )) );

if ( raw_bitmap == NULL )
{
printf ( "Cannot allocate memory for texture\n" );
system("pause");
fclose ( file );
exit ( 1 );
}
fread ( raw_bitmap , width * height * depth, 1 , file );
fclose ( file);
//glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter_type );
//glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter_type );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf ( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
gluBuild2DMipmaps ( GL_TEXTURE_2D, colour_type, width, height, colour_type, GL_UNSIGNED_BYTE, raw_bitmap );
free ( raw_bitmap );
}

void init ( void )
{
glEnable ( GL_DEPTH_TEST );
glClearColor ( 0.0, 0.0, 0.0, 0.0 );

glEnable ( GL_TEXTURE_2D );
glPixelStorei ( GL_UNPACK_ALIGNMENT, 1 );
load_texture ( "C:/Documents and Settings/User/My Documents/My Pictures/aaliyah.raw", 332, 425, 3, GL_RGB, GL_LINEAR );


}

void square (void)
{

//glColor3f (1.0,1.0,1.0);
glBindTexture(GL_TEXTURE_2D, 13);
glBegin (GL_QUADS);
glTexCoord2f (0.0, 0.0);
glVertex3f (0.0, 0.0, 0.0);
glTexCoord2f (1.0, 0.0);
glVertex3f (10.0, 0.0, 0.0);
glTexCoord2f (1.0, 1.0);
glVertex3f (10.0, 10.0, 0.0);
glTexCoord2f (0.0, 1.0);
glVertex3f (0.0, 10.0, 0.0);
glEnd ();
//glutWireCube(1.0) ;

}


void display ( void )
{
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix ( );
square();
glPopMatrix ( );
glutSwapBuffers ( );
}

void reshape(int w, int h)
{
glViewport ( 0, 0, w, h );
glMatrixMode ( GL_PROJECTION );
glLoadIdentity ( );
if ( h==0 )
gluPerspective ( 80, ( float ) w, 1.0, 5000.0 );
else
gluPerspective ( 80, ( float ) w / ( float ) h, 1.0, 5000.0 );
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity ( );
}


int main(int argc,char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (800,500);
glutInitWindowPosition (100,100);
glutCreateWindow ("Ben's window");

init();
//glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Bob
07-29-2003, 03:27 PM
You're not binding a texture name before uploading the texture.

c0d3Junk3Y
07-30-2003, 05:14 AM
Try this :

#include <windows.h>
#include <stdio.h>
#include <scrnsave.h>
#include <commctrl.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>

GLuint texture[1];

AUX_RGBImageRec *LoadBMP(char *Filename)
{
FILE *File=NULL;
if(!Filename)
{
return NULL;
}
File=fopen(Filename, "r");
if(File)
{
fclose(File);
return auxDIBImageLoad(Filename);
}
return NULL;
}

int LoadGLTextures()
{
int Status=FALSE;

AUX_RGBImageRec *TextureImage[1];

memset(TextureImage, 0, sizeof(void *)*1);

// Load the bitmap, check for errors, if bitmaps not found quit
if(TextureImage[0]=LoadBMP("Data/light.bmp"))
{
Status=TRUE;

glGenTextures(1, &texture[0]);

glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
if(TextureImage[0])
{
if(TextureImage[0]->data)
{
free(TextureImage[0]->data);
}
free(TextureImage[0]);
}
return Status;
}


Check out http://nehe.gamedev.net/ for some excellent tutorials on texture mapping and filters. I hope this helps.

ben1441
07-30-2003, 05:43 AM
Ok, I've got something, but it's just a smudge of blue on the right hand corner! Has the palette gone to pot? How do u fix it? baffled ben

c0d3Junk3Y
07-30-2003, 07:34 AM
Ben

Is there blue in your texture?

If so you may need to rethink the math to align the texture properly. Also I forgot to mention this works the best when you use a 16x16 32x32 256x256 image, you get corruption with anything above 256. There is a work around for a larger image, but I am not aware of how to do it.

ben1441
07-31-2003, 12:52 AM
Ok, having resized to 256by256 has made a difference, there's multicoloured streaks now across the square. Hmm.. (Strokes beard) The only thing I can think of is that I should change from using the .raw loader,.. only prob is I couldn't get the bmp loader to work. Anyone know what's up with the .raw?