Minggu, 12 Juni 2011

How stupid I am...

video

Tiada yang salah
Hanya aku manusia bodoh
Yang biarkan semua ini permainkanku
Berulang ulang kali

Mencoba bertahan sekuat hati
Layaknya karang yang dihempas sang ombak
Jalani hidup dalam buai belaka
Serahkan cinta tulus di dalam takdir
Tak ayal tingkah lakumu
Buatku putus asa
Kadang akal sehat ini
Tak cukup membendungnya
Hanya kepedihan
Yang selalu datang menertawakanku
Engkau belahan jiwa
Tega menari indah di atas tangisanku

Senin, 06 Juni 2011

Objek 3D Menggunakan Glut



source code:

//#include "color.h"
#include "cmath"
#include "cstdio"
#include "cstdlib"

#ifdef __APPLE__
#include
#else
#include
#endif

using namespace std;

// Constants -------------------------------------------------------------------

#define WHEEL_UP 3
#define WHEEL_DOWN 4

#define CAMERA_DISTANCE_MIN 1.0
#define CAMERA_DISTANCE_MAX 100.0

enum {
DL_WAGON = 1,
DL_GRID,
DL_JENNY_CUB,
DL_BOX
};

enum {
CAMERA_FIRST_PERSON,
CAMERA_BIRDS_EYE,
CAMERA_THIRD_PERSON,
CAMERA_ORBIT
};

// Global variables ------------------------------------------------------------

static size_t WindowWidth = 640;
static size_t WindowHeight = 480;

static GLint MouseX = 0;
static GLint MouseY = 0;

static double CameraLatitude = 45.0;
static double CameraLongitude = 25.0;
static double CameraDistance = 50.0;

static double EyeX = -100.0;
static double EyeY = 50.0;
static double EyeZ = 100.0;

static double DirX = 0.0;
static double DirY = 0.0;
static double DirZ = 0.0;

static double WagonOrientation = 0.0;
static double WagonSpeed = 0.0;

static double WagonX = 0.0;
static double WagonZ = 0.0;

static double WagonLength = 10.0;
static double WagonWidth = 4.0;
static double WagonSideWidth = 0.5;

static double JennyCubOrientation = 0.0;
static double JennyCubOrientationDelta = 2.0;

static GLUquadric *Quadric = NULL;

static int CameraMode = CAMERA_ORBIT;
static bool WireFrameMode = false;

// Update camera ---------------------------------------------------------------

void
update_camera_location()
{
double L = CameraDistance * cos(M_PI*CameraLongitude/180.0);


EyeX = WagonX + L * -sin(M_PI*CameraLatitude/180.0);
EyeY = CameraDistance * sin(M_PI*CameraLongitude/180.0);
EyeZ = WagonZ + L * cos(M_PI*CameraLatitude/180.0);
DirX = WagonX;
DirY = 0.0;
DirZ = WagonZ;


glutPostRedisplay();
}

// Initialize scene ------------------------------------------------------------

void
init_scene()
{
glEnable(GL_DEPTH_TEST);

GLfloat diffuse0[] = { 0.8, 0.8, 0.8, 1.0 };
GLfloat ambient0[] = { 0.2, 0.2, 0.2, 1.0 };
GLfloat specular0[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat position0[] = { 100.0, 100.0, 100.0, 1.0 };

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular0);
glLightfv(GL_LIGHT0, GL_POSITION, position0);

glEnable(GL_COLOR_MATERIAL) ;
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);

glEnable(GL_NORMALIZE);
glEnable(GL_SMOOTH);

if (Quadric == NULL)
Quadric = gluNewQuadric();

update_camera_location();
}

// Draw wagon ------------------------------------------------------------------


// Draw jenny cub --------------------------------------------------------------

void
draw_jenny_cub_body()
{

glPushMatrix(); {
glColor3f(0.75, 0.5, 0.0);
glScalef(1.0, 1.25, 1.0);
glTranslatef(0.0, -5.25, 0.0);
glutSolidSphere(5.0, 24, 24);

} glPopMatrix();

glPushMatrix(); {
glColor3f(0.9,0.7,0.0);

glScalef(1.0, 1.25, 1.0);
glTranslatef(1.5, -5.25, 0.0);
glutSolidSphere(4.0, 24, 24);


} glPopMatrix();
}

void draw_jenny_cub_foot()
{
/* explore sendiri ya... */

}

void draw_jenny_cub_hand()
{

/* explore sendiri ya... */
}

void
draw_jenny_cub_ears()
{
glPushMatrix(); {
glRotatef(90.0, 0.0, 1.0, 0.0);
glPushMatrix(); {
glTranslatef(2.5, 2, 0.0);//hor, ver
//glTranslatef( cos(M_PI/5), sin(M_PI/5), 0.0);
glutSolidTorus(0.7, 1.0, 24, 24);
gluDisk(Quadric, 0.0, 1.25, 24, 24);
} glPopMatrix();
glPushMatrix(); {
glTranslatef(-2.5, 2, 0.0);//hor, ver
//glTranslatef( cos(M_PI/5), sin(M_PI/5), 0.0);
glutSolidTorus(0.7, 1.0, 24, 24);
gluDisk(Quadric, 0.0, 1.25, 24, 24);
} glPopMatrix();
} glPopMatrix();
}

