You are on page 1of 30

Menggambar dengan Mode

Grafik
Kurniawan Teguh Martono
Mengenal Modus Menggambar grafik
• Ada tiga macam modus menggambar grafik :
– Modus Layar penuh :
• Tergantung dari seting resolusi monitor
– Modus window : x ke arah kanan, y ke arah bawah
– Modus window termodifikasi : x ke arah kanan, y
ke arah atas
Lanjutan …
Menggambar titik
• Untuk menggambar titik masing-masing
(100,50), (100, 130), (150,130)
• Perintahnya :
glBegin(GL_POINT);
glVertex2i(100,50);
glVertex2i(100,130);
glVertex2i(150,130);
glEnd();
Ingat pertemuan minggu lalu
OpenGL Command Notation (2/2)
glVertex3fv( ... )

Number of Data Type Vector


components b - byte
omit “v” for
ub - unsigned byte
2 - (x,y) s - short scalar form
3 - (x,y,z) us - unsigned short
4 - (x,y,z,w) i - int glVertex2f( x, y )
ui - unsigned int
f - float
d - double
Menggambar Garis
• Menggambar garis mirip dengan menggambar
titik, bedanya hanya ada pada argumen
glBegin(GL_LINES)
Menggambar polyline dan poligon
• Pada prinsipnya sama
dengan menggambar
garis
• Polyline adalah garis-
garis yang saling
berhubung yang ujung-
ujungnya tidak bertemu
• Poligon garis-garis yang
saling berhubung dan
ujung-ujungnya bertemu
Kind of Drawing
Drawing 2D
• 2D means no depth!
• 2D drawing principles
• Just have 2 Dimension
2D means no depth!
 We should also turn off the depth buffer.
 The depth buffer is what OpenGL uses to
determine which objects are infront or behind
each other in 3D space, based on their pixel's Z
coordinates.
 Because we're not drawing in 3D, and aren't
using the Z coordinates, we don't need the depth
buffer. Infact it may interfere with our drawing.
Therefore we turn it off with:
glDisable(GL_DEPTH_TEST)
2D drawing principles
• 2D drawing in OpenGL is much like 3D
drawing.
• We first clear the screen:
glClear(GL_COLOR_BUFFER_BIT)
• (Notice we didn't use the
GL_DEPTH_BUFFER_BIT, because we're not
using the depth buffer.)
Continue………
 We then draw our graphics primatives
(GL_QUADS, GL_TRIANGLES, GL_LINES etc), with
standard glBegin()..glEnd() blocks.
(We typically use the glVertex2f instruction to
specify the vertices instead of glVertex3f.)
 We can use all the standard OpenGL
transformations if we want to (glTranslate,
glScale, glRotate etc).
 When we've finished rendering the scene, we
display it with
SwapBuffers()
To draw a single point

glBegin(GL_POINTS)
glVertex2f(x1, y1)
glEnd()
To draw a line

glBegin(GL_LINES)
glVertex2f(x1, y1);
glVertex2f(x2,y2);
glEnd()
To draw a square or rectangle
glBegin(GL_QUADS);
glVertex2f(x1, y1);
glVertex2f(x2, y1);
glVertex2f(x2, y2);
glVertex2f(x1, y2);
glEnd();
To draw a circle
• OpenGL does not have any primitives for
drawing curves or circles.
• However we can approximate a circle
using a triangle fan like this:
glBegin(GL_TRIANGLE_FAN) ;
glVertex2f(x1, y1) ;
for angle# = 0 to 360 step 5;
glVertex2f(x1 + sind(angle#) * radius#, y1 +
cosd(angle#) * radius#) next;
glEnd();
To draw in different colours
glColor3f(red, green, blue)

Value :
R=0–1
G=0–1
B=0–1
Drawing Lines in D
glBegin(GL_LINE_STRIP)
glVertex f( . f, . f, . f); // v
glVertex f( . f, . f, . f); // v
glEnd();
Y

v
X
v
Draw Line in D
Y
glBegin(GL_LINE_STRIP) v
glVertex f( . f, . f, . f); // v
glVertex f( . f, . f, . f); // v v
glVertex f( . f, . f, . f); // v
glEnd(); v X

glBegin(GL_LINE_LOOP) Y
glVertex f( . f, . f, . f); // v v
glVertex f( . f, . f, . f); // v
glVertex f( . f, . f, . f); // v v
glEnd();
v X
Drawing Triangles in D
Y
glBegin(GL_TRIANGLES)
glVertex f( . f, . f); // v
glVertex f( . f, . f); // v v
glVertex f( . f, . f); // v
glEnd(); X
v v

Choose the Fastest Primitives for Performance Tip


Most D accelerated hardware is highly optimized for the drawing
of triangles.
To set the background colour
glClearColor(red, green, blue, 0)
Color

OpenGL supports two colors model


• RGBA mode
• color-index mode

violet blue green yellow orange red

nm nm
The Color Cube
Green

Yellow

Cyan
White

Black
Red

Magenta

Blue
Color and Shading
• Color in RGBA mode is set by specifying the
red, green, blue and alpha intensities.
• alpha = //opaque
• alpha = // transparent
RGB color
• Each color component stored separately in the frame buffer
• Usually 8 bits per component in buffer
• Note in glColor3f the color values range from 0.0 (none) to
1.0 (all), while in glColor3ub the values range from 0 to 255
Indexed Color
• Colors are indices into tables of RGB values
• Requires less memory
–indices usually 8 bits
–not as important now
• Memory inexpensive
• Need more colors for shading
Color and State
• The color as set by glColor becomes part of the
state and will be used until changed
–Colors and other attributes are not part of the
object but are assigned when the object is
rendered
• We can create conceptual vertex colors by code
such as
glColor
glVertex
glColor
glVertex
Example of setting color

glColor f( . f, . f, . f); // no alpha value form


glBegin( GL_TRIANGLEs);
glVertex f( - . f, . f, . f);
glVertex f( . f, . f, . f);
glVertex f( . f, . f, . f);
glEnd();
• Note that the glColor*() function can be placed inside a
glBegin()/glEnd() pair. Therefore you can specify individual colors
for each individual vertex
vertices of different color

What happen if vertices have different colors?

glBegin( GL_TRIANGLEs)
glColor f( . f, . f, . f); // red
glVertex f( - . f, . f, . f)
glColor f( . f, . f, . f); // green
glVertex f( . f, . f, . f)
glColor f( . f, . f, . f); // blue
glVertex f( . f, . f, . f)
glEnd()

You might also like