RSS

RESUME PROGRAM OPENGL

06 Jun

Haritz Cahya Nugraha – 23213344

Program1.cpp

Program pertama akan menunjukkan pembuatan aplikasi sederhana menggunakan GLUT dan OpenGL. Pada kode di atas, program akan membuat sebuah jendela baru dengan memanggil method glutCreateWindow(“simple”)
yang akan membuat sebuah jendela baru dengan judul “simple”. Kemudian program menentukan method yang digunakan untuk menampilkan gambar dengan membuat method myDisplay(), dimana program melakukan fungsi untuk mengupdate layar, dan menjadikannya sebagai metode utama display dengan memanggil method glutDisplayFunc(myDisplay); koordinat vertex ditentukan dengan memanggil method glVertex2F().

#include
<stdio.h>

#include
<stdlib.h>

#include
<string.h>

#include
<stdarg.h>

#include
<glut.h>

//pemanggilan library

void mydisplay(){ //method untuk proses layar

    glClear(GL_COLOR_BUFFER_BIT); //menghapus layar

    glBegin(GL_POLYGON); //mulai menggambar persegi empat

    glVertex2f(-0.5, -0.5); //menentukan vertex

    glVertex2f(-0.5, 0.5);

    glVertex2f(0.5, 0.5);

    glVertex2f(0.5, -0.5);

    glEnd(); //menyelesaikan penentuan vertex

    glFlush(); //menggambar ke layar

}

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

    printf(“Hello World… this is 2D Rectangle”);

    glutCreateWindow(“simple”);

    glutDisplayFunc(mydisplay);

    glutMainLoop();

}

Output

Program2.cpp

Program dibawah merupakan variasi dari program pertama, dimana program kedua tidak menampilkan bentuk segiempat, namun segitiga. Sehingga program kedua ini tidak menggunakan method glBegin(GL_POLYGON); namun menggunakan glBegin(GL_TRIANGLES); di program kedua ini juga ditunjukkan contoh untuk merubah warna yang digunakan dengan memanggil method glColor3f(1,0,1);.

#include
<stdio.h>

#include
<stdlib.h>

#include
<string.h>

#include
<stdarg.h>

#include
<glut.h>

//pemanggilan library yang diberikan

void mydisplay(){

    glClear(GL_COLOR_BUFFER_BIT); //menghapus layar

    glBegin(GL_TRIANGLES); //mulai menggambar bentuk segitiga

    glColor3f(1,0,1); //menentukan warna

    glVertex2f(-0.5, -0.5); //menentukan vertex

    glVertex2f(-0.5, 0.5);

    glVertex2f(0.5, 0.5);

    glEnd();

    glFlush(); //menggambar ke layar

}

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

    glutCreateWindow(“simple”);

    glutDisplayFunc(mydisplay);

    glutMainLoop();

}

Output:

Program3.cpp

Program ketiga merupakan modifikasi lanjutan dari program pertama, dengan menggunakan method glBegin(GL_POLYGON); namun jika di program pertama hanya menggunakan 4 titik vertex untuk membuat persegi empat, maka di program ini kita menentukan 8 titik untuk membuat bidang persegi 8.

#include
<stdio.h>

#include
<stdlib.h>

#include
<string.h>

#include
<stdarg.h>

#include
<glut.h>

//pemanggilan library

void mydisplay(){

glClear(GL_COLOR_BUFFER_BIT);//menghapus layar

glBegin(GL_POLYGON); //mulai membuat gambar poligon

glColor3f(0, 1, 0); //merubah warna menjadi hijau

glVertex2f(-0.5, -0.5); //penentuan vertex

glVertex2f(-0.75, 0);

glVertex2f(-0.5, 0.5);

glVertex2f(0, 0.75);

glVertex2f(0.5, 0.5);

glVertex2f(0.75, 0);

glVertex2f(0.5, -0.5);

glVertex2f(0,-0.75);

glEnd();

glFlush(); //gambar ke layar

}

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

glutCreateWindow(“simple”);

glutDisplayFunc(mydisplay);

glutMainLoop();

}

Program4.cpp

Program dibawah merupakan pengembangan dari program ketiga, dengan koordinat vertex yang membentuk persegi delapan. Program ini mengeksplorasi method glColor3f(R, G, B) untuk penentuan warna. Pada program Kita mencoba merubah warna untuk setiap vertexnya.

#include
<stdio.h>

#include
<stdlib.h>

#include
<string.h>

#include
<stdarg.h>

#include
<glut.h>

//import library

void mydisplay(){

glClear(GL_COLOR_BUFFER_BIT); //bersihkan layar

glBegin(GL_POLYGON); //mulai menggambar poligon

glColor3f(0, 1, 0); //ganti warna menjadi hijau

glVertex2f(-0.5, -0.5); //koordinat vertex 1

glColor3f(0, 0, 1); //ganti warna menjadi biru

glVertex2f(-0.75, 0);

glColor3f(1, 0, 0); //ganti warna jadi merah

glVertex2f(-0.5, 0.5);

glColor3f(0, 1, 0);

glVertex2f(0, 0.75);

glColor3f(0, 0, 1);

glVertex2f(0.5, 0.5);

glColor3f(1, 0, 0);

glVertex2f(0.75, 0);

glColor3f(0, 1, 0);

glVertex2f(0.5, -0.5);

glColor3f(0, 0, 1);

glVertex2f(0,-0.75);

glEnd();

glFlush(); //gambar ke layar

}

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

glutCreateWindow(“simple”);

glutDisplayFunc(mydisplay);

glutMainLoop();

}

Program5.cpp

Program diatas menampilkan hasil yang sama dengan program 4, tetapi program ini menggunakan method glVertex3f(0,-0.75, -1), perbedaan dari penggunaan method glVertex2f dan 3f adalah dari dimensinya, kalau 2f hanya mewakili koordinat 2 dimensi (x dan y), maka 3f menambahkan satu dimensi lagi yaitu kedalaman (x, y, dan z). Namun dikarenakan viewpoint yang tidak menampilkan perbedaan kedalaman dari obyek yang dibuat, sehingga tampilan hasil akhirnya terlihat sama dengan program4.

#include
<stdio.h>

#include
<stdlib.h>

#include
<string.h>

#include
<stdarg.h>

#include
<glut.h>

//import library yang diperlukan

void mydisplay(){

    glClear(GL_COLOR_BUFFER_BIT); //bersihkan layar

    glBegin(GL_POLYGON); //mulai menggambar poligon

    glColor3f(0, 1, 0); //merubah warna

    glVertex3f(-0.5, -0.5, 1); //menentukan vertex

    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(); //gambar ke layar

}

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

    glutCreateWindow(“simple”);

    glutDisplayFunc(mydisplay);

    glutMainLoop();

}

Program6.cpp

Pada program ini, kita meng-override fungsi reshape dari program opengl dengan membuat method void resize( int w, int h ), dan menjadikannya sebagai fungsi yang dijalankan saat jendela program berubah ukurannya dengan memanggil fungsi     glutReshapeFunc(resize);.Tanpa override method resize, maka ukuran obyek di program akan mengikuti ukuran window program.

#include
<stdio.h>

#include
<stdlib.h>

#include
<string.h>

#include
<stdarg.h>

#include
<glut.h>

//include library

void mydisplay(){

    glClear(GL_COLOR_BUFFER_BIT); // Menghapus layar

    glBegin(GL_POLYGON); // Menghapus layar

    glVertex2f(-0.5, -0.5); //penentuan vertex

    glVertex2f(-0.5, 0.5);

    glVertex2f(0.5, 0.5);

    glVertex2f(0.5, -0.5);

    glEnd();

    glFlush(); //gambar ke layar

}

void resize( int w, int h ) //method yang dipanggil saat window berubah

{

    if (w >= h) //jika lebar lebih dari tinggi (landscape)

        glViewport(0, 0, (GLsizei)h, (GLsizei)h) ; //rubah tampilan

    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);

    glutReshapeFunc(resize);

    glutMainLoop();

}


Program7.cpp

Program ini memberikan contoh transformasi bentuk dengan memanggil method glTranslatef(0.0,0.0f,z_pos) dan     glRotatef(rot, 0, 0, 1) kedua method tersebut dipanggil di mydisplay, dan dipanggil secara otomatis menggunakan method glutTimerFunc(100, myTimeOut, 0) yang akan menjalankan method myTimeOut setiap 100 milidetik sekali

#include
<stdio.h>

#include
<stdlib.h>

#include
<string.h>

#include
<stdarg.h>

#include
<glut.h>

//include library

float z_pos=0.0f;

float rot=0.0f;

//variable yang digunakan untuk rotasi

void mydisplay(){

    glClear(GL_COLOR_BUFFER_BIT); //bersihakan layar

    glLoadIdentity();

    glTranslatef(0.0,0.0f,z_pos); //memindahkan posisi obyek

    glRotatef(rot, 0, 0, 1); //merotasi obyek

    glBegin(GL_POLYGON); //membuat obyek poligon

    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(); //tampilkan ke layar

    glutSwapBuffers();

}

void init( void )

{

    glClearColor( 1.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);

}

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){

//method menangani input user untuk memindahkan obyek

    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_DOUBLE|GLUT_RGB);

    glutInitWindowSize(500,500);

    glutInitWindowPosition(0,0);

    glutCreateWindow(“simple”);

    // callbacks

    glutDisplayFunc(mydisplay);

    glutKeyboardFunc(myKeyboard);

    glutTimerFunc(100, myTimeOut, 0);

    glutReshapeFunc(resize);

    init();

    glutMainLoop();

}

Program8.cpp

Program di bawah merupakan pengembangan dari program7.cpp, dan menunjukkan contoh transformasi beserta contoh penentan warna untuk setiap sisi obyek 3D yang dibuat.

#include
<stdio.h>

#include
<stdlib.h>

#include
<string.h>

#include
<stdarg.h>

#include
<glut.h>

//import library

float z_pos=-10.0f;

float rot=0.0f;

//variable untuk posisi dan rotasi

void resize(int width, int height){

//method yang menangani perubahan ukuran window aplikasi

    glViewport(0, 0, width, height);

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    gluPerspective(45.0, (float)width/(float)height, 1.0, 300.0);

    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){

//method untuk menangani input dari pengguna

    if((key==‘<‘)||(key==‘,’)) z_pos-=0.1f;

    if((key==‘>’)||(key==‘.’)) z_pos+=0.1f;

}

void mydisplay(void){

    glClear(GL_COLOR_BUFFER_BIT ); //membersihkan layar

    glLoadIdentity();

    glTranslatef(0.0,0.0f,z_pos);

    glRotatef(rot, 0, 1, 0);

//mengaplikasikan rotasi dan translasi pada obyek

    glBegin(GL_QUADS); //mulai menggambar kubus

    // 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_DEPTH*/ );

    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );

    glutInitWindowSize(500,500);

    glutInitWindowPosition(0,0);

    glutCreateWindow(“simple”);

    // callbacks

    glutDisplayFunc(mydisplay);

    glutKeyboardFunc(myKeyboard);

    glutTimerFunc(100, myTimeOut, 0);

    glutReshapeFunc(resize);

    init();

    glutMainLoop();

    return 0;

}

Program9.cpp

Program ini menunjukkan cara memberi texture pada bangun, dengan memanfaatkan kelas bmp.h dan bmp.cpp serta memanfaatkan library glaux.lib, permasalahan yang timbul adalah ketika pemanggilan method return auxDIBImageLoad(Filename), tetapi tidak terdeteksi oleh visual studio, sehingga perlu diganti menjadi return auxDIBImageLoadA(Filename), serta perlu ditambahkan baris berikut #pragma
comment( lib, “glaux.lib” )
di bagian atas.

#include
<windows.h>

#include
<stdio.h>

#include
<stdlib.h>

#include
<string.h>

#include
<stdarg.h>

#define GLUT_DISABLE_ATEXIT_HACK

#include
<glut.h>

#include
“bmp.h”

float z_pos=-5.0f;

float rot=0.0f;

GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };

GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };

GLfloat LightPosition[]= { 0.0f, 0.0f, 2.0f, 1.0f };

/* array to hold texture handles */

GLuint filter; // Which Filter To Use

GLuint texture[3]; // Storage For 3 Textures

int LoadGLTextures() //Load Bitmaps And Convert To Textures

{

int Status=FALSE; //Status Indicator

AUX_RGBImageRec *TextureImage[1]; // CreateStorage Space For The Texture

memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL

// Load The Bitmap, Check For Errors, If Bitmap’s Not Found Quit

if (TextureImage[0]=LoadBMP(“Crate.bmp”))

{

Status=TRUE;

// Set The Status To TRUE

glGenTextures(3, &texture[0]); //Create Three Textures

// Create Nearest Filtered Texture

glBindTexture(GL_TEXTURE_2D, texture[0]);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);

glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX,

TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

// Create Linear Filtered Texture

glBindTexture(GL_TEXTURE_2D, texture[1]);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX,

TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

// Create MipMapped Texture

glBindTexture(GL_TEXTURE_2D, texture[2]);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);

gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX,

TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

}

if (TextureImage[0]) //If Texture Exists

{

if (TextureImage[0]->data) //If Texture Image Exists

{

free(TextureImage[0]->data); // FreeThe Texture Image Memory

}

free(TextureImage[0]); //Free The Image Structure

}

return Status;

// Return The Status

}

