Loading existing BMP image, drawing onto it, save it back again.

I might need your help guys, cant figure it out.
so i’ll try to explain shortly what i need to do.

writing a program in C:

  1. Load existing BMP image
  2. Draw line onto it
  3. Save it

im stuck at 1st part, for some reason the window is completely black without the desired image.
any help would be appriciated.

#include <stdio.h>
#include <stdlib.h>	
#include <math.h>	
#include <string.h> 

#ifdef __APPLE__
#include <GLUT/glut.h>
#include <GL/glut.h>         /* glut.h includes gl.h and glu.h*/

GLuint texture;

GLuint loadBMP_custom(const char * imagepath)
	// 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
"); return 0; }

	if (fread(header, 1, 54, file) != 54) { // If not 54 bytes read : problem
		printf("Not a correct BMP file
		return 0;

	if (header[0] != 'B' || header[1] != 'M') {
		printf("Not a correct BMP file
		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 done that way

										 // Create a buffer
	data =(unsigned char*)malloc(sizeof(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_RGB, GL_UNSIGNED_BYTE, data);

	return textureID;

void display(void) {

	glClearColor(0.0, 0.0, 0.0, 1.0);

	glBindTexture(GL_TEXTURE_2D, texture);

	glTexCoord2d(0.0, 0.0); glVertex2d(0.0, 0.0);
	glTexCoord2d(1.0, 0.0); glVertex2d(1024.0, 0.0);
	glTexCoord2d(1.0, 1.0); glVertex2d(1024.0, 512.0);
	glTexCoord2d(0.0, 1.0); glVertex2d(0.0, 512.0);


void reshape(int w, int h) {
	glViewport(0, 0, (GLsizei)w, (GLsizei)h);
	gluPerspective(60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
int main(int argc, char **argv)

	glutInit(&argc, argv);

	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(1024, 512);
	glutInitWindowPosition(100, 100);
	glutCreateWindow("A basic OpenGL Window");

	texture = loadBMP_custom("Directory\\image.bmp");
	return 0;

i dont really need to open the BMP image, i just need to load it, draw on it and save it again.
i’ve tried to open it at my program to make sure i’ve done the 1st phase correctly.

You’re trying to draw a polygon on the Z=0 plane while using a perspective projection. Use an orthographic projection instead. Or if you must use a perspective projection, use appropriate vertex coordinates.

I really have no clue how to do it unfortunately.
could you navigate me to the solution please ?
i dont really need to open the BMP image, i just need to load it, draw on it and save it again.
i’ve tried to open it at my program to make sure i’ve done the 1st phase correctly.


	gluPerspective(60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);


	gluOrtho2D(0.0, 1024.0, 0.0, 512.0);