How to Load a Bmp on Glut to Use It as a Texture

How to load a bmp on GLUT to use it as a texture?

Look my simple c implementation function to load texture.

GLuint LoadTexture( const char * filename )
{
GLuint texture;
int width, height;
unsigned char * data;

FILE * file;
file = fopen( filename, "rb" );

if ( file == NULL ) return 0;
width = 1024;
height = 512;
data = (unsigned char *)malloc( width * height * 3 );
//int size = fseek(file,);
fread( data, width * height * 3, 1, file );
fclose( file );

for(int i = 0; i < width * height ; ++i)
{
int index = i*3;
unsigned char B,R;
B = data[index];
R = data[index+2];

data[index] = R;
data[index+2] = B;
}

glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );

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 );
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,GL_RGB, GL_UNSIGNED_BYTE, data );
free( data );

return texture;
}

Above function returns the texture data. Store the texture data in variable

GLuint texture;
texture= LoadTexture( "your_image_name.bmp" );

Now you can bind the texture using glBindTexture

glBindTexture (GL_TEXTURE_2D, texture);

Opening bmp image to texture in OpenGL

You don't get a current GL context with GLUT until glutCreateWindow() successfully returns. glutInitDisplayMode() is not sufficient.

All the GL functions you call in loadTexture() require a current GL context to function.

Move texture = loadTexture(); to somewhere after glutCreateWindow() and before glutMainLoop();.

Also, if you're going to be using 3-component BGR/RGB make sure to use glPixelStorei() to set GL_UNPACK_ALIGNMENT to 1 (instead of the default 4) before your glTexImage2D() call.

Here's the simplest thing that works on my system:

// http://glew.sourceforge.net/
#include <GL/glew.h>
#include <GL/glut.h>

GLuint loadTexture()
{
const unsigned char data[] =
{
255, 0, 0, 0, 255, 0,
0, 0, 255, 255, 255, 255,
};
const GLsizei width = 2;
const GLsizei height = 2;

GLuint textureID = 0;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
return textureID;
}

GLuint texture = 0;
void display()
{
glClearColor( 0, 0, 0, 1 );
glClear( GL_COLOR_BUFFER_BIT );

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( -2, 2, -2, 2, 2, -2 );

glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,texture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_REPLACE);

glBegin(GL_QUADS);
glTexCoord2f( 0, 0 );
glVertex2i( -1, -1 );
glTexCoord2f( 1, 0 );
glVertex2i( 1, -1 );
glTexCoord2f( 1, 1 );
glVertex2i( 1, 1 );
glTexCoord2f( 0, 1 );
glVertex2i( -1, 1 );
glEnd();

glutSwapBuffers();
}

int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize( 640, 480 );
glutCreateWindow( "GLUT" );
glewInit();
texture = loadTexture();
glutDisplayFunc( display );
glutMainLoop();
return 0;
}

Texture wont appear FreeGLUT

You missed to set the Set the texture parameters by glTexParameter.

If you do not generate mipmaps (by glGenerateMipmap), then setting the GL_TEXTURE_MIN_FILTER is important. Since the default filter is GL_NEAREST_MIPMAP_LINEAR the texture would be mipmap incomplete, if you don not change the minifying function to GL_NEAREST or GL_LINEAR.

When the image is loaded to a texture object, then GL_UNPACK_ALIGNMENT has to be set to 1.

By default GL_UNPACK_ALIGNMENT is 4, so each line of an image is assumed to be aligned to 4 bytes. The pixels of a BMP-file in common have a size of 3 bytes and are tightly packed, this would cause a misalignment.

GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, outWidth, outHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*)data);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

Of course you have to set the texture parameter by glTexCoord, before specifying the vertex coordinate (glVertex) as mention in the other answer:

glBindTexture(GL_TEXTURE_2D, earthTextureData);

glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);

glTexCoord2f(0.0, 0.0);
glVertex3f(-0.5, -0.5, 0);

glTexCoord2f(0.0, 1.0);
glVertex3f(-0.5, 0.5, 0);

glTexCoord2f(1.0, 1.0);
glVertex3f(0.5, 0.5, 0);

glTexCoord2f(1.0, 0.0);
glVertex3f(0.5, -0.5, 0);

glEnd();
glDisable(GL_TEXTURE_2D);

Loading and Converting a HBITMAP to an OpenGL Texture

From the GetObject documentation:

If hgdiobj is a handle to a bitmap created by any other means, GetObject returns only the width, height, and color format information of the bitmap. You can obtain the bitmap's bit values by calling the GetDIBits or GetBitmapBits function.

In context, "other means" is anything other than CreateDIBSection. You're not using CreateDIBSection, you're using LoadImage. Which category the LR_CREATEDIBSECTION flag puts you into is unclear, but the workaround is clear: Use GetDIBits.



Related Topics



Leave a reply



Submit