Skip to content

Instantly share code, notes, and snippets.

@dwichan0905
Created January 13, 2020 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwichan0905/81c561a8d9482feb117be37a5e473fbe to your computer and use it in GitHub Desktop.
Save dwichan0905/81c561a8d9482feb117be37a5e473fbe to your computer and use it in GitHub Desktop.
Membuat Animasi Linear Sederhana pada C++ OpenGL
#include <GL\glut.h>
GLfloat xRotated, yRotated, zRotated;
void init(void)
{
glClearColor(0, 0, 0, 0);
}
void DrawCube(void)
{
glMatrixMode(GL_MODELVIEW);
// Bersihkan Buffer Gambar.
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0, 0.0, -10.5);
glRotatef(xRotated, 1.0, 0.0, 0.0);
// Rotasi Sumbu Y
glRotatef(yRotated, 0.0, 1.0, 0.0);
// Rotasi Sumbu Z
glRotatef(zRotated, 0.0, 0.0, 1.0);
glBegin(GL_QUADS); // Gambar kubus menggunakan kotak
glColor3f(0.0f, 1.0f, 0.0f); // Warna Biru
glVertex3f(1.0f, 1.0f, -1.0f); // Kanan atas kotak (Atas)
glVertex3f(-1.0f, 1.0f, -1.0f); // Kiri atas kotak (Atas)
glVertex3f(-1.0f, 1.0f, 1.0f); // Kiri Bawah kotak (Atas)
glVertex3f(1.0f, 1.0f, 1.0f); // Kanan bawah kotak (Atas)
glColor3f(1.0f, 0.5f, 0.0f); // Warna Orange
glVertex3f(1.0f, -1.0f, 1.0f); // Kanan atas kotak (Bawah)
glVertex3f(-1.0f, -1.0f, 1.0f); // Kiri atas kotak (Bawah)
glVertex3f(-1.0f, -1.0f, -1.0f); // Kiri Bawah kotak (Bawah)
glVertex3f(1.0f, -1.0f, -1.0f); // Kanan bawah kotak (Bawah)
glColor3f(1.0f, 0.0f, 0.0f); // Warna Merah
glVertex3f(1.0f, 1.0f, 1.0f); // Kanan atas kotak (Depan)
glVertex3f(-1.0f, 1.0f, 1.0f); // Kiri atas kotak (Depan)
glVertex3f(-1.0f, -1.0f, 1.0f); // Kiri Bawah kotak (Depan)
glVertex3f(1.0f, -1.0f, 1.0f); // Kanan bawah kotak (Depan)
glColor3f(1.0f, 1.0f, 0.0f); // Warna Kuning
glVertex3f(1.0f, -1.0f, -1.0f); // Kanan atas kotak (Belakang)
glVertex3f(-1.0f, -1.0f, -1.0f); // Kiri atas kotak (Belakang)
glVertex3f(-1.0f, 1.0f, -1.0f); // Kiri Bawah kotak (Belakang)
glVertex3f(1.0f, 1.0f, -1.0f); // Kanan bawah kotak (Belakang)
glColor3f(0.0f, 0.0f, 1.0f); // Warna Biru
glVertex3f(-1.0f, 1.0f, 1.0f); // Kanan atas kotak (Kiri)
glVertex3f(-1.0f, 1.0f, -1.0f); // Kiri atas kotak (Kiri)
glVertex3f(-1.0f, -1.0f, -1.0f); // Kiri Bawah kotak (Kiri)
glVertex3f(-1.0f, -1.0f, 1.0f); // Kanan bawah kotak (Kiri)
glColor3f(1.0f, 0.0f, 1.0f); // Warna Ungu
glVertex3f(1.0f, 1.0f, -1.0f); // Kanan atas kotak (Kanan)
glVertex3f(1.0f, 1.0f, 1.0f); // Kiri atas kotak (Kanan)
glVertex3f(1.0f, -1.0f, 1.0f); // Kiri Bawah kotak (Kanan)
glVertex3f(1.0f, -1.0f, -1.0f); // Kanan bawah kotak (Kanan)
glEnd(); // Selesai
glFlush();
}
void animation(void)
{
yRotated += 0.01;
xRotated += 0.02;
DrawCube();
}
void reshape(int x, int y)
{
if (y == 0 || x == 0) return; //Tidak ada yang terlihat, jadi return aja
// Buat Metrix proyeksi baru
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Sudut:40 Derajat
// Jarak terdekat: 0.5
// Jarak terjauh : 20.0
gluPerspective(40.0, (GLdouble)x / (GLdouble)y, 0.5, 20.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0, 0, x, y); //Gunakan semua kotak untuk rendering
}
int main(int argc, char** argv){
glutInit(&argc, argv);
// Kita initialisasi glut functionnya
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(DrawCube);
glutReshapeFunc(reshape);
// Atur fungsi animasinya
glutIdleFunc(animation);
glutMainLoop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment