boedak kubu
Showing posts with label Komputer Grafik. Show all posts
Showing posts with label Komputer Grafik. Show all posts

Friday, June 15, 2012

Interaksi Mouse

#include "stdlib.h"
#include "stdio.h"
#include "gl/glut.h"

int x=0,y=0,z=0;
int w=480, h=480;

void drawQuad() {
glBegin(GL_POLYGON);

glColor3f(0.0, 1.0, 0.0);
glVertex2f(77.,70.);
glVertex2f (80.,146.);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(99.,90.);
glVertex2f(157.,90.);
glColor3f(0.0, 1.0, 1.0);
glVertex2f (110.,55.);
glVertex2f (128.,1.);
glVertex2f (80.,34.);
glVertex2f (32.,1.);
glColor3f(1.0, 1.0, 0.0);
glVertex2f (54.,55.);
glVertex2f (3.,90.);
glColor3f(0.0, 0.0, 1.0);
glVertex2f (63.,90.);
glVertex2f (80.,146.);
glEnd();
}

void mouse(int button, int state, int xmouse, int ymouse){
if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN){

x = xmouse-(w/2);
y = (h/2)-ymouse;
printf(" x = %d y = %d",x,y);
}
}

void renderScene(void){
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1,1,1,1);
glPushMatrix();
glTranslatef(x,y,z);
drawQuad();
glPopMatrix();
glFlush();
}

void timer(int value){
glutPostRedisplay();
glutTimerFunc(50,timer,0);
}

int main (int argc, char **argv){
glutInit(&argc, argv);
glutInitWindowPosition(100,100);
glutInitWindowSize(w,h);
glutCreateWindow("Interaksi Mouse");
gluOrtho2D(-w/2,w/2,-h/2,h/2);
glutDisplayFunc(renderScene);
glutMouseFunc(mouse);
glutTimerFunc(1,timer,0);
glutMainLoop();
}


Bintang 4 Sudut

#include <glut.h>
#include <windows.h>
#include <stdio.h>
void display()
{
/* Membersihkan window */
glClear(GL_COLOR_BUFFER_BIT);

// Bintang yang kecil
glLineWidth(5.0);
glBegin(GL_LINES);
glColor3f(0.0, 1.0, 0.0);
glVertex2f(0.0,0.1);
glVertex2f(0.2,0.2);
glEnd();

glBegin(GL_LINES);
glColor3f(0.0, 1.0, 0.0);
glVertex2f(0.2,0.2);
glVertex2f(0.1,0.0);
glEnd();

glBegin(GL_LINES);
glColor3f(0.0, 1.0, 0.0);
glVertex2f(0.1,0.0);
glVertex2f(0.2,-0.2);
glEnd();

glBegin(GL_LINES);
glColor3f(0.0, 1.0, 0.0);
glVertex2f(0.2,-0.2);
glVertex2f(0.0,-0.1);
glEnd();

// KIRI
glBegin(GL_LINES);
glColor3f(0.0, 1.0, 0.0);
glVertex2f(0.0,0.1);
glVertex2f(-0.2,0.2);
glEnd();

glBegin(GL_LINES);
glColor3f(0.0, 1.0, 0.0);
glVertex2f(-0.2,0.2);
glVertex2f(-0.1,0.0);
glEnd();

glBegin(GL_LINES);
glColor3f(0.0, 1.0, 0.0);
glVertex2f(-0.1,0.0);
glVertex2f(-0.2,-0.2);
glEnd();

glBegin(GL_LINES);
glColor3f(0.0, 1.0, 0.0);
glVertex2f(-0.2,-0.2);
glVertex2f(0.0,-0.1);
glEnd();

// Bintang yang besar
glLineWidth(5.0);
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(0.0,0.2);
glVertex2f(0.4,0.4);
glEnd();

glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(0.4,0.4);
glVertex2f(0.2,0.0);
glEnd();

glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(0.2,0.0);
glVertex2f(0.4,-0.4);
glEnd();

glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(0.4,-0.4);
glVertex2f(0.0,-0.2);
glEnd();

// KIRI
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(0.0,0.2);
glVertex2f(-0.4,0.4);
glEnd();

glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(-0.4,0.4);
glVertex2f(-0.2,0.0);
glEnd();

glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(-0.2,0.0);
glVertex2f(-0.4,-0.4);
glEnd();

glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(-0.4,-0.4);
glVertex2f(0.0,-0.2);
glEnd();

/* mengosongkan buffer GL */
glFlush();
}
void inisial() // inisialisasi warna
{
/* mengubah warna terang ke hitam*/
glClearColor(0.0, 0.0, 0.0, 1.0);
/* mengisi warna putih */
glColor3f(1.0, 0.0, 0.0);
}

