SlideShare a Scribd company logo
CSC 307 1.0
Graphics Programming
Budditha Hettige
Department of Statistics and Computer Science
Graphics Programming
OpenGL 3D Drawing
2Budditha Hettige
3D Graphics
• Projections – Getting 3D to 2D
3
3D scene 2D image
Budditha Hettige
Projections
• Orthographic Projection
• Perspective Projection
4
Orthographic
Perspective
Budditha Hettige
Orthographic Projections
• glOrtho ( double left, double right, double
bottom, double top, double near, double
far);
5
Toward the
viewport
Top
Bottom
Left
Right
Near Far
Viewing Volume
Budditha Hettige
Perspective Projection
• gluPerspective ( double angle, double
aspect, double near, double far);
6
zoom in zoom out
map map
Observer
w
h
aspect = w/h
Budditha Hettige
Draw a 3D Model
• Object Composition
7
3D Model
Polygon
Budditha Hettige
Object Composition
• Use Transformation
– glPushMatrix ( );
– glPopMatrix ( );
– Save the current transformation state and
then restore it after some objects have been
placed
Budditha Hettige 8
The Matrix Stack
• Draw a car use transformation
– The car body and four wheeles
The Car Body
Wheele 1
Wheele 2
Wheele 3
Wheele 4
Wheele 4
Transformation state
Budditha Hettige 9
Hidden Surface Removal
• Enable Depth Testing
– glEnable (GL_DEPTH_TEST);
10
Hidden Surface
Budditha Hettige
Drawing 3D Objects
• Cube
– void glutWireCube(GLdouble size);
– void glutSolidCube(GLdouble size);
• Sphere
– void glutWireSphere(GLdouble radius, GLint slices, GLint stacks);
– void glutSolidSphere(GLdouble radius, GLint slices, GLint stacks);
• Cone
11Budditha Hettige
12
Three-dimensional Applications
•In OpenGL, two-dimensional applications
are a special case of three-dimensional
graphics
–Not much changes
–Use glVertex3*( )
–Have to worry about the order in which
polygons are drawn or use hidden-surface
removal
–Polygons should be simple, convex, flat
Budditha Hettige
13
Sierpinski Gasket (2D)
• Start with a triangle
• Connect bisectors of sides and remove central
triangle
• Repeat
• Example : Gasket.cpp
Budditha Hettige
14
Moving to 3D
•We can easily make the program three-
dimensional by using
typedef Glfloat point3[3]
glVertex3f
glOrtho
•But that would not be very interesting
•Instead, we can start with a tetrahedron
Budditha Hettige
15
3D Gasket
• We can subdivide each of the four faces
• We can easily make the program three-dimensional by using
typedef Glfloat point3[3]
glVertex3f
glOrtho
Budditha Hettige
16
Example
After 4 interations
Budditha Hettige
17
triangle code
void triangle( point a, point b, point c)
{// right-hand rule
glBegin(GL_POLYGON);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glEnd();
}
Budditha Hettige
18
subdivision code
void divide_triangle(point a, point b, point c,
int m)
{
point v1, v2, v3;
int j;
if(m>0)
{
for(j=0; j<3; j++) v1[j]=(a[j]+b[j])/2;
for(j=0; j<3; j++) v2[j]=(a[j]+c[j])/2;
for(j=0; j<3; j++) v3[j]=(b[j]+c[j])/2;
divide_triangle(a, v1, v2, m-1);
divide_triangle(c, v2, v3, m-1);
divide_triangle(b, v3, v1, m-1);
}
else(triangle(a,b,c));
}
Budditha Hettige
19
tetrahedron code
void tetrahedron( int m)
{
glColor3f(1.0,0.0,0.0);
divide_triangle(v[0], v[1], v[2], m);
glColor3f(0.0,1.0,0.0);
divide_triangle(v[3], v[2], v[1], m);
glColor3f(0.0,0.0,1.0);
divide_triangle(v[0], v[3], v[1], m);
glColor3f(0.0,0.0,0.0);
divide_triangle(v[0], v[2], v[3], m);
}
Budditha Hettige
20
Problem
• Because the triangles are drawn in the order they
are defined in the program, the front triangles are
not always rendered in front of triangles behind
them
get this
want this
Budditha Hettige
21
Solution: Hidden-Surface Removal
• We want to see only those surfaces in front of
other surfaces
• OpenGL uses a hidden-surface method called the
z-buffer algorithm that saves depth information as
objects are rendered so that only the front objects
appear in the image
Budditha Hettige
Locating the camera
22
Position and orient the camera – three inputs
Eye, at, and direction – up
Camera is on Model-View mode
Budditha Hettige
Default Camera Settings
• With default, camera is located at the origin and
oriented at the negative z-axes
• If place a cube at the origin, only one side of the
cube is visible
• Look at objects from
different angles
– Move the objects
– Move the camera
• Example – a cube
• Glu function
void gluLookAt(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz)
gluLookAt(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
23Budditha Hettige
24
Code: Display a cube
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glutWireCube(1.0f);
glFlush();
}
Budditha Hettige