void resize(int width, int height)

{

glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluPerspective(45.0, (float)width/(float)height, 1.0, 300.0);

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;

else
if((key==‘>’)||(key==‘.’)) z_pos+=0.1f;

else
if((key==‘F’)||(key=‘f’))

{

filter+=1;

if (filter>2)

{

filter=0;

}

printf(“filter: %i”,filter);

}

}

void mydisplay(void)

{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

glLoadIdentity();

glTranslatef(0.0,0.0f,z_pos);

glRotatef(rot, 0, 1, 0);

glBindTexture(GL_TEXTURE_2D, texture[filter]);

glBegin(GL_QUADS);

// Front Face

glColor3f(1.0,0.0,0.0);

glNormal3f( 0.0f, 0.0f, 1.0f);

glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);

glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);

glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);

glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);

// Back Face

glNormal3f( 0.0f, 0.0f,-1.0f);

glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);

glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);

glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);

glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);

// Top Face

glNormal3f( 0.0f, 1.0f, 0.0f);

glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);

glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);

glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);

glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);

// Bottom Face

glNormal3f( 0.0f,-1.0f, 0.0f);

glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);

glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);

glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);

glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);

// Right face

glNormal3f( 1.0f, 0.0f, 0.0f);

glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);

glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);

glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);

glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);

// Left Face

glNormal3f(-1.0f, 0.0f, 0.0f);

glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);

glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);

glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);

glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);

glEnd();

glFlush();

glutSwapBuffers();

}

void init()

{

if (!LoadGLTextures()) //Jump To Texture Loading Routine

{

return; //If Texture Didn’t Load Return FALSE

}

glEnable(GL_TEXTURE_2D); //Enable Texture Mapping

glShadeModel(GL_SMOOTH); //Enable Smooth Shading

glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // BlackBackground

glClearDepth(1.0f);

// Depth Buffer Setup

glEnable(GL_DEPTH_TEST); //Enables Depth Testing

glDepthFunc(GL_LEQUAL); //The Type Of Depth Testing To Do

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations

glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); // Setup The Ambient Light

glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); // Setup The Diffuse Light

glLightfv(GL_LIGHT1, GL_POSITION,LightPosition); // Position The Light

glEnable(GL_LIGHT1);

return;

}

int main(int argc, char** argv)

{

glutInit(&argc,argv);

glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );

glutInitWindowSize(500,500);

glutInitWindowPosition(0,0);

glutCreateWindow(“simple”);

// callbacks

glutDisplayFunc(mydisplay);

glutKeyboardFunc(myKeyboard);

glutTimerFunc(100, myTimeOut, 0);

glutReshapeFunc(resize);

init();

glutMainLoop();

return 0;

}

Bmp.h

#include
<glaux.h>

AUX_RGBImageRec *LoadBMP(char *Filename);

Bmp.cpp

#include
<windows.h>

#include
<stdio.h>
// Header File For Standard Input/Output

#include
<GL.h>
// Header File For The OpenGL32 Library

#include
<GLU.h>
// Header File For The GLu32 Library

#include
<GLAUX.h>
// Header File For The Glaux Library

#include
“bmp.h”

#pragma
comment( lib, “glaux.lib” )

// Loads A Bitmap Imag

AUX_RGBImageRec *LoadBMP(char *Filename) {

FILE *File=NULL; //File Handle

// Make Sure A Filename Was Given

if (!Filename){

return NULL;

// If Not Return NULL

}

File=fopen(Filename,“r”); //Check To See If The File Exists

if (File)

// Does The File Exist?

{

fclose(File);

// Close The Handle

return auxDIBImageLoadA(Filename); // LoadThe Bitmap And Return A Pointer

}

return NULL;

// If Load Failed Return NULL

}

Program10.cpp

Program ini merupakan pengembangan dari kelas sebelumnya, menunjukkan cara melakukan transofrmasi pada bangun 3D, penggunaan material untuk tekstur, dan menambahkan fitur lighting dengan warna pada tekstur yang digunakan.

#include
<windows.h>

#include
<stdio.h>

#include
<stdlib.h>

#include
<string.h>

#include
<stdarg.h>

#define GLUT_DISABLE_ATEXIT_HACK

#include
<glut.h>

#include
“bmp.h”

float z_pos=-5.0f;

float rot=0.0f;

GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };

GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };

GLfloat LightPosition[]= { 0.0f, 0.0f, 2.0f, 1.0f };

/* array to hold texture handles */

GLuint filter; // Which Filter To Use

GLuint texture[3]; // Storage For 3 Textures

int LoadGLTextures() //Load Bitmaps And Convert To Textures

{

    int Status=FALSE; //Status Indicator

    AUX_RGBImageRec *TextureImage[1]; // CreateStorage Space For The Texture

    memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL

    // Load The Bitmap, Check For Errors, If Bitmap’s Not Found Quit

    if (TextureImage[0]=LoadBMP(“Crate.bmp”))

    {

        Status=TRUE;

        // Set The Status To TRUE

        glGenTextures(3, &texture[0]); //Create Three Textures

        // Create Nearest Filtered Texture

        glBindTexture(GL_TEXTURE_2D, texture[0]);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);

        glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX,

        TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

        // Create Linear Filtered Texture

        glBindTexture(GL_TEXTURE_2D, texture[1]);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

        glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX,

        TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

        // Create MipMapped Texture

        glBindTexture(GL_TEXTURE_2D, texture[2]);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);

        gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX,

        TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

    }

    if (TextureImage[0]) //If Texture Exists

    {

        if (TextureImage[0]->data) //If Texture Image Exists

        {

            free(TextureImage[0]->data); // FreeThe Texture Image Memory

        }

        free(TextureImage[0]); //Free The Image Structure

    }

    return Status;

    // Return The Status

}

void resize(int width, int height)

{

    glViewport(0, 0, width, height);

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    gluPerspective(45.0, (float)width/(float)height, 1.0, 300.0);

    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;

    else
if((key==‘>’)||(key==‘.’)) z_pos+=0.1f;

    else
if((key==‘F’)||(key=‘f’))

    {

        filter+=1;

        if (filter>2)

        {

            filter=0;

        }

        printf(“filter: %i”,filter);

    }

}

void mydisplay(void)

{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

glLoadIdentity();

glTranslatef(0.0,0.0f,z_pos);

glRotatef(rot, 0, 1, 0);

glEnable(GL_BLEND); // Turn Blending On

glColor3f(1.0,0.0,0.0); // Blending Color

glBindTexture(GL_TEXTURE_2D, texture[filter]);

glBegin(GL_QUADS);

// Front Face

glNormal3f( 0.0f, 0.0f, 1.0f);

glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);

glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);

glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);

glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);

// Back Face

glColor3f(0.0,1.0,0.0); // Blending Color

glNormal3f( 0.0f, 0.0f,-1.0f);

glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);

glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);

glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);

glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);

// Top Face

glColor3f(0.0,0.0,1.0); // Blending Color

glNormal3f( 0.0f, 1.0f, 0.0f);

glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);

glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);

glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);

glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);

// Bottom Face

glColor3f(1.0,1.0,0.0); // Blending Color

glNormal3f( 0.0f,-1.0f, 0.0f);

glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);

glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);

glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);

glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);

// Right face

glColor3f(1.0,1.0,1.0); // Blending Color

glNormal3f( 1.0f, 0.0f, 0.0f);

glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);

glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);

glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);

glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);

// Left Face

glColor3f(1.0,0.0,1.0); // Blending Color

glNormal3f(-1.0f, 0.0f, 0.0f);

glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);

glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);

glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);

glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);

glEnd();

glFlush();

glutSwapBuffers();

}

void init()

{

    if (!LoadGLTextures()) //Jump To Texture Loading Routine

    {

        return; //If Texture Didn’t Load Return FALSE

    }

    glEnable(GL_TEXTURE_2D); //Enable Texture Mapping

    glShadeModel(GL_SMOOTH); //Enable Smooth Shading

    glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // BlackBackground

    glClearDepth(1.0f);

    // Depth Buffer Setup

    glEnable(GL_DEPTH_TEST); //Enables Depth Testing

    glDepthFunc(GL_LEQUAL); //The Type Of Depth Testing To Do

    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations

    glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); // Setup The Ambient Light

    glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); // Setup The Diffuse Light

    glLightfv(GL_LIGHT1, GL_POSITION,LightPosition); // Position The Light

    glEnable(GL_LIGHT1);

    return;

}

int main(int argc, char** argv)

{

    glutInit(&argc,argv);

    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );

    glutInitWindowSize(500,500);

    glutInitWindowPosition(0,0);

    glutCreateWindow(“simple”);

    // callbacks

    glutDisplayFunc(mydisplay);

    glutKeyboardFunc(myKeyboard);

    glutTimerFunc(100, myTimeOut, 0);

    glutReshapeFunc(resize);

    init();

    glutMainLoop();

    return 0;

}

Program11.cpp

Program ini menunjukkan fitur tranparansi (alpha channel) pada sebuah bangun, untuk mengaktifkan fitur transparansi, anda harus memanggil method glEnable(GL_BLEND); dan mendisable fitur depth test glEnable(GL_DEPTH_TEST); kemudian untuk menentukan ketebalan transparansi dapat dilakukan saat memanggil fungsi glColor4f(1.0f, 1.0f, 1.0f, 1.0), dimana parameter ke 4 merupakan nilai untuk transparansi dari objek kubus tersebut.

// OpenGL

// – Function to load bitmap

// – Texture Mapping Magnification Filter

// filter=0 –> Nearest Filtered Texture

// filter=1 –> Linear Interpolation Texture

// filter=2 –> Mipmapped Texture

#include
<windows.h>

#include
<stdio.h>

#include
<stdlib.h>

#include
<string.h>

#include
<stdarg.h>

#include
<glut.h>

#include
“bmp.h”

float z_pos=-5.0f;

float rot=0.0f;

GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };

GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };

GLfloat LightPosition[]= { 0.0f, 0.0f, 2.0f, 1.0f };

/* array to hold texture handles */

GLuint filter; // Which Filter To Use

GLuint texture[3]; // Storage For 3 Textures

int LoadGLTextures() //Load Bitmaps And Convert To Textures

{

    int Status=FALSE; // Status Indicator

    AUX_RGBImageRec *TextureImage[1]; // Create Storage Space For The Texture

    memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL

    // Load The Bitmap, Check For Errors, If Bitmap’s Not Found Quit

    if (TextureImage[0]=LoadBMP(“crate.bmp”)) // manggil gambar

    {

        Status=TRUE;

        // Set The Status To TRUE

        glGenTextures(3, &texture[0]); //Create Three Textures

        // Create Nearest Filtered Texture

        glBindTexture(GL_TEXTURE_2D, texture[0]);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);

        glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX,

        TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

        // Create Linear Filtered Texture

        glBindTexture(GL_TEXTURE_2D, texture[1]);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

        glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX,

        TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);


        // Create MipMapped Texture

        glBindTexture(GL_TEXTURE_2D, texture[2]);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);

        gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX,

        TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

    }

    if (TextureImage[0]) //If Texture Exists

    {

        if (TextureImage[0]->data) //If Texture Image Exists

        {

            free(TextureImage[0]->data); // FreeThe Texture Image Memory

        }

        free(TextureImage[0]); //Free The Image Structure

    }

    return Status;

    // Return The Status

}

void resize(int width, int height)

{

    glViewport(0, 0, width, height);

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    gluPerspective(45.0, (float)width/(float)height, 1.0, 300.0);

    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;

    else
if((key==‘>’)||(key==‘.’)) z_pos+=0.1f;

    else
if((key==‘F’)||(key=‘f’))

    {

        filter+=1;

        if (filter>2)

        {

            filter=0;

        }

        printf(“filter: %i”,filter);

    }

}

