Showing posts with label OpenGL graphics. Show all posts
Showing posts with label OpenGL graphics. Show all posts

Tuesday, May 29, 2012

Simple method for texture mapping on sphere Using gluSphere and gluQuadricTexture in OpenGL

In my previous article [here] I have shown you how to map texture in solid sphere. In that method whole sphere is render using triangles and providing texture co-ordinates which seems difficult to understand. Now in this article I am going to show you simple method to map texture on Sphere. We first draw a sphere using gluSphere and texture co-ordinates are automatically generated by gluQuadricTexture function for Sphere.
gluSphere:
syntax:
void WINAPI gluSphere( GLUquadric *qobj, GLdouble radius, GLint slices, GLint stacks);

Wednesday, May 9, 2012

Creating multiple windows with OpenGL and GLUT

We may need the multiple OpenGL windows for our program for example we can show orthographic view of object (top view, side view,front view)  in separate  windows.
Steps for Creating Multiple Windows:-
1.Initialize drawing context and frame buffer using glutInit(), glutInitDisplayMode(), and glutInitWindowSize().
2.Create first window
3.Register callbacks for first window
4.Create second window
5.Position the second window
6.Register callbacks for second window
7.Register (shared) idle callback
8.Enter the main loop
Note: Callbacks can be shared between windows, if desired



Friday, February 3, 2012

Draw a 3D Rubik cube using OpenGL utility toolkits GLUT with source code

This article is similar  to my previous article drawing 3D chess board.To make Rubik cube real I have used texture mapping.At first I draw a one cube where I mapped texture in all six side of cube with six different color images and you can save these images from here.After that 27 cube is drawn by looping and final Rubik cube is drawn, rotating in (2,2,0) axis.For texture mapping we have to write a code to load a images and store a value of (R1,G1,B1,R2,G2,B2,……………)pixels in pixels array.In this project there is a Image class where you can see in source code below in file name imageloader.h and imageloader.cpp which I have take from a website www.videotutorialsrock.com.
All the images for this program is given below save it in your project file location.

Monday, November 21, 2011

Draw a 3D Chess Board using OpenGL utility toolkits (GLUT), a graphics project with source code

This program  may help you in graphics project of your college.I simply  tries to draw the 3D Chess Board.  For this our main goal is to draw white and black rectangular box alternately along both x and y-axis.Drawing rectangular box is simple ,it is drawn by providing all side co-ordinates with the help of glVertex3f function and only top face is assign with black or white color and other face with brown color with the help of glColor3f function. I have made two function void draw_BlackArea() and void draw_whiteArea() to draw black and white square box respectively .Inside two for loop I have call these function by creating displaylist and with the help of glTranslatef(GLfloat x,GLfloat y,GLfloat z) function I draw  alternatively by translating their position.

Tuesday, November 15, 2011

Draw Reflection of Object on the floor using OpenGL Utility Toolkit (GLUT)

Drawing reflection capture the extra realism of the Object in 3D programming.For example Bouncing ball having its reflection on the floor can make the bouncing scenario more beautiful and realistic. So,OpenGL has all the features needed to render fast high-quality reflections on planar surfaces.
        Reflection technique is not so hard its pretty simple.If you want to draw reflection on floor then suppose your floor is in x-z plane and your object along positive y-axis above the plane then for reflection of object draw the copy of object along the negative y-axis vertically below the plane or floor.Your floor must look like the reflecting surface to see the object which is below the floor for this we use alpha blending which make the floor transparent by certain percent say 40% then it is specified to be 40% the color of the floor and 60% the color of the reflection or object.But only alpha blending doesn’t make the reflection real because the reflected copy of the object may leak outside or bottom of the floor so to fixed this problem we actually going to use the stencil buffer concept.With OpenGL stenciling, we can "pre-draw" the floor into the stencil buffer without updating the color or depth buffers. Then when we go to draw the reflection,  just only allow the reflection to update pixels marked as belonging to the floor's stencil value. The floor can then be any complex (but coplanar) polygon and the reflection only shows up in the floor.

Drawing a Bitmapped Character or string in OpenGL sample example

A bitmap is a rectangular array of 0s and 1s that serves as a drawing mask for a corresponding rectangular portion of the window. Suppose you're drawing a bitmap and that the current raster color is red. Wherever there's a 1 in the bitmap, the corresponding pixel is replaced by a red pixel and If there's a 0 in the bitmap, the contents of thepixel are unaffected. The most common use of bitmaps is for drawing characters on the screen.
            In the simple example we use two main function glBitmap and glRasterPos2i function.
The glBitmap function draws a bitmap.

Saturday, November 12, 2011

Rotate an Object in OpenGL(GLUT) sample example with source

In GLUT we rotate the object by glRotate3f(angle_rotation,GLfloat x, GLfloat y ,GLfloat z) function angle_rotation parameter is value of angle to rotate and x,y,z co-ordinate are axis of rotation.But it does not rotate object continuously for that we have to change angle of rotation by small amount of increment. We do this by using timer thread with the help of glutTimerFunc function.Here is the sample source code copy and compile it.

