Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
OpenGL (Open Graphics Library) is an application program interface (API) that is used to
define 2D and 3D computer graphics.
The interface consists of over 250 different function calls which can be used to draw complex
three-dimensional scenes from simple primitives. OpenGL was developed by Silicon Graphics
Inc. (SGI) in 1992 and is widely used in CAD, virtual reality, scientific visualization, information
visualization, and flight simulation.
OpenGL's basic operation is to accept primitives such as points, lines and polygons, and
convert them into pixels. This is done by a graphics pipeline known as the OpenGL state
machine.
FEATURES OF OpenGL:-
Geometric Primitives Allow you to construct mathematical descriptions of objects.
Color coding in RGBA (Red-Green-Blue-Alpha) or in color index mode.
Viewing and Modeling permits arranging objects in a 3-dimensional scene, move our
camera around space and select the desired vantage point for viewing the scene to be
rendered.
Texture mapping helps to bring realism into our models by rendering images of realistic
looking surfaces on to the faces of the polygon in our model.
Materials lighting OpenGL provides commands to compute the color of any point given the
properties of the material and the sources of light in the room.
Double buffering helps to eliminate flickering from animations. Each successive frame in an
animation is built in a separate memory buffer and displayed only when rendering of the
frame is complete.
Anti-aliasing reduces jagged edges in lines drawn on a computer display. Jagged lines often
appear when lines are drawn at low resolution. Anti-aliasing is a common computer graphics
technique that modifies the color and intensity of the pixels near the line in order to reduce
the artificial zig-zag.
Gouraud shading is a technique used to apply smooth shading to a 3D object and provide
subtle color differences across its surfaces.
Z-buffering keeps track of the Z coordinate of a 3D object. The Z-buffer is used to keep track
of the proximity of the viewer's object. It is also crucial for hidden surface removal
Transformations: rotation, scaling, translations, perspectives in 3D, etc.
GLUT Functions:-
The OpenGL Utility Toolkit (GLUT) is a programming interface with ANSI C for writing window
system independent OpenGL programs.
argcp
A pointer to the program's unmodified argc variable from main. Upon return, the value
pointed to by argcp will be updated, because glutInit extracts any command line options
intended for the GLUT library.
argv
The program's unmodified argv variable from main. Like argcp, the data for argv will be
updated because glutInit extracts any command line options understood by the GLUT library.
2. glutInitWindowPosition, glutInitWindowSize :-
glutInitWindowPosition and glutInitWindowSize set the initial window position and size
respectively.
mode :- Display mode, normally the bitwise OR-ing of GLUT display mode bit masks. Mode
can take following values.
Values Meaning
Bit mask to select an RGBA mode window. This is the default
GLUT_RGBA
if neither GLUT_RGBA nor GLUT_INDEX are specified.
GLUT_RGB An alias for GLUT_RGBA.
GLUT_INDEX Bit mask to select a color index mode window. This overrides
GLUT_RGBA if it is also specified.
GLUT_SINGLE Bit mask to select a single buffered window. This is the
default if neither
GLUT_DOUBLE Bit mask to select a double buffered window. This overrides
GLUT_SINGLE if it is also specified.
GLUT_DEPTH Bit mask to select a window with a depth buffer.
7. glutReshapeFunc :- glutReshapeFunc sets the reshape callback for the current window.
Syntax:- void glutReshapeFunc(void (*func)(int width, int height));
func :- The new reshape callback function.
8. glutDisplayFunc:- glutDisplayFunc sets the display callback for the current window.
Syntax:- void glutDisplayFunc(void (*func)(void));
func :- The new display callback function.
9. glutKeyboardFunc :- glutKeyboardFunc sets the keyboard callback for the current window.
Syntax:- void glutKeyboardFunc(void (*func)(unsigned char key, int x, int y));
func :- The new keyboard callback function.
10. glutMouseFunc:- glutMouseFunc sets the mouse callback for the current window.
Syntax:- void glutMouseFunc(void (*func)(int button, int state, int x, int y));
func :- The new mouse callback function.
GL Functions:-
1. glBegin & glEnd :- The glBegin and glend functions delimit the vertices of a primitive or a
group of like primitives.
Syntax :- void glBegin( GLenum mode );
mode :- The primitive or primitives that will be created from vertices presented between
glBegin and the subsequent glEnd. The following are accepted symbolic constants and their
meanings:
Value Meaning
GL_POINTS Treats each vertex as a single point. Vertex n defines point n.
N points are drawn.
GL_LINES Treats each pair of vertices as an independent line segment.
Vertices 2n - 1 and 2n define line n. N/2 lines are drawn.
GL_LINE_STRIP Draws a connected group of line segments from the first vertex
to the last. Vertices n and n+1 define line n. N - 1 lines are
drawn.
GL_LINE_LOOP Draws a connected group of line segments from the first vertex
to the last, then back to the first. Vertices n and n + 1 define
line n. The last line, however, is defined by vertices N and 1. N
lines are drawn.
GL_TRIANGLES Treats each triplet of vertices as an independent triangle.
Vertices 3n - 2, 3n - 1, and 3n define triangle n. N/3 triangles
are drawn.
GL_TRIANGLE_STRIP Draws a connected group of triangles. One triangle is defined
for each vertex presented after the first two vertices. For odd
n, vertices n, n + 1, and n + 2 define triangle n. For even n,
vertices n + 1, n, and n + 2 define triangle n. N - 2 triangles are
drawn.
GL_TRIANGLE_FAN Draws a connected group of triangles. one triangle is defined
for each vertex presented after the first two vertices. Vertices
1, n + 1, n + 2 define triangle n. N - 2 triangles are drawn.
GL_QUADS Treats each group of four vertices as an independent
quadrilateral. Vertices 4n - 3, 4n - 2, 4n - 1, and 4n define
quadrilateral n. N/4 quadrilaterals are drawn.
GL_QUAD_STRIP Draws a connected group of quadrilaterals. One quadrilateral
is defined for each pair of vertices presented after the first pair.
Vertices 2n - 1, 2n, 2n + 2, and 2n + 1 define quadrilateral n.
N/2 - 1 quadrilaterals are drawn. Note that the order in which
vertices are used to construct a quadrilateral from strip data is
different from that used with independent data.
GL_POLYGON Draws a single, convex polygon. Vertices 1 through N define
this polygon.
Value Meaning
GL_COLOR_BUFFER_BIT The buffers currently enabled for color writing.
GL_DEPTH_BUFFER_BIT The depth buffer.
GL_ACCUM_BUFFER_BIT The accumulation buffer.
GL_STENCIL_BUFFER_BIT The stencil buffer.
3. glClearColor:- The glClearColor function specifies clear values for the color buffers.
Syntax :-void glClearColor(red, green, blue, alpha);
red :- The red value that glClear uses to clear the color buffers. The default value is zero.
green :-The green value that glClear uses to clear the color buffers. The default value is zero.
blue :- The blue value that glClear uses to clear the color buffers. The default value is zero.
alpha :-The alpha value that glClear uses to clear the color buffers. The default value is zero.
6. glEnable, glDisable :- The glEnable and glDisable functions enable or disable OpenGL
capabilities.
Value Meaning
GL_DEPTH_TEST If enabled, do depth comparisons and update the depth buffer. See
glDepthFunc and glDepthRange.
GL_LINE_SMOOTH If enabled, draw lines with correct filtering. If disabled, draw aliased
lines. See glLineWidth.
GL_LINE_STIPPLE If enabled, use the current line stipple pattern when drawing lines. See
glLineStipple.
GL_NORMALIZE If enabled, normal vectors specified with glNormal are scaled to unit
length after transformation. See glNormal.
GL_POINT_SMOOTH If enabled, draw points with proper filtering. If disabled, draw aliased
points. See glPointSize.
7. glFlush:- The glFlush function forces execution of OpenGL functions in finite time.
Syntax:- void glFlush(void);
This function has no parameters.
8. glFrustum:- The glFrustum function multiplies the current matrix by a perspective matrix.
Syntax :- void glFrustum(GLdouble left, GLdouble right, GLdouble bottom,
GLdouble top, GLdouble zNear, GLdouble zFar);
left :- The coordinate for the left-vertical clipping plane.
right :- The coordinate for the right-vertical clipping plane.
bottom :- The coordinate for the bottom-horizontal clipping plane.
top :- The coordinate for the bottom-horizontal clipping plane.
zNear :-The distances to the near-depth clipping plane. Must be positive.
zFar :- The distances to the far-depth clipping planes. Must be positive.
param :- Specifies the value that parameter pname of light source light will be set to.
10. glLoadIdentity :- The glLoadIdentity function replaces the current matrix with the identity
matrix.
Syntax :- void WINAPI glLoadIdentity(void);’
11. glMatrixMode:- The glMatrixMode function specifies which matrix is the current matrix.
Syntax:- void glMatrixMode(GLenum mode);
mode :- The matrix stack that is the target for subsequent matrix operations. The mode
parameter can assume one of three values.
Value Meaning
GL_MODELVIEW Applies subsequent matrix operations to the modelview matrix
stack.
GL_PROJECTION Applies subsequent matrix operations to the projection matrix
stack.
GL_TEXTURE Applies subsequent matrix operations to the texture matrix stack.
12. glOrtho:- The glOrtho function multiplies the current matrix by an orthographic matrix.
Syntax:- void glOrtho(GLdouble left, GLdouble right, GLdouble bottom,
GLdouble top, GLdouble zNear, GLdouble zFar);
13. glPointSize :- The glPointSize function specifies the diameter of rasterized points.
Syntax :- void glPointSize(GLfloat size);
Size:-The diameter of rasterized points. The default is 1.0.
14. glPushMatrix & glPopMatrix:- The glPushMatrix and glPopMatrix functions push and pop
the current matrix stack.
Syntax:- void WINAPI glPopMatrix(void);
15. glRotatef:- The glRotatef function multiplies the current matrix by a rotation matrix.
Syntax :- void glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z);
16. glScalef :- The glScaled and glScalef functions multiply the current matrix by a general scaling
matrix.
Syntax :- void glScalef(GLfloat x, GLfloat y, GLfloat z);
17. glTranslatef :- The glTranslatef function multiplies the current matrix by a translation matrix.
Syntax:- void glTranslatef(GLfloat x, GLfloat y, GLfloat z);
x :- The lower-left corner of the viewport rectangle, in pixels. The default is (0,0).
y :- The lower-left corner of the viewport rectangle, in pixels. The default is (0,0).
Width :-The width of the viewport. When an OpenGL context is first attached to a window,
width and height are set to the dimensions of that window.
height :- The height of the viewport. When an OpenGL context is first attached to a window,
width and height are set to the dimensions of that window.
GLU Functions:-
1. gluLookAt :- The gluLookAt function defines a viewing transformation.
Syntax :-void gluOrtho2D(GLdouble left, GLdouble right, GLdouble top, GLdouble bottom);
#include <GL/glut.h>
void init2D(float r, float g, float b)
{
glClearColor(r,g,b,0.0);
glMatrixMode (GL_PROJECTION);
gluOrtho2D (0.0, 200.0, 0.0, 150.0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POINTS);
for(int i = 0; i < 30; i++)
{
glVertex2i(10+5*i,110);
}
glEnd();
glBegin(GL_LINES);
glVertex2i(10,10);
glVertex2i(100,100);
glEnd();
glFlush();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Points & Lines");
init2D(0.0,0.0,0.0);
glutDisplayFunc(display);
glutMainLoop();
}
*************************** OUTPUT ***************************
//2. OpenGL program to draw a Line Segment.
#include<GL/glut.h>
void draw()
{
glClearColor(100,100,10,0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,20.0,0.0,20.0,0.0,20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(5,-2,0);
glColor3f(1,0,0);
glBegin(GL_LINES);
glVertex2f(5.0,5.0);
glVertex2f(10.0,10.0);
glEnd();
glutSwapBuffers();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
glutInitWindowSize(250,250);
glutCreateWindow("Line Segment");
glutDisplayFunc(draw);
glutMainLoop();
}
*************************** OUTPUT ***************************
//3. OpenGL program in C to draw a line using simple DDA.
#include<GL/glut.h>
#include<stdlib.h>
#include<stdio.h>
#define ROUND(x) ((int)(x+0.5))
const A=500;
int xa,xb,ya,yb,i;
void display (void)
{
int dx=xb-xa,dy=yb-ya,steps,k;
float xIncrement,yIncrement,x=xa,y=ya;
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 0.0);
if(abs(dx)>abs(dy))
steps=abs(dx);
else steps=abs(dy);
xIncrement=dx/(float)steps;
yIncrement=dy/(float)steps;
glBegin(GL_POINTS);
glVertex2s(ROUND(x),ROUND(y));
for(k=0;k<steps;k++)
{
x+=xIncrement;
y+=yIncrement;
glVertex2s(ROUND(x),ROUND(y));
printf("%lf %lf\n",x,y);
}
glColor3f (1.0, 1.0, 1.0);
for( i=-100 ; i<=100 ; i++)
{
glVertex2s(i,0);
glVertex2s(0,i);
}
for( i=-2; i<=2 ; i++)
{
glVertex2s(95+i,4+i);
glVertex2s(95-i,4+i);
}
for( i=0; i<=2 ; i++)
{
glVertex2s(4+i,95+i);
glVertex2s(4-i,95+i);
glVertex2s(4,95-i);
}
glEnd();
glFlush();
}
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glOrtho(-100.0, 100.0, -100.0, 100.0, -1.0, 1.0);
}
#include<GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(50.0,10.0);
glVertex2f(150.0,80.0);
glVertex2f(100.0,20.0);
glVertex2f(200.0,100.0);
glEnd();
glFlush();
}
void myinit()
{
glClearColor(1.0,1.0,1.0,1.0);
glPointSize(3.0);
glLoadIdentity();
glOrtho(0.0,499.0,0.0,499.0,0.0,499.0);
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
glutInitWindowSize(250,250);
glutCreateWindow("Line Loop");
glutDisplayFunc(display);
myinit();
glutMainLoop();
}
*************************** OUTPUT ***************************
#include <GL/glut.h>
const int A = 500;
const float B = 500;
const float C = 200;
void myinit(void)
{
glClearColor(0.7, 0.7, 0.7, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D( -B/2, B/2, -B/2, B/2);
glMatrixMode(GL_MODELVIEW);
}
void display( )
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBegin(GL_POLYGON) ;
glColor3f ( 1.0, 0.3, 0.2);
glVertex2f( -C/2, -C/2 );
glVertex2f( C/2, -C/2 );
glVertex2f( C/2, C/2 );
glVertex2f( -C/2, C/2 );
glEnd();
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitWindowSize( A, A );
glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE);
glutCreateWindow("My Rectangle");
glutDisplayFunc(display);
myinit();
glutMainLoop();
}
*************************** OUTPUT ***************************
#include <GL/glut.h>
void displayCB(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex2i(200,125);
glVertex2i(100,375);
glVertex2i(300,375);
glEnd();
glFlush();
}
void keyCB(unsigned char key, int x, int y)
{
if( key == 'q' ) exit(0);
}
int main(int argc, char *argv[])
{
int win;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(400,500);
win = glutCreateWindow("Triangle");
glClearColor(0.0,0.0,0.0,0.0);
gluOrtho2D(0,400,0,500);
glutDisplayFunc(displayCB);
glutKeyboardFunc(keyCB);
glutMainLoop();
return 0;
}
*************************** OUTPUT ***************************
#include <GL/glut.h>
void display(void)
{
glClear( GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(2.0, 4.0, 0.0);
glVertex3f(4.0, 4.0, 0.0);
glVertex3f(4.0, 6.0, 0.0);
glVertex3f(2.0, 6.0, 0.0);
glEnd();
glFlush();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(100,100);
glutInitWindowSize(300,300);
glutCreateWindow ("Square");
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 10.0, 0.0, 10.0, -1.0, 1.0);
glutDisplayFunc(display);
glutMainLoop();
}
*************************** OUTPUT ***************************
#include <GL/glut.h>
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, w, 0, h, -1, 1);
glScalef(1, -1, 1);
glTranslatef(0, -h, 0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(0.0, 0.0, 1.0);
glVertex2i(20, 20);
glColor3f(0.0, 1.0, 0.0);
glVertex2i(200, 200);
glColor3f(1.0, 0.0, 0.0);
glVertex2i(20, 200);
glEnd();
glFlush();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutCreateWindow("Single Triangle");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
}
*************************** OUTPUT ***************************
//9. OpenGL Program for 2D Sierpinski Gasket (Recursive Sub Division).
#include<GL/glut.h>
void myInit()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 10.0, 0.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(0.0, 0.0, 1.0);
}
void triangle(GLfloat *a, GLfloat *b, GLfloat *c)
{
glVertex2fv(a);
glVertex2fv(b);
glVertex2fv(c);
}
void draw_triangle(GLfloat *a, GLfloat *b, GLfloat *c, int k)
{
GLfloat ab[2], bc[2], ac[2];
int j;
if(k>1)
{
for(j=0;j<2;j++) ab[j] = (a[j]+b[j])/2.0;
for(j=0;j<2;j++) bc[j] = (b[j]+c[j])/2.0;
for(j=0;j<2;j++) ac[j] = (a[j]+c[j])/2.0;
draw_triangle(a, ab, ac, k-1);
draw_triangle(b, bc, ab, k-1);
draw_triangle(c, ac, bc, k-1);
}
else
{
triangle(a, b, c);
}
}
void display()
{
GLfloat a[2] = {1.0, 1.0};
GLfloat b[2] = {6.0, 1.0};
GLfloat c[2] = {3.5, 5.0};
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
draw_triangle(a, b, c, 6);
glEnd();
glFlush();
}
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(-0.7f, -0.6f);
glVertex2f(-0.1f, -0.6f);
glVertex2f(-0.1f, 0.0f);
glVertex2f(-0.7f, 0.0f);
glEnd();
glBegin(GL_TRIANGLES);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2f(0.1f, -0.6f);
glVertex2f(0.7f, -0.6f);
glVertex2f(0.4f, -0.1f);
glEnd();
glBegin(GL_POLYGON);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex2f(0.4f, 0.2f);
glVertex2f(0.6f, 0.2f);
glVertex2f(0.7f, 0.4f);
glVertex2f(0.6f, 0.6f);
glVertex2f(0.4f, 0.6f);
glVertex2f(0.3f, 0.4f);
glEnd();
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutCreateWindow("Vertex, Primitive & Color");
glutInitWindowSize(320, 320);
glutDisplayFunc(display);
glutMainLoop();
}
*************************** OUTPUT ***************************
//11. OpenGL Program to spin a circle.
#include <GL/glut.h>
#include <math.h>
typedef struct
{
float x;
float y;
}CIRCLE;
CIRCLE circle;
float rot = 0;
int i;
void createcircle (int k, int r, int h)
{
glBegin(GL_LINES);
for (i = 0; i < 180; i++)
{
circle.x = r * cos(i) - h;
circle.y = r * sin(i) + k;
glVertex3f(circle.x + k,circle.y - h,0);
circle.x = r * cos(i + 0.1) - h;
circle.y = r * sin(i + 0.1) + k;
glVertex3f(circle.x + k,circle.y - h,0);
}
glEnd();
}
void display ()
{
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0,0,-20);
glRotatef(rot,0,1,0);
glRotatef(rot,1,0,0);
glRotatef(rot,0,0,1);
glColor3f(1,1,1);
createcircle(0,10,0);
glRotatef(rot,0,1,0);
glRotatef(rot,1,0,0);
glRotatef(rot,0,0,1);
glColor3f(0,1,0);
createcircle(-1,6,-1);
glutSwapBuffers();
rot++;
}
#include<GL/glut.h>
#define maxx 20
#define maxy 25
#define dx 15
#define dy 10
GLfloat x[maxx]={0.0},y[maxy]={0.0};
GLfloat x0=50,y0=50;
GLint i,j;
void init()
{
glClearColor(1.0,1.0,1.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,499.0,0.0,499.0);
glutPostRedisplay();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,1.0);
for(i=0;i<maxx;i++)
x[i]=x0+i*dx;
for(j=0;j<maxy;j++)
y[j]=y0+j*dy;
glColor3f(0.0,0.0,1.0);
for(i=0;i<maxx-1;i++)
for(j=0;j<maxy-1;j++)
{
glColor3f(0.0,0.0,1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(x[i],y[j]);
glVertex2f(x[i],y[j+1]);
glVertex2f(x[i+1],y[j+1]);
glVertex2f(x[i+1],y[j]);
glEnd();
glFlush();
}
glFlush();
}
int main(int argc,char ** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(400,400);
glutCreateWindow("RECTANGULAR MESH");
glutDisplayFunc(display);
init();
glutMainLoop();
}
*************************** OUTPUT ***************************
#include <GL/glut.h>
void display(void)
{
GLdouble eqn[4] = {0.0, 1.0, 0.0, 0.0};
GLdouble eqn2[4] = {1.0, 0.0, 0.0, 0.0};
glClear(GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glTranslatef (0.0, 0.0, -5.0);
glClipPlane (GL_CLIP_PLANE0, eqn);
glEnable (GL_CLIP_PLANE0);
glClipPlane (GL_CLIP_PLANE1, eqn2);
glEnable (GL_CLIP_PLANE1);
glRotatef (90.0, 1.0, 0.0, 0.0);
glutWireSphere(1.0, 20, 16);
glFlush ();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode (GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (300, 300);
glutInitWindowPosition (100, 100);
glutCreateWindow("Clip Wired Globe");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
}
*************************** OUTPUT ***************************
#include <GL/glut.h>
GLfloat light_diffuse[] ={1.0, 0.0, 0.0, 1.0};
GLfloat light_position[] ={1.0, 1.0, 1.0, 0.0};
GLUquadricObj *qobj;
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glCallList(1);
glutSwapBuffers();
}
void gfxinit(void)
{
qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj, GLU_FILL);
glNewList(1, GL_COMPILE);
gluSphere(qobj, 1.0, 20,20);
glEndList();
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
gluPerspective(40.0,1.0,1.0, 10.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0,0.0, 1.0, 0.);
glTranslatef(0.0, 0.0, -1.0);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("Sphere");
glutDisplayFunc(display);
gfxinit();
glutCreateWindow("Second Window");
glutDisplayFunc(display);
gfxinit();
glutMainLoop();
}
*************************** OUTPUT ***************************
#include <GL/glut.h>
void parallelepiped(int x1, int x2,int y1, int y2, int y3, int y4)
{
glColor3f(0.0, 0.0, 1.0);
glPointSize(2.0);
glBegin(GL_LINE_LOOP);
glVertex2i(x1,y1);
glVertex2i(x2,y3);
glVertex2i(x2,y4);
glVertex2i(x1,y2);
glEnd();
}
void parallelepiped_draw()
{
int x1=200,x2=300,y1=100,y2=175,y3=100,y4=175;
GLint i,n=40;
for(i=0;i<n;i+=2)
{
parallelepiped(x1+i,x2+i,y1+i,y2+i,y3+i,y4+i);
}
}
void init(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,400.0,0.0,300.0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glPointSize(2.0);
parallelepiped_draw();
glFlush();
}
int main(int argc, char **argv)
{
glutInit(&argc,argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutCreateWindow("ParallelePiped Display");
init();
glutDisplayFunc(display);
glutMainLoop();
}
glBegin(GL_TRIANGLE_STRIP);
glColor3f(1, 1, 1);
glVertex3f(0, 2, 0);
glColor3f(1, 0, 0);
glVertex3f(-1, 0, 1);
glColor3f(0, 1, 0);
glVertex3f(1, 0, 1);
glColor3f(0, 0, 1);
glVertex3f(0, 0, -1.4);
glColor3f(1, 1, 1);
glVertex3f(0, 2, 0);
glColor3f(1, 0, 0);
glVertex3f(-1, 0, 1);
glEnd();
glFlush();
}
void init()
{
glClearColor(0.1, 0.39, 0.88, 1.0);
glColor3f(1.0, 1.0, 1.0);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glMatrixMode(GL_PROJECTION);
glFrustum(-2, 2, -1.5, 1.5, 1, 40);
glMatrixMode(GL_MODELVIEW);
glTranslatef(0, 0, -3);
glRotatef(50, 1, 0, 0);
glRotatef(70, 0, 1, 0);
}
#include <GL\glut.h>
GLfloat xRotated, yRotated, zRotated;
GLdouble base=1;
GLdouble height=1.5;
GLint slices =50;
GLint stacks =50;
void displayCone()
{
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0,0.0,-4.5);
glColor3f(0.8, 0.2, 0.1);
glRotatef(xRotated,1.0,0.0,0.0);
glRotatef(yRotated,0.0,1.0,0.0);
glRotatef(zRotated,0.0,0.0,1.0);
glScalef(1.0,1.0,1.0);
glutSolidCone(base,height,slices,stacks);
glFlush();
glutSwapBuffers();
}
void reshapeCone(int x, int y)
{
if (y == 0 || x == 0) return;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0,(GLdouble)x/(GLdouble)y,0.5,20.0);
glViewport(0,0,x,y);
}
void idleCone(void)
{
yRotated += 0.01;
displayCone();
}
int main (int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400,350);
glutCreateWindow("Cone Rotating Animation");
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
xRotated = yRotated = zRotated = 30.0;
xRotated=33;
yRotated=40;
glClearColor(0.0,0.0,0.0,0.0);
glutDisplayFunc(displayCone);
glutReshapeFunc(reshapeCone);
glutIdleFunc(idleCone);
glutMainLoop();
return 0;
}
#include <math.h>
#include <stdio.h>
#include<gl/glut.h>
float posX = 0.01, posY = -0.1, posZ = 0,angle,PI=3.142,x,y;
int i;
float move_unit = 0.04f;
void circ()
{
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_TRIANGLE_FAN);
for (i = 0; i <= 300; i++)
{
angle = 2 * PI * i / 300;
x = cos(angle) / 10;
y = sin(angle) / 10;
glVertex2d(x, y);
}
glEnd();
}
void display()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(posX, posY, posZ);
circ();
glPopMatrix();
glutSwapBuffers();
}
void keyboardown(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_RIGHT:
posX += move_unit;
break;
case GLUT_KEY_LEFT:
posX -= move_unit;
break;
case GLUT_KEY_UP:
posY += move_unit;
break;
case GLUT_KEY_DOWN:
posY -= move_unit;
break;
default:
break;
}
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("Circle Move with Keyboard arrow keys");
glutDisplayFunc(display);
glutSpecialFunc(keyboardown);
glutMainLoop();
}
#include<stdio.h>
#include<GL/glut.h>
double xmin=50,ymin=50,xmax=100,ymax=100;
double xvmin=200,yvmin=200,xvmax=300,yvmax=300;
double x0,y0,x1,y1;
int cliptest(double p,double q,double *t1,double *t2)
{
double t=q/p;
if(p<0.0)
{
if(t>*t1)
*t1=t;
if(t>*t2)
return 0;//(false);
}
else if(p>0.0)
{
if(t<*t2)
*t2=t;
if(t<*t1)
return 0;//(false);
}
else if(p==0.0)
{
if(q<0.0)
return 0;//(false);
}
return 1;//(true);
}
void LiangBarsky(double x0,double y0,double x1,double y1)
{
double sx,sy,vx0,vy0,vx1,vy1;
double dx=x1-x0,dy=y1-y0,te=0.0,tl=1.0;
if(cliptest(-dx,x0-xmin,&te,&tl))
if(cliptest(dx,xmax-x0,&te,&tl))
if(cliptest(-dy,y0-ymin,&te,&tl))
if(cliptest(dy,ymax-y0,&te,&tl))
{
if(tl<1.0)
{
x1=x0+tl*dx;
y1=y0+tl*dy;
}
if(te>0.0)
{
x0=x0+te*dx;
y0=y0+te*dy;
}
sx=(xvmax-xvmin)/(xmax-xmin);
sy=(yvmax-yvmin)/(ymax-ymin);
vx0=xvmin+(x0-xmin)*sx;
vy0=yvmin+(y0-ymin)*sy;
vx1=xvmin+(x1-xmin)*sx;
vy1=yvmin+(y1-ymin)*sy;
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(xvmin,yvmin);
glVertex2f(xvmax,yvmin);
glVertex2f(xvmax,yvmax);
glVertex2f(xvmin,yvmax);
glEnd();
glColor3f(0.0,0.0,1.0);
glBegin(GL_LINES);
glVertex2d(vx0,vy0);
glVertex2d(vx1,vy1);
glEnd();
}
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINES);
glVertex2d(x0,y0);
glVertex2d(x1,y1);
glEnd();
glColor3f(0.0,0.0,1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(xmin,ymin);
glVertex2f(xmax,ymin);
glVertex2f(xmax,ymax);
glVertex2f(xmin,ymax);
glEnd();
LiangBarsky(x0,y0,x1,y1);
glFlush();
}
void myinit()
{
glClearColor(1.0,1.0,1.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,499.0,0.0,499.0);
}