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

Introduction to openGL

OpenGL is a low-level graphics rendering API that enables the generation of high-quality color images from geometric and image primitives, ensuring maximal portability across different display devices, window systems, and operating systems. It primarily functions by rendering geometric and raster primitives, and while it does not manage window functions, it can be used with additional libraries like GLUT for window management and event handling. The document provides code examples and explains the structure and usage of OpenGL and GLUT for creating graphics applications.

Uploaded by

Dani Abera
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Introduction to openGL

OpenGL is a low-level graphics rendering API that enables the generation of high-quality color images from geometric and image primitives, ensuring maximal portability across different display devices, window systems, and operating systems. It primarily functions by rendering geometric and raster primitives, and while it does not manage window functions, it can be used with additional libraries like GLUT for window management and event handling. The document provides code examples and explains the structure and usage of OpenGL and GLUT for creating graphics applications.

Uploaded by

Dani Abera
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Computer graphics & multimedia

Chapter Five
Introduction to openGL

1
WHAT IS OPENGL?

 An application programming interface (API)


 A (low-level) Graphics rendering API

 Generate high-quality color images composed


of geometric and image primitives
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

(150, 100) Moveto(100,50) - device/lib 2


Lineto(150,100)
OPENGL BASICS

 OpenGL’s primary function – Rendering

 Rendering? – converting geometric/mathematical


object descriptions into frame buffer values

 OpenGL can render:


 Geometric primitives
 Bitmaps and Images (Raster primitives)
CODE EXAMPLE

void Display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor4f(1,1,0,1);
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
glFlush();
}
….
SPECIFYING GEOMETRIC PRIMITIVES

 Primitives are specified using


glBegin(primType);
// define your primitives here

glEnd();

 primType:
GL_POINTS, GL_LINES,
GL_TRIANGLES, GL_QUADS, …
Primitive Types
SAMPLE EXAMPLE

Void DrawQuad(GLfloat color[])


{
glColor3f(0,0,1);
glBegin(GL_QUADS);
glVertex2f(0,0);
glVertex2f(1.0, 0,0);
glVertex2f(1.0, 1.0);
glVertex2f(0.0, 1.0);
glEnd();
}
OPENGL COMMAND FORMATS

glVertex2f(x, y)
Add ‘v’ for vector
form
number of B – byte
glVertex2fv(v)
Components/ ub – unsigned byte
Dimensions s – short
us – unsigned short
2 – (x,y) i – int
3 – (x,y,z) ui – unsigned int
4 – (x,y,z,w) or f – float
(r,g,b,a) d – double
SHAPE EXAMPLE
WINDOW-BASED PROGRAMMING

 Mostof the modern graphics systems are


window-based
Window based environment
Non-window based environment
WINDOW SYSTEM INDEPENDENT

 OpenGL is window system independent


 No window management functions – create windows,
resize windows, event handling, etc
 This is to ensure the application’s portability
 Create some headache though – just a pure OpenGL
program won’t work anywhere.
MORE APIS ARE NEEDED

 X window system: GLX


 Apple Macintosh: AGL

 Microsoft Windows: WGL

These libraries provide complete functionality to create


Graphics User Interface (GUI) such as sliders, buttons,
, menus etc.

Problem – you need to learn and implement them all to


write a true portable software
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 GLUT
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 GLUT
#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 GLUT
#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 GLUT
#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
glutDisplayFunc(display); 500 x 500
glutKeyboardFunc(key);
glutMainLoop();
}
Sample Program GLUT
#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 initialization
glutDisplayFunc(display); code (Optional)
glutKeyboardFunc(key);
glutMainLoop();
}
Sample Program GLUT
#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); Register your call back
glutKeyboardFunc(key); functions
glutMainLoop();
}
CALLBACK FUNCTIONS? GLUT

 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.
GLUT
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 GLUT
Keyboard
Event queue ….

MainLoop() Mouse

Window

Mouse_callback() Keypress_callback() window_callback()


{ { {
…. …. ….
{ { {
AND MANY MORE … GLUT

 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 buton is
pressed
 glutIdleFunc() – register the callback that will be
called when nothing is going on (no event)
glutMainLoop() GLUT
#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
THANK YOU FOR YOUR ATTENTION

26

You might also like