void mydisplay(void) {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

    glLoadIdentity();

    glTranslatef(0.0,0.0f,z_pos);

    glRotatef(rot, 0, 1, 0);

    glBindTexture(GL_TEXTURE_2D, texture[filter]);

    glDisable(GL_BLEND); // Turn Blending On

    glEnable(GL_DEPTH_TEST); // Turn Depth Testing Off


    // Front Face

    glEnable(GL_BLEND);

    glColor4f(1.0f, 1.0f, 1.0f, 1.0);

    glBegin(GL_QUADS);

    glNormal3f( 0.0f, 0.0f, 1.0f);

    glTexCoord2f(0.0f, 0.0f);

    glVertex3f(-1.0f, -1.0f, 1.0f);

    glTexCoord2f(1.0f, 0.0f);

    glVertex3f( 1.0f, -1.0f, 1.0f);

    glTexCoord2f(1.0f, 1.0f);

    glVertex3f( 1.0f, 1.0f, 1.0f);

    glTexCoord2f(0.0f, 1.0f);

    glVertex3f(-1.0f, 1.0f, 1.0f);


    glEnable(GL_BLEND);

    glColor4f(1.0f, 1.0f, 1.0f, 1.0);

    // Back Face

    glNormal3f( 0.0f, 0.0f,-1.0f);

    glTexCoord2f(1.0f, 0.0f);

    glVertex3f(-1.0f, -1.0f, -1.0f);

    glTexCoord2f(1.0f, 1.0f);

    glVertex3f(-1.0f, 1.0f, -1.0f);

    glTexCoord2f(0.0f, 1.0f);

    glVertex3f( 1.0f, 1.0f, -1.0f);

    glTexCoord2f(0.0f, 0.0f);

    glVertex3f( 1.0f, -1.0f, -1.0f);


    // Top Face

    glNormal3f( 0.0f, 1.0f, 0.0f);

    glTexCoord2f(0.0f, 1.0f);

    glVertex3f(-1.0f, 1.0f, -1.0f);

    glTexCoord2f(0.0f, 0.0f);

    glVertex3f(-1.0f, 1.0f, 1.0f);

    glTexCoord2f(1.0f, 0.0f);

    glVertex3f( 1.0f, 1.0f, 1.0f);

    glTexCoord2f(1.0f, 1.0f);

    glVertex3f( 1.0f, 1.0f, -1.0f);


    // Bottom Face

    glNormal3f( 0.0f,-1.0f, 0.0f);

    glTexCoord2f(1.0f, 1.0f);

    glVertex3f(-1.0f, -1.0f, -1.0f);

    glTexCoord2f(0.0f, 1.0f);

    glVertex3f( 1.0f, -1.0f, -1.0f);

    glTexCoord2f(0.0f, 0.0f);

    glVertex3f( 1.0f, -1.0f, 1.0f);

    glTexCoord2f(1.0f, 0.0f);

    glVertex3f(-1.0f, -1.0f, 1.0f);


    // Right face

    glNormal3f( 1.0f, 0.0f, 0.0f);

    glTexCoord2f(1.0f, 0.0f);

    glVertex3f( 1.0f, -1.0f, -1.0f);

    glTexCoord2f(1.0f, 1.0f);

    glVertex3f( 1.0f, 1.0f, -1.0f);

    glTexCoord2f(0.0f, 1.0f);

    glVertex3f( 1.0f, 1.0f, 1.0f);

    glTexCoord2f(0.0f, 0.0f);

    glVertex3f( 1.0f, -1.0f, 1.0f);


    // Left Face

    glNormal3f(-1.0f, 0.0f, 0.0f);

    glTexCoord2f(0.0f, 0.0f);

    glVertex3f(-1.0f, -1.0f, -1.0f);

    glTexCoord2f(1.0f, 0.0f);

    glVertex3f(-1.0f, -1.0f, 1.0f);

    glTexCoord2f(1.0f, 1.0f);

    glVertex3f(-1.0f, 1.0f, 1.0f);

    glTexCoord2f(0.0f, 1.0f);

    glVertex3f(-1.0f, 1.0f, -1.0f);


    glEnd();

    glFlush();

    glutSwapBuffers();

}

void init()

{

    if (!LoadGLTextures())

    // Jump To Texture Loading Routine

    { return;

        // If Texture Didn’t Load Return FALSE

    }


    glEnable(GL_TEXTURE_2D); // Enable Texture Mapping

    glShadeModel(GL_SMOOTH); // Enable Smooth Shading

    glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background

    glClearDepth(1.0f); // Depth Buffer Setup

    glEnable(GL_DEPTH_TEST); // Enables Depth Testing

    glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do

    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations

    glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); // Setup The Ambient Light

    glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); // Setup The Diffuse Light

    glLightfv(GL_LIGHT1, GL_POSITION,LightPosition); // Position The Light

    glEnable(GL_LIGHT1); glColor4f(1.0f, 1.0f, 1.0f, 0.5); // Full Brightness. 50% Alpha

    glBlendFunc(GL_SRC_ALPHA,GL_ONE);

    return;

}

int main(int argc, char** argv)

{

    glutInit(&argc,argv);

    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );

    glutInitWindowSize(500,500);

    glutInitWindowPosition(0,0);

    glutCreateWindow(“simple”);

    // callbacks

    glutDisplayFunc(mydisplay);

    glutKeyboardFunc(myKeyboard);

    glutTimerFunc(100, myTimeOut, 0);

    glutReshapeFunc(resize);

    init();

    glutMainLoop();

    return 0;

}

Program12.cpp

Program ini menunjukkan cara penggunaan fitur kabut (fog) pada OpenGL, method-method yang digunakan yaitu:

glClearColor(0.5f,0.5f,0.5f,1.0f); // Menghapus warna di kabut yang ada

glFogi(GL_FOG_MODE, fogMode[fogfilter]); // Menggunakan kabut

glFogfv(GL_FOG_COLOR, fogColor); // Menentukan warna kabut

glFogf(GL_FOG_DENSITY, 0.35f); // Seberapa tebal kabut

glHint(GL_FOG_HINT, GL_DONT_CARE); // Fog Hint Value

glFogf(GL_FOG_START, 100.0f); // Menentukan kedalaman kabut

glFogf(GL_FOG_END, 1000.0f); // Menentukan kedalaman kabut

glEnable(GL_FOG); // Mengaktifkan kabut

// OpenGL

// – Fog Filter

// fogfilter=0 –> Nearest Filtered Texture

// fogfilter=1 –> Linear Interpolation Texture

// fogfilter=2 –> Mipmapped Texture

#include
<windows.h>

#include
<stdio.h>

#include
<stdlib.h>

#include
<string.h>

#include
<stdarg.h>

#include
<glut.h>

#include
“bmp.h”

float z_pos=-5.0f;

float rot=0.0f;

GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };

GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };

GLfloat LightPosition[]= { 0.0f, 0.0f, 2.0f, 1.0f };

/* array to hold texture handles */

GLuint filter; // Which Filter To Use

GLuint texture[3]; // Storage For 3 Textures

// Fog

bool gp;

GLuint fogfilter;

GLuint fogMode[]={GL_EXP, GL_EXP2, GL_LINEAR};

GLfloat fogColor[4]={0.5f, 0.5f, 0.5f, 1.0f};

int LoadGLTextures() //Load Bitmaps And Convert To Textures

{

    int Status=FALSE; //Status Indicator

    AUX_RGBImageRec *TextureImage[1]; // Create Storage Space For The Texture

    memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL

    // Load The Bitmap, Check For Errors, If Bitmap’s Not Found Quit

    if (TextureImage[0]=LoadBMP(“crate.bmp”))

    {

        Status=TRUE;

        // Set The Status To TRUE

        glGenTextures(3, &texture[0]); //Create Three Textures

        // Create Nearest Filtered Texture

        glBindTexture(GL_TEXTURE_2D, texture[0]);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);

        glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX,

        TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

        // Create Linear Filtered Texture

        glBindTexture(GL_TEXTURE_2D, texture[1]);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

        glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX,

        TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

        // Create MipMapped Texture

        glBindTexture(GL_TEXTURE_2D, texture[2]);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);

        gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX,

        TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

    }

    if (TextureImage[0]) //If Texture Exists

    {

        if (TextureImage[0]->data) //If Texture Image Exists

        {

            free(TextureImage[0]->data); // Free The Texture Image Memory

        }

        free(TextureImage[0]); //Free The Image Structure

    }

    return Status;

    // Return The Status

}

void resize(int width, int height)

{

    glViewport(0, 0, width, height);

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    gluPerspective(45.0, (float)width/(float)height, 1.0, 300.0);

    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();

}

void myTimeOut(int id)

{

    // called if timer event

    // …advance the state of animation incrementally…

    rot+=1;

    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;

    else
if((key==‘>’)||(key==‘.’)) z_pos+=0.1f;

    else
if((key==‘F’)||(key=‘f’))

    {

        filter+=1;

        if (filter>2)

        {

            filter=0;

        }

        printf(“filter: %i \n”,filter);

    }

    else
if( (key==‘G’) || (key==‘g’))

    {

        if(gp==FALSE)

        {

            gp=TRUE;

            fogfilter+=1;

            if (fogfilter>2) // Is fogfilter Greater Than 2?

            {

                fogfilter=0; // If So, Set fogfilter To Zero

            }

            glFogi (GL_FOG_MODE, fogMode[fogfilter]); // Fog Mode

        } else

        {

            gp=FALSE;

        }

        printf(“filter: %i \n”,fogfilter);

    }

}

void mydisplay(void)

{

    glEnable(GL_DEPTH_TEST);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

    glLoadIdentity();

    glTranslatef(0.0,0.0f,z_pos);

    glRotatef(rot, 0, 1, 0);

    glBindTexture(GL_TEXTURE_2D, texture[filter]);

    glBegin(GL_QUADS);

    // Front Face

    glNormal3f( 0.0f, 0.0f, 1.0f);

    glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);

    glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);

    glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);

    glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);

    // Back Face

    glNormal3f( 0.0f, 0.0f,-1.0f);

    glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);

    glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);

    glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);

    glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);

    // Top Face

    glNormal3f( 0.0f, 1.0f, 0.0f);

    glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);

    glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);

    glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);

    glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);

    // Bottom Face

    glNormal3f( 0.0f,-1.0f, 0.0f);

    glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);

    glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);

    glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);

    glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);

    // Right face

    glNormal3f( 1.0f, 0.0f, 0.0f);

    glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);

    glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);

    glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);

    glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);

    // Left Face

    glNormal3f(-1.0f, 0.0f, 0.0f);

    glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);

    glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);

    glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);

    glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);

    glEnd();

    glFlush();

    glutSwapBuffers();

}

void init()

{

    if (!LoadGLTextures()) //Jump To Texture Loading Routine

    {

        return; //If Texture Didn’t Load Return FALSE

    }

    glEnable(GL_TEXTURE_2D); //Enable Texture Mapping

    glShadeModel(GL_SMOOTH); //Enable Smooth Shading

    // FOG

    glClearColor(0.5f,0.5f,0.5f,1.0f); // We’ll Clear To The Color Of The Fog

    glFogi(GL_FOG_MODE, fogMode[fogfilter]); // Fog Mode

    glFogfv(GL_FOG_COLOR, fogColor); // Set Fog Color

    glFogf(GL_FOG_DENSITY, 0.35f); // How Dense Will The Fog Be

    glHint(GL_FOG_HINT, GL_DONT_CARE); // Fog Hint Value

    glFogf(GL_FOG_START, 100.0f); // Fog Start Depth

    glFogf(GL_FOG_END, 1000.0f); // Fog End Depth

    glEnable(GL_FOG);

    glClearDepth(1.0f);

    // Depth Buffer Setup

    glEnable(GL_DEPTH_TEST); //Enables Depth Testing

    glDepthFunc(GL_LEQUAL); //The Type Of Depth Testing To Do

    glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); // Setup The Ambient Light

    glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); // Setup The Diffuse Light

    glLightfv(GL_LIGHT1, GL_POSITION,LightPosition); // Position The Light

    glEnable(GL_LIGHT1);

    return;

}

int main(int argc, char** argv)

{

    glutInit(&argc,argv);

    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );

    glutInitWindowSize(500,500);

    glutInitWindowPosition(0,0);

    glutCreateWindow(“simple”);

    // callbacks

    glutDisplayFunc(mydisplay);

    glutKeyboardFunc(myKeyboard);

    glutTimerFunc(100, myTimeOut, 0);

    glutReshapeFunc(resize);

    init();

    glutMainLoop();

    return 0;

}

Program13.cpp

Program ini akan me-load dunia dari daftar vertex yang tersimpan dalam sebuah textfile, serta kemudian memberikan texture ke bentuk tersebut.

#include
<windows.h>

#include
<math.h>
// Math Library Header File

#include
<stdio.h>

#include
<stdlib.h>

#include
<string.h>

#include
<stdarg.h>

#include
<glut.h>

#include
<glaux.h>

#pragma
comment (lib, “glaux.lib”)

const
float piover180 = 0.0174532925f;

float heading;

float xpos;

float zpos;

GLfloat yrot; // Y Rotation

GLfloat walkbias = 0;

GLfloat walkbiasangle = 0;

GLfloat lookupdown = 0.0f;

GLfloat z=0.0f; // Depth Into The Screen

GLuint filter; // Which Filter To Use

GLuint texture[3]; // Storage For 3 Textures

typedef
struct tagVERTEX

{

    float x, y, z;

    float u, v;

} VERTEX;

typedef
struct tagTRIANGLE

{

    VERTEX vertex[3];

} TRIANGLE;

typedef
struct tagSECTOR

{

    int numtriangles;

    TRIANGLE* triangle;

} SECTOR;

SECTOR sector1; // Our Model Goes Here:

void readstr(FILE *f,char *string)

{

    do

    {

        fgets(string, 255, f);

    } while ((string[0] == ‘/’) || (string[0] == ‘\n’));

    return;

}

void SetupWorld()

{

    float x, y, z, u, v;

    int numtriangles;

    FILE *filein;

    char oneline[255];

    filein = fopen(“world.txt”, “rt”); // File To Load World Data From

    readstr(filein,oneline);

    sscanf(oneline, “NUMPOLLIES %d\n”, &numtriangles);

    sector1.triangle = new TRIANGLE[numtriangles];

    sector1.numtriangles = numtriangles;

    for (int loop = 0; loop < numtriangles; loop++)

    {

        for (int vert = 0; vert < 3; vert++)

        {

            readstr(filein,oneline);

            sscanf(oneline, “%f %f %f %f %f”, &x, &y, &z, &u, &v);

            sector1.triangle[loop].vertex[vert].x = x;

            sector1.triangle[loop].vertex[vert].y = y;

            sector1.triangle[loop].vertex[vert].z = z;

            sector1.triangle[loop].vertex[vert].u = u;

            sector1.triangle[loop].vertex[vert].v = v;

        }

    }

    fclose(filein);

    return;

}

AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image

{

    FILE *File=NULL; // File Handle

    if (!Filename) // Make Sure A Filename Was Given

    {

        return NULL; // If Not Return NULL

    }

    File=fopen(Filename,“r”); // Check To See If The File Exists

    if (File) // Does The File Exist?

    {

        fclose(File); // Close The Handle

        return auxDIBImageLoadA(Filename); // Load The Bitmap And Return A Pointer

    }

    return NULL; // If Load Failed Return NULL

}

int LoadGLTextures() // Load Bitmaps And Convert To Textures

{

    int Status=FALSE; // Status Indicator

    AUX_RGBImageRec *TextureImage[1]; // Create Storage Space For The Texture

    memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL

    // Load The Bitmap, Check For Errors, If Bitmap’s Not Found Quit

    if (TextureImage[0]=LoadBMP(“mud.bmp”))

    {

        Status=TRUE; // Set The Status To TRUE

        glGenTextures(3, &texture[0]); // Create Three Textures

        // Create Nearest Filtered Texture

        glBindTexture(GL_TEXTURE_2D, texture[0]);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);

        glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX,

        TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

        // Create Linear Filtered Texture

        glBindTexture(GL_TEXTURE_2D, texture[1]);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

        glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX,

        TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

        // Create MipMapped Texture

        glBindTexture(GL_TEXTURE_2D, texture[2]);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);

        gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX,

        TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

    }

    if (TextureImage[0]) // If Texture Exists

    {

        if (TextureImage[0]->data) // If Texture Image Exists

        {

            free(TextureImage[0]->data); // Free The Texture Image Memory

        }

        free(TextureImage[0]); // Free The Image Structure

    }

    return Status; // Return The Status

}

void resize(int width, int height) // Resize And Initialize The GL Window

{

    if (height==0)

    // Prevent A Divide By Zero By

    {

        height=1;

        // Making Height Equal One

    }

    glViewport(0,0,width,height); // Reset The Current Viewport

    glMatrixMode(GL_PROJECTION); // Select The Projection Matrix

    glLoadIdentity(); //Reset The Projection Matrix

    // Calculate The Aspect Ratio Of The Window

    gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

    glMatrixMode(GL_MODELVIEW); //Select The Modelview Matrix

    glLoadIdentity(); //Reset The Modelview Matrix

}

void init() // All Setup For OpenGL Goes Here

{

    if (!LoadGLTextures()) //Jump To Texture Loading Routine

    {

        return; //If Texture Didn’t Load Return FALSE

    }

    glEnable(GL_TEXTURE_2D); //Enable Texture Mapping

    glBlendFunc(GL_SRC_ALPHA,GL_ONE); // SetThe Blending Function For Translucency

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black

    glClearDepth(1.0);

    // Enables Clearing Of The Depth Buffer

    glDepthFunc(GL_LESS); //The Type Of Depth Test To Do

    glEnable(GL_DEPTH_TEST); //Enables Depth Testing

    glShadeModel(GL_SMOOTH); //Enables Smooth Color Shading

    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations

    SetupWorld();

    return; //Initialization Went OK

}

void myTimeOut(int id)

{

    // called if timer event

    // …advance the state of animation incrementally…

    //rot+=1;

    glutPostRedisplay(); // request redisplay

    glutTimerFunc(100, myTimeOut, 0); // request next timer event

}

void myKeyboard(unsigned
char key,int x, int y)

{

}

void mySpecialKeyboard(int key,int x, int y)

{

    if(key==GLUT_KEY_UP)

    {

        xpos -= (float)sin(heading*piover180) * 0.05f;

        zpos -= (float)cos(heading*piover180) * 0.05f;

        if (walkbiasangle >= 359.0f)

        {

            walkbiasangle = 0.0f;

        }

        else

        {

            walkbiasangle+= 10;

        }

        walkbias = (float)sin(walkbiasangle * piover180)/20.0f;

    }

    else
if(key==GLUT_KEY_DOWN)

    {

        xpos += (float)sin(heading*piover180) * 0.05f;

        zpos += (float)cos(heading*piover180) * 0.05f;

        if (walkbiasangle <= 1.0f)

        {

            walkbiasangle = 359.0f;

        }

        else

        {

            walkbiasangle-= 10;

        }

        walkbias = (float)sin(walkbiasangle * piover180)/20.0f;

    }

    else
if(key==GLUT_KEY_RIGHT)

    {

        heading -= 1.0f;

        yrot = heading;

    }

    else
if(key==GLUT_KEY_LEFT)

    {

        heading += 1.0f;

        yrot = heading;

    }

    else
if(key==GLUT_KEY_PAGE_UP)

    {

        z-=0.02f;

        lookupdown-= 1.0f;

    }

    else
if(key==GLUT_KEY_PAGE_DOWN)

    {

        z+=0.02f;

        lookupdown+= 1.0f;

    }

}

void mydisplay(void)

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer

    glLoadIdentity(); //Reset The View

    GLfloat x_m, y_m, z_m, u_m, v_m;

    GLfloat xtrans = -xpos;

    GLfloat ztrans = -zpos;

    GLfloat ytrans = -walkbias-0.25f;

    GLfloat sceneroty = 360.0f – yrot;

    int numtriangles;

    glRotatef(lookupdown,1.0f,0,0);

    glRotatef(sceneroty,0,1.0f,0);

    glTranslatef(xtrans, ytrans, ztrans);

    glBindTexture(GL_TEXTURE_2D, texture[filter]);

    numtriangles = sector1.numtriangles;

    // Process Each Triangle

    for (int loop_m = 0; loop_m < numtriangles; loop_m++)

    {

        glBegin(GL_TRIANGLES);

        glNormal3f( 0.0f, 0.0f, 1.0f);

        x_m = sector1.triangle[loop_m].vertex[0].x;

        y_m = sector1.triangle[loop_m].vertex[0].y;

        z_m = sector1.triangle[loop_m].vertex[0].z;

        u_m = sector1.triangle[loop_m].vertex[0].u;

        v_m = sector1.triangle[loop_m].vertex[0].v;

        glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);

        x_m = sector1.triangle[loop_m].vertex[1].x;

        y_m = sector1.triangle[loop_m].vertex[1].y;

        z_m = sector1.triangle[loop_m].vertex[1].z;

        u_m = sector1.triangle[loop_m].vertex[1].u;

        v_m = sector1.triangle[loop_m].vertex[1].v;

        glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);

        x_m = sector1.triangle[loop_m].vertex[2].x;

        y_m = sector1.triangle[loop_m].vertex[2].y;

        z_m = sector1.triangle[loop_m].vertex[2].z;

        u_m = sector1.triangle[loop_m].vertex[2].u;

        v_m = sector1.triangle[loop_m].vertex[2].v;

        glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);

        glEnd();

    }

    glFlush();

    glutSwapBuffers();

}

int main(int argc, char** argv)

{

    glutInit(&argc,argv);

    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );

    glutInitWindowSize(500,500);

    glutInitWindowPosition(0,0);

    glutCreateWindow(“simple”);

    // callbacks

    glutDisplayFunc(mydisplay);

    glutKeyboardFunc(myKeyboard);

    glutSpecialFunc(mySpecialKeyboard);

    glutTimerFunc(100, myTimeOut, 0);

    glutReshapeFunc(resize);

    init();

    glutMainLoop();

    return 0;

}

Program 14

Program ini mendemonstrasikan cara mengambil model dari file .3ds serta memberikan texture , file tersebut di load menggunakan file 3dsloader.cpp

Main.cpp

#include
<windows.h>

#include
<glut.h>

#include
“tutorial14.h”

#include
“texture.h”

#include
“3dsloader.h”

// The width and height of your window, change them as you like

int screen_width=640;

int screen_height=480;

// Absolute rotation values (0-359 degrees) and rotation increments for each frame

double rotation_x=0, rotation_x_increment=0.1;

double rotation_y=0, rotation_y_increment=0.05;

double rotation_z=0, rotation_z_increment=0.03;

// Flag for rendering as lines or filled polygons

int filling=1; //0=OFF 1=ON

//Now the object is generic, the cube has annoyed us a little bit, or not?

obj_type object;

void init(void)

{

glClearColor(0.0, 0.0, 0.0, 0.0); // This clear the background color to black

glShadeModel(GL_SMOOTH); // Type of shading for the polygons

// Viewport transformation

glViewport(0,0,screen_width,screen_height);

// Projection transformation

glMatrixMode(GL_PROJECTION); // Specifies which matrix stack is the target for matrix operations

glLoadIdentity(); // We initialize the projection matrix as identity

gluPerspective(45.0f,(GLfloat)screen_width/(GLfloat)screen_height,10.0f, 10000.0f); // We define the “viewing volume”

glEnable(GL_DEPTH_TEST); // We enable the depth test (also called z buffer)

glPolygonMode (GL_FRONT_AND_BACK, GL_FILL); // Polygon rasterization mode (polygon filled)

glEnable(GL_TEXTURE_2D); // This Enable the Texture mapping

Load3DS (&object,“spaceship.3ds”);

object.id_texture=LoadBitmap(“spaceshiptexture.bmp”); // The Function LoadBitmap() return the current texture ID

// If the last function returns -1 it means the file was not found so we exit from the program

if (object.id_texture==-1)

{

MessageBox(NULL,“Image file: spaceshiptexture.bmp not found”, “Zetadeck”, MB_OK | MB_ICONERROR);

exit (0);

}

}

void resize (int width, int height)

    {

screen_width=width; // We obtain the new screen width values and store it

screen_height=height; // Height value

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // We clear both the color and the depth buffer so to draw the next frame

glViewport(0,0,screen_width,screen_height); // Viewport transformation

glMatrixMode(GL_PROJECTION); // Projection transformation

glLoadIdentity(); // We initialize the projection matrix as identity

gluPerspective(45.0f,(GLfloat)screen_width/(GLfloat)screen_height,10.0f,

10000.0f);

glutPostRedisplay (); // This command redraw the scene (it calls the same routine of glutDisplayFunc)

}

void keyboard (unsigned
char key, int x, int y)

{

switch (key)

{

case
‘ ‘:

rotation_x_increment=0;

rotation_y_increment=0;

rotation_z_increment=0;

break;

case
‘r’: case
‘R’:

if (filling==0)

{

glPolygonMode (GL_FRONT_AND_BACK, GL_FILL); // Polygon rasterization mode (polygon filled)

filling=1;

}

else

{

glPolygonMode (GL_FRONT_AND_BACK, GL_LINE); // Polygon rasterization mode (polygon outlined)

filling=0;

}

break;

}

}

void keyboard_s (int key, int x, int y)

{

switch (key)

{

case GLUT_KEY_UP:

rotation_x_increment = rotation_x_increment +0.005;

break;

case GLUT_KEY_DOWN:

    rotation_x_increment = rotation_x_increment -0.005;

break;

case GLUT_KEY_LEFT:

rotation_y_increment = rotation_y_increment +0.005;

break;

case GLUT_KEY_RIGHT:

rotation_y_increment = rotation_y_increment -0.005;

break;

}

}

void display(void)

{

int l_index;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW); // Modeling transformation

glLoadIdentity(); // Initialize the model matrix as identity

glTranslatef(0.0,0.0,-300); // We move the object forward (the model matrix is multiplied by the translation matrix)

rotation_x = rotation_x + rotation_x_increment;

rotation_y = rotation_y + rotation_y_increment;

rotation_z = rotation_z + rotation_z_increment;

if (rotation_x > 359) rotation_x = 0;

if (rotation_y > 359) rotation_y = 0;

if (rotation_z > 359) rotation_z = 0;

glRotatef(rotation_x,1.0,0.0,0.0); // Rotations of the object (the model matrix is multiplied by the rotation matrices)

glRotatef(rotation_y,0.0,1.0,0.0);

glRotatef(rotation_z,0.0,0.0,1.0);

glBindTexture(GL_TEXTURE_2D, object.id_texture); // We set the active texture

glBegin(GL_TRIANGLES); // glBegin and glEnd delimit the vertices that define a primitive (in our case triangles)

for (l_index=0;l_index<object.polygons_qty;l_index++)

{

//—————– FIRST VERTEX —————–

// Texture coordinates of the first vertex

glTexCoord2f( object.mapcoord[ object.polygon[l_index].a ].u,

object.mapcoord[ object.polygon[l_index].a ].v);

// Coordinates of the first vertex

glVertex3f( object.vertex[ object.polygon[l_index].a ].x,

object.vertex[ object.polygon[l_index].a ].y,

object.vertex[ object.polygon[l_index].a ].z);

//Vertex definition

//—————– SECOND VERTEX —————–

// Texture coordinates of the second vertex

glTexCoord2f( object.mapcoord[ object.polygon[l_index].b ].u,

object.mapcoord[ object.polygon[l_index].b ].v);

// Coordinates of the second vertex

glVertex3f( object.vertex[ object.polygon[l_index].b ].x,

object.vertex[ object.polygon[l_index].b ].y,

object.vertex[ object.polygon[l_index].b ].z);

//—————– THIRD VERTEX —————–

// Texture coordinates of the third vertex

glTexCoord2f( object.mapcoord[ object.polygon[l_index].c ].u,

object.mapcoord[ object.polygon[l_index].c ].v);

// Coordinates of the Third vertex

glVertex3f( object.vertex[ object.polygon[l_index].c ].x,

object.vertex[ object.polygon[l_index].c ].y,

object.vertex[ object.polygon[l_index].c ].z);

}