More Related Content

What's hot (20)

PPTX
golden rules of user interface design
gadige harshini
 
PPTX
Software design
Zulqarnaintayyab
 
PPTX
Ch4 req eng
software-engineering-book
 
PDF
Unit 2
pm_ghate
 
PPTX
Image compression 14_04_2020 (1)
Joel P
 
PPTX
Unit 7 performing user interface design
Preeti Mishra
 
PPTX
Linker and Loader
sonalikharade3
 
PPT
Debugging
Indu Sharma Bhardwaj
 
PDF
Computer Graphics Project Report on Sinking Ship using OpenGL
Sharath Raj
 
PPTX
Design of a two pass assembler
Dhananjaysinh Jhala
 
PPTX
Modules and modularization criteria
Umaselvi_R
 
PPTX
Graphical User Interface
Deepa Ram Suthar
 
PPT
Chapter_04.ppt
oluobes
 
PPT
Pressman ch-3-prescriptive-process-models
saurabhshertukde
 
PPTX
Defining the Problem - Goals and requirements
Stephennancy
 
PPTX
unit testing and debugging
KarthigaGunasekaran1
 
PPT
Analysis concepts and principles
saurabhshertukde
 
PDF
Chapter 4 software project planning
despicable me
 
PPTX
iOS Architecture
Jacky Lian
 
golden rules of user interface design
gadige harshini
 
Software design
Zulqarnaintayyab
 
Unit 2
pm_ghate
 
Image compression 14_04_2020 (1)
Joel P
 
Unit 7 performing user interface design
Preeti Mishra
 
Linker and Loader
sonalikharade3
 
Computer Graphics Project Report on Sinking Ship using OpenGL
Sharath Raj
 
Design of a two pass assembler
Dhananjaysinh Jhala
 
Modules and modularization criteria
Umaselvi_R
 
Graphical User Interface
Deepa Ram Suthar
 
Chapter_04.ppt
oluobes
 
Pressman ch-3-prescriptive-process-models
saurabhshertukde
 
Defining the Problem - Goals and requirements
Stephennancy
 
unit testing and debugging
KarthigaGunasekaran1
 
Analysis concepts and principles
saurabhshertukde
 
Chapter 4 software project planning
despicable me
 
iOS Architecture
Jacky Lian
 

Similar to OpenGL 3D Drawing (20)

PPTX
CGLabLec6.pptx
Mohammad7Abudosh7
 
PPT
Programming with OpenGL
Syed Zaid Irshad
 
PDF
Viewing
Budditha Hettige
 
PPT
Drawing Figures
Ghaffar Khan
 
DOCX
Ghana National Flag
SH Rajøn
 
PPT
Computer Graphics involves technology to access. The Process transforms and p...
ErNandiniDharne
 
PPT
Working with Callbacks
Syed Zaid Irshad
 
