PDA

View Full Version : hello there need some urgent help regarding a source code



rezashah59
05-24-2013, 11:07 PM
i want someone to explain me the source code for mirror image




#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>

void idleFunc( );
void displayFunc( );
void reshapeFunc( GLsizei width,

GLsizei height );
void keyboardFunc( unsigned char,

int, int );
void mouseFunc( int button, int

state, int x, int y );
void initialize( );

void render();
void mirror( GLfloat val );
#define INC_VAL 1.0f
#ifndef M_PI
#define M_PI 3.14159265359
#endif

GLsizei g_width = 1000;
GLsizei g_height = 800;
GLboolean g_rotate = GL_TRUE;
GLfloat g_light0_pos[4] = { 10.0f,

5.0f, 0.0f, 1.0f };
GLdouble eqn1[4] = { 0.0, 0.0, 1.0,

0.0 };
GLfloat g_inc = 0.0f;

double angle = 0.0;

int main( int argc, char ** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE

| GLUT_RGB | GLUT_DEPTH |GLUT_STENCIL

);
glutInitWindowSize( g_width,

g_height );
glutInitWindowPosition( 100, 100

);
glutCreateWindow( "Mirror Sample"

);
glutIdleFunc(idleFunc);
glutDisplayFunc( displayFunc );
glutReshapeFunc( reshapeFunc );
glutKeyboardFunc( keyboardFunc );
glutMouseFunc( mouseFunc );
initialize();
glutMainLoop();
return 0;
}
void initialize()
{
glClearColor( 0.0f, 0.0f,0.0f,

1.0f );
glShadeModel( GL_SMOOTH );
glEnable( GL_DEPTH_TEST );
glFrontFace( GL_CCW );
glPolygonMode( GL_FRONT_AND_BACK,

GL_FILL );
glLineWidth( 2.0f );
glEnable( GL_LIGHTING );
glLightModeli( GL_FRONT, GL_TRUE

);
glColorMaterial( GL_FRONT,

GL_AMBIENT_AND_DIFFUSE );
glEnable( GL_COLOR_MATERIAL );
glEnable( GL_LIGHT0 );
}

void reshapeFunc( GLsizei w, GLsizei

h )
{
g_width = w; g_height = h;
glViewport( 0, 0, w, h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );
gluPerspective( 45.0, (GLfloat) w

/ (GLfloat) h, 1.0, 300.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );
gluLookAt( 0.0f, 1.0f, 5.0f,

0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f );
}

void keyboardFunc( unsigned char key,

int x, int y )
{
switch( key )
{
case 'Q':
case 'q':
exit( 1 );
break;
}
reshapeFunc( g_width, g_height );
glutPostRedisplay( );
}

void mouseFunc( int button, int

state, int x, int y )
{
if( button == GLUT_LEFT_BUTTON )
{
if( state == GLUT_DOWN )
g_inc -= INC_VAL;
else
g_inc += INC_VAL;
}
else if ( button ==

GLUT_RIGHT_BUTTON )
{
if( state == GLUT_DOWN )
g_inc += INC_VAL;
else
g_inc -= INC_VAL;
}
else
g_inc = 0.0f;
glutPostRedisplay( );
}

void idleFunc(void)
{
if (angle > 360.0)
angle = angle -

360.0;
angle += 1;
glutPostRedisplay();
}

void displayFunc( )
{
static GLfloat angle = 0.0;
GLfloat val = .8f;
GLint buffers = GL_NONE;
glGetIntegerv( GL_DRAW_BUFFER,

&buffers );
glPushMatrix( );
glRotatef( angle += g_inc, 0.0f,

1.0f, 0.0f );
glClearStencil( 0x0 );
glClear( GL_STENCIL_BUFFER_BIT );
glStencilFunc(GL_ALWAYS, 0x1, 0x1

);
glStencilOp( GL_REPLACE,

GL_REPLACE, GL_REPLACE );
glDrawBuffer( GL_NONE );
glEnable( GL_STENCIL_TEST );
glBegin( GL_QUADS );
mirror( val );
glEnd();
glDrawBuffer( (GLenum) buffers );
glStencilOp( GL_KEEP, GL_KEEP,

GL_KEEP );
glClear( GL_COLOR_BUFFER_BIT |

GL_DEPTH_BUFFER_BIT );
glPushMatrix();
glScalef( 1.0f, 1.0f, -1.0f );
if( cos( angle * M_PI / 180.0

) < 0.0 )
eqn1[2] = -1.0;
else
eqn1[2] = 1.0;
glClipPlane( GL_CLIP_PLANE0,

eqn1 );
glEnable( GL_CLIP_PLANE0 );
glStencilFunc( GL_EQUAL, 0x1,

0x1 );
render();
glDisable( GL_CLIP_PLANE0 );
glPopMatrix( );
glDisable( GL_STENCIL_TEST );
glDrawBuffer( GL_NONE );
glBegin( GL_QUADS );
mirror( val );
glEnd();
glDrawBuffer( (GLenum) buffers );
glPushMatrix( );
render();
glPopMatrix( );
glColor3f( 0.4f, 0.4f, 1.0f );
glBegin( GL_LINE_LOOP );
mirror( val );
glEnd( );
glBlendFunc( GL_SRC_ALPHA,

GL_ONE_MINUS_SRC_ALPHA );
glEnable( GL_BLEND );
glDepthMask( GL_FALSE );
glDepthFunc( GL_LEQUAL );
glDisable( GL_LIGHTING );
glColor4f( 1.0f, 1.0f, 1.0f, .2f

);
glTranslatef( 0.0f, 0.0f, 0.01f *

eqn1[2] );
glBegin( GL_QUADS );
mirror( val );
glEnd( );
glDisable( GL_BLEND );
glDepthMask( GL_TRUE );
glDepthFunc( GL_LESS );
glEnable( GL_LIGHTING );
glPopMatrix( );
glFlush( );
glutSwapBuffers( );
}

void mirror( GLfloat val )
{
glVertex3f( val, val, 0.0f );
glVertex3f( -val, val, 0.0f );
glVertex3f( -val, -val, 0.0f );
glVertex3f( val, -val, 0.0f );
}
void render( )
{
glLightfv( GL_LIGHT0,

GL_POSITION, g_light0_pos );
glPushMatrix();
glColor3f( .4f, 1.0f, .4f );
glTranslatef( 0.0f, 0.0f, 2.5f );
glutSolidSphere( 0.5, 12, 12 );
glTranslatef( 0.5f, 0.0f, -0.7f

);
glColor3f( 1.0f, .4f, .4f );
glutSolidCube( .3 );
glPopMatrix();
glPushMatrix();
glTranslatef( -0.3f, 0.0f,

1.0f );
glScalef(.3,.3,.3);
glColor3f( 1.0f, .8f, .4f );
glRotatef(angle, 1.0, 1.0,

0.0);
glutWireIcosahedron();
glPopMatrix();
glPushMatrix();
glTranslatef( 0.2f, -0.1f, -2.0f

);
glColor3f( 1.0f, .4f, .4f );
glutWireTorus(0.1, 0.5, 7,

7);
glPopMatrix();
}