glEnd();

glFlush(); // This force the execution of OpenGL commands

glutSwapBuffers(); // In double buffered mode we invert the positions of the visible buffer and the writing buffer

}

int main(int argc, char **argv)

{

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

glutInitWindowSize(screen_width,screen_height);

glutInitWindowPosition(0,0);

glutCreateWindow(“simple”);

glutDisplayFunc(display);

glutIdleFunc(display);

glutReshapeFunc (resize);

glutKeyboardFunc (keyboard);

glutSpecialFunc (keyboard_s);

init();

glutMainLoop();

return(0);

}

Main.h

#define MAX_VERTICES 8000 // Max number of vertices (for each object)

#define MAX_POLYGONS 8000 // Max number of polygons (for each object)

// Our vertex type

typedef
struct{

float x,y,z;

}vertex_type;

// The polygon (triangle), 3 numbers that aim 3 vertices

typedef
struct{

int a,b,c;

}polygon_type;

// The mapcoord type, 2 texture coordinates for each vertex

typedef
struct{

float u,v;

}mapcoord_type;

// The object type

typedef
struct {

char name[20];

int vertices_qty;

int polygons_qty;

vertex_type vertex[MAX_VERTICES];

polygon_type polygon[MAX_POLYGONS];

mapcoord_type mapcoord[MAX_VERTICES];

int id_texture;

} obj_type, *obj_type_ptr;

3dsloader.h

/**********************************************************

*

* FUNCTION Load3DS (obj_type_ptr, char *)

*

* This function loads a mesh from a 3ds file.

* Please note that we are loading only the vertices, polygons and

mapping lists.

* If you need to load meshes with advanced features as for example:

* multi objects, materials, lights and so on, you must insert other

chunk parsers.

*

*********************************************************/

extern
char Load3DS (obj_type_ptr ogg, char *filename);

3dsloader.cpp

#include
<stdio.h>

#include
<stdlib.h>

#include
<conio.h>

#include
<io.h>

#include
“tutorial14.h”

#include
“3dsloader.h”

/**********************************************************

*

* FUNCTION Load3DS (obj_type_ptr, char *)

*

* This function loads a mesh from a 3ds file.

* Please note that we are loading only the vertices, polygons and

mapping lists.

* If you need to load meshes with advanced features as for example:

* multi objects, materials, lights and so on, you must insert other

chunk parsers.

*

*********************************************************/

char Load3DS (obj_type_ptr p_object, char *p_filename)

{

int i; //Index variable

FILE *l_file; //File pointer

unsigned
short l_chunk_id; //Chunk identifier

unsigned
int l_chunk_lenght; //Chunk lenght

unsigned
char l_char; //Char variable

unsigned
short l_qty; //Number of elements in each chunk

unsigned
short l_face_flags; //Flag that stores some face information

if ((l_file=fopen (p_filename, “rb”))== NULL) return 0; //Open the file

while (ftell (l_file) < filelength (fileno (l_file))) //Loop to scan the whole file

{

//getche(); //Insert this command for debug (to wait for keypress for each chuck reading)

fread (&l_chunk_id, 2, 1, l_file); //Read the chunk header printf(“ChunkID: %x\n”,l_chunk_id);

fread (&l_chunk_lenght, 4, 1, l_file); //Read the lenght of the chunk

printf(“ChunkLenght: %x\n”,l_chunk_lenght);

switch (l_chunk_id)

{

//—————– MAIN3DS —————–

// Description: Main chunk, contains all the other chunks

// Chunk ID: 4d4d

// Chunk Lenght: 0 + sub chunks

//——————————————-

case 0x4d4d:

break;

//—————– EDIT3DS —————–

// Description: 3D Editor chunk, objects layout info

// Chunk ID: 3d3d (hex)

// Chunk Lenght: 0 + sub chunks

//——————————————-

case 0x3d3d:

break;

//————— EDIT_OBJECT —————

// Description: Object block, info for each object

// Chunk ID: 4000 (hex)

// Chunk Lenght: len(object name) + sub chunks

//——————————————-

case 0x4000:

i=0;

do

{

fread (&l_char, 1, 1, l_file);

p_object->name[i]=l_char;

i++;

}while(l_char != && i<20);

break;

//————— OBJ_TRIMESH —————

// Description: Triangular mesh, contains chunks for 3d mesh info

// Chunk ID: 4100 (hex)

// Chunk Lenght: 0 + sub chunks

//——————————————-

case 0x4100:

break;

//————— TRI_VERTEXL —————

// Description: Vertices list

// Chunk ID: 4110 (hex)

// Chunk Lenght: 1 x unsigned short (number of vertices)

// + 3 x float (vertex coordinates) x (number of vertices)

// + sub chunks

//——————————————-

case 0x4110:

fread (&l_qty, sizeof (unsigned
short), 1, l_file);

p_object->vertices_qty = l_qty;

printf(“Number of vertices: %d\n”,l_qty);

for (i=0; i<l_qty; i++)

{

fread (&p_object->vertex[i].x, sizeof(float), 1, l_file);

printf(“Vertices list x: %f\n”,p_object->vertex[i].x);

fread (&p_object->vertex[i].y, sizeof(float), 1, l_file);

printf(“Vertices list y: %f\n”,p_object->vertex[i].y);

fread (&p_object->vertex[i].z, sizeof(float), 1, l_file);

printf(“Vertices list z: %f\n”,p_object->vertex[i].z);

}

break;

//————— TRI_FACEL1 —————-

// Description: Polygons (faces) list

// Chunk ID: 4120 (hex)

// Chunk Lenght: 1 x unsigned short (number ofpolygons)

// + 3 x unsigned short (polygon points) x (number of polygons)

// + sub chunks

//——————————————-

case 0x4120:

fread (&l_qty, sizeof (unsigned
short), 1,

l_file);

p_object->polygons_qty = l_qty;

printf(“Number of polygons: %d\n”,l_qty);

for (i=0; i<l_qty; i++){

fread (&p_object->polygon[i].a, sizeof(unsigned
short), 1, l_file);

printf(“Polygon point a: %d\n”,p_object->polygon[i].a);

fread (&p_object->polygon[i].b, sizeof(unsigned
short), 1, l_file);

printf(“Polygon point b: %d\n”,p_object->polygon[i].b);

fread (&p_object->polygon[i].c, sizeof(unsigned
short), 1, l_file);

printf(“Polygon point c: %d\n”,p_object->polygon[i].c);

fread (&l_face_flags, sizeof (unsigned
short), 1, l_file);

printf(“Face flags: %x\n”,l_face_flags);

}

break;

//————- TRI_MAPPINGCOORS ————

// Description: Vertices list

// Chunk ID: 4140 (hex)

// Chunk Lenght: 1 x unsigned short (number of mapping points)

// + 2 x float (mapping coordinates) x (number of mapping points)

// + sub chunks

//——————————————-

case 0x4140:

fread (&l_qty, sizeof (unsigned
short), 1, l_file);

for (i=0; i<l_qty; i++)

{

fread (&p_object->mapcoord[i].u, sizeof (float), 1, l_file);

printf(“Mapping list u: %f\n”,p_object->mapcoord[i].u);

fread (&p_object->mapcoord[i].v, sizeof (float), 1, l_file);

printf(“Mapping list v: %f\n”,p_object->mapcoord[i].v);

}

break;

//———– Skip unknow chunks ————

//We need to skip all the chunks that currently wedon’t use

//We use the chunk lenght information to set the file pointer

//to the same level next chunk

//——————————————-

default:

fseek(l_file, l_chunk_lenght-6, SEEK_CUR);

}

}

fclose (l_file); // Closes the file stream

return (1); // Returns ok

}

Texture.h

extern
int num_texture;

extern
int LoadBitmap(char *filename);

Texture.cpp

#include
<stdio.h>

#include
<windows.h>

#include
<glut.h>

#include
“texture.h”

/**********************************************************

*

* VARIABLES DECLARATION

*

*********************************************************/

int num_texture=-1; //Counter to keep track of the last loaded texture

/**********************************************************

*

* FUNCTION LoadBitmap(char *)

*

* This function loads a bitmap file and return the OpenGL reference ID

to use that texture

*

*********************************************************/

int LoadBitmap(char *filename)

{

int i, j=0; //Index variables

FILE *l_file; //File pointer

unsigned
char *l_texture; //The pointer to the memory zone in which we will load the texture

// windows.h gives us these types to work with the Bitmap files

BITMAPFILEHEADER fileheader;

BITMAPINFOHEADER infoheader;

RGBTRIPLE rgb;

num_texture++; // The counter of the current texture is increased

if( (l_file = fopen(filename, “rb”))==NULL) return (-1); // Open the file for reading

fread(&fileheader, sizeof(fileheader), 1, l_file); // Read the fileheader

fseek(l_file, sizeof(fileheader), SEEK_SET); // Jump the fileheader

fread(&infoheader, sizeof(infoheader), 1, l_file); // and read the infoheader

// Now we need to allocate the memory for our image (width * height * color deep)

l_texture = (byte *) malloc(infoheader.biWidth * infoheader.biHeight * 4);

// And fill it with zeros

memset(l_texture, 0, infoheader.biWidth * infoheader.biHeight * 4);

// At this point we can read every pixel of the image

for (i=0; i < infoheader.biWidth*infoheader.biHeight; i++)

{

// We load an RGB value from the file

fread(&rgb, sizeof(rgb), 1, l_file);

// And store it

l_texture[j+0] = rgb.rgbtRed; // Red component

l_texture[j+1] = rgb.rgbtGreen; // Green component

l_texture[j+2] = rgb.rgbtBlue; // Blue component

l_texture[j+3] = 255; // Alpha value

j += 4; // Go to the next position

}

fclose(l_file); // Closes the file stream

glBindTexture(GL_TEXTURE_2D, num_texture); // Bind the ID texturespecified by the 2nd parameter

// The next commands sets the texture parameters

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // Ifthe u,v coordinates overflow the range 0,1 the image is repeated

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //The magnification function (“linear” produces better results)

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,

GL_LINEAR_MIPMAP_NEAREST); //The minifying function

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

// Wedon’t combine the color with the original surface color, use only thetexture map.

// Finally we define the 2d texture

glTexImage2D(GL_TEXTURE_2D, 0, 4, infoheader.biWidth,

infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);

// And create 2d mipmaps for the minifying function

gluBuild2DMipmaps(GL_TEXTURE_2D, 4, infoheader.biWidth,

infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);

free(l_texture); // Free the memory we used to load the texture

return (num_texture); // Returns the current texture OpenGL ID

}

Program15.cpp

Program ini menunjukkan fitur partikel yang dapat digunakan di openGL, dimana partikel tersebut dapat digunakan untuk meniru bentuk dari benda-benda yang abstrak, seperti api, air, pasir, dll.

// OpenGL

// – Texture Mapping Magnification Filter

// filter=0 –> Nearest Filtered Texture

// filter=1 –> Linear Interpolation Texture

// filter=2 –> Mipmapped Texture

#include
<windows.h>

#include
<stdio.h>

#include
<stdlib.h>

#include
<string.h>

#include
<stdarg.h>

#include
<glut.h>

#include
“bmp.h”

#include
“particle.h”

float z_pos=-5.0f;

float rot=0.0f;

GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };

GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };

GLfloat LightPosition[]= { 0.0f, 0.0f, 2.0f, 1.0f };

/* array to hold texture handles */

GLuint filter; // Which Filter To Use

GLuint texture[1]; // Storage For 3 Textures

/* Particle System */

extern
const
int maxparticle = 2000; // set maximum number of particles

extern PARTICLE particle[maxparticle];

int LoadGLTextures() // Load Bitmaps And Convert To Textures

{

int Status=FALSE; //Status Indicator

AUX_RGBImageRec *TextureImage[1]; // Create Storage Space For The Texture

memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL

// Load The Bitmap, Check For Errors, If Bitmap’s Not Found Quit

if (TextureImage[0]=LoadBMP(“particle.bmp”))

{

Status=TRUE;

// Set The Status To TRUE

glGenTextures(1, &texture[0]); //Create Three Textures

// Create Nearest Filtered Texture

glBindTexture(GL_TEXTURE_2D, texture[0]);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX,

TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

}

if (TextureImage[0]) // If Texture Exists

{

if (TextureImage[0]->data) // If Texture Image Exists

{

free(TextureImage[0]->data); // Free The Texture Image Memory

}

free(TextureImage[0]); //Free The Image Structure

}

glEnable(GL_TEXTURE_2D); //Enable Texture Mapping

return Status;

// Return The Status

}

void resize(int width, int height)

{

glViewport(0, 0, width, height);

//glMatrixMode(GL_PROJECTION);

//glLoadIdentity();

//gluPerspective(45.0, (float)width/(float)height, 0.0, 300.0);

glMatrixMode(GL_PROJECTION); // the following operations affect the projection matrix

glLoadIdentity(); // restore matrix to original state

glOrtho( -0.60,0.60,-0.20,0.60,-0.60,0.60); // defines the viewing volume

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

}