PDF
203 Is It Real or Is It Virtual? Augmented Reality on the iPhone
jonmarimba
 
PDF
2D Drawing
Budditha Hettige
 
PPTX
OpenGL_summer2012.ccccccccccccccccccpptx
arcse1
 
PDF
DrTAD Blender software. Example 3. Images as Planes. Spin (3D Object). Materi...
FIDE Master Tihomir Dovramadjiev PhD
 
DOCX
Lab Practices and Works Documentation / Report on Computer Graphics
Rup Chowdhury
 
DOCX
FINALCG4.docxSOLAR SYASTEN PROJECTAREIMPORTANT
mayurkutal5
 
PPTX
Trident International Graphics Workshop 2014 1/5
Takao Wada
 
DOCX
Togo National Flag
SH Rajøn
 
PDF
OpenCVand Matlab based Car Parking System Module for Smart City using Circle ...
JANAK TRIVEDI
 
PPT
CS 354 Transformation, Clipping, and Culling
Mark Kilgard
 
PDF
Intro to OpenGL ES 2.0
Oleksandr Kozubets
 
PDF
PART 4: GEOGRAPHIC SCRIPTING
Andrea Antonello
 
CGLabLec6.pptx
Mohammad7Abudosh7
 
Programming with OpenGL
Syed Zaid Irshad
 
Drawing Figures
Ghaffar Khan
 
Ghana National Flag
SH Rajøn
 
Computer Graphics involves technology to access. The Process transforms and p...
ErNandiniDharne
 
Working with Callbacks
Syed Zaid Irshad
 
203 Is It Real or Is It Virtual? Augmented Reality on the iPhone
jonmarimba
 
2D Drawing
Budditha Hettige
 
OpenGL_summer2012.ccccccccccccccccccpptx
arcse1
 
DrTAD Blender software. Example 3. Images as Planes. Spin (3D Object). Materi...
FIDE Master Tihomir Dovramadjiev PhD
 
Lab Practices and Works Documentation / Report on Computer Graphics
Rup Chowdhury
 
FINALCG4.docxSOLAR SYASTEN PROJECTAREIMPORTANT
mayurkutal5
 
Trident International Graphics Workshop 2014 1/5
Takao Wada
 
Togo National Flag
SH Rajøn
 
OpenCVand Matlab based Car Parking System Module for Smart City using Circle ...
JANAK TRIVEDI
 
CS 354 Transformation, Clipping, and Culling
Mark Kilgard
 
Intro to OpenGL ES 2.0
Oleksandr Kozubets
 
PART 4: GEOGRAPHIC SCRIPTING
Andrea Antonello
 
Ad

More from Budditha Hettige (20)

PDF
Algorithm analysis
Budditha Hettige
 
PDF
Sorting
Budditha Hettige
 
PDF
Link List
Budditha Hettige
 
PDF
02 Stack
Budditha Hettige
 
PDF
Data Structures 01
Budditha Hettige
 
PDF
Drawing Fonts
Budditha Hettige
 
PDF
Texture Mapping
Budditha Hettige
 
PDF
Lighting
Budditha Hettige
 
PDF
Graphics Programming OpenGL & GLUT in Code::Blocks
Budditha Hettige
 
PDF
Introduction to Computer Graphics
Budditha Hettige
 
PPTX
Computer System Architecture Lecture Note 9 IO fundamentals
Budditha Hettige
 
PPTX
Computer System Architecture Lecture Note 8.1 primary Memory
Budditha Hettige
 
PPTX
Computer System Architecture Lecture Note 8.2 Cache Memory
Budditha Hettige
 
PPTX
Computer System Architecture Lecture Note 7 addressing
Budditha Hettige
 
PPT
Computer System Architecture Lecture Note 6: hardware performance
Budditha Hettige
 
PPT
Computer System Architecture Lecture Note 5: microprocessor technology
Budditha Hettige
 
