Gambar 3D sederhana
Source Code :
#include <stdlib.h> // standard definitions
#include <math.h> // math definitions
#include <stdio.h> // standard I/O
#include <iostream>
// include files are in a slightly different location for MacOS
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
using namespace std;
GLuint texture[4];
GLint slices=16;
GLint stacks=16;
struct Image {
unsigned long sizeX;
unsigned long sizeY;
char *data;
};
typedef struct Image Image;
static unsigned int getint(FILE *fp)
{
int c, c1, c2, c3;
// get 4 bytes
c = getc(fp);
c1 = getc(fp);
c2 = getc(fp);
c3 = getc(fp);
return ((unsigned int) c) +
(((unsigned int) c1) << 8) +
(((unsigned int) c2) << 16) +
(((unsigned int) c3) << 24);
}
static unsigned int getshort(FILE *fp)
{
int c, c1;
//get 2 bytes
c = getc(fp);
c1 = getc(fp);
return ((unsigned int) c) + (((unsigned int) c1) << 8);
}
int ImageLoad(const char *filename, Image *image) {
FILE *file;
unsigned long size; // size of the image in bytes.
unsigned long i; // standard counter.
unsigned short int planes; // number of planes in image (must be 1)
unsigned short int bpp; // number of bits per pixel (must be 24)
char temp; // used to convert bgr to rgb color.
// make sure the file is there.
if ((file = fopen(filename, "rb"))==NULL) {
printf("File Not Found : %s\n",filename);
return 0;
}
// seek through the bmp header, up to the width/height:
fseek(file, 18, SEEK_CUR);
// No 100% errorchecking anymore!!!
// read the width
image->sizeX = getint (file);
//printf("Width of %s: %lu\n", filename, image->sizeX);
// read the height
image->sizeY = getint (file);
//printf("Height of %s: %lu\n", filename, image->sizeY);
// calculate the size (assuming 24 bits or 3 bytes per pixel).
size = image->sizeX * image->sizeY * 3;
// read the planes
planes = getshort(file);
if (planes != 1) {
printf("Planes from %s is not 1: %u\n", filename, planes);
return 0;
}
// read the bpp
bpp = getshort(file);
if (bpp != 24) {
printf("Bpp from %s is not 24: %u\n", filename, bpp);
return 0;
}
// seek past the rest of the bitmap header.
fseek(file, 24, SEEK_CUR);
// read the data.
image->data = (char *) malloc(size);
if (image->data == NULL) {
printf("Error allocating memory for color-corrected image data");
return 0;
}
if ((i = fread(image->data, size, 1, file)) != 1) {
printf("Error reading image data from %s.\n", filename);
return 0;
}
for (i=0;i<size;i+=3) { // reverse all of the colors. (bgr -> rgb)
temp = image->data[i];
image->data[i] = image->data[i+2];
image->data[i+2] = temp;
}
// we're done.
return 1;
}
Image * loadTexture(const char *filename)
{
Image *image1;
// allocate space for texture
image1 = (Image *) malloc(sizeof(Image));
if (image1 == NULL) {
printf("Error allocating space for image");
exit(0);
}
if (!ImageLoad(filename, image1)) {
exit(1);
}
return image1;
}
void setTexture(void)
{
glClearColor(1., 0.0, 0.5, 0.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
Image *image1[4];
image1[0] = loadTexture("worldmap.bmp");
image1[1] = loadTexture("rivet_steel_1.bmp");
image1[2] = loadTexture("test.bmp");
image1[3] = loadTexture("black.bmp");
glGenTextures(4, texture);
for (int i=0; i<4; i++)
{
if(image1[0] == NULL)
{
printf("Image was not returned from loadTexture\n");
exit(0);
}
glBindTexture(GL_TEXTURE_2D, texture[i]);
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, image1[i]->sizeX, image1[i]->sizeY, 0,
GL_RGB, GL_UNSIGNED_BYTE, image1[i]->data);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_FLAT);
}
void setMaterial()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial ( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE ) ;
}
void setLighting()
{
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
// Create light components
GLfloat ambientLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat diffuseLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat specularLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat position[] = { -2.5f, 2.0f, 3.0f, 1.0f };
// Assign created components to GL_LIGHT0
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
glLightfv(GL_LIGHT0, GL_POSITION, position);
}
void setCamera(){
// setting proyeksi kamera
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 3.0/3.0, 1, 40);
// setting posisi kamera
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(2, 2, 2, 0, 0, 0, 0, 1, 0);
}
void drawBox(float x1,float y1,float z1, float x2, float y2, float z2)
{
glPushMatrix();
glBegin(GL_QUADS);
//depan
glTexCoord2f(0.0, 0.0);glVertex3f(x1,y1,z1);
glTexCoord2f(1.0, 0.0);glVertex3f(x2,y1,z1);
glTexCoord2f(1.0, 1.0);glVertex3f(x2,y2,z1);
glTexCoord2f(0.0, 1.0);glVertex3f(x1,y2,z1);
//atas
glTexCoord2f(0.0, 0.0);glVertex3f(x1,y2,z1);
glTexCoord2f(0.0, 1.0);glVertex3f(x2,y2,z1);
glTexCoord2f(1.0, 1.0);glVertex3f(x2,y2,z2);
glTexCoord2f(1.0, 0.0);glVertex3f(x1,y2,z2);
//belakang
glTexCoord2f(0.0, 0.0);glVertex3f(x1,y2,z2);
glTexCoord2f(0.0, 1.0);glVertex3f(x2,y2,z2);
glTexCoord2f(1.0, 1.0);glVertex3f(x2,y1,z2);
glTexCoord2f(1.0, 0.0);glVertex3f(x1,y1,z2);
//bawah
glTexCoord2f(0.0, 0.0);glVertex3f(x1,y1,z2);
glTexCoord2f(1.0, 0.0);glVertex3f(x2,y1,z2);
glTexCoord2f(1.0, 1.0);glVertex3f(x2,y1,z1);
glTexCoord2f(0.0, 1.0);glVertex3f(x1,y1,z1);
//samping kiri
glTexCoord2f(0.0, 0.0);glVertex3f(x1,y1,z1);
glTexCoord2f(1.0, 0.0);glVertex3f(x1,y2,z1);
glTexCoord2f(1.0, 1.0);glVertex3f(x1,y2,z2);
glTexCoord2f(0.0, 1.0);glVertex3f(x1,y1,z2);
//samping kanan
glTexCoord2f(0.0, 0.0);glVertex3f(x2,y1,z1);
glTexCoord2f(1.0, 0.0);glVertex3f(x2,y2,z1);
glTexCoord2f(1.0, 1.0);glVertex3f(x2,y2,z2);
glTexCoord2f(0.0, 1.0);glVertex3f(x2,y1,z2);
glEnd();
glPopMatrix();
}
void kunci(unsigned char key, int x, int y) {
switch (key)
{
case 27 :
case 'q':
exit(0);
break;
}
glutPostRedisplay();
}
void drawPC()
{
glBindTexture(GL_TEXTURE_2D, texture[2]);
drawBox(-0.5,0.0,0.5,0.5,0.7,0.45);
glBindTexture(GL_TEXTURE_2D, texture[3]);
drawBox(-0.55,-0.05,0.48,0.55,0.75,0.40);
drawBox(-0.1,-0.1,0.48,0.1,-0.05,0.40);
drawBox(-0.2,-0.15,0.6,0.2,-0.1,0.30);
glBindTexture(GL_TEXTURE_2D, texture[2]);
drawBox(-0.5,0.0,0.5,0.5,0.7,0.45);
glBindTexture(GL_TEXTURE_2D, texture[3]);
drawBox(0.55,-0.05,0.48,0.55,0.75,0.40);
drawBox(0.1,-0.1,0.48,0.1,-0.05,0.40);
drawBox(0.2,-0.15,0.6,0.2,-0.1,0.30);
}
void renderScene(void){
glClearColor(1,1,1,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glShadeModel(GL_SMOOTH);
glPushMatrix();
//glTranslatef(1.0,0.50,0.12);
glScalef(0.5,0.5,0.5);
drawPC();
glPopMatrix();
/*
//CONTOH MENGGAMBAR SPHERE
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glutSolidSphere(0.5,50,50);
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
*/
// Draw a red x-axis, a green y-axis, and a blue z-axis.
glBegin(GL_LINES);
glColor3f(1, 0, 0); glVertex3f(0, 0, 0); glVertex3f(10, 0, 0);
glColor3f(0, 1, 0); glVertex3f(0, 0, 0); glVertex3f(0, 10, 0);
glColor3f(0, 0, 1); glVertex3f(0, 0, 0); glVertex3f(0, 0, 10);
glEnd();
glutSwapBuffers();
}
void init(){
//setMaterial();
setTexture();
setLighting();
setCamera();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(600,600);
glutInitWindowPosition(100,100);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutCreateWindow("Primitive Object");
glutDisplayFunc(renderScene);
glutKeyboardFunc(kunci);
init();
glutMainLoop();
return 0;
}
Wednesday, 3 December 2014
3D Sederhana Dengan OpenGL
Screenshot :
Post Author
Pakai kode disamping emoticon, terus tambahkan satu spasi sob..
:j :k :l :m :n :o :p :q :r
:s :t :u :v :w :x :y :z :ab
0 Comments