Вы находитесь на странице: 1из 126

Computer Graphics Laboratory

1
Introduction to OpenGL
• Operating system independent graphics rendering
software interface (API)
• OpenGL is a library for doing interactive computer
graphics applications.
• Capable of rendering high-quality color two- and
three dimensional images
• It processing several steps on this data to convert it to
pixels to form the final desired image in the frame
buffer

2
Setup openGL with Dev-cpp
• Install Dev-cpp
• Unzip the GLUT folder
• Copy glut.h from GLUTMingw32\include\GL
• Paste glut.h to C:\Dev-Cpp\include\GL
• Copy libglut32.a from GLUTMingw32\lib
• Paste libglut32.a to C:\Dev-Cpp\lib
• Copy glut32.dll from GLUTMingw32
• Paste glut32.dll to C:\Windows\System32

3
Another way to setup Devcpp with OpenGL
• Install Dev-cpp
• In menu Tools  Package manager  install
• Select files from download glui.2.2-1, glut.3.7.6+,
OpenGLUT.0.6.3 to install.
• Go to File  new  project  select multimedia tab
 select glut package  ok
• Create new source file under the project
• Write the program
• Compile the program
• Run the program

4
Run the OpenGL program
• Create a new project in Dev-cpp
• Create new source file under the project
• Write the program
• Select the Project tab  Project Option  parameters
• Add the three file from C:\Dev-Cpp\lib in same order
– libopengl32.a
– libglu32.a
– libglut32.a
• Compile the program
• Run the program

