PDA

View Full Version : Add Bitmap



rmmlg
04-27-2005, 07:05 PM
I am attempting to insert bitmaps into OpenGL, but I have been unsuccessful. Evidently, there is something wrong with my code, but I am not quite sure what. Instead of drawing a box with a texture on it, it just draws a white box. Here are the snippets of my code that matter to the bitmap mapping:

void paint()
{
InitGL();
glEnable(GL_TEXTURE_2D);
//draw a square with specified texture coordinates
double xvals[] = {cur_x_cross, cur_x_cross + 10, cur_x_cross + 10, cur_x_cross};
double yvals[] = {cur_y_cross, cur_y_cross, cur_y_cross + 10, cur_y_cross + 10};
float svals[] = {0.0f, 0.0f, 1.0f, 1.0f};
float tvals[] = {1.0f, 0.0f, 0.0f, 1.0f};

glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
for (int i=0; i < 4; i++)
{
glVertex2f(xvals[i], yvals[i]);
glTexCoord2f(svals[i], tvals[i]);
}
glEnd();
glDisable(GL_TEXTURE_2D);
//glFlush();
glutSwapBuffers();
}

HDC hDC=NULL; // Private GDI Device Context
HGLRC hRC=NULL; // Permanent Rendering Context
HWND hWnd=NULL; // Holds Our Window Handle
HINSTANCE hInstance; // Holds The Instance Of The Application

bool keys[256]; // Array Used For The Keyboard Routine
bool active=TRUE; // Window Active Flag Set To TRUE By Default
bool fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default

GLuint texture[1]; // Storage For One Texture ( NEW )

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc

AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image
{

FILE *File=NULL; // File Handle

if (!Filename) // Make Sure A Filename Was Given
{
return NULL; // If Not Return NULL
}

File=fopen(Filename,"r"); // Check To See If The File Exists

if (File) // Does The File Exist?
{
fclose(File); // Close The Handle
return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
}

return NULL; // If Load Failed Return NULL
}

int LoadGLTextures() // Load Bitmaps And Convert To Textures
{

int Status=FALSE; // Status Indicator

AUX_RGBImageRec *TextureImage[1]; // Create Storage Space For The Texture

memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL

// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
if (TextureImage[0]=LoadBMP("untitled.bmp"))
{
status2 = 1;
Status=TRUE; // Set The Status To TRUE

glGenTextures(1, &amp;texture[0]); // Create The Texture

// Typical Texture Generation Using Data From The Bitmap
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_MIN_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
}

if (TextureImage[0]) // If Texture Exists
{
if (TextureImage[0]->data) // If Texture Image Exists
{
free(TextureImage[0]->data); // Free The Texture Image Memory
}

free(TextureImage[0]); // Free The Image Structure
}

return Status; // Return The Status
}

int InitGL(GLvoid) // All Setup For OpenGL Goes Here
{
if (!LoadGLTextures()) // Jump To Texture Loading Routine ( NEW )
{
return FALSE; // If Texture Didn't Load Return FALSE
}

//
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
LoadGLTextures();
return TRUE; // Initialization Went OK
}

MMMovania
04-27-2005, 08:56 PM
Hello there,
There are a couple ofhings that i think are not correct in the given code.
1) You are giving texture coordinates after you are creating the vertices.This will result in an unordered texturing i think you should move it before glvertex function in your paint method.
2) Perhaps try to texture the cube manuallly without using the for loop. I don;t think your texture coordinates are correct.
3) Remove the bindtexture call in the paint method you have already bound the textures in initgl funcion.
See if this sorts out the problem. One more thing do not use the auxlibraryfunctions because they are only here for compatability reasons. You can create your on bitmap loader quite comfortably or have a look at some earlier thrreads in the forum for help on this.
Thanx
MMM

CrazyButcher
04-28-2005, 12:42 AM
you are currently loading the texture every paint which is not good.
also you load textures twice in the initgl func

try to load the image only once (in main func before you go into your main loop) and then later you purely use the texture by the texture[0] id

so move that LoadGLTextures() func totally out of the painting/initgl

also what mmm said is true you must specify texcoords before vertex call

rmmlg
04-29-2005, 08:10 AM
Thank you for your replies. I am still just getting little white squares after I have made all of the changes. The original code that I used was copied from a variety of sources and attempted bt be debugged, but finding the problem seems to be incredibly difficult.

- Ryan