FRESH BLOG

FRESH CAN BE ENJOYED FOR US


Follower

Wednesday, December 26, 2012

Membuat fungsi Teksture pada OpenGL

Fungsi Teksture berfungsi untuk mengolah image menjadi bentuk yang diinginkan...

=> pada cpp

#include
#include
#include
#include
#include
#include "texture.h"



//FUNCTION LoadBitmap(char *)
//untuk meload bitmap file dan mengembalikan ke dalam openGL menunjuk ID untuk digunakan pada texture
int LoadBitmap(char *filename)
{
FILE * file;
char temp;
long i;

bitmapinfoheader infoheader;

if( (file = fopen(filename, "rb"))==NULL) return (-1); // membuka file untuk dibaca

fseek(file, 18, SEEK_CUR); // memulai membaca width dan height
fread(&infoheader.biWidth, sizeof(int), 1, file);

fread(&infoheader.biHeight, sizeof(int), 1, file);

fread(&infoheader.biPlanes, sizeof(short int), 1, file);
if (infoheader.biPlanes != 1) {
printf("Planes from %s is not 1: %u\n", filename, infoheader.biPlanes);
return 0;
}

// baca bpp
fread(&infoheader.biBitCount, sizeof(unsigned short int), 1, file);
if (infoheader.biBitCount != 24) {
printf("Bpp from %s is not 24: %d\n", filename, infoheader.biBitCount);
return 0;
}

fseek(file, 24, SEEK_CUR);

// baca data
infoheader.data = (char *) malloc(infoheader.biWidth * infoheader.biHeight * 3);
if (infoheader.data == NULL) {
printf("Error allocating memory for color-corrected image data\n");
return 0;
}

if ((i = fread(infoheader.data, infoheader.biWidth * infoheader.biHeight * 3, 1, file)) != 1) {
printf("Error reading image data from %s.\n", filename);
return 0;
}

for (i=0; i<(infoheader.biWidth * infoheader.biHeight * 3); i+=3) { // membalik semua warna. (bgr ke rgb) temp = infoheader.data[i]; infoheader.data[i] = infoheader.data[i+2]; infoheader.data[i+2] = temp; } fclose(file); // menutup file // mengikat id texture ditentukan dengan parameter 2D GLuint textureId; glGenTextures(1, &textureId); glBindTexture(GL_TEXTURE_2D,textureId); //glBindTexture(GL_TEXTURE_2D, 1); // definisikan texture 2D glTexImage2D(GL_TEXTURE_2D, 0, 3, infoheader.biWidth, infoheader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, infoheader.data); // membuat mipmaps 2D gluBuild2DMipmaps(GL_TEXTURE_2D, 3, infoheader.biWidth, infoheader.biHeight, GL_RGB, GL_UNSIGNED_BYTE, infoheader.data); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);//GL_LINEAR_MIPMAP_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);//GL_LINEAR_MIPMAP_LINEAR); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); free(infoheader.data); // membebaskan memory yang kita gunakan untuk meload texture return (textureId); // mengembalikan texture sekarang } => pada header

#ifndef _TEXTURE
#define _TEXTURE

extern int LoadBitmap(char *filename);

typedef struct /**** Struktur info file BMP****/
{
unsigned int biSize; /* info ukuran header */
int biWidth; /* panjang gambar */
int biHeight; /* tinggi gambar */
unsigned short biPlanes; /* nomor dari warna bidang */
unsigned short biBitCount; /* nomor dari bits per pixel */
unsigned int biCompression; /* tipe kompresi yang digunakan */
unsigned int biSizeImage; /* ukuran data gambar */
int biXPelsPerMeter; /* X pixels per meter */
int biYPelsPerMeter; /* Y pixels per meter */
unsigned int biClrUsed; /* jumlah warna yang digunakan */
unsigned int biClrImportant; /* jumlah warna yang penting */
char *data;
} bitmapinfoheader;

#endif




silahkan comment dibawah kalau measih ada masalah.. Terima Kasih..

Read More...

Tuesday, December 11, 2012

Input Image pada OpenGL

Sekedar share aja... Bagi para programmer dan desainer yang kenal dengan istiah OpenGL, pasti sering mengalami kegusaran.

Disini saya ingin berbagi tutorial mengenai cara memasukkan Image ke OpenGL

----------pada header, tuliskan code seperti ini...
#ifndef __IMAGE_H__
#define __IMAGE_H__

typedef struct{
unsigned short imagic;
unsigned short type;
unsigned short dim;
unsigned short sizeX, sizeY, sizeZ;
char name[128];
unsigned char *data;
} IMAGE;

IMAGE *ImageLoad(char *);

GLuint loadTexture(char* textureName);

#endif /* !__IMAGE_H__! */


------------Sedangkan pada cpp

#include
#include
#include
#include
#include "image.h"

#define IMAGIC 0x01da
#define IMAGIC_SWAP 0xda01

#define SWAP_SHORT_BYTES(x) ((((x) & 0xff) << 8) | (((x) & 0xff00) >> 8))
#define SWAP_LONG_BYTES(x) (((((x) & 0xff) << 24) | (((x) & 0xff00) << 8)) | \ ((((x) & 0xff0000) >> 8) | (((x) & 0xff000000) >> 24)))

typedef struct {
unsigned short imagic;
unsigned short type;
unsigned short dim;
unsigned short sizeX, sizeY, sizeZ;
unsigned long min, max;
unsigned long wasteBytes;
char name[80];
unsigned long colorMap;
FILE *file;
unsigned char *tmp[5];
unsigned long rleEnd;
unsigned long *rowStart;
unsigned long *rowSize;
} Image;

static Image *ImageOpen(char *fileName) {
Image *image;
unsigned long *rowStart, *rowSize, ulTmp;
int x, i;

image = (Image *) malloc(sizeof(Image));
if (image == NULL) {
fprintf(stderr, "Out of memory!\n");
exit(-1);
}
if ((image->file = fopen(fileName, "rb")) == NULL) {
perror(fileName);
exit(-1);
}

//baca header gambar
fread(image, 1, 12, image->file);

//periksa urutan byte
if (image->imagic == IMAGIC_SWAP) {
image->type = SWAP_SHORT_BYTES(image->type);
image->dim = SWAP_SHORT_BYTES(image->dim);
image->sizeX = SWAP_SHORT_BYTES(image->sizeX);
image->sizeY = SWAP_SHORT_BYTES(image->sizeY);
image->sizeZ = SWAP_SHORT_BYTES(image->sizeZ);
}

for (i = 0; i <= image->sizeZ; i++) {
image->tmp[i] = (unsigned char *) malloc(image->sizeX * 256);
if (image->tmp[i] == NULL) {
fprintf(stderr, "Out of memory!\n");
exit(-1);
}
}

if ((image->type & 0xFF00) == 0x0100) { /* RLE image */
x = image->sizeY * image->sizeZ * sizeof(long);
image->rowStart = (unsigned long *) malloc(x);
image->rowSize = (unsigned long *) malloc(x);
if (image->rowStart == NULL || image->rowSize == NULL) {
fprintf(stderr, "Out of memory!\n");
exit(-1);
}
image->rleEnd = 512 + (2 * x);
fseek(image->file, 512, SEEK_SET);
fread(image->rowStart, 1, x, image->file);
fread(image->rowSize, 1, x, image->file);
if (image->imagic == IMAGIC_SWAP) {
x /= sizeof(long);
rowStart = image->rowStart;
rowSize = image->rowSize;
while (x--) {
ulTmp = *rowStart;
*rowStart++ = SWAP_LONG_BYTES(ulTmp);
ulTmp = *rowSize;
*rowSize++ = SWAP_LONG_BYTES(ulTmp);
}
}
}
return image;
}

static void ImageClose(Image *image) {
int i;

fclose(image->file);
for (i = 0; i <= image->sizeZ; i++)
free(image->tmp[i]);
free(image);
}

static void ImageGetRow(Image *image, unsigned char *buf, int y, int z) {
unsigned char *iPtr, *oPtr, pixel;
int count;

if ((image->type & 0xFF00) == 0x0100) { /* RLE image */
fseek(image->file, image->rowStart[y + z * image->sizeY], SEEK_SET);
fread(image->tmp[0], 1, (unsigned int) image->rowSize[y + z
* image->sizeY], image->file);

iPtr = image->tmp[0];
oPtr = buf;
while (1) {
pixel = *iPtr++;
count = (int) (pixel & 0x7F);
if (!count)
return;
if (pixel & 0x80) {
while (count--) {
*oPtr++ = *iPtr++;
}
} else {
pixel = *iPtr++;
while (count--) {
*oPtr++ = pixel;
}
}
}
} else { /* verbatim image */
fseek(image->file, 512 + (y * image->sizeX) + (z * image->sizeX
* image->sizeY), SEEK_SET);
fread(buf, 1, image->sizeX, image->file);
}
}

static void ImageGetRawData(Image *image, char *data) {
int i, j, k;
int remain;

switch (image->sizeZ) {
case 1:
remain = image->sizeX % 4;
break;
case 2:
remain = image->sizeX % 2;
break;
case 3:
remain = (image->sizeX * 3) & 0x3;
if (remain)
remain = 4 - remain;
break;
case 4:
remain = 0;
break;
}

for (i = 0; i < image->sizeY; i++) {
for (k = 0; k < image->sizeZ; k++)
ImageGetRow(image, image->tmp[k + 1], i, k);
for (j = 0; j < image->sizeX; j++)
for (k = 1; k <= image->sizeZ; k++)
*data++ = *(image->tmp[k] + j);
data += remain;
}
}

IMAGE *ImageLoad(char *fileName) {
Image *image;
IMAGE *final;
int sx;

image = ImageOpen(fileName);

final = (IMAGE *) malloc(sizeof(IMAGE));
if (final == NULL) {
fprintf(stderr, "Out of memory!\n");
exit(-1);
}
final->imagic = image->imagic;
final->type = image->type;
final->dim = image->dim;
final->sizeX = image->sizeX;
final->sizeY = image->sizeY;
final->sizeZ = image->sizeZ;

//menyamakan size
sx = ((image->sizeX) * (image->sizeZ) + 3) >> 2;

final->data = (unsigned char *) malloc(sx * image->sizeY
* sizeof(unsigned int));

if (final->data == NULL) {
fprintf(stderr, "Out of memory!\n");
exit(-1);
}

ImageGetRawData(image, (char*) (final->data));
ImageClose(image);
return final;
}

GLuint loadTexture(char* textureName) {

IMAGE *img;
GLenum gluerr;
GLuint textureId;

glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D,textureId);

if (!(img = ImageLoad(textureName))) {
fprintf(stderr,"Error reading a texture.\n");
exit(-1);
}
gluerr = gluBuild2DMipmaps(GL_TEXTURE_2D, 3, img->sizeX, img->sizeY,
GL_RGB, GL_UNSIGNED_BYTE,
(GLvoid *) (img->data));
if (gluerr) {
fprintf(stderr,"GLULib%s\n", gluErrorString(gluerr));
exit(-1);
}

//texture parameter
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);//GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);//GL_LINEAR_MIPMAP_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

return textureId;
}




