Search Projects

Saturday, July 26, 2025

Drawing Mountains in OpenGL Using Bitmap Loaders

Using the OpenGL Glut library, we have created numerous straightforward and intricate programs, but none of them have used texturing or image loading. We tried to find how to load image in the Opengl? The stackoverflow question pertaining to "Loading images in openGL" yielded the best response.

Images Loading in OpenGL Graphics Programming

We can claim that working with OpenGL graphics programmiong, the image loading is seems to be difficult task. External libraries such as stb_image.h, DevIL (Developer's Image Library), FreeImage, and SOIL (Simple OpenGL Image Library) are used to read and decode images into raw pixel data; OpenGL does not import image files directly. This data is then sent to the GPU as a texture using OpenGL methods such as glGenTextures, glBindTexture, and glTexImage2D for rendering.

In addition to images/photos/pictures, textures can be used to contain a sizable collection of arbitrary data that can be sent to the shaders for rendering the object.

Drawing Mountains in OpenGL

In this Computer graphics Open project we are going to generate the mountains using the image texture with use of bitmap loaders.



1. Grass Like Texture to Mountains

We are randomly generating the mountains using a normalized pseudo-random number generator and fractal techniques. The windows are displayed differently each time you resize them or start the project program application. This application relies on GLUT and uses a bitmap for the grass texture. Therefore, they were loaded into the program for use using BitmapLoader.h.

2. Random Number for (Gaussian) distribution

Let first define the randomNormal utility function, which uses the central limit theorem approximation (sum of uniform random variables) to produce a random number with a normal (Gaussian) distribution.

double randomNormal (double mu, double sigma)
{
double sum = 0;
for(int i = 0; i < 12; i++)
sum = sum + (double)rand()/RAND_MAX;
sum -= 6;
return sigma*sum + mu;
} 

3. Texture Creation and Circle Drawing Functions

Using raw bitmap data, createTexture1 loads a texture into OpenGL and sets its filtering mode to nearest neighbor (pixelated look) and wrapping mode to clamp.

Draw_circle uses a polygon of 36 vertices (one every 10 degrees) to approximate a 2D filled circle. This might stand in for the sun or another round object in the picture.


4. Fractal Mountain Generation Function

By periodically splitting a quadrilateral and applying a random height displacement, this recursive program creates fractal landscape.
Lighting effects are used to draw the quad once it is sufficiently small (perim <= tolerance). Using the Lambertian model, the lighting simulates diffuse light by shading the polygon according to its normal and light positions.
In order to produce four smaller quads recursively and add height perturbations for realistic terrain detail, it computes a displaced midpoint if it is not small enough.

5. Initialization of OpenGL and Textures

Uses the external method LoadBitmapFile to load the grass bitmap texture from the file. The program exits and prints an error if loading is unsuccessful. Specifies the scene's clipping volume and sets up the OpenGL projection matrix with an orthographic projection (no perspective). transforms the clean background into a dark gray.

Running the Program to get Desire Result

Before compiling the program place the grass image in the source folder, let the program load and run pefectly. If the grass image not present then comiplation will be error free but non loading of image lead to failure of outcome.

Downloads

Source code will be provie for both main program and header file including the bitmap image on request by you to our email id openglprojects@gmail.com

No comments:

Post a Comment