Grafika Komputer, Membuat objek segitiga(model berbeda) Warna warni


Jika postingan sebelumnya membuat segitiga siku-siku, kali ini membuat objek segitiga dengan model lainnya. Untuk lebih jelasnya seperti gambar dibawah ini

2

Untuk membuat objek segitiga seperti gambar diatas, berikut ini scriptnya

#include <GL/glut.h>
#include <windows.h>

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.8;
x3=-0.85;

y1=0.9;
y2=0.9;
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.8;
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(“Membuat Segitiga”);
glutDisplayFunc(myDisplay);
glutMainLoop();
}

Jika gagal, coba cek kembali konfigurasi 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