0% found this document useful (0 votes)
48 views

Unit I: Raphics Primitives and Scan Conversion Algorithm

Here are the key points about callback functions in OpenGL: - Callback functions are functions that are registered with GLUT (OpenGL Utility Toolkit) to handle specific events like drawing, keyboard input, window resizing, etc. - When the event occurs (e.g. need to redraw the screen), GLUT will call ("callback") the registered function to handle that event. - Common callback functions include: - glutDisplayFunc() - Called when the window needs redrawing - glutKeyboardFunc() - Called when a key is pressed - glutMouseFunc() - Called when a mouse button is pressed/released - The programmer defines the callback functions, for example a display function that redraws the scene. These

Uploaded by

Shishir Agrahari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Unit I: Raphics Primitives and Scan Conversion Algorithm

Here are the key points about callback functions in OpenGL: - Callback functions are functions that are registered with GLUT (OpenGL Utility Toolkit) to handle specific events like drawing, keyboard input, window resizing, etc. - When the event occurs (e.g. need to redraw the screen), GLUT will call ("callback") the registered function to handle that event. - Common callback functions include: - glutDisplayFunc() - Called when the window needs redrawing - glutKeyboardFunc() - Called when a key is pressed - glutMouseFunc() - Called when a mouse button is pressed/released - The programmer defines the callback functions, for example a display function that redraws the scene. These

Uploaded by

Shishir Agrahari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

Unit I

GRAPHICS PRIMITIVES AND SCAN


CONVERSION ALGORITHM
Circle Drawing Algorithm
1. DDA Circle Drawing Algorithm
2. Bresenhamn’s Circle Drawing Algorithm
3. Midpoint Circle Drawing Algorithm
•The equation for a circle is:
•x2+y2=r2
•where r is the radius of the circle
•So, we can write a simple circle
drawing algorithm by solving the
equation for y at unit x intervals using:
Eight-Way Symmetry
•The first thing we can notice to make our
circle drawing algorithm more efficient is
that circles centred at (0, 0) have eight-
way symmetry
Introduction to OpenGL
What is OpenGL?
• A software interface to graphics hardware
• Graphics rendering API (Low Level)
– High-quality color images composed of geometric
and image primitives
– Window system independent
– Operating system independent
What is OpenGL?
Maximal Portability
• Display device independent
• Window system independent
• Operating system independent
Without a standard API (such as OpenGL) - impossible to port

(100, 50) Line(100,50,150,80) - device/lib 1

Moveto(100,50) - device/lib 2
Lineto(150,100)
(150, 100)
Commands may either be accumulated in display lists, or processed immediately
through the pipeline. Display lists allow for greater optimization and command
reuse, but not all commands can be put in display lists.

The first stage in the pipeline is the evaluator. This stage effectively takes any
polynomial evaluator commands and evaluates them into their corresponding vertex
and attribute commands.

The second stage is the per-vertex operations, including transformations,


lighting, primitive assembly, clipping, projection, and viewport mapping.

The third stage is rasterization. This stage produces fragments, which are series
of framebuffer addresses and values, from the viewport-mapped primitives as well
as bitmaps and pixel rectangles.

The fourth stage is the per-fragment operations. Before fragments go to the


framebuffer, they may be subjected to a series of conditional tests and
modifications, such as blending or z-buffering.

Parts of the framebuffer may be fed back into the pipeline as pixel rectangles.
Texture memory may be used in the rasterization process when texture mapping is
enabled.
OpenGL Basics
• OpenGL’s primary function –
Rendering
• Rendering? – converting
geometric/mathematical object descriptions
into frame buffer values
• OpenGL can render:
– Geometric primitives
• Lines, points, polygons, etc…
– Bitmaps and Images
• Images and geometry linked through
texture mapping
OpenGL Geometric Primitives
• The geometry is specified by vertices.
• The primitive types:
OpenGL Command Format

glVertex3fv
Vertices and Primitives
• Primitives are specified using
glBegin( primType );

glEnd();

– primType determines how vertices are combined

GLfloat red, green, blue;