ini hanya sekedar prototype.. jika mengalami masalah, bisa comment dibawah. Terima Kasih..

Read More...

Saturday, May 7, 2011

Osama Bin Landen was Killed ?



President Barack Obama has confirmed the death of Osama bin Laden, which came during a U.S. military operation in Pakistan. During the assault on a compound where bin Laden was hiding, the terrorist leader was killed and U.S. forces took custody on the body. In an address to the nation from the White House, Obama said no Americans were killed in the operation and there were no civilian casualties.

"Justice has been done," the president said.

Today's operation actually began in August with a credible tip on bin Laden's possible whereabouts, according to the president. That information uncovered a trail to bin Laden.

A small team of U.S. operatives targeted bin Laden in the compound in Abbottabad in Pakistan. Intensive intelligence work since that August tip determined that bin Laden was at the compound. The president said that once he got that confirmation from military authorities today, he authorized the operation.

This operation ended a manhunt that lasted for almost 10 years, and through two U.S. presidencies.

Former President George W. Bush was told of the news in a telephone call from President Obama. Bush, who was president during the 9/11 attacks, said the operation was a "momentous achievement."

"I congratulated him and the men and women of our military and intelligence communities who devoted their lives to this mission," Bush said in a statement. "They have our everlasting gratitude. The fight against terror goes on, but tonight America has sent an unmistakable message: No matter how long it takes, justice will be done."