PPT
Computer System Architecture Lecture Note 3: computer architecture
Budditha Hettige
 
PPT
Computer System Architecture Lecture Note 2: History
Budditha Hettige
 
PPT
Computer System Architecture Lecture Note 1: introduction
Budditha Hettige
 
Algorithm analysis
Budditha Hettige
 
Link List
Budditha Hettige
 
Data Structures 01
Budditha Hettige
 
Drawing Fonts
Budditha Hettige
 
Texture Mapping
Budditha Hettige
 
Graphics Programming OpenGL & GLUT in Code::Blocks
Budditha Hettige
 
Introduction to Computer Graphics
Budditha Hettige
 
Computer System Architecture Lecture Note 9 IO fundamentals
Budditha Hettige
 
Computer System Architecture Lecture Note 8.1 primary Memory
Budditha Hettige
 
Computer System Architecture Lecture Note 8.2 Cache Memory
Budditha Hettige
 
Computer System Architecture Lecture Note 7 addressing
Budditha Hettige
 
Computer System Architecture Lecture Note 6: hardware performance
Budditha Hettige
 
Computer System Architecture Lecture Note 5: microprocessor technology
Budditha Hettige
 
Computer System Architecture Lecture Note 3: computer architecture
Budditha Hettige
 
Computer System Architecture Lecture Note 2: History
Budditha Hettige
 
Computer System Architecture Lecture Note 1: introduction
Budditha Hettige
 
Ad

Recently uploaded (20)

PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PPTX
LDP-2 UNIT 4 Presentation for practical.pptx
abhaypanchal2525
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PDF
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
PDF
EXCRETION-STRUCTURE OF NEPHRON,URINE FORMATION
raviralanaresh2
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
PPTX
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PPTX
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
PPTX
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPTX
Unlock the Power of Cursor AI: MuleSoft Integrations
Veera Pallapu
 
PDF
John Keats introduction and list of his important works
vatsalacpr
 
PPTX
Applied-Statistics-1.pptx hardiba zalaaa
hardizala899
 
PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
PDF
Tips for Writing the Research Title with Examples
Thelma Villaflores
 
PPTX
Virus sequence retrieval from NCBI database
yamunaK13
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PDF
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
LDP-2 UNIT 4 Presentation for practical.pptx
abhaypanchal2525
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
EXCRETION-STRUCTURE OF NEPHRON,URINE FORMATION
raviralanaresh2
 
Basics and rules of probability with real-life uses
ravatkaran694
 
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
Unlock the Power of Cursor AI: MuleSoft Integrations
Veera Pallapu
 
John Keats introduction and list of his important works
vatsalacpr
 
Applied-Statistics-1.pptx hardiba zalaaa
hardizala899
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
Tips for Writing the Research Title with Examples
Thelma Villaflores
 
Virus sequence retrieval from NCBI database
yamunaK13
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 