Friday, November 11, 2011

How to change the color of object in OpenGL(GLUT) using keyboard function

If you have problem in changing color of object in OpenGL using keyboard while running program then this article may help you.Simply we can’t change the color of object while running for this we have to create a timer thread by using
Void  glutTimerFunc(unsigned int msecs,void (*func(int value),value)  function or other techinque can also be use.
Parameter:
msecs

Number of milliseconds to pass before calling the callback.

func

The timer callback function.

value

Integer value to pass to the timer callback.

Thursday, June 30, 2011

BItmap maping and texture map in OpenGL

This the sample code for texture map in teapot and bit map is used to draw chess board.Code is compiled in codeblocks you can copy and paste this code for your study or project.Please use your own image for texture map.Image must be in bmp formate.


#include <windows.h>
#include <GL/glut.h>
#include <stdio.h>
GLuint texture[2];
GLint slices=16;
GLint stacks=16;
struct Image {
    unsigned long sizeX;
    unsigned long sizeY;
    char *data;
};
typedef struct Image Image;

Friday, June 17, 2011

Mini project "3D Bounce " in OpenGL

This project is my second-year mini-project. I have use the GLUT(OpenGL) library in c++.3D Bounce is a game in which we have to bounce the ball to collect the floating star,after collecting all star then a door opens for the next room. Due to lack of time, I can't complete this project but all image drawing part is completed and I hope you will complete it for your collage project.You can download it source code from this link http://www.4shared.com/file/J-cjGaN-/bounce.html .This program is written in complete object oriented environment ,I have made different class in different file and It's code is very lengthy so  I can't give source code here.

Wednesday, June 15, 2011

Drawing a snowman using OpenGL source code


This source code is written in c using the OpenGL library. This program is just a demo to draw a different shape object and I draw a man like an object in this program. I have also added a keyboard event in this program.

#include <math.h>
#include <GL/glut.h>
#include <stdio.h>
#include <string.h>
//#include <cstdlib>
//if you got error in exit() by compiler then does not incluede stdlib.h because //exit() is also defined in glut.h file.

float angle=0.0,deltaAngle = 0.0,ratio;
float x=0.0f,y=1.75f,z=5.0f;
float lx=0.0f,ly=0.0f,lz=-1.0f;
int deltaMove = 0,h,w;
int font=(int)GLUT_BITMAP_8_BY_13;
static GLint snowman_display_list;
int bitmapHeight=13;

int frame,time,timebase=0;
char s[30];

void initWindow();

void changeSize(int w1, int h1)
{

// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h1 == 0)
h1 = 1;

w = w1;
h = h1;
ratio = 1.0f * w / h;
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Set the viewport to be the entire window
    glViewport(0, 0, w, h);

// Set the clipping volume
gluPerspective(45,ratio,0.1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(x, y, z,
     x + lx,y + ly,z + lz,
 0.0f,1.0f,0.0f);


}


Sunday, June 12, 2011

Mouse event handling source code in OpenGL



This source code is written in C++ with OpenGL.It is just a demo for mouse event handling you can use this code for your project. In this example void motionPassive(int x, int y) to find out the cursor position in the window. By finding the difference between the previous and initial position the direction of the camera is changed.

Texture map in solid sphere using GLUT(OpenGL)


This source code is written in C used to draw a solid sphere using OpenGL and you can map texture in it. Create a project for OpenGL and copy and paste this code and use your desire image file for texture mapping.

//main.c

#include <GL/gl.h>
#include <GL/glut.h>
#include <windows.h>
#include <stdio.h>
#include <math.h>
GLuint texture;
double angle = 0;
typedef struct
{
    int X;
    int Y;
    int Z;
    double U;
    double V;
}VERTICES;

const double PI = 3.1415926535897;
const int space = 10;
const int VertexCount = (90 / space) * (360 / space) * 4;
VERTICES VERTEX[VertexCount];
GLuint LoadTextureRAW( const char * filename );

Tuesday, February 22, 2011

Setup GLUT (OpenGL) in Codeblocks


GLUT (OpenGL Utility Toolkit) is a toolkit to write OpenGL programs. It makes learning OpenGL programming very easy and simple. However, it is very old and unsupported for the last 20 years or more and it does not support modern requirements. I personally do not recommend this library and I request to choose one of the alternate libraries such as GLFW, freeglut, and SDL.

If you still need to setup this library to execute old code downloaded from the internet written in GLUT then follow the steps provided below:

Download the glut library from the website http://code.google.com/p/rawvideoplayer/downloads/detail?name=glut-3.7.6-bin.zip&can=2&q= for windows.

Steps to create GLUT project in Code::block.
Step 1: Extract the downloaded zip file.

Step 2: Browse extracted files. Copy the glut.h file and paste it into the directory "C:\ProgramFiles(x86)\CodeBlocks\MinGW\include\GL". Copy glut.lib file and paste it into the directory "C:\Program Files(x86)\CodeBlocks\MinGW\lib". Also, copy the glut.dll and paste it into
the directory "C:\Windows\System32".