GLUT: 3 Dimensi yang bergerak
#include <GL/glut.h>
#include <stdlib.h>
int xRot = 0, yRot = 0;
void grid3d(int baris, int kolom, int z) {
int i;
int kiri = -kolom / 2; //batas kiri x
int kanan = kolom / 2; //batas kanan x
int bawah = -baris / 2; //batas bawah y
int atas = baris / 2; //batas atas y
glBegin(GL_LINES);
glLineWidth(1);
//Baris digambar dari atas ke bawah
for (i = 0; i < baris + 1; i++) {
glVertex3f(kiri, baris / 2 - i, z);
glVertex3f(kanan, baris / 2 - i, z);
}
//Kolom digambar dari kanan ke kiri
for (i = 0; i < kolom + 1; i++) {
glVertex3f(kolom / 2 - i, bawah, z);
glVertex3f(kolom / 2 - i, atas, z);
}
glLineWidth(3);
glColor3f(0.5, 0.5, 0.5);
glVertex3f(kiri, 0, z);
glVertex3f(kanan, 0, z);
glVertex3f(0, bawah, z);
glVertex3f(0, atas, z);
glEnd();
}
void pressKey(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_LEFT :
yRot -= 1;
break;
case GLUT_KEY_RIGHT :
yRot += 1;
break;
case GLUT_KEY_UP :
xRot -= 1;
break;
case GLUT_KEY_DOWN :
xRot += 1;
break;
}
glutPostRedisplay();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Let there be the grid!
glColor3f(0.3, 0.3, 0.3);
grid3d(20, 20, -5);
glPushMatrix();
glRotatef(xRot, 1.0, 0.0, 0.0);
glRotatef(yRot, 0.0, 1.0, 0.0);
// Your object here
glColor3ub(255, 0, 0);
glBegin(GL_POLYGON);
glVertex3f(-7.0f, 7.0f, -5.0f);
glVertex3f(-7.0f, -7.0f, -5.0f);
glVertex3f( 5.0f, -5.0f, -10.0f);
glVertex3f( 5.0f, 5.0f, -10.0f);
glEnd();
glPopMatrix();
glutSwapBuffers();
glFlush();
}
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glFrustum(-10.0, 10.0, -10.0, 10.0, 4.0, 50.0);
gluPerspective(45, 1, 1, 100);
gluLookAt(0, 0, 30, 0, 0, 0, 0.0, 1.0, 0.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char * * argv) {
glutInit( & argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(400, 400);
glutInitWindowPosition(100, 100);
glutCreateWindow("Grid");
glutDisplayFunc(display);
glutSpecialFunc(pressKey);
init();
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glutMainLoop();
return 0;
}
#include <stdlib.h>
int xRot = 0, yRot = 0;
void grid3d(int baris, int kolom, int z) {
int i;
int kiri = -kolom / 2; //batas kiri x
int kanan = kolom / 2; //batas kanan x
int bawah = -baris / 2; //batas bawah y
int atas = baris / 2; //batas atas y
glBegin(GL_LINES);
glLineWidth(1);
//Baris digambar dari atas ke bawah
for (i = 0; i < baris + 1; i++) {
glVertex3f(kiri, baris / 2 - i, z);
glVertex3f(kanan, baris / 2 - i, z);
}
//Kolom digambar dari kanan ke kiri
for (i = 0; i < kolom + 1; i++) {
glVertex3f(kolom / 2 - i, bawah, z);
glVertex3f(kolom / 2 - i, atas, z);
}
glLineWidth(3);
glColor3f(0.5, 0.5, 0.5);
glVertex3f(kiri, 0, z);
glVertex3f(kanan, 0, z);
glVertex3f(0, bawah, z);
glVertex3f(0, atas, z);
glEnd();
}
void pressKey(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_LEFT :
yRot -= 1;
break;
case GLUT_KEY_RIGHT :
yRot += 1;
break;
case GLUT_KEY_UP :
xRot -= 1;
break;
case GLUT_KEY_DOWN :
xRot += 1;
break;
}
glutPostRedisplay();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Let there be the grid!
glColor3f(0.3, 0.3, 0.3);
grid3d(20, 20, -5);
glPushMatrix();
glRotatef(xRot, 1.0, 0.0, 0.0);
glRotatef(yRot, 0.0, 1.0, 0.0);
// Your object here
glColor3ub(255, 0, 0);
glBegin(GL_POLYGON);
glVertex3f(-7.0f, 7.0f, -5.0f);
glVertex3f(-7.0f, -7.0f, -5.0f);
glVertex3f( 5.0f, -5.0f, -10.0f);
glVertex3f( 5.0f, 5.0f, -10.0f);
glEnd();
glPopMatrix();
glutSwapBuffers();
glFlush();
}
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glFrustum(-10.0, 10.0, -10.0, 10.0, 4.0, 50.0);
gluPerspective(45, 1, 1, 100);
gluLookAt(0, 0, 30, 0, 0, 0, 0.0, 1.0, 0.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char * * argv) {
glutInit( & argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(400, 400);
glutInitWindowPosition(100, 100);
glutCreateWindow("Grid");
glutDisplayFunc(display);
glutSpecialFunc(pressKey);
init();
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glutMainLoop();
return 0;
}
Comments
Post a Comment