GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: Add Line Numbers


hhhh by banshee23
#include <windows.h>   // Standard Header For Most Programs
#include <gl/gl.h>     // The GL Header File
#include <gl/glut.h>   // The GL Utility Toolkit (Glut) Header
#include <fstream.h>
#include <iostream.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>

#define BITMAP_ID 0x4D42        // the universal bitmap ID

const int MAX_POLYGONS = 25000;
int i = 0;

float XPOS = -1.5;
float YPOS = -20.0;
float ZPOS = -6.0;

int max_vertices = 0;
float a = 0;
float b = 0;
float c = 0;
float MOUSE_X = 0;
float MOUSE_Y = 0;
double data[MAX_POLYGONS * 3]; //MAX Polygons


int mapdata();
void gravity();

////// Texture Information
BITMAPINFOHEADER    bitmapInfoHeader;    // bitmap info header
unsigned char*        bitmapData;            // the texture data
unsigned int        texture;            // the texture object

// LoadBitmapFile
// desc: Returns a pointer to the bitmap image of the bitmap specified
//       by filename. Also returns the bitmap header information.
//         No support for 8-bit bitmaps.
unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
    FILE *filePtr;                            // the file pointer
    BITMAPFILEHEADER    bitmapFileHeader;        // bitmap file header
    unsigned char        *bitmapImage;            // bitmap image data
    int                    imageIdx = 0;        // image index counter
    unsigned char        tempRGB;                // swap variable

    // open filename in "read binary" mode
    filePtr = fopen(filename, "rb");
    if (filePtr == NULL)
        return NULL;

    // read the bitmap file header
    fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
    
    // verify that this is a bitmap by checking for the universal bitmap id
    if (bitmapFileHeader.bfType != BITMAP_ID)
    {
        fclose(filePtr);
        return NULL;
    }

    // read the bitmap information header
    fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);

    // move file pointer to beginning of bitmap data
    fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);

    // allocate enough memory for the bitmap image data
    bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);

    // verify memory allocation
    if (!bitmapImage)
    {
        free(bitmapImage);
        fclose(filePtr);
        return NULL;
    }

    // read in the bitmap image data
    fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr);

    // make sure bitmap image data was read
    if (bitmapImage == NULL)
    {
        fclose(filePtr);
        return NULL;
    }

    // swap the R and B values to get RGB since the bitmap color format is in BGR
    for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx+=3)
    {
        tempRGB = bitmapImage[imageIdx];
        bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
        bitmapImage[imageIdx + 2] = tempRGB;
    }

    // close the file and return the bitmap image data
    fclose(filePtr);
    return bitmapImage;
}

// WriteBitmapFile()
// desc: takes image data and saves it into a 24-bit RGB .BMP file
//       with width X height dimensions
int WriteBitmapFile(char *filename, int width, int height, unsigned char *imageData)
{
    FILE             *filePtr;        // file pointer
    BITMAPFILEHEADER bitmapFileHeader;    // bitmap file header
    BITMAPINFOHEADER bitmapInfoHeader;    // bitmap info header
    int                 imageIdx;    // used for swapping RGB->BGR
    unsigned char     tempRGB;            // used for swapping

    // open file for writing binary mode
    filePtr = fopen(filename, "wb");
    if (!filePtr)
        return 0;

    // define the bitmap file header
    bitmapFileHeader.bfSize = sizeof(BITMAPFILEHEADER);
    bitmapFileHeader.bfType = 0x4D42;
    bitmapFileHeader.bfReserved1 = 0;
    bitmapFileHeader.bfReserved2 = 0;
    bitmapFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
    
    // define the bitmap information header
    bitmapInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
    bitmapInfoHeader.biPlanes = 1;
    bitmapInfoHeader.biBitCount = 24;                        // 24-bit
    bitmapInfoHeader.biCompression = BI_RGB;                // no compression
    bitmapInfoHeader.biSizeImage = width * abs(height) * 3;    // width * height * (RGB bytes)
    bitmapInfoHeader.biXPelsPerMeter = 0;
    bitmapInfoHeader.biYPelsPerMeter = 0;
    bitmapInfoHeader.biClrUsed = 0;
    bitmapInfoHeader.biClrImportant = 0;
    bitmapInfoHeader.biWidth = width;                        // bitmap width
    bitmapInfoHeader.biHeight = height;                    // bitmap height

    // switch the image data from RGB to BGR
    for (imageIdx = 0; imageIdx < bitmapInfoHeader.biSizeImage; imageIdx+=3)
    {
        tempRGB = imageData[imageIdx];
        imageData[imageIdx] = imageData[imageIdx + 2];
        imageData[imageIdx + 2] = tempRGB;
    }

    // write the bitmap file header
    fwrite(&bitmapFileHeader, 1, sizeof(BITMAPFILEHEADER), filePtr);

    // write the bitmap info header
    fwrite(&bitmapInfoHeader, 1, sizeof(BITMAPINFOHEADER), filePtr);

    // write the image data
    fwrite(imageData, 1, bitmapInfoHeader.biSizeImage, filePtr);

    // close our file
    fclose(filePtr);

    return 1;
}

// DrawTextureCube
// desc: draws a cube at the specified location in
//         the current coordinate space

void processMousePassiveMotion(int x, int y)
{
    
    //XPOS = x / 10;
    //YPOS = y / 10;
    
    MOUSE_X = x;
    MOUSE_Y = y;

}