5
Sample Program
int main(int argc, char **argv)
#include <stdio.h>
{
#include <GL/glut.h>
glutInit(&argc, argv);
void display(void) glutInitDisplayMode ( GLUT_SINGLE |
{ GLUT_RGB | GLUT_DEPTH);
glClear(GL_COLOR_BUFFER_BIT); glutInitWindowPosition(100,100);
glColor3f(0.0, 1.0, 0.0); glutInitWindowSize(300,300);
glPointSize(3); glutCreateWindow ("Points");
glBegin(GL_POINTS); glClearColor(0.0, 0.0, 0.0, 0.0);
glVertex3f(2.0, 4.0, 0.0); glMatrixMode(GL_PROJECTION);
glEnd(); glLoadIdentity();
glColor3f(1.0, 0.0, 0.0); glOrtho(0.0, 10.0, 0.0, 10.0, -1.0, 1.0);
glutDisplayFunc(display);
glPointSize(5);
glutMainLoop();
glBegin(GL_POINTS);
return 0;
glVertex3f(8.0, 4.0, 0.0); }
glEnd();
glFlush();
}
6
7
• glutInit(&argc, argv) – initilize the GLUT library
• glutInitDisplayMode () – set the initial display mode
– Different arguments are allowed by ORing. Arguments
are:
– GLUT_RGBA - to select an RGBA mode window. This is the default
– GLUT_RGB - An alias for GLUT_RGBA
– GLUT_INDEX - to select a color index mode window. This
overrides GLUT_RGBA if it is also specified
– GLUT_SINGLE - to select a single buffered window. This is the default
– GLUT_DOUBLE - to select a double buffered window. This
overrides GLUT_SINGLE if it is also specified.
– GLUT_ACCUM - to select a window with an accumulation buffer
– GLUT_ALPHA - to select a window with an alpha component to
the color buffer(s)
– GLUT_DEPTH - to select a window with a depth buffer.
GLUT_STENCIL - to select a window with a stencil buffer
– etc
8
• glutInitWindowPosition(x, y) – fix the position of
windowleft top corner point
• glutInitWindowSize(width, height) – fix the window size
• glutCreateWindow (“window name") – crate window
with name
• glClearColor(GLfloat Red, GLfloat Green, GLfloat Blue,
GLfloat Alpha ) – it will clear the color buffers using the
new color value
• glMatrixMode(mode) - Specify which matrix is the
current matrix
– MODE: GL_MODELVIEW, GL_PROJECTION, GL_TEXTURE, GL_COLOR
• glLoadIdentity() – reset the current matrix to identity matrix

9
Output Primitives - Lines
#include <stdio.h> int main(int argc, char **argv) {
#include <GL/glut.h> glutInit(&argc, argv);
void display(void) glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB |
{ GLUT_DEPTH);
glClear( GL_COLOR_BUFFER_BIT); glutInitWindowPosition(100,100); // Position of
glColor3f(0.0, 1.0, 0.0); output window in screen
glLineWidth(3); glutInitWindowSize(300,300); // output window size
glBegin(GL_LINES); glutCreateWindow ("Lines"); // output window name
glVertex3f(2.0, 4.0, 0.0); glClearColor(0.0, 0.0, 0.0, 0.0); // black
glVertex3f(6.0, 4.0, 0.0); background
glEnd(); glMatrixMode(GL_PROJECTION); // setup viewing
projection
glColor3f(1.0, 0.0, 0.0); glLoadIdentity(); // start with identity matrix
glLineWidth(5); glOrtho(0.0, 10.0, 0.0, 10.0, -1.0, 1.0); // setup a
glBegin(GL_LINES);
10x10x2 viewing world
glVertex3f(8.0, 4.0, 0.0);
glutDisplayFunc(display);
glVertex3f(11.0, 10.0, 0.0);
glutMainLoop();
glEnd();
glFlush(); return 0;
} }
10
11
Different Type of Line
glBegin(GL_LINES);
#include <stdio.h>
glVertex3f(2.0, 6.0, 0.0);
#include <GL/glut.h>
glVertex3f(6.0, 6.0, 0.0);
void display(void)
glEnd();
{
glDisable(GL_LINE_STIPPLE);
glClear( GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0);
glColor3f(1.0, 0.0, 0.0);
glLineWidth(3);
glLineWidth(3);
glEnable(GL_LINE_STIPPLE);
glBegin(GL_LINES); /* normal line */
glLineStipple(2, 0x00FF); /* dashed line
glVertex3f(2.0, 4.0, 0.0);
*/
glVertex3f(6.0, 4.0, 0.0);
glBegin(GL_LINES);
glEnd();
glVertex3f(2.0, 8.0, 0.0);
glVertex3f(6.0, 8.0, 0.0);
glColor3f(0.0, 1.0, 0.0);
glEnd();
glLineWidth(3);
glDisable(GL_LINE_STIPPLE);
glEnable(GL_LINE_STIPPLE);
glLineStipple(1, 0x0101); /* dotted line,
arguments are factor(1 to 255) and glFlush();
pattern */ } 12
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(100,100); // Position of output window in
screen
glutInitWindowSize(300,300); // output window size
glutCreateWindow ("Different Type of Lines"); // output window name
glClearColor(0.0, 0.0, 0.0, 0.0); // black background
glMatrixMode(GL_PROJECTION); // setup viewing projection
glLoadIdentity(); // start with identity matrix
glOrtho(0.0, 10.0, 0.0, 10.0, -1.0, 1.0); // setup a 10x10x2 viewing world
glutDisplayFunc(display);
glutMainLoop();
return 0;
13
}
14
Triangle
glBegin(GL_LINE_LOOP);
#include <stdio.h>
glVertex3f(5.0, 6.0, 0.0);
#include <GL/glut.h>
glVertex3f(7.0, 6.0, 0.0);
void display(void) { glVertex3f(6.0, 10.0, 0.0);
glClear( GL_COLOR_BUFFER_BIT); glEnd();
glDisable(GL_LINE_STIPPLE);
glColor3f(1.0, 0.0, 0.0); glColor3f(0.0, 1.0, 1.0);
glLineWidth(3); glLineWidth(3);
glEnable(GL_LINE_STIPPLE);
glBegin(GL_TRIANGLES);
glLineStipple(2, 0x00FF);
glVertex3f(2.0, 6.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex3f(4.0, 6.0, 0.0); glVertex3f(8.0, 6.0, 0.0);
glVertex3f(3.0, 10.0, 0.0); glVertex3f(10.0, 6.0, 0.0);
glEnd(); glVertex3f(9.0, 10.0, 0.0);
glEnd();
glColor3f(0.0, 0.0, 1.0); glDisable(GL_LINE_STIPPLE);
glLineWidth(3);
glFlush();
glEnable(GL_LINE_STIPPLE);
} 15
glLineStipple(1, 0x0101);
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(100,100); // Position of output
window in screen
glutInitWindowSize(400,400); // output window size
glutCreateWindow ("Triangles"); // output window name
glClearColor(1.0, 1.0, 1.0, 0.0); // black background
glMatrixMode(GL_PROJECTION); // setup viewing projection
glLoadIdentity(); // start with identity matrix
glOrtho(0.0, 10.0, 0.0, 10.0, -1.0, 1.0); // setup a 10x10x2
viewing world
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
16
17
Polygon
#include <stdio.h>
#include <GL/glut.h>
void display(void) {
glClear( GL_COLOR_BUFFER_BIT); glColor3f(0.0, 1.0, 0.0);
glColor3f(1.0, 0.0, 0.0); glLineWidth(3);
glLineWidth(3); glBegin(GL_LINE_LOOP);
glBegin(GL_POLYGON); glVertex3f(2.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0); glVertex3f(4.0, 0.0, 0.0);
glVertex3f(1.0, 0.0, 0.0); glVertex3f(4.0, 2.0, 0.0);
glVertex3f(1.0, 2.0, 0.0); glVertex3f(2.0, 2.0, 0.0);
glVertex3f(0.0, 2.0, 0.0); glEnd();
glEnd();
glColor3f(0.0, 0.0, 1.0);
glColor3f(0.0, 1.0, 0.0);
glLineWidth(3);
glLineWidth(3);
glEnable(GL_LINE_STIPPLE);
glLineStipple(1, 0x0101);
glBegin(GL_POLYGON);
glBegin(GL_LINE_LOOP);
glVertex3f(2.0, 6.0, 0.0);
glVertex3f(5.0, 6.0, 0.0);
glVertex3f(4.0, 6.0, 0.0);
glVertex3f(7.0, 6.0, 0.0);
glVertex3f(4.0, 8.0, 0.0);
glVertex3f(7.0, 8.0, 0.0);
glVertex3f(3.0, 9.0, 0.0);
glVertex3f(5.0, 8.0, 0.0);
glVertex3f(2.0, 8.0, 0.0);
glEnd(); glDisable(GL_LINE_STIPPLE);
18
glEnd();
glColor3f(0.0, 1.0, 1.0); int main(int argc, char **argv) {
glLineWidth(3); glutInit(&argc, argv);
glEnable(GL_LINE_STIPPLE); glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB |
GLUT_DEPTH);
glLineStipple(2, 0x00FF); glutInitWindowPosition(100,100); //
glBegin(GL_LINE_LOOP); Position of output window in screen
glVertex3f(8.0, 6.0, 0.0); glutInitWindowSize(500,500); //
glVertex3f(10.0, 6.0, 0.0); output window size
glVertex3f(10.0, 8.0, 0.0); glutCreateWindow ("Polygons"); //
output window name
glVertex3f(8.0, 8.0, 0.0); glClearColor(0.0, 0.0, 0.0, 0.0); // black
glEnd(); background
glDisable(GL_LINE_STIPPLE); glMatrixMode(GL_PROJECTION); // setup
viewing projection
glFlush(); glLoadIdentity(); // start
with identity matrix
} glOrtho(0.0, 10.0, 0.0, 10.0, -1.0, 1.0); //
setup a 10x10x2 viewing world
glutDisplayFunc(display);
glutMainLoop();
return 0;
19
}
20
Colors
#include <stdio.h> int main(int argc, char **argv)
#include <GL/glut.h> {
void display(void) { glutInit(&argc, argv);
glClear( GL_COLOR_BUFFER_BIT); glutInitDisplayMode ( GLUT_SINGLE |
glShadeModel(GL_SMOOTH); GLUT_RGB | GLUT_DEPTH);
glShadeModel(GL_FLAT) glutInitWindowPosition(100,100);
glLineWidth(3); glutInitWindowSize(400,400);
glBegin(GL_TRIANGLES); glutCreateWindow ("Triangles");
glColor3f(1.0, 0.0, 0.0); glClearColor(1.0, 1.0, 1.0, 0.0);
glVertex3f(2.0, 6.0, 0.0); glMatrixMode(GL_PROJECTION);
glColor3f(0.0, 0.5, 0.0); glLoadIdentity();
glVertex3f(4.0, 6.0, 0.0); glOrtho(0.0, 10.0, 0.0, 10.0, -1.0, 1.0);
glColor3f(0.0, 0.0, 1.0); glutDisplayFunc(display);
glVertex3f(3.0, 10.0, 0.0); glutMainLoop();
glEnd(); return 0;
glFlush(); } 21
}
22
Perspective Projection
gluPerspective(Gldouble fovy, Gldouble aspect,
Gldouble zNear, Gldouble zFar)
fovy: Specifies the field of view angle, in degrees,
in the y direction.
Aspect: Specifies the aspect ratio that determines
the field of view in the x direction. The aspect ratio
is the ratio of x (width) to y (height)
zNear: Specifies the distance from the viewer to
the near clipping plane (always positive)
zFar: Specifies the distance from the viewer to the
far clipping plane (always positive).
23
Perspective Projection