void main(int argc, char** argv)
{
/* Inisialisasi mode layar dan membuka window pada pojok kiri atas layar */

glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500); // Menetapkan ukuran jendela
glutInitWindowPosition(400, 100); //Menetapkan Poisi Window
glutCreateWindow("Muhammad Hasim - Bintang 4 Sudut"); //Membuat window dan menentukan judulnya
glutDisplayFunc(display); //Memanggil fungsi untuk menampilkan objek
inisial(); //Inisialisasi Warna Gambar
glutMainLoop(); //Terus tampilkan hingga program diakhiri.
}


Transprmasi 3D

//editing: Muhammad Hasim


//#include "stdafx.h"
#include <GL/glut.h>
#include "stdio.h"
//#include "math.h"

GLfloat moveX=1.0,moveY=0.0,moveZ=0.0;

void init() {

    glEnable(GL_DEPTH_TEST);
    glShadeModel(GL_FLAT);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glColor3f(1.0,1.0,1.0);
   

}
void display() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//    glClear(GL_COLOR_BUFFER_BIT );
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(moveX,moveY,moveZ,0.0,0.0,0.0,0.0,1.0,0.0);
    printf("X: %f, Y: %f, Z: %f\n",moveX,moveY,moveZ); //ini untuk menampilkan posisi objek discreen

    glColor3f(0.0f,0.0f,1.0f);//warna line kotak object
    glutWireCube(01.5);//size kotak object
    glBegin(GL_POLYGON);
      glColor3f(1.0f,1.0f,1.0f);//warna object
      glVertex3f(0.5f,0.5f,0.5f);
      glVertex3f(0.5f,0.5f,-0.5f);
      glVertex3f(-0.5f,0.5f,-0.5f);
      glVertex3f(-0.5f,0.5f,0.5f);     
    glEnd();
   
    glBegin(GL_QUADS);
      glColor3f(1.0f,0.0f,0.0f);//warna object
      glVertex3f(0.5f,-0.5f,0.5f);
      glVertex3f(0.5f,0.5f,0.5f);
      glVertex3f(0.5f,0.5f,-0.5f);
      glVertex3f(0.5f,-0.5f,-0.5f);

      glColor3f(1.0f,1.0f,0.0f);//warna object
      glVertex3f(-0.5f,0.5f,-0.5f);
      glVertex3f(-0.5f,-0.5f,-0.5f);
      glVertex3f(0.5f,-0.5f,-0.5f);
      //glVertex3f(0.5f,0.5f,-0.5f);
      glVertex3f(0.7f,0.5f,-0.5f);
    glEnd();
    glutSwapBuffers();

}

void reshape(int w, int h) {

    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-2.0,2.0,-2.0,2.0,-2.0,2.0);//size object
}

void mykeyboard(unsigned char key, int x,int y) {

    if (key == 'a')//huruf penggerak di keyboard
        moveX+=0.1;
    if (key == 'b')//huruf penggerak di keyboard
        moveX-=0.1;
    if (key == 'c')//huruf penggerak di keyboard
        moveY+=0.1;
    if (key == 'd')//huruf penggerak di keyboard
        moveY-=0.1;
    if (key == 'e')//huruf penggerak di keyboard
        moveZ+=0.1;
    if (key == 'f')//huruf penggerak di keyboard
        moveZ-=0.1;
    glutPostRedisplay();
}

int main(int argc, char* argv[])
{

    glutInit(&argc,argv);
//    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(700,500);
    glutInitWindowPosition(50,50);
    glutCreateWindow("OpenGL - 3D Programming");
    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutKeyboardFunc(mykeyboard);

    init();
    glutMainLoop();
    return 0;
}