SlideShare a Scribd company logo
1 of 41
Introduction to OpenGL and GLUT
GLUT
2
Outline
 OpenGL & GLUT basics
 Grpahics Pipeline
 2-D drawing
 User interaction
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
(100, 50)
(150, 100)
Line(100,50,150,80) - device/lib 1
Moveto(100,50) - device/lib 2
Lineto(150,100)
Without a standard API (such as OpenGL) - impossible to port
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();
}
….
7
Graphics Pipeline
8
OpenGL – What is It?
 GL (Graphics Library): Library of 2-D, 3-
D drawing primitives and operations
 API for 3-D hardware acceleration
 GLU (GL Utilities): Miscellaneous
functions dealing with camera set-up
and higher-level shape descriptions
 GLUT (GL Utility Toolkit): Window-
system independent toolkit with
numerous utility functions, mostly
dealing with user interface
9
Event-driven GLUT program structure
1. Configure and open window
2. Initialize OpenGL state, program
variables
3. Register callback functions
• Display (where rendering occurs)
• Resize
• User input: keyboard, mouse clicks,
motion, etc.
4. Enter event processing loop
Call Back Function
 A callback function is
a function passed into
another function as an argument, which
is then invoked inside the
outer function to complete some kind of
routine or action.
 In C, a callback function is a function that
is called through a function pointer
10
11
Simple OpenGL program
#include <stdio.h>
#include <GL/glut.h>
void main(int argc, char** argv)
{
glutInit(&argc, argv); // configure and open
window
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(100, 100);
glutCreateWindow(“hello”);
init(); // set OpenGL states, variables
glutDisplayFunc(display); // register callback
routines
glutKeyboardFunc(keyboard);
glutMainLoop(); // enter event-driven loop
}
12
Configure and open window
• glutInit: Pass command-line flags on to
GLUT
• glutInitDisplayMode: OR together bit
masks to set modes on pixel type
• glutInitWindowSize, glutCreateWindow:
Set drawing window attributes, then make
it
13
Initialize OpenGL state
• init(): Set OpenGL state, program variables
– Use GL types/typedefs GLfloat, GLint, GL_TRUE,
GL_FALSE, etc. for
cross-platform compatibility
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, right, 0, top);
}
sets “units” of subsequent draw commands
14
OpenGL screen coordinates
• Bottom left corner is origin
• gluOrtho2D() sets the units of the screen
coordinate system
•gluOrtho2D(0, w, 0, h) means the
coordinates are in units of pixels
•gluOrtho2D(0, 1, 0, 1) means the
coordinates are in units of “fractions of window
size” (regardless of actual window size)
 glOrtho2D(L,R,B,T);
 glutInitWindowSize sets the size of the
window.
 Use glViewport to specify the window
region in window coordinates to be drawn
to.
 gluOrtho2D specifies the coordinates to be
used with the viewport which defaults to
the window size.
15
 So, if you want create a 400 x 400 window and