24
Orthographic Projection

glOrtho(left, bottom, -near, right, top, -near )

25
Transform
glTranslatef(0.0,0.0,4.0);

glRotatef(xRotate, 1.0,0.0,0.0);
glRotatef(yRotate, 0.0,1.0,0.0);
glRotatef(zRotate, 0.0,0.0,1.0);

glScalef(1.0,1.0,1.0);

26
#include <GL\glut.h> void reshapeFunc(int x, int y)
{
GLfloat xRotated=30, yRotated=30, if (y == 0 || x == 0) return;
zRotated=30; //Set a new projection matrix
//GLdouble radius=1; glMatrixMode(GL_PROJECTION);
glLoadIdentity();
void redisplayFunc(void) //Angle of view:40 degrees
{ //Near clipping plane distance: 0.5
glMatrixMode(GL_MODELVIEW); //Far clipping plane distance: 20.0
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity(); gluPerspective(40.0,(GLdouble)x/(GLdou
traslate the draw by z = -4.0 ble)y,0.5,20.0);
glTranslatef(0.0,0.0,-4.0); glMatrixMode(GL_MODELVIEW);
glColor3f(0.8, 0.2, 0.1); glViewport(0,0,x,y); //Use the whole
glRotatef(xRotated,1.0,0.0,0.0); window for rendering
glRotatef(yRotated,0.0,1.0,0.0); }
glRotatef(zRotated,0.0,0.0,1.0);
void idleFunc(void)
glScalef(1.0,1.0,1.0);
{
glutSolidCube(1);
yRotated += 0.01;
glFlush(); redisplayFunc();
} } 27
int main (int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400,350);
glutCreateWindow("Sphere Rotating Animation");
glPointSize(5);
glPolygonMode(GL_FRONT_AND_BACK,GL_POINT);
glClearColor(1.0,1.0,1.0,0.0);
glutDisplayFunc(redisplayFunc);
glutReshapeFunc(reshapeFunc);
glutIdleFunc(idleFunc);
glutMainLoop();
return 0;
}
28
29
Cube dimensions

