cara membuat project seperti dia atas saya menggunakan DEV++ dan codingnya saya publikasikan ni gan,silahkan mencoba dan mengembangkan nya gan :D.
Class imageloader
#include <assert.h>
#include <fstream>
#include "imageloader.h"
using namespace std;
Image::Image(char* ps, int w, int h) : pixels(ps), width(w), height(h)
{
}
Image::~Image() {
delete[] pixels;
}
namespace {
//Konversi 4 buah karakter
ke integer, menggunakan bentuk little-endian
int toInt(const
char* bytes) {
return
(int)(((unsigned char)bytes[3] << 24) |
((unsigned char)bytes[2] << 16) |
((unsigned char)bytes[1] << 8) |
(unsigned char)bytes[0]);
}
//Konversi 2 buah
karakter ke integer, menggunakan bentuk little-endian
short
toShort(const char* bytes) {
return
(short)(((unsigned char)bytes[1] << 8) |
(unsigned char)bytes[0]);
}
//Membaca 4 byte
selanjutnya sebagai integer, menggunakan bentuk little-endian
int
readInt(ifstream &input) {
char
buffer[4];
input.read(buffer,
4);
return
toInt(buffer);
}
short
readShort(ifstream &input) {
char
buffer[2];
input.read(buffer,
2);
return
toShort(buffer);
}
template<class
T>
class auto_array {
private:
T*
array;
mutable
bool isReleased;
public:
explicit
auto_array(T* array_ = NULL) :
array(array_),
isReleased(false) {
}
auto_array(const
auto_array<T> &aarray) {
array
= aarray.array;
isReleased
= aarray.isReleased;
aarray.isReleased
= true;
}
~auto_array()
{
if
(!isReleased && array != NULL) {
delete[]
array;
}
}
T*
get() const {
return
array;
}
T
&operator*() const {
return
*array;
}
void
operator=(const auto_array<T> &aarray) {
if
(!isReleased && array != NULL) {
delete[]
array;
}
array
= aarray.array;
isReleased
= aarray.isReleased;
aarray.isReleased
= true;
}
T*
operator->() const {
return
array;
}
T*
release() {
isReleased
= true;
return
array;
}
void
reset(T* array_ = NULL) {
if
(!isReleased && array != NULL) {
delete[]
array;
}
array
= array_;
}
T*
operator+(int i) {
return
array + i;
}
T
&operator[](int i) {
return
array[i];
}
};
}
Image* loadBMP(const char* filename) {
ifstream input;
input.open(filename,
ifstream::binary);
assert(!input.fail()
|| !"File tidak ditemukan!!!");
char buffer[2];
input.read(buffer,
2);
assert(buffer[0]
== 'B' && buffer[1] == 'M' || !"Bukan file bitmap!!!");
input.ignore(8);
int dataOffset =
readInt(input);
int headerSize =
readInt(input);
int width;
int height;
switch(headerSize)
{
case
40:
width
= readInt(input);
height
= readInt(input);
input.ignore(2);
assert(readShort(input)
== 24 || !"Gambar tidak 24 bits per pixel!");
assert(readShort(input)
== 0 || !"Gambar dikompres!");
break;
case
12:
width
= readShort(input);
height
= readShort(input);
input.ignore(2);
assert(readShort(input)
== 24 || !"Gambar tidak 24 bits per pixel!");
break;
case
64:
assert(!"Tidak
dapat mengambil OS/2 V2 bitmaps");
break;
case
108:
assert(!"Tidak
dapat mengambil Windows V4 bitmaps");
break;
case
124:
assert(!"Tidak
dapat mengambil Windows V5 bitmaps");
break;
default:
assert(!"Format
bitmap ini tidak diketahui!");
}
//Membaca data
int bytesPerRow =
((width * 3 + 3) / 4) * 4 - (width * 3 % 4);
int size = bytesPerRow
* height;
auto_array<char>
pixels(new char[size]);
input.seekg(dataOffset,
ios_base::beg);
input.read(pixels.get(),
size);
//Mengambil data
yang mempunyai format benar
auto_array<char>
pixels2(new char[width * height * 3]);
for(int y = 0; y
< height; y++) {
for(int
x = 0; x < width; x++) {
for(int
c = 0; c < 3; c++) {
pixels2[3
* (width * y + x) + c] =
pixels[bytesPerRow
* y + 3 * x + (2 - c)];
}
}
}
input.close();
return new
Image(pixels2.release(), width, height);
}
Class main_teksture
#include <iostream>
#include <stdlib.h>
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include "imageloader.h"
using namespace std;
const float BOX_SIZE = 7.0f; //Panjang tiap sisi kubus
float _angle = 0;
//Rotasi terhadap box
GLuint _gambar1; //ID
OpenGL untuk tekstur
GLuint _gambar2;
GLuint _gambar3;
GLuint _gambar4;
GLuint _gambar5;
GLuint _gambar6;
void handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case
27: //Tekan Escape untuk EXIT
exit(0);
}
}
//Membuat gambar menjadi tekstur kemudian berikan ID pada tekstur
GLuint loadTexture(Image* image) {
GLuint textureId;
glGenTextures(1,
&textureId);
glBindTexture(GL_TEXTURE_2D,
textureId);
glTexImage2D(GL_TEXTURE_2D,
0, GL_RGB, image->width, image->height, 0, GL_RGB, GL_UNSIGNED_BYTE,
image->pixels);
return textureId;
}
void initRendering() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
Image* image1 =
loadBMP("a.bmp");
Image* image2 =
loadBMP("b.bmp");
Image* image3 =
loadBMP("c.bmp");
Image* image4 =
loadBMP("d.bmp");
Image* image5 =
loadBMP("e.bmp");
Image* image6 =
loadBMP("f.bmp");
_gambar1 =
loadTexture(image1);
_gambar2 =
loadTexture(image2);
_gambar3 =
loadTexture(image3);
_gambar4 =
loadTexture(image4);
_gambar5 =
loadTexture(image5);
_gambar6 =
loadTexture(image6);
delete image1;
delete image2;
delete image3;
delete image4;
delete image5;
delete image6;
}
void handleResize(int w, int h) {
glViewport(0, 0,
w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,
(float)w / (float)h, 1.0, 200.0);
}
void drawScene() {
glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f,
0.0f, -20.0f);
GLfloat
ambientLight[] = {0.3f, 0.3f, 0.3f, 1.0f};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,
ambientLight);
GLfloat
lightColor[] = {0.7f, 0.7f, 0.7f, 1.0f};
GLfloat lightPos[]
= {-2 * BOX_SIZE, BOX_SIZE, 4 * BOX_SIZE, 1.0f};
glLightfv(GL_LIGHT0,
GL_DIFFUSE, lightColor);
glLightfv(GL_LIGHT0,
GL_POSITION, lightPos);
glRotatef(-_angle,
30.0f, 10.0f, 20.0f);
//glBegin(GL_QUADS);
//Sisi atas
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,
_gambar1);
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor3f(1.0f,
1.0f, 1.0f);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glNormal3f(0.0,
1.0f, 0.0f);
glVertex3f(-BOX_SIZE
/ 2, BOX_SIZE / 2, -BOX_SIZE / 2);
glTexCoord2f(1.0f,
0.0f);
glVertex3f(-BOX_SIZE
/ 2, BOX_SIZE / 2, BOX_SIZE / 2);
glTexCoord2f(1.0f,
1.0f);
glVertex3f(BOX_SIZE
/ 2, BOX_SIZE / 2, BOX_SIZE / 2);
glTexCoord2f(0.0f,
1.0f);
glVertex3f(BOX_SIZE
/ 2, BOX_SIZE / 2, -BOX_SIZE / 2);
glEnd();
//Sisi bawah
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,
_gambar2);
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor3f(1.0f,
1.0f, 1.0f);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glNormal3f(0.0,
-1.0f, 0.0f);
glVertex3f(-BOX_SIZE
/ 2, -BOX_SIZE / 2, -BOX_SIZE / 2);
glTexCoord2f(1.0f,
0.0f);
glVertex3f(BOX_SIZE
/ 2, -BOX_SIZE / 2, -BOX_SIZE / 2);
glTexCoord2f(1.0f,
1.0f);
glVertex3f(BOX_SIZE
/ 2, -BOX_SIZE / 2, BOX_SIZE / 2);
glTexCoord2f(0.0f,
1.0f);
glVertex3f(-BOX_SIZE
/ 2, -BOX_SIZE / 2, BOX_SIZE / 2);
glEnd();
//Sisi kiri
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,
_gambar3);
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor3f(1.0f,
1.0f, 1.0f);
glBegin(GL_QUADS);
glNormal3f(-1.0,
0.0f, 0.0f);
glTexCoord2f(0.0f,
0.0f);
glVertex3f(-BOX_SIZE
/ 2, -BOX_SIZE / 2, -BOX_SIZE / 2);
glTexCoord2f(1.0f,
0.0f);
glVertex3f(-BOX_SIZE
/ 2, -BOX_SIZE / 2, BOX_SIZE / 2);
glTexCoord2f(1.0f,
1.0f);
glVertex3f(-BOX_SIZE
/ 2, BOX_SIZE / 2, BOX_SIZE / 2);
glTexCoord2f(0.0f,
1.0f);
glVertex3f(-BOX_SIZE
/ 2, BOX_SIZE / 2, -BOX_SIZE / 2);
glEnd();
//Sisi kanan
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,
_gambar4);
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor3f(1.0f,
1.0f, 1.0f);
glBegin(GL_QUADS);
glNormal3f(1.0,
0.0f, 0.0f);
glTexCoord2f(0.0f,
0.0f);
glVertex3f(BOX_SIZE
/ 2, -BOX_SIZE / 2, -BOX_SIZE / 2);
glTexCoord2f(1.0f,
0.0f);
glVertex3f(BOX_SIZE
/ 2, BOX_SIZE / 2, -BOX_SIZE / 2);
glTexCoord2f(1.0f,
1.0f);
glVertex3f(BOX_SIZE
/ 2, BOX_SIZE / 2, BOX_SIZE / 2);
glTexCoord2f(0.0f,
1.0f);
glVertex3f(BOX_SIZE
/ 2, -BOX_SIZE / 2, BOX_SIZE / 2);
glEnd();
//Sisi depan
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,
_gambar5);
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor3f(1.0f,
1.0f, 1.0f);
glBegin(GL_QUADS);
glNormal3f(0.0,
0.0f, 1.0f);
glTexCoord2f(0.0f,
0.0f);
glVertex3f(-BOX_SIZE
/ 2, -BOX_SIZE / 2, BOX_SIZE / 2);
glTexCoord2f(1.0f,
0.0f);
glVertex3f(BOX_SIZE
/ 2, -BOX_SIZE / 2, BOX_SIZE / 2);
glTexCoord2f(1.0f,
1.0f);
glVertex3f(BOX_SIZE
/ 2, BOX_SIZE / 2, BOX_SIZE / 2);
glTexCoord2f(0.0f,
1.0f);
glVertex3f(-BOX_SIZE
/ 2, BOX_SIZE / 2, BOX_SIZE / 2);
glEnd();
//Sisi belakang
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,
_gambar6);
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor3f(1.0f,
1.0f, 1.0f);
glBegin(GL_QUADS);
glNormal3f(0.0,
0.0f, -1.0f);
glTexCoord2f(0.0f,
0.0f);
glVertex3f(-BOX_SIZE
/ 2, -BOX_SIZE / 2, -BOX_SIZE / 2);
glTexCoord2f(1.0f,
0.0f);
glVertex3f(-BOX_SIZE
/ 2, BOX_SIZE / 2, -BOX_SIZE / 2);
glTexCoord2f(1.0f,
1.0f);
glVertex3f(BOX_SIZE
/ 2, BOX_SIZE / 2, -BOX_SIZE / 2);
glTexCoord2f(0.0f,
1.0f);
glVertex3f(BOX_SIZE
/ 2, -BOX_SIZE / 2, -BOX_SIZE / 2);
glEnd();
glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
}
//Panggil setiap 25ms
void update(int value) {
_angle += 1.0f;
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("672012228");
initRendering();
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutTimerFunc(25,
update, 0);
glutMainLoop();
return 0;
}
YUSUF BAGAS SAPUTRO
672012228
672012228.blogspot.com
0 komentar:
Posting Komentar