only want to draw to the rectangle(50, 50, 350,
350) within the window, then use
glViewport(50, 50, 350, 350). You would then
use gluOrtho2D to set your GL coordinates
wthin the viewport. So, if you used
gluOrtho2D(-1.0, 1.0, -1.0, 1.0) then visible GL
coordinates would range from -1.0 to 1.0 in the
x direction, from -1.0 to 1.0 in the y direction.
16
17
Example: Specifying the center of a
square
18
Example: Specifying the center of a
square
19
A complete OpenGL program
#include <stdio.h>
#include <GL/glut.h>
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (640, 480);
glutInitWindowPosition (100, 150);
glutCreateWindow ("my first attempt");
glutDisplayFunc(myDisplay);
myInit ();
glutMainLoop();
}
20
A complete OpenGL program
(cont.)
void myDisplay(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 0.0, 0.0);
glPointSize(4.0);
glBegin(GL_POINTS);
glVertex2i(100, 50);
glVertex2i(100, 130);
glVertex2i(150, 130);
glEnd();
glFlush ();
}
21
A complete OpenGL program
(cont.)
void myInit (void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0,
480.0);
}
22
Rendering Steps
In function registered with glutDisplayFunc():
1. Clear window
• glClear(GL_COLOR_BUFFER_BIT);
2. Draw shapes
• Set colors, patterns, point/line sizes
• Specify type of geometric primitive(s) and
list vertices
3. Swap buffers if display mode is
GLUT_DOUBLE
4. Force all operations to complete with
glFlush()
23
Single- vs. double-buffering
• Single-buffering: Draw directly to screen
buffer
• Double-buffering: Draw to offscreen
buffer, then make that the screen
buffer when done
• For animation, double-buffering is
better because it eliminates flickering
24
OpenGL Geometric Primitives
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)
number of
Components/
Dimensions
2 – (x,y)
3 – (x,y,z)
4 – (x,y,z,w) or
(r,g,b,a)
B – byte
ub – unsigned byte
s – short
us – unsigned short
i – int
ui – unsigned int
f – float
d – double
Add ‘v’ for vector
form
glVertex2fv(v)
Shape Example
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 Basics
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)
Program Structure
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();
}
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(“Simple”);
init();
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutMainLoop();
}
Specify the display
Mode – RGB or color
Index, single or double
Buffer
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(“Simple”);
init();
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutMainLoop();
}
Create a window
Named “simple”
with resolution
500 x 500
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(“Simple”);
init();
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutMainLoop();
}
Your OpenGL initialization
code (Optional)
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(“Simple”);
init();
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutMainLoop();
}
Register your call back
functions
GLUT
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.
GLUT
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.
glutDisplayFunc(void (*func)(void) )
GLUT
Event Queue
Event queue
Keyboard
Mouse
Window
….
Mouse_callback()
{
….
{
Keypress_callback()
{
….
{
window_callback()
{
….
{
MainLoop()
GLUT
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 buton
is pressed
 glutIdleFunc() – register the callback that will be
called when nothing is going on (no event)
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();
}
GLUT
glutMainLoop()
The program goes into a infinite
loop waiting for events
Windows Config: glut
 Unzip glut-3.7.6-bin.zip and you will get
 1. glut.h
 2. glut32.dll
 3. glut32.lib
41

More Related Content

Similar to 01.Opengl_intro-2.ppt

Programa de objetos 3 d wire
Programa de objetos 3 d wirePrograma de objetos 3 d wire
Programa de objetos 3 d wireRené Domínguez
 
computer graphics at openGL
computer graphics at openGLcomputer graphics at openGL
computer graphics at openGLYasir Khan
 
FLTK Summer Course - Part VIII - Eighth Impact - Exercises
FLTK Summer Course - Part VIII - Eighth Impact - ExercisesFLTK Summer Course - Part VIII - Eighth Impact - Exercises
FLTK Summer Course - Part VIII - Eighth Impact - ExercisesMichel Alves
 
openGL basics for sample program (1).ppt
openGL basics for sample program (1).pptopenGL basics for sample program (1).ppt
openGL basics for sample program (1).pptHIMANKMISHRA2
 
openGL basics for sample program.ppt
openGL basics for sample program.pptopenGL basics for sample program.ppt
openGL basics for sample program.pptHIMANKMISHRA2
 
The Ring programming language version 1.6 book - Part 56 of 189
The Ring programming language version 1.6 book - Part 56 of 189The Ring programming language version 1.6 book - Part 56 of 189
The Ring programming language version 1.6 book - Part 56 of 189Mahmoud Samir Fayed
 
Lecture 6 introduction to open gl and glut
Lecture 6   introduction to open gl and glutLecture 6   introduction to open gl and glut
Lecture 6 introduction to open gl and glutsimpleok
 
The Ring programming language version 1.5.2 book - Part 53 of 181
The Ring programming language version 1.5.2 book - Part 53 of 181The Ring programming language version 1.5.2 book - Part 53 of 181
The Ring programming language version 1.5.2 book - Part 53 of 181Mahmoud Samir Fayed
 
The Ring programming language version 1.5.1 book - Part 52 of 180
The Ring programming language version 1.5.1 book - Part 52 of 180The Ring programming language version 1.5.1 book - Part 52 of 180
The Ring programming language version 1.5.1 book - Part 52 of 180Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 60 of 202
The Ring programming language version 1.8 book - Part 60 of 202The Ring programming language version 1.8 book - Part 60 of 202
The Ring programming language version 1.8 book - Part 60 of 202Mahmoud Samir Fayed
 
computer graphics at openGL (2)
computer graphics at openGL (2)computer graphics at openGL (2)
computer graphics at openGL (2)Yasir Khan
 
Convert Your Legacy OpenGL Code to Modern OpenGL with Qt
Convert Your Legacy OpenGL Code to Modern OpenGL with QtConvert Your Legacy OpenGL Code to Modern OpenGL with Qt
Convert Your Legacy OpenGL Code to Modern OpenGL with QtICS
 
[FOSDEM 2015] How to test OpenGL drivers using Free Software
[FOSDEM 2015] How to test OpenGL drivers using Free Software[FOSDEM 2015] How to test OpenGL drivers using Free Software
[FOSDEM 2015] How to test OpenGL drivers using Free SoftwareSamuel Iglesias Gonsálvez
 
How to test OpenGL drivers using Free Software (FOSDEM 2015)
How to test OpenGL drivers using Free Software (FOSDEM 2015)How to test OpenGL drivers using Free Software (FOSDEM 2015)
How to test OpenGL drivers using Free Software (FOSDEM 2015)Igalia
 

Similar to 01.Opengl_intro-2.ppt (20)

Programa de objetos 3 d wire
Programa de objetos 3 d wirePrograma de objetos 3 d wire
Programa de objetos 3 d wire
 
Bai 1
Bai 1Bai 1
Bai 1
 
computer graphics at openGL
computer graphics at openGLcomputer graphics at openGL
computer graphics at openGL
 
201707 SER332 Lecture 03
201707 SER332 Lecture 03   201707 SER332 Lecture 03
201707 SER332 Lecture 03
 
FLTK Summer Course - Part VIII - Eighth Impact - Exercises
FLTK Summer Course - Part VIII - Eighth Impact - ExercisesFLTK Summer Course - Part VIII - Eighth Impact - Exercises
FLTK Summer Course - Part VIII - Eighth Impact - Exercises
 
openGL basics for sample program (1).ppt
openGL basics for sample program (1).pptopenGL basics for sample program (1).ppt
openGL basics for sample program (1).ppt
 
openGL basics for sample program.ppt
openGL basics for sample program.pptopenGL basics for sample program.ppt
openGL basics for sample program.ppt
 
The Ring programming language version 1.6 book - Part 56 of 189
The Ring programming language version 1.6 book - Part 56 of 189The Ring programming language version 1.6 book - Part 56 of 189
The Ring programming language version 1.6 book - Part 56 of 189
 
BYO3D 2011: Rendering
BYO3D 2011: RenderingBYO3D 2011: Rendering
BYO3D 2011: Rendering
 
Lecture 6 introduction to open gl and glut
Lecture 6   introduction to open gl and glutLecture 6   introduction to open gl and glut
Lecture 6 introduction to open gl and glut
 
The Ring programming language version 1.5.2 book - Part 53 of 181
The Ring programming language version 1.5.2 book - Part 53 of 181The Ring programming language version 1.5.2 book - Part 53 of 181
The Ring programming language version 1.5.2 book - Part 53 of 181
 
The Ring programming language version 1.5.1 book - Part 52 of 180
The Ring programming language version 1.5.1 book - Part 52 of 180The Ring programming language version 1.5.1 book - Part 52 of 180
The Ring programming language version 1.5.1 book - Part 52 of 180
 
The Ring programming language version 1.8 book - Part 60 of 202
The Ring programming language version 1.8 book - Part 60 of 202The Ring programming language version 1.8 book - Part 60 of 202
The Ring programming language version 1.8 book - Part 60 of 202
 
Development with OpenGL and Qt
Development with OpenGL and QtDevelopment with OpenGL and Qt
Development with OpenGL and Qt
 
computer graphics at openGL (2)
computer graphics at openGL (2)computer graphics at openGL (2)
computer graphics at openGL (2)
 
Convert Your Legacy OpenGL Code to Modern OpenGL with Qt
Convert Your Legacy OpenGL Code to Modern OpenGL with QtConvert Your Legacy OpenGL Code to Modern OpenGL with Qt
Convert Your Legacy OpenGL Code to Modern OpenGL with Qt
 
201801 SER332 Lecture 04
201801 SER332 Lecture 04201801 SER332 Lecture 04
201801 SER332 Lecture 04
 
Grafika komputer 2
Grafika komputer 2Grafika komputer 2
Grafika komputer 2
 
[FOSDEM 2015] How to test OpenGL drivers using Free Software
[FOSDEM 2015] How to test OpenGL drivers using Free Software[FOSDEM 2015] How to test OpenGL drivers using Free Software
[FOSDEM 2015] How to test OpenGL drivers using Free Software
 
How to test OpenGL drivers using Free Software (FOSDEM 2015)
How to test OpenGL drivers using Free Software (FOSDEM 2015)How to test OpenGL drivers using Free Software (FOSDEM 2015)
How to test OpenGL drivers using Free Software (FOSDEM 2015)
 

Recently uploaded

Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 

Recently uploaded (20)

Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 

01.Opengl_intro-2.ppt

  • 1. Introduction to OpenGL and GLUT GLUT
  • 2. 2 Outline  OpenGL & GLUT basics  Grpahics Pipeline  2-D drawing  User interaction
  • 3. 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
  • 4. Maximal Portability  Display device independent  Window system independent  Operating system independent (100, 50) (150, 100) Line(100,50,150,80) - device/lib 1 Moveto(100,50) - device/lib 2 Lineto(150,100) Without a standard API (such as OpenGL) - impossible to port
  • 5. 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)
  • 6. 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(); } ….
  • 8. 8 OpenGL – What is It?  GL (Graphics Library): Library of 2-D, 3- D drawing primitives and operations  API for 3-D hardware acceleration  GLU (GL Utilities): Miscellaneous functions dealing with camera set-up and higher-level shape descriptions  GLUT (GL Utility Toolkit): Window- system independent toolkit with numerous utility functions, mostly dealing with user interface
  • 9. 9 Event-driven GLUT program structure 1. Configure and open window 2. Initialize OpenGL state, program variables 3. Register callback functions • Display (where rendering occurs) • Resize • User input: keyboard, mouse clicks, motion, etc. 4. Enter event processing loop
  • 10. Call Back Function  A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.  In C, a callback function is a function that is called through a function pointer 10
  • 11. 11 Simple OpenGL program #include <stdio.h> #include <GL/glut.h> void main(int argc, char** argv) { glutInit(&argc, argv); // configure and open window glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); glutInitWindowSize(100, 100); glutCreateWindow(“hello”); init(); // set OpenGL states, variables glutDisplayFunc(display); // register callback routines glutKeyboardFunc(keyboard); glutMainLoop(); // enter event-driven loop }
  • 12. 12 Configure and open window • glutInit: Pass command-line flags on to GLUT • glutInitDisplayMode: OR together bit masks to set modes on pixel type • glutInitWindowSize, glutCreateWindow: Set drawing window attributes, then make it
  • 13. 13 Initialize OpenGL state • init(): Set OpenGL state, program variables – Use GL types/typedefs GLfloat, GLint, GL_TRUE, GL_FALSE, etc. for cross-platform compatibility void init() { glClearColor(0.0, 0.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, right, 0, top); } sets “units” of subsequent draw commands
  • 14. 14 OpenGL screen coordinates • Bottom left corner is origin • gluOrtho2D() sets the units of the screen coordinate system •gluOrtho2D(0, w, 0, h) means the coordinates are in units of pixels •gluOrtho2D(0, 1, 0, 1) means the coordinates are in units of “fractions of window size” (regardless of actual window size)  glOrtho2D(L,R,B,T);
  • 15.  glutInitWindowSize sets the size of the window.  Use glViewport to specify the window region in window coordinates to be drawn to.  gluOrtho2D specifies the coordinates to be used with the viewport which defaults to the window size. 15
  • 16.  So, if you want create a 400 x 400 window and only want to draw to the rectangle(50, 50, 350, 350) within the window, then use glViewport(50, 50, 350, 350). You would then use gluOrtho2D to set your GL coordinates wthin the viewport. So, if you used gluOrtho2D(-1.0, 1.0, -1.0, 1.0) then visible GL coordinates would range from -1.0 to 1.0 in the x direction, from -1.0 to 1.0 in the y direction. 16
  • 17. 17 Example: Specifying the center of a square
  • 18. 18 Example: Specifying the center of a square
  • 19. 19 A complete OpenGL program #include <stdio.h> #include <GL/glut.h> void main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (640, 480); glutInitWindowPosition (100, 150); glutCreateWindow ("my first attempt"); glutDisplayFunc(myDisplay); myInit (); glutMainLoop(); }
  • 20. 20 A complete OpenGL program (cont.) void myDisplay(void) { glClear (GL_COLOR_BUFFER_BIT); glColor3f (0.0, 0.0, 0.0); glPointSize(4.0); glBegin(GL_POINTS); glVertex2i(100, 50); glVertex2i(100, 130); glVertex2i(150, 130); glEnd(); glFlush (); }
  • 21. 21 A complete OpenGL program (cont.) void myInit (void) { glClearColor(1.0, 1.0, 1.0, 0.0); glColor3f(0.0f, 0.0f, 0.0f); glPointSize(4.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 640.0, 0.0, 480.0); }
  • 22. 22 Rendering Steps In function registered with glutDisplayFunc(): 1. Clear window • glClear(GL_COLOR_BUFFER_BIT); 2. Draw shapes • Set colors, patterns, point/line sizes • Specify type of geometric primitive(s) and list vertices 3. Swap buffers if display mode is GLUT_DOUBLE 4. Force all operations to complete with glFlush()
  • 23. 23 Single- vs. double-buffering • Single-buffering: Draw directly to screen buffer • Double-buffering: Draw to offscreen buffer, then make that the screen buffer when done • For animation, double-buffering is better because it eliminates flickering
  • 26. 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(); }
  • 27. OpenGL Command Formats glVertex2f(x, y) number of Components/ Dimensions 2 – (x,y) 3 – (x,y,z) 4 – (x,y,z,w) or (r,g,b,a) B – byte ub – unsigned byte s – short us – unsigned short i – int ui – unsigned int f – float d – double Add ‘v’ for vector form glVertex2fv(v)
  • 29. 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
  • 30. GLUT Basics 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) Program Structure GLUT
  • 31. 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(); } GLUT
  • 32. 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(); glutDisplayFunc(display); glutKeyboardFunc(key); glutMainLoop(); } Specify the display Mode – RGB or color Index, single or double Buffer GLUT
  • 33. 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(); glutDisplayFunc(display); glutKeyboardFunc(key); glutMainLoop(); } Create a window Named “simple” with resolution 500 x 500 GLUT
  • 34. 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(); glutDisplayFunc(display); glutKeyboardFunc(key); glutMainLoop(); } Your OpenGL initialization code (Optional) GLUT
  • 35. 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(); glutDisplayFunc(display); glutKeyboardFunc(key); glutMainLoop(); } Register your call back functions GLUT
  • 36. 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. GLUT
  • 37. 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. glutDisplayFunc(void (*func)(void) ) GLUT
  • 39. 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 buton is pressed  glutIdleFunc() – register the callback that will be called when nothing is going on (no event) GLUT
  • 40. #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(); } GLUT glutMainLoop() The program goes into a infinite loop waiting for events
  • 41. Windows Config: glut  Unzip glut-3.7.6-bin.zip and you will get  1. glut.h  2. glut32.dll  3. glut32.lib 41