22 June 2012

opengl segitiga dengan dev


#include <cstdlib>
#include <iostream>
#include <windows.h> 
#include <iostream>
#include <stdlib.h>
#include <stdio.h> 
#include <string.h>
#include <stdarg.h> 
#include <GL/glut.h>
#include <GL/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() {
     glClearColor (1.0, 1.0, 1.0, 0.0);
      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);
     
      //atas kiri
    glColor3f(1.0f, 1.0f, 0.0f);
      //glNormal3f(0.0f, 0.0f, 1.0f);
      //glTexCoord2f(0.0f, 0.0f);
      glVertex3f(-2.0f, 0.0f, 0.0f);
     
    //glNormal3f(1.0f, 0.0f, 1.0f);
      //glTexCoord2f(1.0f, 0.0f);
      glVertex3f(0.0f, 0.0f, 3.0f);
     
    //glNormal3f(1.0f, 0.0f, 1.0f);
      //glTexCoord2f(1.0f, 1.0f);
      glVertex3f(0.0f, 3.0f, 0.0f);
     
    //glNormal3f(-1.0f, 0.0f, 1.0f);
      //glTexCoord2f(0.0f, 1.0f);
      glVertex3f(-2.0f, 0.0f, 0.0f);
     
      //atas kanan
      glColor3f(0.0f, 1.0f, 0.0f);
      //glNormal3f(1.0f, 0.0f, 0.0f);
      //glTexCoord2f(2.0f, 0.0f);
      glVertex3f(0.0f, 0.0f, 3.0f);
     
      //glNormal3f(1.0f, 0.0f, -1.0f);
      //glTexCoord2f(2.0f, 2.0f);
      glVertex3f(2.0f, 0.0f, 0.0f);
     
      //glNormal3f(1.0f, 0.0f, 1.0f);
      //glTexCoord2f(0.0f, 2.0f);
      glVertex3f(0.0f, 3.0f, 0.0f);
     
      //glNormal3f(1.0f, 0.0f, 1.0f);
      //glTexCoord2f(0.0f, 0.0f);
      glVertex3f(0.0f, 0.0f, 3.0f);
     
      //kiri bawah
      glColor3f(0.0f, 1.0f, 0.0f);
      //glNormal3f(0.0f, 0.0f, -1.0f);
      //glTexCoord2f(3.0f, 0.0f);
      glVertex3f(-2.0f, 0.0f, 0.0f);
     
    //glNormal3f(-1.0f, 0.0f, -1.0f);
    //glTexCoord2f(3.0f, 3.0f);
      glVertex3f(0.0f, 0.0f, 3.0f);
     
    //glNormal3f(1.0f, 0.0f, -1.0f);
    //glTexCoord2f(0.0f, 3.0f);
      glVertex3f(0.0f, -3.0f, 0.0f);
     
    //glNormal3f(1.0f, 0.0f, -1.0f);
      //glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-2.0f, 0.0f, 0.0f);
     
      //kanan bawah
      glColor3f(1.0f, 1.0f, 0.0f);
      //glNormal3f(-1.0f, 0.0f, 0.0f);
    //glTexCoord2f(0.0f, 0.0f);
      glVertex3f(0.0f, 0.0f, 3.0f);
     
    //glNormal3f(-1.0f, 0.0f, 1.0f);
    //glTexCoord2f(4.0f, 0.0f);
      glVertex3f(2.0f, 0.0f, 0.0f);
     
    //glNormal3f(-1.0f, 0.0f, 1.0f);
    //glTexCoord2f(4.0f, 4.0f);
      glVertex3f(0.0f, -3.0f, 0.0f);
     
    //glNormal3f(-1.0f, 0.0f, -1.0f);
    //glTexCoord2f(0.0f, 4.0f);
      glVertex3f(0.0f, 0.0f, 3.0f);
     
    //belakang atas
      glColor3f(0.0f, 0.0f, 1.0f);
      //glNormal3f(-1.0f, 0.0f, 0.0f);
    //glTexCoord2f(0.0f, 0.0f);
      glVertex3f(-2.0f, 0.0f, 0.0f);
     
    //glNormal3f(-1.0f, 0.0f, 1.0f);
    //glTexCoord2f(4.0f, 0.0f);
      glVertex3f(2.0f, 0.0f, 0.0f);
     
    //glNormal3f(-1.0f, 0.0f, 1.0f);
    //glTexCoord2f(4.0f, 4.0f);
      glVertex3f(0.0f, 3.0f, 0.0f);
     
    //glNormal3f(-1.0f, 0.0f, -1.0f);
    //glTexCoord2f(0.0f, 4.0f);
      glVertex3f(-2.0f, 0.0f, 0.0f);
   
   
    //balakang bawah
      glColor3f(1.0f, 0.0f, 1.0f);
      //glNormal3f(-1.0f, 0.0f, 0.0f);
    //glTexCoord2f(0.0f, 0.0f);
      glVertex3f(-2.0f, 0.0f, 0.0f);
     
    //glNormal3f(-1.0f, 0.0f, 1.0f);
    //glTexCoord2f(4.0f, 0.0f);
      glVertex3f(2.0f, 0.0f, 0.0f);
     
    //glNormal3f(-1.0f, 0.0f, 1.0f);
    //glTexCoord2f(4.0f, 4.0f);
      glVertex3f(0.0f, -3.0f, 0.0f);
     
    //glNormal3f(-1.0f, 0.0f, -1.0f);
    //glTexCoord2f(0.0f, 4.0f);
      glVertex3f(-2.0f, 0.0f, 0.0f);
   
    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[])
{
    printf("dyasprogramming.blogspot.com");
    glutInit(&argc, argv);
      glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
      glutInitWindowSize(400, 400);
      glutCreateWindow("Responsi Grafika-dyasprogramming.blogspot.com");
      initRendering();
      glutDisplayFunc(drawScene);
      glutReshapeFunc(handleResize);     
      glutTimerFunc(25, update, 0);
     
      glutMainLoop();
    system("PAUSE");
    return EXIT_SUCCESS;
}