void InitGL ( GLvoid )     // Create Some Everyday Functions
{
    glShadeModel(GL_SMOOTH);                            // Enable Smooth Shading
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);                // Black Background
    glClearDepth(1.0f);                                    // Depth Buffer Setup
    glEnable(GL_DEPTH_TEST);                            // Enables Depth Testing
    glDepthFunc(GL_LEQUAL);                                // The Type Of Depth Testing To Do
    glEnable ( GL_COLOR_MATERIAL );
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

    
    // added!
    glEnable(GL_TEXTURE_2D);                    // enable 2D texturing

    // load our bitmap file
    bitmapData = LoadBitmapFile("checker.bmp", &bitmapInfoHeader);

    glGenTextures(1, &texture);                  // generate texture object
    glBindTexture(GL_TEXTURE_2D, texture);       // enable our texture object

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

     // generate the texture image
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth,
                 bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData);
    
}




void display ( void )   // Create The Display Function
{    
    i = 0;
    
    float tex1 = 0.0;
    float tex = 1.0;
    
    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    // Clear Screen And Depth Buffer
    glLoadIdentity();                                    // Reset The Current Modelview Matrix
    glPushMatrix();
    //glTranslatef(XPOS,YPOS,ZPOS);    // Move Left 1.5 Units And Into The Screen 6.0
    gluPerspective(60, 1.0, 1.0, 10000.0);
    gluLookAt  ( 0.0, 0.0, ZPOS, 
                 MOUSE_X, MOUSE_Y, 0.0, 
                 0.0, 1.0, 0.0); 
    
    //glRotatef(MOUSE_X,MOUSE_Y, 0.0, 0.0);
    glBegin(GL_TRIANGLES);    
    while(i <= max_vertices)
    {        
        
        glTexCoord2f(i + 0.5, i + 0.5);
        //glColor3f(data[0],data[i+1],data[0]);
        glVertex3f(data[i],data[i+1],data[i+2]);    
        i = i + 3;    
    }
    glEnd();                                                    // Done Drawing The triangles
    glPopMatrix();
    glutSwapBuffers ( );
    //gravity(); //Call gravity
}

void reshape ( int width , int height )   // Create The Reshape Function (the viewport)
{
  if (height==0)                                        // Prevent A Divide By Zero By
    {
        height=1;                                        // Making Height Equal One
    }

    glViewport(0,0,width,height);                        // Reset The Current Viewport

    glMatrixMode(GL_PROJECTION);                        // Select The Projection Matrix
    glLoadIdentity();                                    // Reset The Projection Matrix

    // Calculate The Aspect Ratio Of The Window
    gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,10000.0f);

    glMatrixMode(GL_MODELVIEW);                            // Select The Modelview Matrix
    glLoadIdentity();                                    
}

void keyboard ( unsigned char key, int x, int y )  // Create Keyboard Function
{
  switch ( key ) 
  {
        case 27:        // When Escape Is Pressed...
        exit ( 0 );   // Exit The Program
        break;        // Ready For Next Case
        default:        // Now Wrap It Up
        break;
  }
}

void arrow_keys ( int a_keys, int x, int y )  // Movement!
{
    switch (a_keys)
    {    
        case GLUT_KEY_UP:    
                            ZPOS = ZPOS + 10.1;
                            break;
        case GLUT_KEY_DOWN:              
    
                            ZPOS = ZPOS - 10.1;
                            break;
        case GLUT_KEY_LEFT:     
   
                            XPOS = XPOS + 10.1;
                            break;
        case GLUT_KEY_RIGHT: 
                            XPOS = XPOS - 10.1;
                            break;
        
        default:            break;                    
    }
}

void main ( int argc, char** argv )   // Create Main Function For Bringing It All Together
{
  mapdata();            //read the map data
  glutInit            ( &argc, argv ); // Erm Just Write It =)
  glutInitDisplayMode ( GLUT_RGBA | GLUT_DOUBLE ); // Display Mode
  glutInitWindowSize  ( 500, 500 ); // If glutFullScreen wasn't called this is the window size
  glutCreateWindow    ( "NeHe's OpenGL Framework" ); // Window Title (argv[0] for current directory as title)
  glutFullScreen      ( );          // Put Into Full Screen
  InitGL ();
  glutDisplayFunc     ( display );  // Matching Earlier Functions To Their Counterparts
  glutReshapeFunc     ( reshape );
  glutKeyboardFunc    ( keyboard );
  glutPassiveMotionFunc(processMousePassiveMotion);
  glutSpecialFunc     ( arrow_keys );
  glutIdleFunc          ( display );
  
  
  
  //adding here the mouse processing callbacks
    


    glutMainLoop        ( );          // Initialize The Main Loop

}

int mapdata() //Raw data loader pumps values into an array
{
    ifstream file_in("ahh.txt"); 
    
    if(!file_in)
            return 1; 
    int i = 0;                                
    while(!file_in.eof())
    {                
        file_in >> data[i];  
        cout << data[i] << endl;
        i++;                                            
    }
    max_vertices = i;
    cout << "Vertices = " << max_vertices << endl;
    cout << "Polygons = " << max_vertices / 3 <<endl;
    
    file_in.close(); 
    return 0;
}


void gravity()
{
    
    YPOS = YPOS + 0.01;

}