void
draw_jenny_cub_eyes()
{
glPushAttrib(GL_ALL_ATTRIB_BITS);
glColor3f(0.0, 0.0, 0.0);
glPushMatrix(); {
//glTranslatef(0.9*cos(M_PI*0.1), 0.3, 0.9*sin(M_PI*0.1));
glTranslatef(2.0, 1.5, 1.2);
glutSolidSphere(0.4, 24, 24);
} glPopMatrix();
glPushMatrix(); {
//glTranslatef(0.9*cos(M_PI*0.1), 0.3, -0.9*sin(M_PI*0.1));
glTranslatef(2.0, 1.5, -1.2);
glutSolidSphere(0.4, 24, 24);
} glPopMatrix();
glPopAttrib();
}

void
draw_jenny_cub_nose()
{
glPushAttrib(GL_ALL_ATTRIB_BITS);
glColor3f(0.0, 0.0, 0.0);
glPushMatrix(); {
//glTranslatef(0.5*0.9*cos(M_PI*0.1), 0.5*0.9*sin(M_PI*0.1), 0.0);
glTranslatef(0.95, 0.2, 0.0);
glutSolidSphere(0.7, 24, 24);
} glPopMatrix();
glPopAttrib();
}

void
draw_jenny_cub_head()
{
glPushMatrix(); {
glColor3f(0.75, 0.5, 0.0);
glTranslatef(0.0, 2.5, 0.0);
glutSolidSphere(3.0, 24, 24);

draw_jenny_cub_ears();
draw_jenny_cub_eyes();

glTranslatef(2.0, -0.25, 0.0);
glutSolidSphere(1.5, 24, 24);

draw_jenny_cub_nose();
} glPopMatrix();
}

void
draw_jenny_cub()
{
draw_jenny_cub_body();
draw_jenny_cub_foot();
draw_jenny_cub_head();
draw_jenny_cub_hand();
}

// Make display lists ----------------------------------------------------------

void
make_display_lists()
{

glNewList(DL_JENNY_CUB, GL_COMPILE); {
draw_jenny_cub();
}; glEndList();

glNewList(DL_BOX, GL_COMPILE); {
glBegin(GL_POLYGON); {
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f( 0.5, 0.5);
glVertex2f( 0.5, -0.5);
} glEnd();
}; glEndList();
}

// Draw scene ------------------------------------------------------------------

void
draw_scene()
{
glViewport(0, 0, WindowWidth, WindowHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLdouble)WindowWidth/(GLdouble)WindowHeight, 1.0, 750.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(EyeX, EyeY, EyeZ, DirX, DirY, DirZ, 0.0, 1.0, 0.0);

glPolygonMode(GL_FRONT_AND_BACK, WireFrameMode ? GL_LINE : GL_FILL);
glEnable(GL_LIGHTING);
glShadeModel(GL_SMOOTH);
glPushMatrix(); {
glTranslatef(WagonX, 3.0, WagonZ);
glRotatef(WagonOrientation, 0.0, 1.0, 0.0);

glTranslatef(-WagonLength/4.0, 0.0, 0.0);
glRotatef(JennyCubOrientation, 0.0, 1.0, 0.0);
glCallList(DL_JENNY_CUB);
} glPopMatrix();

glCallList(DL_GRID);
}

// Draw minimap ----------------------------------------------------------------

void
draw_minimap()
{

}

// Display callback ------------------------------------------------------------

void
display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw_scene();
glClearColor(1,1,1,1);
glutSwapBuffers();
}

// Reshape callback ------------------------------------------------------------

void
reshape(GLsizei nw, GLsizei nh)
{
WindowWidth = nw;
WindowHeight = nh;

init_scene();
}

// Keyboard callback -----------------------------------------------------------

// Special callback -----------------------------------------------------------

void
special(int key, int x, int y)
{
switch (key) {
case GLUT_KEY_LEFT: WagonOrientation += 10.0; break;
case GLUT_KEY_RIGHT: WagonOrientation -= 10.0; break;
case GLUT_KEY_UP: CameraDistance=CameraDistance+1;break;
case GLUT_KEY_DOWN:CameraDistance=CameraDistance-1;break;
}

update_camera_location();
}

// Mouse callback --------------------------------------------------------------

void
mouse(int button, int state, int x, int y)
{
MouseX = x;
MouseY = y;

switch (button) {
case WHEEL_UP:
CameraDistance = (CameraDistance > CAMERA_DISTANCE_MIN ? CameraDistance - 1.0 : CAMERA_DISTANCE_MIN);
break;
case WHEEL_DOWN:
CameraDistance = (CameraDistance < CAMERA_DISTANCE_MAX ? CameraDistance + 1.0 : CAMERA_DISTANCE_MAX);
break;
}

update_camera_location();
}

// Motion callback -------------------------------------------------------------

void
motion(int x, int y)
{
CameraLatitude += 180.0*(double)(x - MouseX)/WindowWidth;
CameraLongitude += 180.0*(double)(y - MouseY)/WindowHeight;

if (CameraLongitude < -90.0)
CameraLongitude = -90.0;
if (CameraLongitude > 90.0)
CameraLongitude = 90.0;

MouseX = x;
MouseY = y;

update_camera_location();

}

// Timer callback --------------------------------------------------------------

void
timer(int value)
{

}


// Main execution --------------------------------------------------------------

int
main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(WindowWidth, WindowHeight);
glutCreateWindow("Teddy Bear Ndut");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutSpecialFunc(special);
glutMouseFunc(mouse);
glutTimerFunc(100, timer, 0);
glutMotionFunc(motion);

init_scene();
make_display_lists();

glutMainLoop();

return (EXIT_SUCCESS);
}

// vim: sts=4 sw=4 ts=8 ft=cpp