دوست من ببنید هر فایل دارای خصوصیات مخصوص به خودشه ولی من برای مثال ساده ترین فایل های تصویری که bitmap باشه رو بررسی کردم شما اول هدر فایل را می خونید و با اطلاعات بدست اومده سراغ دیتا ها ی اون میرید (حالا در مثال قبل ما این کار را با کتابخانه gd کردیم اینجا دستی)و با تابع :
glDrawPixels
تصویر خوتون را در مکان دلخواهتون ترسیم می کنید فایل ضمیمه :
im.jpg
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glut32.lib")
#include <windows.h>
#include <GL/glut.h>
#include <fstream>
//#include <iostream>
#include <cstdlib>
typedef char byte1;
typedef unsigned char ubyte1;
struct pixmap
{
unsigned width,
height;
ubyte1 *data;
enum {RED = 0, GREEN, BLUE};
pixmap(const char *fname);
pixmap();
~pixmap();
void read(const char *fname);
ubyte1 pixel_elem(int x, int y, int elem);
ubyte1 *pixel_pos(int x, int y);
};
pixmap::pixmap(const char *fname)
: width(0), height(0), data(0)
{
this->read(fname);
}
pixmap::pixmap()
: width(0), height(0), data(0) {}
pixmap::~pixmap()
{
if( data )
delete[] data;
}
void pixmap::read(const char *fname)
{
using namespace std;
unsigned short planes; // number of planes in image (must be 1)
unsigned short bpp; // number of bits per pixel (must be 24)
ifstream fin(fname, ios::in | ios::binary);
if( !fin )
{
//cerr << "File not found " << fname << '\n';
exit(1);
}
fin.seekg(18, ios::cur);
fin.read((byte1 *)&width, sizeof(unsigned));
fin.read((byte1 *)&height, sizeof(unsigned));
//cout << "width: " << width << " height: " << height << '\n';
fin.read((byte1 *)&planes, sizeof(unsigned short));
if( planes != 1 )
{
//cout << "Planes from " << fname << " is not 1: " << planes << "\n";
exit(1);
}
fin.read((byte1 *)&bpp, sizeof(unsigned short));
if( bpp != 24 )
{
//cout << "Bpp from " << fname << " is not 24: " << bpp << "\n";
exit(1);
}
fin.seekg(24, ios::cur);
unsigned size(width * height * 3); // size of the image in bytes (3 is to RGB component).
data = new ubyte1[size];
fin.read((byte1 *)data, size);
ubyte1 R,G,B; // temporary color storage for gbr-rgb conversion.
for( int i(0); i < size; i += 3 )
{
G=data[i];
B=data[i+1];
R=data[i+2];
data[i]=R;
data[i+1]=G;
data[i+2]=B;
}
}
ubyte1 pixmap::pixel_elem(int x, int y, int elem)
{
int pos = (y*width+x) * 3 + elem;
return data[pos];
}
ubyte1 *pixmap::pixel_pos(int x, int y)
{
int pos = (y * width + x) * 3;
return &data[pos];
}
void init(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,200.0,0.0,150.0);
}
pixmap img;
void lineSegments(void)
{
glClear(GL_COLOR_BUFFER_BIT); //clears display window ??? ???? ???? ?????
glRasterPos2i(0, 0);
glDrawPixels(img.width, img.height, GL_RGB, GL_UNSIGNED_BYTE, img.data);
glFlush(); //process all OGL functions immediately ???? ?? ???? ????
}
void keyboard(unsigned char key, int x, int y)
{
if( key == 27)
exit(0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv); //initialise GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(50, 100); //top left display window pos
glutInitWindowSize(500, 500); //display win width and height in pixel coords
img.read("g:/ff.bmp");
glutCreateWindow(" lines"); //now create display win
init(); //run initilization procs
glutDisplayFunc(lineSegments); //call drawing func
glutKeyboardFunc(keyboard);
glutMainLoop(); //display all and wait
return 0;
}