30
Lighting
Functions:
glEnable(GL_LIGHTING)
This function is used to enable lighting option in the program

glDisable(GL_LIGHTING)
This function is used to turn off the lighting option in program

Enable lighting source in program:


You can use the below function to enable any number of light sources in your
program.
glEnable(GL_LIGHT0), glEnable(GL_LIGHT1)….. glEnable(GL_LIGHT7)
Maximum we can use 8 light sources in our program.

Disable light source:


You can use the below function to disable any number of light sources in your
program.
glDisable(GL_LIGHT0), glDisable(GL_LIGHT1), glDisable(GL_LIGHT2)

31
Types of light:
Ambient light (scattered light. not possible to find the light direction)
Diffuse light (scattering the light equally in image: particular direction and position
available for light)

GLfloat ambientColor[] = {R, G, B, A} This array is used to define which color light
source going to use for ambient lighting.
Ex: GLfloat ambientColor[] = {0.2, 0.2, 0.2, 1} last argument must be one for all colors.

To add ambient light source:


glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor)
This function is used to define the lighting type and light source color

32
To add diffuse light source:
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0)
This function is used to add the light source, light type and
set the light color too.
First argument is light number
Second argument is light type
Third argument is light color array name
Ex: GLfloat lightColor0[] = {0.2, 0.2, 0.2, 1}
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0)
Here light source number is 0:
Light type is diffuse
Light array name is lightCol

