Grafika komputer, Membuat objek segetiga warna warni


Lanjutan dari pembahasan sebelumnya mengenai grafika komputer membuat objek. Kali ini saya akan membahas membuat objek segitiga berwarna warni. Untuk lebih jelasnya monggo lihat gambar dibawah ini sebagai hasilnya

1

Untuk membuat objek seperti gambar diatas, ini scriptnya

#include <GL/glut.h>
#include <windows.h>
//https://duniagama.wordpress.com

void myDisplay (void) {

float x1,y1,x2,y2,x3,y3,s;
int i,j;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

x1=-0.9;
x2=-0.9;
x3=-0.85;

y1=0.9;
y2=0.8;
y3=0.8;

s=0;

for (i=1;i<=10;i++)
{
for (j=1;j<=10;j++)
{
s=s+1;
if (s==4) {s=1;}
if (s==1) {
glColor3f(1.0,0.0,0.0);
}
else
if (s==2) {
glColor3f(0.0,0.0,1.0);
}
else
if (s==3) {
glColor3f(0.0,1.0,0.0);
}

glBegin(GL_POLYGON);
//glColor3f(0.0,1.0,0.0);
glVertex2f (x1, y1);
//glColor3f(0.0,1.0,1.0);
glVertex2f (x2, y2);
//glColor3f(0.0,1.0,0.0);
glVertex2f (x3, y3);

//Pengaturan jarak antar kotak (+)
x1+=0.2;
x2+=0.2;
x3+=0.2;

glEnd();
}
//Pengaturan jarak antar kotak (-)
y1-=0.2;
y2-=0.2;
y3-=0.2;

//Sinkronisasi
x1=-0.9;
x2=-0.9;
x3=-0.85;

}

glutSwapBuffers();
glFlush();
}
void main (int argc, char** argv){
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition (0, 0);
glutInitWindowSize(1500, 1500);
glutCreateWindow(“https://duniagama.wordpress.com&#8221;);
glutDisplayFunc(myDisplay);
glutMainLoop();
}

Jika gagal, coba cek kembali komfigurasi openGL anda

Semoga bermanfaat

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s