void myTimeOut(int id)

{

// called if timer event

// …advance the state of animation incrementally…

rot+=1;

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;

else
if((key==‘>’)||(key==‘.’)) z_pos+=0.1f;

else
if((key==‘F’)||(key=‘f’))

{

filter+=1;

if (filter>2)

{

filter=0;

}

printf(“filter: %i”,filter);

}

}

void mydisplay(void)

{

glClear(GL_COLOR_BUFFER_BIT);

//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

glLoadIdentity();

glRotatef(50.0,1.0,0.0,0.0); // show scene from top front

glBindTexture(GL_TEXTURE_2D,texture[0]); // choose particle texture

for (int i=0;i<=maxparticle;i++){

if(particle[i].ypos<0.0) particle[i].lifetime=0.0;

if((particle[i].active==true) && (particle[i].lifetime>0.0)){

glColor3f(particle[i].r,particle[i].g,particle[i].b);

glBegin(GL_TRIANGLE_STRIP);

glTexCoord2f(0.0,1.0); glVertex3f(particle[i].xpos+0.005,

particle[i].ypos+0.005, particle[i].zpos+0.0); // top right

glTexCoord2f(0.0,0.0); glVertex3f(particle[i].xpos-0.005,

particle[i].ypos+0.005, particle[i].zpos+0.0); // top left

glTexCoord2f(1.0,1.0); glVertex3f(particle[i].xpos+0.005,

particle[i].ypos-0.005, particle[i].zpos+0.0); // bottom right

glTexCoord2f(1.0,0.0); glVertex3f(particle[i].xpos-0.005,

particle[i].ypos-0.005, particle[i].zpos+0.0); // bottom left

glEnd();

} else CreateParticle(i);

}

EvolveParticle();

glFlush();

glutSwapBuffers();

}

void init()

{

glDisable(GL_DEPTH_TEST); // deactivate hidden surface removal

glDisable(GL_CULL_FACE); // show backside of polygons

glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black Background

glColor4f(1.0f, 1.0f, 1.0f, 0.5); // Full Brightness. 50% Alpha

if (!LoadGLTextures()) // Jump To Texture Loading Routine

{

return; // If Texture Didn’t Load Return FALSE

}

InitParticle();

glMatrixMode(GL_MODELVIEW);

return;

}

int main(int argc, char** argv)

{

glutInit(&argc,argv);

glutInitDisplayMode( GLUT_DOUBLE);

glutInitWindowSize(500,500);

glutInitWindowPosition(0,0);

glutCreateWindow(“simple”);

// callbacks

glutDisplayFunc(mydisplay);

glutKeyboardFunc(myKeyboard);

glutTimerFunc(100, myTimeOut, 0);

glutReshapeFunc(resize);

init();

glutMainLoop();

return 0;

}

Particle.h

#ifndef PARTICLE_H_

typedef
struct

{

float lifetime; // total lifetime of the particle

float decay; // decay speed of the particle

float r,g,b; // color values of the particle

float xpos,ypos,zpos; // position of the particle

float xspeed,yspeed,zspeed; // speed of the particle

bool active; // is particle active or not?

} PARTICLE;

void CreateParticle(int i);

void InitParticle();

void EvolveParticle();

#endif

Particle.cpp

#include
<windows.h>

#include
<stdlib.h>

#include
<math.h>

#include
“particle.h”

const
int maxparticle = 2000; // set maximum number of particles

PARTICLE particle[maxparticle];

void CreateParticle(int i)

{

particle[i].lifetime= (float)(500000*rand()/RAND_MAX)/500000.0;

particle[i].decay=0.001;

particle[i].r = 0.7;

particle[i].g = 0.7;

particle[i].b = 1.0;

particle[i].xpos= 0.0;

particle[i].ypos= 0.0;

particle[i].zpos= 0.0;

particle[i].xspeed = 10*(0.0005-(float)(100*rand()/RAND_MAX)/100000.0);

particle[i].yspeed = 0.01-(float)(100*rand()/RAND_MAX)/100000.0;

particle[i].zspeed = 0.0005-(float)(100*rand()/RAND_MAX)/100000.0;

particle[i].active = true;

}

//—————————————————————————

void InitParticle()

{

    for(int i=0;i<=maxparticle;i++){ // initialize the particle parameters

CreateParticle(i);

particle[i].active = false; // set all particles inactive

}

}

//—————————————————————————

void EvolveParticle()

{

for(int i=0;i<=maxparticle;i++){ // evolve the particle parameters

particle[i].lifetime-=particle[i].decay;

particle[i].xpos+=particle[i].xspeed;

particle[i].ypos+=particle[i].yspeed;

particle[i].zpos+=particle[i].zspeed;

particle[i].yspeed-=0.00010;

}

}

Program17.cpp

Program ini menunjukkan fitur lighting pada openGL, penempatan serta simulasi dari fitur lighting dari berbagai sudut dan intensitas, serta efeknya terhadap berbagai material.

Material.h

float Brass[] = {

0.329412, 0.223529, 0.027451, 1.000000,

0.780392, 0.568627, 0.113725, 1.000000,

0.992157, 0.941176, 0.807843, 1.000000,

27.897400

};

float Bronze[] = {

0.212500, 0.127500, 0.054000, 1.000000,

0.714000, 0.428400, 0.181440, 1.000000,

0.393548, 0.271906, 0.166721, 1.000000,

25.600000

};

float Polished_Bronze[] = {

0.250000, 0.148000, 0.064750, 1.000000,

0.400000, 0.236800, 0.103600, 1.000000,

0.774597, 0.458561, 0.200621, 1.000000,

76.800003

};

float Chrome[] = {

0.250000, 0.250000, 0.250000, 1.000000,

0.400000, 0.400000, 0.400000, 1.000000,

0.774597, 0.774597, 0.774597, 1.000000,

76.800003

};

float Copper[] = {

0.191250, 0.073500, 0.022500, 1.000000,

0.703800, 0.270480, 0.082800, 1.000000,

0.256777, 0.137622, 0.086014, 1.000000,

12.800000

};

float Polished_Copper[] = {

0.229500, 0.088250, 0.027500, 1.000000,

0.550800, 0.211800, 0.066000, 1.000000,

0.580594, 0.223257, 0.069570, 1.000000,

51.200001

};

float Gold[] = {

0.247250, 0.199500, 0.074500, 1.000000,

0.751640, 0.606480, 0.226480, 1.000000,

0.628281, 0.555802, 0.366065, 1.000000,

51.200001

};

float Polished_Gold[] = {

0.247250, 0.224500, 0.064500, 1.000000,

0.346150, 0.314300, 0.090300, 1.000000,

0.797357, 0.723991, 0.208006, 1.000000,

83.199997

};

float Pewter[] = {

0.105882, 0.058824, 0.113725, 1.000000,

0.427451, 0.470588, 0.541176, 1.000000,

0.333333, 0.333333, 0.521569, 1.000000,

9.846150

};

float Silver[] = {

0.192250, 0.192250, 0.192250, 1.000000,

0.507540, 0.507540, 0.507540, 1.000000,

0.508273, 0.508273, 0.508273, 1.000000,

51.200001

};

float Polished_Silver[] = {

0.231250, 0.231250, 0.231250, 1.000000,

0.277500, 0.277500, 0.277500, 1.000000,

0.773911, 0.773911, 0.773911, 1.000000,

89.599998

};

float Emerald[] = {

0.021500, 0.174500, 0.021500, 0.550000,

0.075680, 0.614240, 0.075680, 0.550000,

0.633000, 0.727811, 0.633000, 0.550000,

76.800003

};

float Jade[] = {

0.135000, 0.222500, 0.157500, 0.950000,

0.540000, 0.890000, 0.630000, 0.950000,

0.316228, 0.316228, 0.316228, 0.950000,

12.800000

};

float Obsidian[] = {

0.053750, 0.050000, 0.066250, 0.820000,

0.182750, 0.170000, 0.225250, 0.820000,

0.332741, 0.328634, 0.346435, 0.820000,

38.400002

};

float Pearl[] = {

0.250000, 0.207250, 0.207250, 0.922000,

1.000000, 0.829000, 0.829000, 0.922000,

0.296648, 0.296648, 0.296648, 0.922000,

11.264000

};

float Ruby[] = {

0.174500, 0.011750, 0.011750, 0.550000,

0.614240, 0.041360, 0.041360, 0.550000,

0.727811, 0.626959, 0.626959, 0.550000,

76.800003

};

float Turquoise[] = {

0.100000, 0.187250, 0.174500, 0.800000,

0.396000, 0.741510, 0.691020, 0.800000,

0.297254, 0.308290, 0.306678, 0.800000,

12.800000

};

float Black_Plastic[] = {

0.000000, 0.000000, 0.000000, 1.000000,

0.010000, 0.010000, 0.010000, 1.000000,

0.500000, 0.500000, 0.500000, 1.000000,

32.000000

};

float Black_Rubber[] = {

0.020000, 0.020000, 0.020000, 1.000000,

0.010000, 0.010000, 0.010000, 1.000000,

0.400000, 0.400000, 0.400000, 1.000000,

10.000000

};

Lighting.cpp

Program18.cpp

Sama seperti program sebelumnya, tetapi menunjukkan pengaruh lightingnya pada objek dan tekstur objek.

/*

lightposition.c

Nate Robins, 1997

Tool for teaching about OpenGL light positioning.

*/

#include
<math.h>

#include
<stdio.h>

#include
<stdlib.h>

#include
<stdarg.h>

#include
<string.h>

#include
<glut.h>

#include
“glm.h”

#pragma
comment( linker, “/entry:\”mainCRTStartup\”” ) // set the entry point to be main()

typedef
struct _cell {

int id;

int x, y;

float min, max;

float value;

float step;

char* info;

char* format;

} cell;

cell lookat[9] = {

{ 1, 180, 120, -5.0, 5.0, 0.0, 0.1,

“Specifies the X position of the eye point.”, “%.2f” },

{ 2, 240, 120, -5.0, 5.0, 0.0, 0.1,

“Specifies the Y position of the eye point.”, “%.2f” },

{ 3, 300, 120, -5.0, 5.0, 2.0, 0.1,

“Specifies the Z position of the eye point.”, “%.2f” },

{ 4, 180, 160, -5.0, 5.0, 0.0, 0.1,

“Specifies the X position of the reference point.”, “%.2f” },

{ 5, 240, 160, -5.0, 5.0, 0.0, 0.1,

“Specifies the Y position of the reference point.”, “%.2f” },

{ 6, 300, 160, -5.0, 5.0, 0.0, 0.1,

“Specifies the Z position of the reference point.”, “%.2f” },

{ 7, 180, 200, -2.0, 2.0, 0.0, 0.1,

“Specifies the X direction of the up vector.”, “%.2f” },

{ 8, 240, 200, -2.0, 2.0, 1.0, 0.1,

“Specifies the Y direction of the up vector.”, “%.2f” },

{ 9, 300, 200, -2.0, 2.0, 0.0, 0.1,

“Specifies the Z direction of the up vector.”, “%.2f” },

};

cell light[4] = {

{ 10, 180, 40, -5.0, 5.0, 1.5, 0.1,

“Specifies X coordinate of light vector.”, “%.2f” },

{ 11, 240, 40, -5.0, 5.0, 1.0, 0.1,

“Specifies Y coordinate of light vector.”, “%.2f” },

{ 12, 300, 40, -5.0, 5.0, 1.0, 0.1,

“Specifies Z coordinate of light vector.”, “%.2f” },

{ 13, 360, 40, 0.0, 1.0, 0.0, 1.0,

“Specifies directional (0) or positional (1) light.”, “%.2f” }

};

GLboolean swapped = GL_FALSE;

GLboolean world_draw = GL_TRUE;

GLMmodel* pmodel = NULL;

GLint selection = 0;

void redisplay_all(void);

GLdouble projection[16], modelview[16], inverse[16];

GLuint window, world, screen, command;

GLuint sub_width = 256, sub_height = 256;

GLvoid *font_style = GLUT_BITMAP_TIMES_ROMAN_10;

void

setfont(char* name, int size)

{

font_style = GLUT_BITMAP_HELVETICA_10;

if (strcmp(name, “helvetica”) == 0) {

if (size == 12)

font_style = GLUT_BITMAP_HELVETICA_12;

else
if (size == 18)

font_style = GLUT_BITMAP_HELVETICA_18;

} else
if (strcmp(name, “times roman”) == 0) {

font_style = GLUT_BITMAP_TIMES_ROMAN_10;

if (size == 24)

font_style = GLUT_BITMAP_TIMES_ROMAN_24;

} else
if (strcmp(name, “8×13”) == 0) {

font_style = GLUT_BITMAP_8_BY_13;

} else
if (strcmp(name, “9×15”) == 0) {

font_style = GLUT_BITMAP_9_BY_15;

}

}

void

drawstr(GLuint x, GLuint y, char* format, …)

{

va_list args;

char buffer[255], *s;

va_start(args, format);

vsprintf(buffer, format, args);

va_end(args);

glRasterPos2i(x, y);

for (s = buffer; *s; s++)

glutBitmapCharacter(font_style, *s);

}

void

cell_draw(cell* cell)

{

glColor3ub(0, 255, 128);

if (selection == cell->id) {

glColor3ub(255, 255, 0);

drawstr(10, 240, cell->info);

glColor3ub(255, 0, 0);

}

drawstr(cell->x, cell->y, cell->format, cell->value);

}