33
To add the light position in image:
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition0)
This function is used to set the light source position in image
Three arguments:
Light number
Setting position
Array of Position of the light source
Ex:
glLightfv(GL_LIGHT0, GL_POSITION, lightColor0)
GLfloat lightPosition[] = {4, 0, 8, 1} (position at (x,y,z) =>
(4,0,8))

34
#include <GL\glut.h>
GLfloat xRotated, yRotated, zRotated;
GLdouble radius=1;
GLfloat lightColor0[] = { 1.0, 1.0, 0.0, 0.0 };
GLfloat light_position0[] = { 1.2, 1.2, 1.2, 0.0 };
void redisplayFunc(void)
{
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0,0.0,-4.5);
glColor3f(0.0, 1.0, 0.0);
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);
glutSolidSphere(radius,20,20);
glLightfv(GL_LIGHT0, GL_AMBIENT, lightColor0);
glLightfv(GL_LIGHT0, GL_POSITION, light_position0);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glFlush();
35
}
void reshapeFunc(int x, int y)
{
if (y == 0 || x == 0) return; //Nothing is visible then, so
return
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0,(GLdouble)x/(GLdouble)y,0.5,20.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0,0,x,y);
}

void idleFunc(void)
{
yRotated += 0.01;
redisplayFunc();
}
36
int main (int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400,350);
glutCreateWindow("Sphere Rotating Animation");
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
xRotated = yRotated = zRotated = 30.0;
glClearColor(1.0,1.0,1.0,0.0);
glutDisplayFunc(redisplayFunc);
glutReshapeFunc(reshapeFunc);
glutIdleFunc(idleFunc);
glutMainLoop();
return 0;
}
37
38
2D Shape Movement using Keyboard
#include <stdio.h> void display(){
#include <gl/glut.h>
GLfloat rotation = 90.0; glClear(GL_COLOR_BUFFER_BIT);
float posX = 0, posY = 0, posZ glMatrixMode(GL_MODELVIEW);
= 0; glLoadIdentity();
void rect(){ glPushMatrix();
glBegin(GL_POLYGON); glTranslatef(posX,posY,posZ);
glColor3f(1.0, 0.0, 0.0); rect();
glVertex2f(-0.1, -0.2); glPopMatrix();
glVertex2f(-0.1, 0.2); glFlush();
}
glVertex2f(0.1, 0.2);
glVertex2f(0.1, -0.2);
glEnd();
}
39
void init(){
glColor3f(1.0, 1.0, 1.0);
case GLUT_KEY_UP:
glMatrixMode(GL_PROJECTION); posY+=move_unit;;
glLoadIdentity(); break;
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
} case GLUT_KEY_DOWN:
float move_unit = 0.1f; posY-=move_unit;;
void keyboardown(int key, int x, int y)
break;
{
switch (key){
case GLUT_KEY_RIGHT: default:
posX+=move_unit;; break;
break; }
glutPostRedisplay();
case GLUT_KEY_LEFT:
}
posX-=move_unit;;
break;
40
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutCreateWindow("2D Movements");
glutDisplayFunc(display);
init();
glutSpecialFunc(keyboardown);
glutMainLoop();
}

41
Assignment:

Modify the previous program 3D Shpere rotating and


controlled by keyboard keys

42
43
2D Ball Bouncing
#include<GL/glut.h>
static int flag=1;
float tx=0.0,ty=0.0,tz=0.0;
float ball_x=0.5,ball_y=0.0,ball_z=0.0;
void reshaped(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,0,1,200);
}

44
void updateBall()
{
if(!flag)
{
ball_y+=0.05;
if(ball_y>1.0)
flag=1;
}
if(flag)
{
ball_y-=0.05;
if(ball_y<-1)
flag=0;
}
}

45
void display()
{

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BI
T);
glClearColor(1,0,1,0);
glPushMatrix();
glColor3f(0,1,0);
glTranslatef(ball_x,ball_y,ball_z);
glutWireSphere(0.1,23,23);
glPopMatrix();
updateBall();
glutSwapBuffers();
}
46
int main(int argc,char **argv)
{
glutInit(&argc,argv);

glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_
DEPTH);
glutInitWindowSize(400,500);
glutCreateWindow("Bouncing Ball");
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshaped);
glutMainLoop();
}

47
Assignment:

Modify the code 3D Bouncing ball

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

Вам также может понравиться