Menggambar Dengan Mode Grafik
Menggambar Dengan Mode Grafik
Menggambar Dengan Mode Grafik
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( ... )
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
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
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()