int

cell_hit(cell* cell, int x, int y)

{

if (x > cell->x && x < cell->x + 60 &&

y > cell->y-30 && y < cell->y+10)

return cell->id;

return 0;

}

void

cell_update(cell* cell, int update)

{

if (selection != cell->id)

return;

cell->value += update * cell->step;

if (cell->value < cell->min)

cell->value = cell->min;

else
if (cell->value > cell->max)

cell->value = cell->max;

}

void

cell_vector(float* dst, cell* cell, int num)

{

while (–num >= 0)

dst[num] = cell[num].value;

}

void

drawmodel(void)

{

if (!pmodel) {

pmodel = glmReadOBJ(“soccerball.obj”);

if (!pmodel) exit(0);

glmUnitize(pmodel);

glmFacetNormals(pmodel);

glmVertexNormals(pmodel, 90.0);

}

glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);

}

void

drawaxes(void)

{

glColor3ub(255, 0, 0);

glBegin(GL_LINE_STRIP);

glVertex3f(0.0, 0.0, 0.0);

glVertex3f(1.0, 0.0, 0.0);

glVertex3f(0.75, 0.25, 0.0);

glVertex3f(0.75, -0.25, 0.0);

glVertex3f(1.0, 0.0, 0.0);

glVertex3f(0.75, 0.0, 0.25);

glVertex3f(0.75, 0.0, -0.25);

glVertex3f(1.0, 0.0, 0.0);

glEnd();

glBegin(GL_LINE_STRIP);

glVertex3f(0.0, 0.0, 0.0);

glVertex3f(0.0, 1.0, 0.0);

glVertex3f(0.0, 0.75, 0.25);

glVertex3f(0.0, 0.75, -0.25);

glVertex3f(0.0, 1.0, 0.0);

glVertex3f(0.25, 0.75, 0.0);

glVertex3f(-0.25, 0.75, 0.0);

glVertex3f(0.0, 1.0, 0.0);

glEnd();

glBegin(GL_LINE_STRIP);

glVertex3f(0.0, 0.0, 0.0);

glVertex3f(0.0, 0.0, 1.0);

glVertex3f(0.25, 0.0, 0.75);

glVertex3f(-0.25, 0.0, 0.75);

glVertex3f(0.0, 0.0, 1.0);

glVertex3f(0.0, 0.25, 0.75);

glVertex3f(0.0, -0.25, 0.75);

glVertex3f(0.0, 0.0, 1.0);

glEnd();

glColor3ub(255, 255, 0);

glRasterPos3f(1.1, 0.0, 0.0);

glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, ‘x’);

glRasterPos3f(0.0, 1.1, 0.0);

glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, ‘y’);

glRasterPos3f(0.0, 0.0, 1.1);

glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, ‘z’);

}

void

identity(GLdouble m[16])

{

m[0+4*0] = 1; m[0+4*1] = 0; m[0+4*2] = 0; m[0+4*3] = 0;

m[1+4*0] = 0; m[1+4*1] = 1; m[1+4*2] = 0; m[1+4*3] = 0;

m[2+4*0] = 0; m[2+4*1] = 0; m[2+4*2] = 1; m[2+4*3] = 0;

m[3+4*0] = 0; m[3+4*1] = 0; m[3+4*2] = 0; m[3+4*3] = 1;

}

GLboolean

invert(GLdouble src[16], GLdouble inverse[16])

{

double t;

int i, j, k, swap;

GLdouble tmp[4][4];

identity(inverse);

for (i = 0; i < 4; i++) {

for (j = 0; j < 4; j++) {

tmp[i][j] = src[i*4+j];

}

}

for (i = 0; i < 4; i++) {

    /* look for largest element in column. */

swap = i;

for (j = i + 1; j < 4; j++) {

if (fabs(tmp[j][i]) > fabs(tmp[i][i])) {

swap = j;

}

}

if (swap != i) {

/* swap rows. */

for (k = 0; k < 4; k++) {

t = tmp[i][k];

tmp[i][k] = tmp[swap][k];

tmp[swap][k] = t;

t = inverse[i*4+k];

inverse[i*4+k] = inverse[swap*4+k];

inverse[swap*4+k] = t;

}

}

if (tmp[i][i] == 0) {

/* no non-zero pivot. the matrix is singular, which

shouldn’t happen. This means the user gave us a bad

matrix. */

return GL_FALSE;

}

t = tmp[i][i];

for (k = 0; k < 4; k++) {

tmp[i][k] /= t;

inverse[i*4+k] /= t;

}

for (j = 0; j < 4; j++) {

if (j != i) {

t = tmp[j][i];

for (k = 0; k < 4; k++) {

tmp[j][k] -= tmp[i][k]*t;

inverse[j*4+k] -= inverse[i*4+k]*t;

}

}

}

}

return GL_TRUE;

}

float

normalize(float* v)

{

float length;

length = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);

v[0] /= length;

v[1] /= length;

v[2] /= length;

return length;

}

void

main_reshape(int width, int height)

{

glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0, width, height, 0);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

#define GAP 25 /* gap between subwindows */

sub_width = (width-GAP*3)/2.0;

sub_height = (height-GAP*3)/2.0;

glutSetWindow(world);

glutPositionWindow(GAP, GAP);

glutReshapeWindow(sub_width, sub_height);

glutSetWindow(screen);

glutPositionWindow(GAP+sub_width+GAP, GAP);

glutReshapeWindow(sub_width, sub_height);

glutSetWindow(command);

glutPositionWindow(GAP, GAP+sub_height+GAP);

glutReshapeWindow(sub_width+GAP+sub_width, sub_height);

}

void

main_display(void)

{

glClearColor(0.8, 0.8, 0.8, 0.0);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glColor3ub(0, 0, 0);

setfont(“helvetica”, 12);

drawstr(GAP, GAP-5, “World-space view”);

drawstr(GAP+sub_width+GAP, GAP-5, “Screen-space view”);

drawstr(GAP, GAP+sub_height+GAP-5, “Command manipulation window”);

glutSwapBuffers();

}

void

main_keyboard(unsigned
char key, int x, int y)

{

switch (key) {

case
‘s’:

swapped = !swapped;

break;

case
‘r’:

light[0].value = 1.5;

light[1].value = 1.0;

light[2].value = 1.0;

light[3].value = 0.0;

lookat[0].value = 0.0;

lookat[1].value = 0.0;

lookat[2].value = 2.0;

lookat[3].value = 0.0;

lookat[4].value = 0.0;

lookat[5].value = 0.0;

lookat[6].value = 0.0;

lookat[7].value = 1.0;

lookat[8].value = 0.0;

break;

case 27:

exit(0);

}

redisplay_all();

}

void

world_reshape(int width, int height)

{

glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluPerspective(60.0, (GLfloat)width/height, 0.01, 256.0);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glTranslatef(0.0, 0.0, -5.0);

glRotatef(-45.0, 0.0, 1.0, 0.0);

glClearColor(0.0, 0.0, 0.0, 0.0);

glEnable(GL_DEPTH_TEST);

glEnable(GL_LIGHT0);

}

void

world_display(void)

{

GLfloat pos[4];

double length;

float l[3];

cell_vector(pos, light, 4);

l[0] = lookat[3].value – lookat[0].value;

l[1] = lookat[4].value – lookat[1].value;

l[2] = lookat[5].value – lookat[2].value;

invert(modelview, inverse);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

if (swapped) {

glPushMatrix();

glTranslatef(l[0], l[1], l[2]);

glMultMatrixd(inverse);

glColor3ub(255, 255, 255);

glBegin(GL_LINE_STRIP);

glVertex3f(0.0, 0.0, 0.0);

if (pos[3] == 0) /* 10.0 = ‘infinite’ light */

glVertex3f(pos[0]*10.0,pos[1]*10.0,pos[2]*10.0);

else

glVertex3f(pos[0], pos[1], pos[2]);

glEnd();

glLightfv(GL_LIGHT0, GL_POSITION, pos);

glPopMatrix();

} else {

glColor3ub(255, 255, 255);

glBegin(GL_LINE_STRIP);

glVertex3f(0.0, 0.0, 0.0);

if (pos[3] == 0) /* 10.0 = ‘infinite’ light */

glVertex3f(pos[0]*10.0,pos[1]*10.0,pos[2]*10.0);

else

glVertex3f(pos[0], pos[1], pos[2]);

glEnd();

glLightfv(GL_LIGHT0, GL_POSITION, pos);

}

length = normalize(l);

if (world_draw) {

glEnable(GL_LIGHTING);

drawmodel();

glDisable(GL_LIGHTING);

}

glPushMatrix();

glMultMatrixd(inverse);

/* draw the axes and eye vector */

glPushMatrix();

glColor3ub(0, 0, 255);

glBegin(GL_LINE_STRIP);

glVertex3f(0.0, 0.0, 0.0);

glVertex3f(0.0, 0.0, -1.0*length);

glVertex3f(0.1, 0.0, -0.9*length);

glVertex3f(-0.1, 0.0, -0.9*length);

glVertex3f(0.0, 0.0, -1.0*length);

glVertex3f(0.0, 0.1, -0.9*length);

glVertex3f(0.0, -0.1, -0.9*length);

glVertex3f(0.0, 0.0, -1.0*length);

glEnd();

glColor3ub(255, 255, 0);

glRasterPos3f(0.0, 0.0, -1.1*length);

glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, ‘e’);

glColor3ub(255, 0, 0);

glScalef(0.4, 0.4, 0.4);

drawaxes();

glPopMatrix();

invert(projection, inverse);

glMultMatrixd(inverse);

/* draw the viewing frustum */

glColor3f(0.2, 0.2, 0.2);

glBegin(GL_QUADS);

glVertex3i(1, 1, 1);

glVertex3i(-1, 1, 1);

glVertex3i(-1, -1, 1);

glVertex3i(1, -1, 1);

glEnd();

glColor3ub(128, 196, 128);

glBegin(GL_LINES);

glVertex3i(1, 1, -1);

glVertex3i(1, 1, 1);

glVertex3i(-1, 1, -1);

glVertex3i(-1, 1, 1);

glVertex3i(-1, -1, -1);

glVertex3i(-1, -1, 1);

glVertex3i(1, -1, -1);

glVertex3i(1, -1, 1);

glEnd();

glEnable(GL_BLEND);

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glColor4f(0.2, 0.2, 0.4, 0.5);

glBegin(GL_QUADS);

glVertex3i(1, 1, -1);

glVertex3i(-1, 1, -1);

glVertex3i(-1, -1, -1);

glVertex3i(1, -1, -1);

glEnd();

glDisable(GL_BLEND);

glPopMatrix();

glutSwapBuffers();

}

void

world_menu(int value)

{

switch (value) {

case
‘m’:

world_draw = !world_draw;

break;

}

redisplay_all();

}

void

screen_reshape(int width, int height)

{

GLfloat pos[4];

cell_vector(pos, light, 4);

glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluPerspective(60.0, (float)width/height, 0.5, 8.0);

glGetDoublev(GL_PROJECTION_MATRIX, projection);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

if (swapped) {

glLightfv(GL_LIGHT0, GL_POSITION, pos);

gluLookAt(lookat[0].value, lookat[1].value, lookat[2].value,

lookat[3].value, lookat[4].value, lookat[5].value,

lookat[6].value, lookat[7].value, lookat[8].value);

} else {

    gluLookAt(lookat[0].value, lookat[1].value, lookat[2].value,

lookat[3].value, lookat[4].value, lookat[5].value,

lookat[6].value, lookat[7].value, lookat[8].value);

glLightfv(GL_LIGHT0, GL_POSITION, pos);

}

glGetDoublev(GL_MODELVIEW_MATRIX, modelview);

glClearColor(0.2, 0.2, 0.2, 0.0);

glEnable(GL_DEPTH_TEST);

glEnable(GL_LIGHTING);

glEnable(GL_LIGHT0);

}

void

screen_display(void)

{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

drawmodel();

glutSwapBuffers();

}

void

screen_menu(int value)

{

char* name = 0;

switch (value) {

case
‘a’:

name = “al.obj”;

break;

case
‘s’:

name = “soccerball.obj”;

break;

case
‘d’:

name = “dolphins.obj”;

break;

case
‘f’:

name = “flowers.obj”;

break;

case
‘j’:

name = “f-16.obj”;

break;

case
‘p’:

name = “porsche.obj”;

break;

case
‘r’:

name = “rose+vase.obj”;

break;

}

if (name) {

pmodel = glmReadOBJ(name);

if (!pmodel) exit(0);

glmUnitize(pmodel);

glmFacetNormals(pmodel);

glmVertexNormals(pmodel, 90.0);

}

redisplay_all();

}

void

command_reshape(int width, int height)

{

glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0, width, height, 0);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glClearColor(0.0, 0.0, 0.0, 0.0);

}

void

command_display(void)