Glfloat coords[nVerts][3];
/*Initialize coords and colors somewhere in
program*/
glBegin( primType );
for ( i = 0; i < nVerts; ++i ) {
glColor3f( red, green, blue );
glVertex3fv( coords[i] );
}
glEnd();
An Example
void
drawParallelogram( GLfloa
t color[] )
{
glBegin( GL_QUADS );
glColor3fv( color );
glVertex2f( 0.0, 0.0 );
glVertex2f( 1.0, 0.0 );
glVertex2f( 1.5,
1.118 );
glVertex2f( 0.5,
1.118 );
glEnd();
}
Vertices and Primitives
• Points, GL_POINTS
– Individual points
– Point size can be altered
• glPointSize (float size)

glBegin(GL_POINTS);
glColor3fv( color );
glVertex2f( P0.x, P0.y );
glVertex2f( P1.x, P1.y );
glVertex2f( P2.x, P2.y );
glVertex2f( P3.x, P3.y );
glVertex2f( P4.x, P4.y );
glVertex2f( P5.x, P5.y );
glVertex2f( P6.x, P6.y );
glVertex2f( P7.x, P7.y );
glEnd();
Vertices and Primitives
• Lines, GL_LINES
– Pairs of vertices interpreted as individual line segments
– Can specify line width using:
• glLineWidth (float width)
glBegin(GL_LINES);
glColor3fv( color );
glVertex2f( P0.x, P0.y );
glVertex2f( P1.x, P1.y );
glVertex2f( P2.x, P2.y );
glVertex2f( P3.x, P3.y );
glVertex2f( P4.x, P4.y );
glVertex2f( P5.x, P5.y );
glVertex2f( P6.x, P6.y );
glVertex2f( P7.x, P7.y );
glEnd();
Vertices and Primitives
• Line Strip, GL_LINE_STRIP
– series of connected line segments
Vertices and Primitives
• Line Loop, GL_LINE_LOOP
– Line strip with a segment added between last and first
vertices
Vertices and Primitives
• Polygon , GL_POLYGON
– boundary of a simple, convex polygon
Vertices and Primitives
• Triangles , GL_TRIANGLES
– triples of vertices interpreted as triangles
Vertices and Primitives
• Triangle Strip , GL_TRIANGLE_STRIP
– linked strip of triangles

v0 v1

v2 v3

v4 v5
v7
v6
Vertices and Primitives
• Triangle Fan ,
GL_TRIANGLE_FAN
– linked fan of triangles

v1 v2
v3
v4
v0
v5
Vertices and Primitives
• Quads , GL_QUADS
– quadruples of vertices interpreted as four-sided polygons
Vertices and Primitives

• Between glBegin/ glEnd, those opengl commands


are allowed:
– glVertex*() : set vertex coordinates
– glColor*() : set current color
– glIndex*() : set current color index
– glNormal*() : set normal vector coordinates (Light.)
– glTexCoord*() : set texture coordinates (Texture)
Attributes 1
• Attribute parameter: determines how a
graphics primitive is displayed
• For instance for line: color, width, style
• OpenGL: simple state system
– Current setting is maintained;
– Setting can be changed with a function.

H&B 5-1:130
Attributes 2
Example:
glLineWidth(width);
glEnable(GL_LINE_STIPPLE);
glLineStipple(repeatfactor, pattern);
// draw stippled lines
...
glDisable(GL_LINE_STIPPLE);

Note: special features often must be enabled


explicitly with glEnable().

H&B 5-7:141-143
Color 1
RGB mode :
• color = (red, green, blue)
• glColor[34][bisf]: set color of vertex
• For example:
– glColor4f(red, green, blue, alpha)
– alpha: opacity (1−transparantie)

H&B 5-2:130-131
Use GLUT (OpenGL Utility Toolkit)
• For fast prototyping, we can use GLUT to interface
with different window systems

• GLUT is a window independent API – programs


written using OpenGL and GLUT can be ported to X
windows, MS windows, and Macintosh with no
effort

• GLUT does not contain all the bells and whistles


