18 June 2012

OpenGL untuk membuat gambar pada segitiga tenda.



Berikut ini adalah program OpenGL untuk membuat gambar pada segitiga tenda.


Praktikum grafika 10.

#include <windows.h> 
#include <iostream>
#include <stdlib.h>
#include <stdio.h> 
#include <string.h>
#include <stdarg.h> 
#include <glut.h>
#include <glu.h>
#include "imageloader.h"
using namespace std;

GLuint loadTexture(Image* image) {
      GLuint textureId;
      glGenTextures(1, &textureId); //Make room for our texture
      glBindTexture(GL_TEXTURE_2D, textureId); //Tell OpenGL which texture to edit
      //Map the image to the texture
      glTexImage2D(GL_TEXTURE_2D,                //Always GL_TEXTURE_2D
                         0,                            //0 for now
                         GL_RGB,                       //Format OpenGL uses for image
                         image->width, image->height,  //Width and height
                         0,                            //The border of the image
                         GL_RGB, //GL_RGB, because pixels are stored in RGB format
                         GL_UNSIGNED_BYTE, //GL_UNSIGNED_BYTE, because pixels are stored
                                           //as unsigned numbers
                         image->pixels);               //The actual pixel data
      return textureId; //Returns the id of the texture
}

GLuint _textureId; //The id of the texture


void initRendering() {
      glEnable(GL_DEPTH_TEST);
      glEnable(GL_COLOR_MATERIAL);
      glEnable(GL_LIGHTING);
      glEnable(GL_LIGHT0);
      glEnable(GL_LIGHT1);
      glEnable(GL_NORMALIZE);
      glShadeModel(GL_SMOOTH);

      Image* image = loadBMP("uadLogo.bmp");
      _textureId = loadTexture(image);
      delete image;
}

void handleResize(int w, int h) {
      glViewport(0, 0, w, h);
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}

float _angle = 60.0f;

void drawScene() {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
      glTranslatef(0.0f, 0.0f, -8.0f);

      GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f};
      glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);

      GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 1.0f};
      GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f};
      glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
      glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
//radius
      GLfloat lightColor1[] = {0.5f, 0.2f, 0.2f, 1.0f};

      GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f};
      glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
      glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);

glEnable(GL_TEXTURE_2D);
      glBindTexture(GL_TEXTURE_2D, _textureId);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

      glRotatef(_angle, 0.0f, 1.0f, 0.0f);
      glColor3f(1.0f, 1.0f, 0.0f);
      //glScalef(1.0f, 1.5f, 1.0f);
      glBegin(GL_QUADS);
     
      //Front

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


     
      glTexCoord2f(0.0f, 0.0f);
      glVertex3f(-1.5f, -1.0f, 1.5f);
      //glNormal3f(1.0f, 0.0f, 1.0f);
      glTexCoord2f(1.0, 0.0f);
      glVertex3f(1.5f, -1.0f, 1.5f);
      //glNormal3f(1.0f, 0.0f, 1.0f);
      glTexCoord2f(1.0f, 1.0f);
      glVertex3f(1.5f, 1.0f, 0.0f);
      //glNormal3f(-1.0f, 0.0f, 1.0f);
      glTexCoord2f(0.0f, 1.0f);
      glVertex3f(-1.5f, 1.0f, 0.0f);
     
      //Right
      glColor3f(1.0f, 0.0f, 0.0f);
      //glNormal3f(1.0f, 0.0f, 0.0f);
      //glNormal3f(1.0f, 0.0f, -1.0f);
      glVertex3f(1.5f, -1.0f, -1.5f);
      //glNormal3f(1.0f, 0.0f, -1.0f);
      glVertex3f(1.5f, 1.0f, 0.0f);
      //glNormal3f(1.0f, 0.0f, 1.0f);
      glVertex3f(1.5f, -1.0f, 1.5f);
      //glNormal3f(1.0f, 0.0f, 1.0f);
     
      glVertex3f(1.5f, -1.0f, -1.5f);
     
      //Back
      glColor3f(0.0f, 0.0f, 1.0f);
      //glNormal3f(0.0f, 0.0f, -1.0f);
      //glNormal3f(-1.0f, 0.0f, -1.0f);
      glTexCoord2f(0.0f, 0.0f);
      glVertex3f(-1.5f, -1.0f, -1.5f);
      //glNormal3f(-1.0f, 0.0f, -1.0f);
      glTexCoord2f(1.0, 0.0f);
      glVertex3f(-1.5f, 1.0f, 0.0f);
      //glNormal3f(1.0f, 0.0f, -1.0f);
      glTexCoord2f(1.0f, 1.0f);
      glVertex3f(1.5f, 1.0f, 0.0f);
      //glNormal3f(1.0f, 0.0f, -1.0f);
      glTexCoord2f(0.0f, 1.0f);
      glVertex3f(1.5f, -1.0f, -1.5f);
     
      //Left
      glColor3f(0.0f, 1.0f, 0.0f);
      //glNormal3f(-1.0f, 0.0f, 0.0f);
      //glNormal3f(-1.0f, 0.0f, -1.0f);
      glVertex3f(-1.5f, -1.0f, -1.5f);
      //glNormal3f(-1.0f, 0.0f, 1.0f);
      glVertex3f(-1.5f, -1.0f, 1.5f);
      //glNormal3f(-1.0f, 0.0f, 1.0f);
      glVertex3f(-1.5f, 1.0f, 0.0f);
      //glNormal3f(-1.0f, 0.0f, -1.0f);
      glVertex3f(-1.5f, -1.0f, -1.5f);
