GRAFIKA KOMPUTER - M. Ali Fauzi | PTIIK Universitas...
Transcript of GRAFIKA KOMPUTER - M. Ali Fauzi | PTIIK Universitas...
GRAFIKA KOMPUTER~ M. Ali Fauzi
DrawingPrimitive Objects
Recall
Although you can draw complex and interestingpictures using OpenGL,they’re all constructed from a small number of primitive graphical items.
Recall
This shouldn’t be too surprising—look at what Leonardo da Vinciaccomplished with just pencils and paintbrushes.
A DRAWING SURVIVAL KIT
Clearing the Window
On a computer, the memory holding the picture is usually filled with the last picture you drew, so you typically need to clear it to some background color before you start to draw the new scene.
Clearing the Window
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Set background color to black and opaque// Clear the color buffer
Specifying a ColorFor example, the pseudocodeset_current_color(red);
draw_object(A);
draw_object(B);
set_current_color(green);
set_current_color(blue);
draw_object(C);
draws objects A and B in red, and object C in blue. The command on thefourth line is wasted.
Forcing Completion of Drawing
glFlush();
OrglFinish();
OrglutSwapBuffers();
Coordinate System Survival KitglutReshapeFunc(reshape);
void reshape(int w, int h)
{
glViewport(0,0,(GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble) w, 0.0,
(GLdouble) h);
}
Coordinate System glViewport() adjusts the pixel rectangle for drawing to be the entire new window. The next three routines adjust the coordinate system for drawing so that the lower left corner is (0, 0) and the upper right corner is (w, h)
Coordinate System
OPENGL GEOMETRIC DRAWING PRIMITIVES
Geometric Primitives
The geometry is specified by vertices.There are ten primitive types
Use glBegin()/glEnd() :
glBegin(Glenum mode);
// some vertices, color, etc
glEnd();
Geometric Primitives
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(-0.5f, -0.5f);
glVertex2f( 0.5f, -0.5f);
glVertex2f( 0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
Geometric Primitives
Between glBegin/ glEnd, those openglcommands are allowed:
glVertex*() : set vertex coordinates
glColor*() : set current color
glTexCoord*(): set texture
coordinates (Texture)
Geometric Primitives
Geometric Primitives
Points, GL_POINTS
> Individual points> Point size can be altered
glPointSize (float size)
Points, GL_POINTS
glPointSize( 0.5 );
glBegin(GL_POINTS);
glColor3f( 0.1, 0.0, 0.0 );
glVertex2f( -0.9, 0 );
glVertex2f( -0.4, 0.4 );
glVertex2f( 0.0, 0.9 );
glVertex2f( 0.4, 0.4 );
glVertex2f( 0.9, 0 );
glVertex2f( 0.4, -0.4 );
glVertex2f( 0, -0.9 );
glVertex2f( -0.4, -0.4 );
glEnd();
Lines, GL_LINES
> Pairs of vertices interpreted as individual line segments> Can specify line width using:
glLineWidth (float width)
Lines, GL_LINES
glLineWidth( 0.5 );
glBegin(GL_LINES);
glColor3f( 0.0, 0.0, 0.0 );
glVertex2f( -0.9, 0 );
glVertex2f( -0.4, 0.4 );
glVertex2f( 0.0, 0.9 );
glVertex2f( 0.4, 0.4 );
glVertex2f( 0.9, 0 );
glVertex2f( 0.4, -0.4 );
glVertex2f( 0, -0.9 );
glVertex2f( -0.4, -0.4 );
glEnd();
Line Strip, GL_LINE_STRIP
Series of connected line segments
glLineWidth( 0.5 );
glBegin(GL_LINE_STRIP);
glColor3f( 0.0, 0.0, 0.0 );
glVertex2f( -0.9, 0 );
glVertex2f( -0.4, 0.4 );
glVertex2f( 0.0, 0.9 );
glVertex2f( 0.4, 0.4 );
glVertex2f( 0.9, 0 );
glVertex2f( 0.4, -0.4 );
glVertex2f( 0, -0.9 );
glVertex2f( -0.4, -0.4 );
glEnd();
Line Strip, GL_LINE_STRIP
Line Loop, GL_LINE_LOOP
Line strip with a segment added between last and first vertices
glLineWidth( 0.5 );
glBegin(GL_LINE_LOOP);
glColor3f( 0.0, 0.0, 0.0 );
glVertex2f( -0.9, 0 );
glVertex2f( -0.4, 0.4 );
glVertex2f( 0.0, 0.9 );
glVertex2f( 0.4, 0.4 );
glVertex2f( 0.9, 0 );
glVertex2f( 0.4, -0.4 );
glVertex2f( 0, -0.9 );
glVertex2f( -0.4, -0.4 );
glEnd();
Line Loop, GL_LINE_LOOP
Polygon, GL_POLYGON
Boundary of a simple, convex polygon
glBegin(GL_POLYGON);
glColor3f( 0.0, 0.1, 0.1 );
glVertex2f( -0.9, 0 );
glVertex2f( -0.4, 0.4 );
glVertex2f( 0.0, 0.9 );
glVertex2f( 0.4, 0.4 );
glVertex2f( 0.9, 0 );
glVertex2f( 0.4, -0.4 );
glVertex2f( 0, -0.9 );
glVertex2f( -0.4, -0.4 );
glEnd();
Polygon, GL_POLYGON
Triangles, GL_TRIANGLES
Triples of vertices interpreted as triangles
glBegin(GL_TRIANGLES);
glColor3f( 0.0, 0.1, 0.1 );
glVertex2f( -0.9, 0 );
glVertex2f( -0.4, 0.4 );
glVertex2f( 0.0, 0.9 );
glVertex2f( 0.4, 0.4 );
glVertex2f( 0.9, 0 );
glVertex2f( 0.4, -0.4 );
glVertex2f( 0, -0.9 );
glVertex2f( -0.4, -0.4 );
glEnd();
Triangles, GL_TRIANGLES
Triangle Strip,GL_TRIANGLE_STRIP
Linked strip of triangles
v0
v2
v1
v3
v4 v5
v6v7
glBegin(GL_TRIANGLE_STRIP);
glColor3f( 0.0, 0.1, 0.1 );
glVertex2f( -0.9, 0 );
glVertex2f( -0.4, 0.4 );
glVertex2f( 0.0, 0.9 );
glVertex2f( 0.4, 0.4 );
glVertex2f( 0.9, 0 );
glVertex2f( 0.4, -0.4 );
glVertex2f( 0, -0.9 );
glVertex2f( -0.4, -0.4 );
glEnd();
Triangle Strip,GL_TRIANGLE_STRIP
Triangle Fan,GL_TRIANGLE_FAN
Linked fan of triangles
v0
v1 v2
v3v4
v5
glBegin(GL_TRIANGLE_FAN);
glColor3f( 0.0, 0.1, 0.1 );
glVertex2f( -0.9, 0 );
glVertex2f( -0.4, 0.4 );
glVertex2f( 0.0, 0.9 );
glVertex2f( 0.4, 0.4 );
glVertex2f( 0.9, 0 );
glVertex2f( 0.4, -0.4 );
glVertex2f( 0, -0.9 );
glVertex2f( -0.4, -0.4 );
glEnd();
Triangle Fan,GL_TRIANGLE_FAN
Quads, GL_QUADS
Quadruples of vertices interpreted as four-sided polygons
glBegin(GL_QUADS);
glColor3f( 0.0, 0.1, 0.1 );
glVertex2f( -0.9, 0 );
glVertex2f( -0.4, 0.4 );
glVertex2f( 0.0, 0.9 );
glVertex2f( 0.4, 0.4 );
glVertex2f( 0.9, 0 );
glVertex2f( 0.4, -0.4 );
glVertex2f( 0, -0.9 );
glVertex2f( -0.4, -0.4 );
glEnd();
Quads, GL_QUADS
Quad Strip,GL_QUAD_STRIP
Linked strip of quads
glBegin(GL_QUAD_STRIP);
glColor3f( 0.0, 0.1, 0.1 );
glVertex2f( -0.9, 0 );
glVertex2f( -0.4, 0.4 );
glVertex2f( 0.0, 0.9 );
glVertex2f( 0.4, 0.4 );
glVertex2f( 0.9, 0 );
glVertex2f( 0.4, -0.4 );
glVertex2f( 0, -0.9 );
glVertex2f( -0.4, -0.4 );
glEnd();
Quad Strip,GL_QUAD_STRIP
EXTRA
Stippled Lines
To make stippled (dotted or dashed) lines, you use the commandglLineStipple() to define the stipple pattern, and then you enable linestippling with glEnable().
glLineStipple(1, 0x3F07);
glEnable(GL_LINE_STIPPLE);
Stippled Lines
Drawing CircleGLfloat jari_jari=150;
// Titik Pusat
GLfloat a = 290, b = 250 ;
GLfloat x=a+jari_jari, y=b;
glLineWidth(7);
glBegin(GL_LINE_LOOP);
for(float teta=0;teta<=360;teta++){
GLfloat x_aksen = (x-a)*cos((teta/180)*(22/7)) -
((y-b)*sin((teta/180)*(22/7))) + a;
GLfloat y_aksen = (x-a)*sin((teta/180)*(22/7)) -
((y-b)*cos((teta/180)*(22/7))) + b;
glVertex2i(x_aksen,y_aksen);
}
glEnd();
Drawing Starsint s; // ukuran titik
float x,y; // posisi titik
for(int i=0;i<1000;i++){
s=rand()%4+1;
glPointSize(s);
glBegin(GL_POINTS);
x=-100+200*(float)rand()/RAND_MAX;
y=-100+200*(float)rand()/RAND_MAX;
glVertex2f(x,y);
glEnd();
}
Tugas
> Draw Circle, Ellips, Cardioid, Treeleaf, Spiral, Limacon and Rain> Write Down the Docs> Format PDF
PLAGIARISM =ZERO
Cheers, Old Sport~ M. Ali [email protected]