though (no sliders, no dialog boxes, no menu bar,
etc)
GLUT Basics
Program Structure
1. Configure and open window (GLUT)
2. Initialize OpenGL (Optional)
3. Register input callback functions (GLUT)
– Render
– Resize
– Input: keyboard, mouse, etc
4. Enter event processing loop (GLUT)
Sample Program

#include <GL/glut.h>
#include <GL/gl.h>

Void main(int argc, char** argv)


{
int mode = GLUT_RGB|GLUT_SINGLE;
glutInitDisplayMode(mode);
glutInitWindowSize(500,500);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutMainLoop();
}
Sample Program
#include <GL/glut.h>
#include <GL/gl.h>

Void main(int argc, char** argv)


{
int mode = GLUT_RGB|GLUT_SINGLE;
glutInitDisplayMode(mode); Specify the display
glutInitWindowSize(500,500); Mode – RGB or color
glutCreateWindow(“Simple”); Index, single or double
init(); Buffer
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutMainLoop();
}
Sample Program
#include <GL/glut.h>
#include <GL/gl.h>

Void main(int argc, char** argv)


{
int mode = GLUT_RGB|GLUT_SINGLE;
glutInitDisplayMode(mode);
glutInitWindowSize(500,500); Create a window
glutCreateWindow(“Simple”); Named “simple”
init(); with resolution
500 x 500
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutMainLoop();
}
Sample Program
#include <GL/glut.h>
#include <GL/gl.h>

Void main(int argc, char** argv)


{
int mode = GLUT_RGB|GLUT_SINGLE;
glutInitDisplayMode(mode);
glutInitWindowSize(500,500);
glutCreateWindow(“Simple”);
init(); Your OpenGL
glutDisplayFunc(display); initialization
glutKeyboardFunc(key); code (Optional)
glutMainLoop();
}
Sample Program
#include <GL/glut.h>
#include <GL/gl.h>

Void main(int argc, char** argv)


{
int mode = GLUT_RGB|
GLUT_SINGLE;
glutInitDisplayMode(mode);
glutInitWindowSize(500,500);
glutCreateWindow(“Simple”);
init(); Register your call back
glutDisplayFunc(display); functions
glutKeyboardFunc(key);
glutMainLoop();
}
Callback functions?

• Most of window-based programs are


event-driven
– which means do nothing until an event happens,
and then execute some pre-defined functions

• Events – key press, mouse button press and


release, window resize, etc.
• What is callback function in OpenGL?
• A callback function is a function which the
library (GLUT) calls when it needs to know
how to proccess something. e.g. when glut
gets a key down event it uses the
glutKeybourdFunc callback routine to find out
what to do with a key press
glutDisplayFunc(void (*func)(void) )
Void main(int argc, char** argv)
{

glutDisplayFunc(display);

}
void display() – the function
you provide. It contains all
the OpenGL drawing
function
calls and will be called
when pixels in the window
need to be refreshed.
Event Queue

Keyboar
Event queue d ….
MainLoop( Mouse
)
Window

Mouse_callback() Keypress_callback() window_callback()


{ { {
…. …. ….
{ { {
Window callbacks indicate when to
redisplay or reshape a window, when
the visibility of the window changes,
and when input is available for
the window. The menu callback is set
by the glutCreateMenu call described
already. The global callbacks manage
the passing of time and menu usage.
And many more …
• glutKeyboardFunc() – register the callback that will be
called when a key is pressed
• glutMouseFunc() – register the callback that will be
called when a mouse button is pressed
• glutMotionFunc() – register the callback that will be
called when the mouse is in motion while a button is
pressed
• glutIdleFunc() – register the callback that will be called
when nothing is going on (no event)
glutMainLoop()
#include <GL/glut.h>
#include <GL/gl.h>

Void main(int argc, char** argv)


{
int mode = GLUT_RGB|GLUT_SINGLE;
glutInitDisplayMode(mode);
glutInitWindowSize(500,500);
glutCreateWindow(“Simple”);
init();
glutDisplayFunc(display);
glutReshapeFunc(resize);
glutKeyboardFunc(key);
glutMainLoop();
} The program goes into a infinite
loop waiting for events

You might also like