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:: No Line Numbers


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

#define BITMAP_ID 0x4D42        // the universal bitmap ID

const int MAX_POLYGONS = 10000;
int i = 0;

float XPOS = 0.0;
float YPOS = 0.0;
float ZPOS = 0.0;
float FOW = 0.0;
float a = 0;
float b = 0;
float c = 0;
float data[MAX_POLYGONS]; //MAX Polygons
float max_vertices = 0;
int mapdata();

////// 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;
}



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);    
    
    glBegin(GL_TRIANGLES);    
        
    while(i <= max_vertices)
    {            
        glTexCoord2f(0.0,0.0); glVertex3f(data[i],data[i+1],data[i+2]);    
        i = i + 3;
        glTexCoord2f(1.0,0.0); glVertex3f(data[i],data[i+1],data[i+2]);    
        i = i + 3;
        glTexCoord2f(0.0,1.0); glVertex3f(data[i],data[i+1],data[i+2]);    
        i = i + 3;
    }
    
    glEnd();                                                    // Done Drawing The triangles
    glPopMatrix();
    glutSwapBuffers ( );
}

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 ()   // Create Main Function For Bringing It All Together
{
    mapdata();            //read the map data
    glutInitDisplayMode        ( GLUT_RGB | GLUT_DOUBLE ); 
    glutInitWindowSize        ( 1024, 768 ); 
    glutCreateWindow        ("BansheeEngine 0.1" ); 
    InitGL                    ();
    glutDisplayFunc            ( display );  
    glutReshapeFunc            ( reshape );
    glutKeyboardFunc        ( keyboard );
    glutSpecialFunc            ( arrow_keys );
    glutIdleFunc            ( display );
    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];  
        i++;                                            
    }
    max_vertices = i;
     
    file_in.close(); 
    return 0;
}