A U.S. official told news reporters that bin Laden's body is being handled in accordance with Islamic tradition.

EARLIER BLOG POST

Almost 10 years ten years after the Sept. 11 attacks on the United States, the mastermind of those terrorists acts, Osama bin Laden, has been killed. The U.S. has control of his body. Several news organizations are reporting bin Laden's death and President Obama was about to address the American people on the issue.

Bin Laden was an enemy of the United States even before the 9/11 attacks. He was involved in the 1998 bombings of two U.S. Embassies in Africa and the attack on the USS Cole in a Yemeni port in 2000. He referred to the acts against the United States as "blessed terrorism."

"His death brings to an end a tumultuous life that saw bin Laden go from being the carefree son of a Saudi billionaire, to terrorist leader and the most wanted man in the world," says ABC News. "Bin Laden created and funded the al Qaeda terror network, which was responsible for the Sept. 11, 2001, attacks on the United States. The Saudi exile had been a man on the run since the U.S.-led invasion of Afghanistan overthrew the ruling Taliban regime, which harbored bin Laden."

Read more: http://fresnobeehive.com/opinion/2011/05/osama_bin_laden_is_dead.html#ixzz1LgAExefs

Read More...

Tuesday, April 26, 2011

THE SCREET OF 2012?


What will happen in 2012? There are numerous rumors floating around about 2012 and mostly related to the end of days, and that proves confusing for most people in here. Is it true, that the end is near? Obviously the 2012 is growing as a meme, that, instead of enlighten the people, only giving more misunderstanding & confusion, about what happen with our nature. In the end, this meme only blurs the understanding between knowledge and urban legend.