/*
      //Front
      glVertex3f(-1.5f, 1.0f, 1.5f);
      glVertex3f(0.0f, -1.0f, 1.5f);
      glVertex3f(1.5f, 1.0f, 1.5f);
      glVertex3f(-1.5f, 1.0f, 1.5f);

      //biru
      //glColor3f(0.0f,0.0f,1.0f);
      glVertex3f(-1.5f, 1.0f, -1.5f);
      glVertex3f(0.0f, -1.0f, -1.5f);
      glVertex3f(1.5f, 1.0f, -1.5);
      glVertex3f(-1.5f, 1.0f, -1.5f);

      //merah
      glColor3f(1.0f,0.0f,0.0f);
      glTexCoord2f(-1.5f, 1.0f);
      glVertex3f(-1.5f, 1.0f, 1.5f);
      glTexCoord2f(-1.5f, 1.0f);
      glVertex3f(-1.5f, 1.0f, -1.5f);
      glTexCoord2f(0.0f, -1.0f);
      glVertex3f(0.0f, -1.0f, -1.5f);
      glTexCoord2f(0.0f, -1.0f);
      glVertex3f(0.0f, -1.0f,1.5f);

      //ijo
      glColor3f(0.0f,1.0f,0.0f);
      glTexCoord2f(1.5f, 1.0f);
      glVertex3f(1.5f,1.0f,1.5f);
      glTexCoord2f(1.5f, 1.0f);
      glVertex3f(1.5f,1.0f,-1.5f);
      glTexCoord2f(0.0f, -1.0f);
      glVertex3f(0.0f, -1.0f, -1.5f);
      glTexCoord2f(0.0f, -1.0f);
      glVertex3f(0.0f, -1.0f,1.5f);
*/

      glEnd();
      glDisable(GL_TEXTURE_2D);
     
      glutSwapBuffers();
}

void update(int value) {
      _angle += 1.5f;
      if (_angle > 360) {
            _angle -= 360;
      }
     
      glutPostRedisplay();
      glutTimerFunc(25, update, 0);
}

int main(int argc, char** argv) {
      glutInit(&argc, argv);
      glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
      glutInitWindowSize(400, 400);
      glutCreateWindow("Lighting");
      initRendering();
      glutDisplayFunc(drawScene);
      glutReshapeFunc(handleResize);     
      glutTimerFunc(25, update, 0);
     
      glutMainLoop();
      return 0;
}




0 komentar:

Post a Comment