#include #ifdef _WIN32 #define NOMINMAX #include #endif #include #include #include #include #include #ifndef M_PI #define M_PI 3.14159265358979323846 #endif #include "cvec2t.h" #include "cvec3t.h" #include "tga.h" typedef CVec2T Vec2f; typedef CVec3T Vec3f; namespace WindowParams { static int WindowWidth = 800; static int WindowHeight = 600; static int MainWindow; }; namespace Params { const float WorldWidth = 2; const float WorldHeight = 2; const int TimerStep = 16; // millisec const Vec3f BackgroundColor(0,0,0); }; const Vec3f white(1,1,1); gliGenericImage *image; unsigned int texture_id; void Reshape(int width, int height) { // glViewport defines part of the window we are going to use WindowParams::WindowWidth = width; WindowParams::WindowHeight = height; glViewport(0,0,width,height); // initialize the viewing transformation (camera position) to identity; // this corresponds to camera looking down negative z axis glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // specify the projection transformation (camera parameters); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, Params::WorldWidth,0,Params::WorldHeight); } void Draw() { // set color to initialize the window to; the 4th component // is the alpha value and is not used unless blending is enabled and the blending mode uses destimation alpha glClearColor( 0, 0,0,1); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glPushMatrix(); // set the position of the origin of the image glRasterPos2f(0,0); // draw pixels 1-1 (need to have sufficiently large viewport cout << image->format << endl; cout << image->width << endl; cout << image->height << endl; glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D,texture_id); glColor3f(1,0,0); glTranslatef(0.5,0.5,0); // glScalef(0.1,0.1,0.1); glBegin(GL_POLYGON); glTexCoord2f(0,0); glVertex3f(0,0,0); float scale = 1; glTexCoord2f(scale,0); glVertex3f(1.,0,0); glTexCoord2f(scale,scale); glVertex3f(1.,1.,0); glTexCoord2f(0,scale); glVertex3f(0,1.,0); glEnd(); glDisable(GL_TEXTURE_2D); glPopMatrix(); glutSwapBuffers(); } void InitTexture() { // get 3 texture ids recorded in the array glGenTextures(1,&texture_id); // set current texture glBindTexture(GL_TEXTURE_2D,texture_id); // filter used to resample texture when the texture pixel screen size is smaller than pixel // mipmap filter if mipmaps are used // glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR); // non-mipmap version glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR); // filter used to resample texture when the texture pixel screen size is larger than pixel glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // how the texture behaves when the texture coordinates are outside 0..1 range glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); /* //this builds and loads mipmaps gluBuild2DMipmaps( GL_TEXTURE_2D, 3, image->width, image->height, image->format, GL_UNSIGNED_BYTE, image->pixels); */ // non-mipmap version glTexImage2D(GL_TEXTURE_2D, 0, // resolution level 0 = base image, other values used only when defining mipmaps manually 3, // number of chann image->width, // must be power of 2 + 2*border image->height, // must be power of 2 (the power may be different) 0, // border image->format, // format in which pixels are stored in the source image GL_UNSIGNED_BYTE, // 8 bits per color per pixel as used image->pixels // pointer to image data ); } int main(int argc, char* argv[]) { // initialize glut and parse command-line aguments that glut understands glutInit(&argc, argv); // initialize dislay mode: 4 color components, double buffer and depth buffer glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH); FILE *file; char* filename = "mandrill.tga"; file = fopen(filename, "rb"); if (file == NULL) { cerr << "could not open " << filename << endl; exit(1); } image = gliReadTGA(file, filename); fclose(file); if( !image ) { cerr << "failed reading " << filename << endl; exit(1); } glutInitWindowSize(512,512); WindowParams::MainWindow = glutCreateWindow("Image"); // register all callbacks // gets called whenever the window needs to be redrawn glutDisplayFunc(Draw); // gets called whenever the window changes shape glutReshapeFunc(Reshape); InitTexture() ; // this is an infinite loop get event - dispatch event which never returns glutMainLoop(); return 0; }