Tutor 1-5
Tutor 1-5
NIM : 1906032
Progranm 1 – 5
Program 4b
// - Try the keyboard callback
// - Reshape callback
// - Timer
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <GL/glut.h>
float z_pos=0.0f;
float rot=0.0f;
void mydisplay(){
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0,0.0f,z_pos);
glRotatef(rot, 0, 0, 1);
glBegin(GL_POLYGON);
glColor3f(0, 1, 0);
glVertex3f(-0.5, -0.5, -5);
glColor3f(0, 0, 1);
glVertex3f(-0.75, 0, -5);
glColor3f(1, 0, 0);
glVertex3f(-0.5, 0.5, -5);
glColor3f(0, 1, 0);
glVertex3f(0, 0.75, -5);
glColor3f(0, 0, 1);
glVertex3f(0.5, 0.5, -5);
glColor3f(1, 0, 0);
glVertex3f(0.75, 0, -5);
glColor3f(0, 1, 0);
glVertex3f(0.5, -0.5, -5);
glColor3f(0, 0, 1);
glVertex3f(0,-0.75, -5);
glEnd();
glFlush();
glutSwapBuffers();
}
void init( void )
{
}
void resize( int w, int h )
{
glViewport( 0, 0, (GLsizei) w, (GLsizei) h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective(45, (GLdouble)w/(GLdouble)h, 0, 100);
glMatrixMode( GL_MODELVIEW );
}
void myTimeOut(int id)
{
// called if timer event
// ...advance the state of animation incrementally...
rot+=10;
glutPostRedisplay(); // request redisplay
glutTimerFunc(100, myTimeOut, 0); // request next timer event
}
void myKeyboard(unsigned char key,int x, int y)
{
if((key=='<')||(key==',')) z_pos-=0.1f;
if((key=='>')||(key=='.')) z_pos+=0.1f;
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
//glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("simple");
// callbacks
glutDisplayFunc(mydisplay);
glutKeyboardFunc(myKeyboard);
glutTimerFunc(100, myTimeOut, 0);
glutReshapeFunc(resize);
init();
glutMainLoop();
}
4a.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <GL/glut.h>
void mydisplay(){
glClear(GL_COLOR_BUFFER_BIT); // Menghapus layar
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
glFlush();
}
void resize( int w, int h )
{
if (w >= h)
glViewport(0, 0, (GLsizei)h, (GLsizei)h) ;
else
glViewport(0, 0, (GLsizei)w, (GLsizei)w) ;
}
int main(int argc, char** argv)
{
printf("Hello World… this is 2D Rectangle");
glutCreateWindow("simple");
glutDisplayFunc(mydisplay);
glutMainLoop();
}
Tutorial 1
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <GL/glut.h>
void mydisplay(){
glClear(GL_COLOR_BUFFER_BIT); // Menghapus layar
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
glFlush();
}
int main(int argc, char** argv)
{
glutCreateWindow("simple");
glutDisplayFunc(mydisplay);
glutMainLoop();
}
Tutorialb2
*Program segitiga
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <GL/glut.h>
void mydisplay(){
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glEnd();
glFlush();
}
int main(int argc, char** argv){
glutCreateWindow("simple");
glutDisplayFunc(mydisplay);
glutMainLoop();
}
Tutorial 3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <GL/glut.h>
void mydisplay(){
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glColor3f(0, 1, 0);
glVertex3f(-0.5, -0.5, 1);
glColor3f(0, 0, 1);
glVertex3f(-0.75, 0, 1);
glColor3f(1, 0, 0);
glVertex3f(-0.5, 0.5, 1);
glColor3f(0, 1, 0);
glVertex3f(0, 0.75, 1);
glColor3f(0, 0, 1);
glVertex3f(0.5, 0.5, -1);
glColor3f(1, 0, 0);
glVertex3f(0.75, 0, -1);
glColor3f(0, 1, 0);
glVertex3f(0.5, -0.5, -1);
glColor3f(0, 0, 1);
glVertex3f(0,-0.75, -1);
glEnd();
glFlush();
}
int main(int argc, char** argv){
glutCreateWindow("simple");
glutDisplayFunc(mydisplay);
glutMainLoop();
}
Tutorial 5
// OpenGL
// - Complex Object
// - Notice:
// 1. There are surfaces that are not correctly rendered in order.
// uncommented the GL_DEPTH
// 2. Flicker can be eliminated by using GL_DOUBLE
//
// Rubah rendering algoritma dengan menggunakan data struktur
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <GL/glut.h>
float z_pos=-10.0f;
float rot=0.0f;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void myTimeOut(int id)
{
// called if timer event
// ...advance the state of animation incrementally...
rot+=10;
glutPostRedisplay(); // request redisplay
glutTimerFunc(100, myTimeOut, 0); // request next timer event
}
void myKeyboard(unsigned char key,int x, int y)
{
if((key=='<')||(key==',')) z_pos-=0.1f;
if((key=='>')||(key=='.')) z_pos+=0.1f;
}
void mydisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT );
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0,0.0f,z_pos);
glRotatef(rot, 0, 1, 0);
glBegin(GL_QUADS);
// Front Face, red
glColor3f(1.0,0.0,0.0);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
// Back Face, green
glColor3f(0.0,1.0,0.0);
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
// Top Face, blue
glColor3f(0.0,0.0,1.0);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, -1.0f);
// Bottom Face, yellow
glColor3f(1.0,1.0,0.0);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face, cyan
glColor3f(0.0,1.0,1.0);
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, 1.0f, -1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
// Left Face, magenta
glColor3f(1.0,0.0,1.0);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd();
glFlush();
glutSwapBuffers();
}
void init()
{
glEnable(GL_DEPTH_TEST);
glClearColor( 0.0, 0.0, 0.0, 1.0 ); // A Background Clear Color
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, (GLdouble)500.0/(GLdouble)500.0, 0, 100);
glMatrixMode(GL_MODELVIEW);
return;
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
//glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPT );
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("simple");
// callbacks
glutDisplayFunc(mydisplay);
glutKeyboardFunc(myKeyboard);
glutTimerFunc(100, myTimeOut, 0);
glutReshapeFunc(resize);
init();
glutMainLoop();
return 0;
}