Load bmp image openGL C++

Load bmp image openGL C++. Window pops up with y axis and x axis but image does not display. Not sure if it is not being called or what?

    #include <iostream>
    //#include <math.h>
    #include <GL/glut.h>				// include GLUT library
    #include <GL/gl.h>
    #include <GL/glu.h>
    #include <stdlib.h>
    #include <Windows.h>
    #include "RGBpixmap.h"
    using namespace std;
    void drawAxis()
    	//axis labels
    	glColor3f(0, 0, 0);
    	glRasterPos2i(159, 0);
    	glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, 'X');
    	glRasterPos2i(0, 159);
    	glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, 'Y');
    	// draw line for x axis
    	glColor3f(0.0, 0.0, 0.0);
    	glVertex3f(-155.0, 0.0, 0.0);
    	glVertex3f(155.0, 0.0, 0.0);
    	// draw line for y axis
    	glColor3f(0.0, 0.0, 0.0);
    	glVertex3f(0.0, 155.0, 0.0);
    	glVertex3f(0.0, -155.0, 0.0);
    GLuint loadBMP_custom(const char* imagepath)
    	GLuint loadBMP_custom(const char* imagepath);
    	GLuint image = loadBMP_custom("./Man.bmp");
    	// Data read from the header of the BMP file
    	unsigned char header[54]; // Each BMP file begins by a 54-bytes header
    	unsigned int dataPos;     // Position in the file where the actual data begins
    	unsigned int width, height;
    	unsigned int imageSize;   // = width*height*3
    	// Actual RGB data
    	unsigned char* data;
    	// Open the file
    	FILE* file = fopen(imagepath, "rb");
    	if (!file) { printf("Image could not be opened\n"); return 0; }
    	if (fread(header, 1, 54, file) != 54) { // If not 54 bytes read : problem
    		printf("Not a correct BMP file\n");
    		return false;
    	if (header[0] != 'B' || header[1] != 'M') {
    		printf("Not a correct BMP file\n");
    		return 0;
    	// Read ints from the byte array
    	dataPos = *(int*) & (header[0x0A]);
    	imageSize = *(int*) & (header[0x22]);
    	width = *(int*) & (header[0x12]);
    	height = *(int*) & (header[0x16]);
    	// Some BMP files are misformatted, guess missing information
    	if (imageSize == 0)    imageSize = width * height * 3; // 3 : one byte for each Red, Green and Blue component
    	if (dataPos == 0)      dataPos = 54; // The BMP header is 
    	// Create a buffer
    	data = new unsigned char[imageSize];
    	// Read the actual data from the file into the buffer
    	fread(data, 1, imageSize, file);
    	//Everything is in memory now, the file can be closed
    	// Create one OpenGL texture
    	GLuint textureID;
    	glGenTextures(1, &textureID);
    	// "Bind" the newly created texture : all future texture functions will modify this texture
    	glBindTexture(GL_TEXTURE_2D, textureID);
    	// Give the image to OpenGL
    	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, data);
    	GLuint Texture = loadBMP_custom("Man.bmp");
    void myInit()
    	glClearColor(1, 1, 1, 0);			// specify a background clor: white 
    	gluOrtho2D(-200, 200, -200, 200);  // specify a viewing area
    	glPointSize(1);		// change point size back to 1
    void myDisplayCallback()
    	glClear(GL_COLOR_BUFFER_BIT);	// draw the background
    	glFlush(); // flush out the buffer contents
    int main(int argc, char** argv)
    	glutInit(&argc, argv);
    	glutInitWindowSize(400, 400);				// specify a window size
    	glutInitWindowPosition(100, 0);			// specify a window position
    	glutCreateWindow("Drawing Window");	// create a titled window
    	myInit();									// setting up
    	GLuint loadBMP_custom(const char* imagepath);
    	glutDisplayFunc(myDisplayCallback);		// register a callback
    	glutMainLoop();							// get into an infinite loop
    	return 0;
GLuint loadBMP_custom(const char* imagepath)
  GLuint loadBMP_custom(const char* imagepath);
  GLuint image = loadBMP_custom("./Man.bmp");

you declare a function inside that function and then call the function from inside the function.
and inside main(), you declare it again.
strangest thing i’ve ever seen.

I deleted the declared function inside that function and it still doesn’t work.

you have to delete the 2 bottom lines of what i quoted in my first reply and change
the line inside main() to

Okay now its just taking a long time to load and it doesn’t display before it quits. I debugged it and this error popped up at data = new unsigned char[imageSize];
Unhandled exception at 0x758D3522 in Lab7.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x002FF200.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.