Senin, 30 Desember 2013
membuat kotak berputar
# include<GL/glut.h>
static GLfloat spin= 0.0;
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(spin,0.0,0.0,1.0);
glColor3f(1.0,1.0,1.0);
glRectf(-25.0,-25.0,25.0,25.0);
glPopMatrix();
glutSwapBuffers();
}
void spinDisplay(void)
{
spin=spin + 2.0;
if(spin>360.0)
glutPostRedisplay();
}
void init(void)
{
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GL_FLAT);
}
void reshape(int w,int h)
{
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
}
void mouse(int button, int state,int x,int y)
{
switch(button)
{
case GLUT_LEFT_BUTTON:
if (state==GLUT_DOWN)
glutIdleFunc(spinDisplay);
break;
case GLUT_MIDDLE_BUTTON:
case GLUT_RIGHT_BUTTON:
if (state==GLUT_DOWN)
glutIdleFunc(0);
break;
default:
break;
}
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(300,300);
glutInitWindowPosition(100,100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
Grafika - matahari mengelilingi bumi
#include <GL/glut.h>
static int spin = 0;
static int kanan = 0;
static int kiri = 0;
void init(void)
{
GLfloat mat_specular[] = { 1.0,1.0, 1.0, 1.0};
GLfloat mat_shininess[] = {50.0};
GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};
glClearColor (0.1,0.1,0.0,0.0);
glShadeModel (GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
void setViewport(int left, int bottom, int width, int height){
glViewport(left, bottom, width, height);
}
void display(void)
{
GLfloat position[] = {0.0, 0.0, 2.0, 0.5};
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.0, 0.0, -5.0);
glPushMatrix();
glRotated((GLdouble) spin, spin, spin, 0.0);
glLightfv(GL_LIGHT0,GL_POSITION, position);
glTranslated(0.0,0.0,1.5);
glDisable(GL_LIGHTING);
glColor3f(1.0, 1.0, 0.0);
glutWireSphere(0.1,50,16);
glEnable(GL_LIGHTING);
glPopMatrix();
//membuat bumi
glutSolidSphere (0.5, 100, 32);
glPopMatrix();
glFlush();
}
void reshape (int w, int h)
{
glViewport (0,0,(GLsizei) w, (GLsizei) h);
glMatrixMode ( GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,
1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
else
glOrtho (-1.5*(GLfloat)w/(GLfloat)h,
1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void mouse(int button , int state, int x, int y)
{
switch (button) {
case GLUT_LEFT_BUTTON :
if (state ==GLUT_DOWN){
spin = (spin + 10) % 360;
glutPostRedisplay();
}
break;
default :
break;
}
}
void update(int value){
glutPostRedisplay();
glutTimerFunc(10,update,0);
spin = (spin +1 ) % 360;
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (700, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init();
glutTimerFunc(100, update, 0);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
static int spin = 0;
static int kanan = 0;
static int kiri = 0;
void init(void)
{
GLfloat mat_specular[] = { 1.0,1.0, 1.0, 1.0};
GLfloat mat_shininess[] = {50.0};
GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};
glClearColor (0.1,0.1,0.0,0.0);
glShadeModel (GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
void setViewport(int left, int bottom, int width, int height){
glViewport(left, bottom, width, height);
}
void display(void)
{
GLfloat position[] = {0.0, 0.0, 2.0, 0.5};
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.0, 0.0, -5.0);
glPushMatrix();
glRotated((GLdouble) spin, spin, spin, 0.0);
glLightfv(GL_LIGHT0,GL_POSITION, position);
glTranslated(0.0,0.0,1.5);
glDisable(GL_LIGHTING);
glColor3f(1.0, 1.0, 0.0);
glutWireSphere(0.1,50,16);
glEnable(GL_LIGHTING);
glPopMatrix();
//membuat bumi
glutSolidSphere (0.5, 100, 32);
glPopMatrix();
glFlush();
}
void reshape (int w, int h)
{
glViewport (0,0,(GLsizei) w, (GLsizei) h);
glMatrixMode ( GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,
1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
else
glOrtho (-1.5*(GLfloat)w/(GLfloat)h,
1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void mouse(int button , int state, int x, int y)
{
switch (button) {
case GLUT_LEFT_BUTTON :
if (state ==GLUT_DOWN){
spin = (spin + 10) % 360;
glutPostRedisplay();
}
break;
default :
break;
}
}
void update(int value){
glutPostRedisplay();
glutTimerFunc(10,update,0);
spin = (spin +1 ) % 360;
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (700, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init();
glutTimerFunc(100, update, 0);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
Langganan:
Postingan (Atom)