{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glColor3ub(255, 255, 255);

setfont(“helvetica”, 18);

if (swapped) {

lookat[0].y = 120;

lookat[1].y = 120;

lookat[2].y = 120;

lookat[3].y = 120+40;

lookat[4].y = 120+40;

lookat[5].y = 120+40;

lookat[6].y = 120+80;

lookat[7].y = 120+80;

lookat[8].y = 120+80;

} else {

lookat[0].y = 80;

lookat[1].y = 80;

lookat[2].y = 80;

lookat[3].y = 80+40;

lookat[4].y = 80+40;

lookat[5].y = 80+40;

lookat[6].y = 80+80;

lookat[7].y = 80+80;

lookat[8].y = 80+80;

}

drawstr(30, light[0].y, “GLfloat pos[4] = {“);

drawstr(230, light[0].y, “,”);

drawstr(290, light[0].y, “,”);

drawstr(350, light[0].y, “,”);

drawstr(410, light[0].y, “};”);

if (swapped)

drawstr(30, 80, “glLightfv(GL_LIGHT0, GL_POSITION, pos);”);

else

drawstr(30, 200, “glLightfv(GL_LIGHT0, GL_POSITION, pos);”);

drawstr(78, lookat[0].y, “gluLookAt(“);

drawstr(230, lookat[0].y, “,”);

drawstr(290, lookat[0].y, “,”);

drawstr(350, lookat[0].y, “,”);

drawstr(380, lookat[0].y, “<- eye”);

drawstr(230, lookat[3].y, “,”);

drawstr(290, lookat[3].y, “,”);

drawstr(350, lookat[3].y, “,”);

drawstr(380, lookat[3].y, “<- center”);

drawstr(230, lookat[6].y, “,”);

drawstr(290, lookat[6].y, “,”);

drawstr(350, lookat[6].y, “);”);

drawstr(380, lookat[6].y, “<- up”);

cell_draw(&light[0]);

cell_draw(&light[1]);

cell_draw(&light[2]);

cell_draw(&light[3]);

cell_draw(&lookat[0]);

cell_draw(&lookat[1]);

cell_draw(&lookat[2]);

cell_draw(&lookat[3]);

cell_draw(&lookat[4]);

cell_draw(&lookat[5]);

cell_draw(&lookat[6]);

cell_draw(&lookat[7]);

cell_draw(&lookat[8]);

if (!selection) {

glColor3ub(255, 255, 0);

drawstr(10, 240,“Click on the arguments and move the mouse to modify values.”);

}

glutSwapBuffers();

}

int old_y;

void

command_mouse(int button, int state, int x, int y)

{

selection = 0;

if (state == GLUT_DOWN) {

/* mouse should only hit _one_ of the cells, so adding up all

the hits just propagates a single hit. */

selection += cell_hit(&light[0], x, y);

selection += cell_hit(&light[1], x, y);

selection += cell_hit(&light[2], x, y);

selection += cell_hit(&light[3], x, y);

selection += cell_hit(&lookat[0], x, y);

selection += cell_hit(&lookat[1], x, y);

selection += cell_hit(&lookat[2], x, y);

selection += cell_hit(&lookat[3], x, y);

selection += cell_hit(&lookat[4], x, y);

selection += cell_hit(&lookat[5], x, y);

selection += cell_hit(&lookat[6], x, y);

selection += cell_hit(&lookat[7], x, y);

selection += cell_hit(&lookat[8], x, y);

}

old_y = y;

redisplay_all();

}

void

command_motion(int x, int y)

{

cell_update(&light[0], old_y-y);

cell_update(&light[1], old_y-y);

cell_update(&light[2], old_y-y);

cell_update(&light[3], old_y-y);

cell_update(&lookat[0], old_y-y);

cell_update(&lookat[1], old_y-y);

cell_update(&lookat[2], old_y-y);

cell_update(&lookat[3], old_y-y);

cell_update(&lookat[4], old_y-y);

cell_update(&lookat[5], old_y-y);

cell_update(&lookat[6], old_y-y);

cell_update(&lookat[7], old_y-y);

cell_update(&lookat[8], old_y-y);

old_y = y;

redisplay_all();

}

void

command_menu(int value)

{

main_keyboard((unsigned
char)value, 0, 0);

}

void

redisplay_all(void)

{

glutSetWindow(command);

glutPostRedisplay();

glutSetWindow(world);

world_reshape(sub_width, sub_height);

glutPostRedisplay();

glutSetWindow(screen);

screen_reshape(sub_width, sub_height);

glutPostRedisplay();

}

int

main(int argc, char** argv)

{

glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);

glutInitWindowSize(512+GAP*3, 512+GAP*3);

glutInitWindowPosition(50, 50);

glutInit(&argc, argv);

window = glutCreateWindow(“Light Positioning”);

glutReshapeFunc(main_reshape);

glutDisplayFunc(main_display);

glutKeyboardFunc(main_keyboard);

world = glutCreateSubWindow(window, GAP, GAP, 256, 256);

glutReshapeFunc(world_reshape);

glutDisplayFunc(world_display);

glutKeyboardFunc(main_keyboard);

glutCreateMenu(world_menu);

glutAddMenuEntry(“Toggle model”, ‘m’);

glutAttachMenu(GLUT_RIGHT_BUTTON);

screen = glutCreateSubWindow(window, GAP+256+GAP, GAP, 256, 256);

glutReshapeFunc(screen_reshape);

glutDisplayFunc(screen_display);

glutKeyboardFunc(main_keyboard);

glutCreateMenu(screen_menu);

glutAddMenuEntry(“Models”, 0);

glutAddMenuEntry(“”, 0);

glutAddMenuEntry(“Soccerball”, ‘s’);

glutAddMenuEntry(“Al Capone”, ‘a’);

glutAddMenuEntry(“F-16 Jet”, ‘j’);

glutAddMenuEntry(“Dolphins”, ‘d’);

glutAddMenuEntry(“Flowers”, ‘f’);

glutAddMenuEntry(“Porsche”, ‘p’);

glutAddMenuEntry(“Rose”, ‘r’);

glutAttachMenu(GLUT_RIGHT_BUTTON);

command = glutCreateSubWindow(window, GAP+256+GAP, GAP+256+GAP,

256, 256);

glutReshapeFunc(command_reshape);

glutDisplayFunc(command_display);

glutMotionFunc(command_motion);

glutMouseFunc(command_mouse);

glutKeyboardFunc(main_keyboard);

glutCreateMenu(command_menu);

glutAddMenuEntry(“Light Positioning”, 0);

glutAddMenuEntry(“”, 0);

glutAddMenuEntry(“[s] Swap lookat/position calls”, ‘s’);

glutAddMenuEntry(“[r] Reset parameters”, ‘r’);

glutAddMenuEntry(“”, 0);

glutAddMenuEntry(“Quit”, 27);

glutAttachMenu(GLUT_RIGHT_BUTTON);

redisplay_all();

glutMainLoop();

return 0;

}

Program19.cpp

Project ini akan meload file sebagai tekstur pada obyek polygon, kemudian menerapkan fitur transformasi flag pada polygon tersebut untuk membuat efek bendera.

#include
<windows.h>

#include
<math.h>
// Math Library Header File

#include
<stdio.h>

#include
<stdlib.h>

#include
<string.h>

#include
<stdarg.h>

#include
<glut.h>

#include
<glaux.h>

#pragma
comment (lib, “glaux.lib”)

float points[45][45][3]; // The Array For The Points On The Grid Of Our “Wave”

int wiggle_count = 0; // Counter Used To Control How Fast Flag Waves

GLfloat xrot; // X Rotation ( NEW )

GLfloat yrot; // Y Rotation ( NEW )

GLfloat zrot; // Z Rotation ( NEW )

GLfloat hold; // Temporarily Holds A Floating Point Value

GLuint texture[1]; // Storage For One Texture ( NEW )

AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image

{

FILE *File=NULL;

// File Handle

if (!Filename)

// Make Sure A Filename Was Given

{

return NULL;

// If Not Return NULL

}

File=fopen(Filename,“r”); //Check To See If The File Exists

if (File)

// Does The File Exist?

{

fclose(File);

// Close The Handle

return auxDIBImageLoad(Filename); // Load TheBitmap And Return A Pointer

}

return NULL;

// If Load Failed Return NULL

}

int LoadGLTextures()

// Load Bitmaps And Convert To Textures

{

int Status=FALSE;

// Status Indicator

AUX_RGBImageRec *TextureImage[1]; // Create Storage Space For The Texture

memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL

// Load The Bitmap, Check For Errors, If Bitmap’s Not Found Quit

if (TextureImage[0]=LoadBMP(“Tim.bmp”))

{

Status=TRUE;

// Set The Status To TRUE

glGenTextures(1, &texture[0]); //Create The Texture

// Typical Texture Generation Using Data From The Bitmap

glBindTexture(GL_TEXTURE_2D, texture[0]);

glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX,

TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

}

if (TextureImage[0])

// If Texture Exists

{

if (TextureImage[0]->data)

// If Texture Image Exists

{

free(TextureImage[0]->data);

// Free The Texture Image Memory

}

free(TextureImage[0]);

// Free The Image Structure

}

return Status;

// Return The Status

}

void resize(GLsizei width, GLsizei height) // Resize And Initialize The GL Window

{

if (height==0)

// Prevent A Divide By Zero By

{

height=1;

// Making Height Equal One

}

glViewport(0,0,width,height); //Reset The Current Viewport

    glMatrixMode(GL_PROJECTION); //Select The Projection Matrix

glLoadIdentity();

// Reset The Projection Matrix

// Calculate The Aspect Ratio Of The Window

gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW);

// Select The Modelview Matrix

glLoadIdentity();

// Reset The Modelview Matrix

}

int init(GLvoid)

// All Setup For OpenGL Goes Here

{

if (!LoadGLTextures())

// Jump To Texture Loading Routine ( NEW )

{

return FALSE;

// If Texture Didn’t Load Return FALSE

}

glEnable(GL_TEXTURE_2D); //Enable Texture Mapping ( NEW )

glShadeModel(GL_SMOOTH); //Enable Smooth Shading

glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // BlackBackground

glClearDepth(1.0f);

// Depth Buffer Setup

glEnable(GL_DEPTH_TEST); //Enables Depth Testing

glDepthFunc(GL_LEQUAL);

// The Type Of Depth Testing To Do

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations

glPolygonMode( GL_BACK, GL_FILL ); //Back Face Is Solid

glPolygonMode( GL_FRONT, GL_LINE ); //Front Face Is Made Of Lines

for(int x=0; x<45; x++)

{

for(int y=0; y<45; y++)

{

points[x][y][0]=float((x/5.0f)-4.5f);

points[x][y][1]=float((y/5.0f)-4.5f);

points[x][y][2]=float(sin((((x/5.0f)*40.0f)/360.0f)*3.141592654*2.0f));

}

}

return TRUE;

// Initialization Went OK

}

void mydisplay(GLvoid)

// Here’s Where We Do All The Drawing

{

int x, y;

float float_x, float_y, float_xb, float_yb;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer

glLoadIdentity();

// Reset The View

glTranslatef(0.0f,0.0f,-12.0f);

glRotatef(xrot,1.0f,0.0f,0.0f);

glRotatef(yrot,0.0f,1.0f,0.0f);

glRotatef(zrot,0.0f,0.0f,1.0f);

glBindTexture(GL_TEXTURE_2D, texture[0]);

glBegin(GL_QUADS);

for( x = 0; x < 44; x++ )

{

for( y = 0; y < 44; y++ )

{

float_x = float(x)/44.0f;

float_y = float(y)/44.0f;

float_xb = float(x+1)/44.0f;

float_yb = float(y+1)/44.0f;

glTexCoord2f( float_x, float_y);

glVertex3f( points[x][y][0], points[x][y][1],

points[x][y][2] );

glTexCoord2f( float_x, float_yb );

glVertex3f( points[x][y+1][0], points[x][y+1][1],

points[x][y+1][2] );

glTexCoord2f( float_xb, float_yb );

glVertex3f( points[x+1][y+1][0], points[x+1][y+1][1],

points[x+1][y+1][2] );

glTexCoord2f( float_xb, float_y );

glVertex3f( points[x+1][y][0], points[x+1][y][1],

points[x+1][y][2] );

}

}

glEnd();

if( wiggle_count == 2 )

{

for( y = 0; y < 45; y++ )

{

hold=points[0][y][2];

for( x = 0; x < 44; x++)

{

points[x][y][2] = points[x+1][y][2];

}

points[44][y][2]=hold;

}

wiggle_count = 0;

}

wiggle_count++;

xrot+=0.3f;

yrot+=0.2f;

zrot+=0.4f;

glFlush();

glutSwapBuffers();

}

void myTimeOut(int id)

{

// called if timer event

// …advance the state of animation incrementally…

//rot+=1;

glutPostRedisplay(); // request redisplay

glutTimerFunc(10, myTimeOut, 0); // request next timer event

}

void myKeyboard(unsigned
char key,int x, int y)

{

}

void mySpecialKeyboard(int key,int x, int y)

{

}

int main(int argc, char** argv)

{

glutInit(&argc,argv);

glutInitDisplayMode( GLUT_RGB |GLUT_DOUBLE| GLUT_DEPTH );

glutInitWindowSize(500,500);

glutInitWindowPosition(0,0);

glutCreateWindow(“simple”);

// callbacks

glutDisplayFunc(mydisplay);

glutKeyboardFunc(myKeyboard);

glutSpecialFunc(mySpecialKeyboard);

glutTimerFunc(100, myTimeOut, 0);

glutReshapeFunc(resize);

init();

glutMainLoop();

return 0;

}

 
Leave a comment

Posted by on June 6, 2014 in Uncategorized

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: