Gamba Primitiv
unknown
c_cpp
a month ago
3.0 kB
3
Indexable
Never
#include <iostream> #include <GL\glew.h> #include <GL\freeglut.h> void kotakMerah() { glBegin(GL_QUADS); // Menggambar kotak merah glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(-0.7, 0.7); glVertex2f(-0.2, 0.7); glVertex2f(-0.2, 0.2); glVertex2f(-0.7, 0.2); glEnd(); } void KotakHijau() { glBegin(GL_QUADS);// warna hijau glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(-0.6, 0.1); glVertex2f(-0.1, 0.1); glVertex2f(-0.1, -0.4); glVertex2f(-0.6, -0.4); glEnd(); } void kotakGradienHitamPutih() { glBegin(GL_TRIANGLES);// glColor3f(1.0f, 1.0f, 1.0f); glVertex2f(-0.8, -0.2); //warna hitam glColor3f(0.0f, 0.0f, 0.0f); glVertex2f(-0.4, -0.2); glVertex2f(-0.8, -0.5); glEnd(); glBegin(GL_TRIANGLES);// warna hijau glColor3f(0.0f, 0.0f, 0.0f); glVertex2f(-0.8, -0.5); glVertex2f(-0.4, -0.2); glColor3f(1.0f, 1.0f, 1.0f); glVertex2f(-0.4, -0.5); glEnd(); } void heksagonKuning() { glBegin(GL_POLYGON);// warna kuning glColor3f(1.0f, 1.0f, 0.0f); glVertex2f(0.3, 0.7); glVertex2f(0.5, 0.7); glVertex2f(0.6, 0.5); glVertex2f(0.5, 0.3); glVertex2f(0.3, 0.3); glVertex2f(0.2, 0.5); glEnd(); } void segitigaBiru() { glBegin(GL_TRIANGLES);// warna biru glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(0.3, 0.0); glVertex2f(0.1, -0.4); glVertex2f(0.5, -0.4); glEnd(); } void segitigaKebalik() { glBegin(GL_TRIANGLES); glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(0.3, -0.2); glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.7, -0.2); glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(0.5, -0.6); glEnd(); } void render() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // membersihkan buffer warna dan kedalaman glLoadIdentity(); // mereset semua transformasi kotakMerah(); // memanggil fungsi kotakMerah KotakHijau(); // memanggil fungsi KotakHijau kotakGradienHitamPutih(); // memanggil fungsi kotakGradienHitamPutih heksagonKuning(); // memanggil fungsi heksagonKuning segitigaBiru(); // memanggil fungsi segitigaBiru segitigaKebalik(); // memanggil fungsi segitigaKebalik glutSwapBuffers(); // menukar buffer depan dan belakang } void changeViewPort(int w, int h) { glViewport(0, 0, w, h); } int main(int argc, char* argv[]) { glutInit(&argc, argv); // Initialize GLUT glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); // Seting memory buffer glutInitWindowSize(500 , 500); // Set windows size glutCreateWindow("Render Shapes"); // Membuat windows dengan title Render Shapes glutReshapeFunc(changeViewPort); // Agar ketika di-resize menggunakan size baru glutDisplayFunc(render); // Subcribe onrender GLenum err = glewInit(); if (GLEW_OK != err) { std::cout << "Error initializing GLEW" << std::endl; return 1; } glutMainLoop(); // Masuk ke dalam loop utama return 0; }
Leave a Comment