VPN termux about China.net

TGA texture withOUT sdl

#include <iostream>
#include <fstream>
#include <stdio.h>
       // Standard header for MS Windows applications
#include <GL/gl.h>           // Open Graphics Library (OpenGL) header
#include <GL/glut.h>       // The GL Utility Toolkit (GLUT) Header
 #define FALSE 0
#define TRUE 1
#define KEY_ESCAPE 27
#define g_rotation_speed 0.2
float g_rotation;
 GLuint  texture[0];

using namespace std;

/************************************************************************************
 *  TGA CLASS
 ************************************************************************************/
class TGA
{
  public:

    bool Load(const char *filename);
    void Release();
    void Draw();

  private:
    char *imageData;


    struct tga_header
    {
        unsigned char idLength;
        unsigned char colorMapType;
        unsigned char imageTypeCode;
        unsigned char colorMapSpec[5];
        unsigned short xOrigin;
        unsigned short yOrigin;   
        unsigned short width;
        unsigned short height;
        unsigned char bpp;
        unsigned char imageDesc;
    };
    tga_header tgaheader;
};


void TGA::Draw()
{

    glPixelStorei (GL_UNPACK_ROW_LENGTH, this->tgaheader.width);
     glGenTextures( 1, &texture[0] );

        /* Typical Texture Generation Using Data From The Bitmap */
        glBindTexture( GL_TEXTURE_2D,texture[0]);

    if (this->tgaheader.bpp == 32)
    {
        glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
        gluBuild2DMipmaps(GL_TEXTURE_2D, 3,this->tgaheader.width  , this->tgaheader.height, GL_BGR, GL_UNSIGNED_BYTE, this->imageData);
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

    glEnable(GL_TEXTURE_2D);
    }
    if (this->tgaheader.bpp == 24)
    {
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
 gluBuild2DMipmaps(GL_TEXTURE_2D, 3,this->tgaheader.width  , this->tgaheader.height,GL_BGR, GL_UNSIGNED_BYTE, this->imageData);
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

    glEnable(GL_TEXTURE_2D);
        /*glDrawPixels(this->tgaheader.width, this->tgaheader.height, GL_BGR, GL_UNSIGNED_BYTE, this->imageData);*/
    }
}

void TGA::Release()
{
    free(imageData);        // Free image data from memory
}

bool TGA::Load(const char* filename)
{
    fstream filestr;

    filestr.open (filename, ios::in | ios::binary);                                // Open file
    if (filestr.is_open())                                                        // Do the following actions, if file is opened
    {
        // read TGA header
        filestr.read((char*) &tgaheader , sizeof(struct tga_header));            // Read tga header. For more information: see tga.h and link above
        printf("image type: %i \n", tgaheader.imageTypeCode);   

        // read pixel data
        int imageSize = tgaheader.width * tgaheader.height * tgaheader.bpp;        // Calculate image size

        this->imageData = (char*) malloc(imageSize);                            // Reserve space in the memory to store our image data
        filestr.read((char*) this->imageData, imageSize);                        // Read image data from file, into the reserved memory place


        /*
         * TGA is stored in BGR (Blue-Green-Red) format,
         * we need to convert this to Red-Green-Blue (RGB).
         * The following section does BGR to RGB conversion
         */

        if (tgaheader.bpp == 24)
        {
            for (int i = 0; i < imageSize; i+=3)
            {   
                char c = this->imageData[i];
                this->imageData[i] = this->imageData[i+2];
                this->imageData[i+2] = c;
            }
        }
        else
        if (tgaheader.bpp == 32)
        {
            for (int i = 0; i < imageSize; i+=4)
            {   
                // 32 bits per pixel   =  4 byte per pixel           
                char c = this->imageData[i];
                this->imageData[i] = this->imageData[i+2];
                this->imageData[i+2] = c;
            }
        }

        filestr.close();
    }
    else
    {
        cout << "Error opening file" << endl;
        return -1;
    }

    return 0;
}


/************************************************************************************
 *  Rest of code
 ************************************************************************************/


typedef struct {
    int width;
    int height;
    char* title;

    float field_of_view_angle;
    float z_near;
    float z_far;
} glutWindow;
glutWindow win;


TGA g_image;

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);        // Clear Screen and Depth Buffer
    glLoadIdentity();
    gluLookAt( 4,2,0, 0,0,0, 0,1,0);                        // Define a viewing transformation
    g_image.Draw();   

 GLUquadricObj *sphere=NULL;
  sphere = gluNewQuadric();
  gluQuadricDrawStyle(sphere, GLU_FILL);
  gluQuadricTexture(sphere, TRUE);
  gluQuadricNormals(sphere, GLU_SMOOTH);
glBindTexture( GL_TEXTURE_2D,texture[0] );

glTranslatef (2.0, 0.0, 0.0);

  gluSphere(sphere, 0.4, 20, 20);


                                // Draw image to the screen
    glutSwapBuffers();
}


void initialize ()
{
    glMatrixMode(GL_PROJECTION);                                                // select projection matrix
    glViewport(0, 0, win.width, win.height);                                    // set the viewport
    glMatrixMode(GL_PROJECTION);                                                // set matrix mode
    glLoadIdentity();                                                            // reset projection matrix
    GLfloat aspect = (GLfloat) win.width / win.height;
    gluPerspective(win.field_of_view_angle, aspect, win.z_near, win.z_far);        // set up a perspective projection matrix
    glMatrixMode(GL_MODELVIEW);                                                    // specify which matrix is the current matrix
    glShadeModel( GL_SMOOTH );
    glClearDepth( 1.0f );                                                        // specify the clear value for the depth buffer
    glEnable( GL_DEPTH_TEST );
    glDepthFunc( GL_LEQUAL );
    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );                        // specify implementation-specific hints

    GLfloat amb_light[] = { 0.1, 0.1, 0.1, 1.0 };
    GLfloat diffuse[] = { 0.6, 0.6, 0.6, 1 };
    GLfloat specular[] = { 0.7, 0.7, 0.3, 1 };
    glLightModelfv( GL_LIGHT_MODEL_AMBIENT, amb_light );
    glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse );
    glLightfv( GL_LIGHT0, GL_SPECULAR, specular );
    glEnable( GL_LIGHT0 );
    glEnable( GL_COLOR_MATERIAL );
    glShadeModel( GL_SMOOTH );
    glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE );
    glDepthFunc( GL_LEQUAL );
    glEnable( GL_DEPTH_TEST );
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glClearColor(0.0, 0.0, 0.0, 1.0);

}


void keyboard ( unsigned char key, int mousePositionX, int mousePositionY )       
{
  switch ( key )
  {
    case KEY_ESCAPE:       
      exit ( 0 );  
      break;     

    default:     
      break;
  }
}

int main(int argc, char **argv)
{
    // set window values
    win.width = 640;
    win.height = 480;
    win.title = "OpenGL/GLUT TGA Loader";
    win.field_of_view_angle = 45;
    win.z_near = 1.0f;
    win.z_far = 500.0f;

    g_image.Load("2010.tga");

    // initialize and run program
    glutInit(&argc, argv);                                      // GLUT initialization
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );  // Display Mode
    glutInitWindowSize(win.width,win.height);                    // set window size
    glutCreateWindow(win.title);                                // create Window
    glutDisplayFunc(display);                                    // register Display Function
    glutIdleFunc( display );                                    // register Idle Function
    glutKeyboardFunc( keyboard );                                // register Keyboard Handler
    initialize();
    glutMainLoop();                                                // run GLUT mainloop
    return 0;
}

No comments:

Post a Comment