OpenGL 3D Drawing

  • 1. CSC 307 1.0 Graphics Programming Budditha Hettige Department of Statistics and Computer Science
  • 2. Graphics Programming OpenGL 3D Drawing 2Budditha Hettige
  • 3. 3D Graphics • Projections – Getting 3D to 2D 3 3D scene 2D image Budditha Hettige
  • 4. Projections • Orthographic Projection • Perspective Projection 4 Orthographic Perspective Budditha Hettige
  • 5. Orthographic Projections • glOrtho ( double left, double right, double bottom, double top, double near, double far); 5 Toward the viewport Top Bottom Left Right Near Far Viewing Volume Budditha Hettige
  • 6. Perspective Projection • gluPerspective ( double angle, double aspect, double near, double far); 6 zoom in zoom out map map Observer w h aspect = w/h Budditha Hettige
  • 7. Draw a 3D Model • Object Composition 7 3D Model Polygon Budditha Hettige
  • 8. Object Composition • Use Transformation – glPushMatrix ( ); – glPopMatrix ( ); – Save the current transformation state and then restore it after some objects have been placed Budditha Hettige 8
  • 9. The Matrix Stack • Draw a car use transformation – The car body and four wheeles The Car Body Wheele 1 Wheele 2 Wheele 3 Wheele 4 Wheele 4 Transformation state Budditha Hettige 9
  • 10. Hidden Surface Removal • Enable Depth Testing – glEnable (GL_DEPTH_TEST); 10 Hidden Surface Budditha Hettige
  • 11. Drawing 3D Objects • Cube – void glutWireCube(GLdouble size); – void glutSolidCube(GLdouble size); • Sphere – void glutWireSphere(GLdouble radius, GLint slices, GLint stacks); – void glutSolidSphere(GLdouble radius, GLint slices, GLint stacks); • Cone 11Budditha Hettige
  • 12. 12 Three-dimensional Applications •In OpenGL, two-dimensional applications are a special case of three-dimensional graphics –Not much changes –Use glVertex3*( ) –Have to worry about the order in which polygons are drawn or use hidden-surface removal –Polygons should be simple, convex, flat Budditha Hettige
  • 13. 13 Sierpinski Gasket (2D) • Start with a triangle • Connect bisectors of sides and remove central triangle • Repeat • Example : Gasket.cpp Budditha Hettige
  • 14. 14 Moving to 3D •We can easily make the program three- dimensional by using typedef Glfloat point3[3] glVertex3f glOrtho •But that would not be very interesting •Instead, we can start with a tetrahedron Budditha Hettige
  • 15. 15 3D Gasket • We can subdivide each of the four faces • We can easily make the program three-dimensional by using typedef Glfloat point3[3] glVertex3f glOrtho Budditha Hettige
  • 17. 17 triangle code void triangle( point a, point b, point c) {// right-hand rule glBegin(GL_POLYGON); glVertex3fv(a); glVertex3fv(b); glVertex3fv(c); glEnd(); } Budditha Hettige
  • 18. 18 subdivision code void divide_triangle(point a, point b, point c, int m) { point v1, v2, v3; int j; if(m>0) { for(j=0; j<3; j++) v1[j]=(a[j]+b[j])/2; for(j=0; j<3; j++) v2[j]=(a[j]+c[j])/2; for(j=0; j<3; j++) v3[j]=(b[j]+c[j])/2; divide_triangle(a, v1, v2, m-1); divide_triangle(c, v2, v3, m-1); divide_triangle(b, v3, v1, m-1); } else(triangle(a,b,c)); } Budditha Hettige
  • 19. 19 tetrahedron code void tetrahedron( int m) { glColor3f(1.0,0.0,0.0); divide_triangle(v[0], v[1], v[2], m); glColor3f(0.0,1.0,0.0); divide_triangle(v[3], v[2], v[1], m); glColor3f(0.0,0.0,1.0); divide_triangle(v[0], v[3], v[1], m); glColor3f(0.0,0.0,0.0); divide_triangle(v[0], v[2], v[3], m); } Budditha Hettige
  • 20. 20 Problem • Because the triangles are drawn in the order they are defined in the program, the front triangles are not always rendered in front of triangles behind them get this want this Budditha Hettige
  • 21. 21 Solution: Hidden-Surface Removal • We want to see only those surfaces in front of other surfaces • OpenGL uses a hidden-surface method called the z-buffer algorithm that saves depth information as objects are rendered so that only the front objects appear in the image Budditha Hettige
  • 22. Locating the camera 22 Position and orient the camera – three inputs Eye, at, and direction – up Camera is on Model-View mode Budditha Hettige
  • 23. Default Camera Settings • With default, camera is located at the origin and oriented at the negative z-axes • If place a cube at the origin, only one side of the cube is visible • Look at objects from different angles – Move the objects – Move the camera • Example – a cube • Glu function void gluLookAt(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz) gluLookAt(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); 23Budditha Hettige
  • 24. 24 Code: Display a cube void display() { glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glutWireCube(1.0f); glFlush(); } Budditha Hettige