To clarify the 2012 from the perspective of astronomy, the Student Union of Astronomy (Himastron, the organization body for students from the Dept. of Astronomy, ITB), on last Sunday (14th of June, 2009) organizing a talk show about 2012. There were around 300 people from various backgrounds & ages eager to flock & discuss about the subject. Quiet a large number, larger than expected.

The talk show is an attempt to give a proper scientific perspective about natural phenomena that might be entangled with the word ‘2012′, and in the same time give scientific insight for general public. There were three experts from three different fields, Solar Physicist, Solar System Scientist, and Archaeo-Astronomer gave talk about the subject.

Is it true, that 2012 is the end of time as prophesied by the Maya civilization? Hardly! If in particular date is the end of a calendrical cycle of Maya calendar, then, it might be the case. But should it be the end of time? Well, the codices left by the ancient civilization of Maya should be interpreted carefully, without subjugate of modern thinking, because of how the people from different era will perceive the nature differently.

Okay then, if the prophecy can be omitted, and should be accepted as the discourse of archaeo-astronomy, let’s put another case? Is it the ‘planet X’ that would jeopardize the Earth on 2012? Okay, the planet X? The discussion about this planet X started sometimes around the 1980s, among the Solar System researchers in attempt to clarify the orbital behavior of Uranus and Neptune. During the course of time, the research of planet X open the new perspective about the edge of the Solar System, with the spin-off the demotion of Pluto from the planet family member. But what is planet X anyway? Up to recent time, the Solar System researchers still search this planet X far onto the edge of the Solar System. So, let’s put the planet X on the proper position, as the subject in the edge of Solar System, instead of threat of near Earth object.

Well, if planet X is no longer a threat, what about the other? Mmmm .. many still confuse with the movie ‘Knowing’ .. (SPOILER ALERT!). Well well .. will the Earth ‘consumed’ by the blazing inferno from the Sun? Hardly, it’s already 4.5 billion years old Sun, and the Earth stood still, regardless continuous bombardment from the Sun. However, since the dependency of human with the space technology grows recently, a slight disruption from the Sun on satellite could be serious problem for human life, for instance, the disruption on GPS signal. Is it new kind of ‘doom’ for modern people?

All in all, the attempt from the students to disentangling the fact & fiction behind the 2012, hopefully could shed the light, and give deeper insight for general public, about how we, the human perceive the nature through scientific way, and not to be confused with the metaphysical stuffs. Besides, this is IYA, so what else can astronomy do, but to give insight about our place in the universe.

Read More...
Powered By Blogger

SHOUT MIX ^^

Translater

English French German Spain Italian Dutch

Russian Portuguese Japanese Korean Arabic

VISITORS

free counters

Popular Posts

clock

back to top