2D texture reading (C ++, OpenGL) from the bitmap image

There are two .cpp file and a .h header file

step:

You need to install GLUT, because GLUT third-party libraries, that it is not part of OpenGL. Therefore, it is not part of the Windows system API, and therefore not part of the standard Windows SDK.

Download Link: http://files.transmissionzero.co.uk/software/development/GLUT/freeglut-MSVC.zip

installation method:

Create a file available to all users to read the folder on your PC, such as "C: \ Program Files \ Common Files \ MSVC \ freeglut \" on a typical Windows system. "Lib" and "include \" Copy to this location the zip archive folder.

Range corresponding file system freeglut DLL can be placed in the same folder as the application, may be installed in the environment variable% PATH% displayed folder. Be careful not to mix with the 32-bit DLL 64-bit DLL, because they are not interchangeable.

Compile 32-bit applications

To create 32 freeglut application, create a new Win32 C ++ project in MSVC. From the "Win32 Application Wizard", select "Windows Application", select the "Empty Project" box and submit.

You now need to configure compiler and linker settings. Open the project properties, and then select "All Configuration" (This is to ensure that our changes apply to debug and release versions necessary). Open the "General" section under the "C / C ++", will be created above "include \" folder configured to "Additional Include Directories." If you have more than GLUT package "glut.h" file, make sure freeglut include folders appear above all other folders contain GLUT.

Now open the "General" section under the "linker" and "lib" folder you created in the above clip is configured to "Additional Library Directories." freeglut applications rely on import library "freeglut.lib" and "opengl32.lib", they may be arranged at the "input" section. However, there is no need for a clear description of these dependencies, because freeglut header will handle this for you. Now open the "Advanced" section, enter "mainCRTStartup" as the application of "entry point." This is necessary because the GLUT application uses the "main" as the application entry point, rather than "WinMain" - when you try to link the application, you will get undefined references.

That's all you configure the project properties, so you can now add the source file to the project and build the application. If you want your application is compatible with GLUT, you should "#include <GL / glut.h>" . If you want to use freeglut specific extensions should be changed to "#include <GL / freeglut.h>" .

Do not forget to include when distributing applications freeglut DLL, or to provide users with some of the ways to obtain it (if they do not have it)!

Ensure freeglut.dll .msvs 11.0 \ vc \ bin, make sure that freeglut.h in pf (86x) .msvs in pf (86x) 11.0 \ vc \ include \ GL, make sure that freeglut.lib in pf (86x) in. msvs 11.0 \ _vc \ lib

Compile 64-bit applications

Construction of 64-bit applications to build is almost the same 32-bit application. When you add a x64 platform using the Configuration Manager, the easiest way is to copy settings from the Win32 platform. If you do, you can simply change the "Other directory containing the" configuration so that it refers to the directory containing the 64-bit rather than 32-bit import library import library.

After the above is done, you can create a new empty win32 project, and then copy the code below into

 

BmpLoader.h

#ifndef BMPLOADER_H
#define BMPLOADER_H

#include <windows.h>

class BmpLoader {
public:
    unsigned char* textureData;
    int iWidth, iHeight;
    BmpLoader(const char*);
    ~BmpLoader();



private:
    BITMAPFILEHEADER bfh;
    BITMAPINFOHEADER bih;
};

#endif

 

 BmpLoader.cpp

#include <stdio.h>
#include <stdlib.h>
#include "BmpLoader.h"
#include <iostream>

using namespace std;
#pragma warning(disable : 4996)
BmpLoader::BmpLoader(const char* filename)
{
    FILE * file = 0;
    file = fopen(filename, "rb");
    if (!file)
        std::cout << "Missing Textures\n";
    fread(&bfh, sizeof(BITMAPFILEHEADER), 1, file);
    if (bfh.bfType != 0x4D42)
        std::cout << "CRASH:Invalid Texture Format\n";
    fread(&bih, sizeof(BITMAPINFOHEADER), 1, file);
    if (bih.biSizeImage == 0)
        bih.biSizeImage = bih.biHeight*bih.biWidth * 3;
    textureData = new unsigned char[bih.biSizeImage];
    fseek(file, bfh.bfOffBits, SEEK_SET);
    fread(textureData, 1, bih.biSizeImage, file);
    unsigned char temp;
    for (int i = 0; i < bih.biSizeImage; i += 3)
    {
        temp = textureData[i];
        textureData[i] = textureData[i + 2];
        textureData[i + 2] = temp;
    }
    iWidth = bih.biWidth;
    iHeight = bih.biHeight;
    fclose(file);
}

BmpLoader::~BmpLoader()
{
    delete[] textureData;

}

 

main.cpp

#include <GL/GLUT.h>
#include "BmpLoader.h"
#include <iostream>

using namespace std;

int theta = 0;

unsigned int ID;
void display();
void anim();
void reshape(int, int);

void init()
{
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
}

void LoadTexture(const char* filename)
{
    BmpLoader bl(filename);
    glGenTextures(1, &ID);
    glBindTexture(GL_TEXTURE_2D, ID);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, bl.iWidth, bl.iHeight, GL_RGB, GL_UNSIGNED_BYTE, bl.textureData);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutInitWindowSize(500, 500);
    glutCreateWindow("TEX");
    LoadTexture("panda.bmp");
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutIdleFunc(anim);
    init();
    glutMainLoop();
    return 0;
}

void display()
{
    glEnable(GL_TEXTURE_2D);
    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(0.0, 0.0, -6.0);
    glRotatef(theta, 1.0, 2.0, 0.0);
    for (int i = 0; i < 6; i++)
    {
        glPushMatrix();
        if (i == 1)
        {
            glTranslatef(1.0, 0.0, 0.0);
            glRotatef(90.0, 0.0, 1.0, 0.0);
            glTranslatef(1.0, 0.0, 0.0);

        }
        else if (i == 2)
        {
            glTranslatef(0.0, 0.0, -2.0);
            glRotatef(180.0, 0.0, 1.0, 0.0);
        }
        else if (i == 3)
        {
            glTranslatef(-1.0, 0.0, 0.0);
            glRotatef(-90.0, 0.0, 1.0, 0.0);
            glTranslatef(-1.0, 0.0, 0.0);
        }
        else if (i == 4)
        {
            glTranslatef(0.0, 1.0, 0.0);
            glRotatef(-90.0, 1.0, 0.0, 0.0);
            glTranslatef(0.0, 1.0, 0.0);
        }
        else if (i == 5)
        {
            glTranslatef(0.0, -1.0, 0.0);
            glRotatef(90.0, 1.0, 0.0, 0.0);
            glTranslatef(0.0, -1.0, 0.0);
        }
        glBegin(GL_QUADS);
        glTexCoord2f(1.0, 1.0);        glVertex3f(1.0, 1.0, 0.0);
        glTexCoord2f(0.0, 1.0);        glVertex3f(-1.0, 1.0, 0.0);
        glTexCoord2f(0.0, 0.0);        glVertex3f(-1.0, -1.0, 0.0);
        glTexCoord2f(1.0, 0.0);        glVertex3f(1.0, -1.0, 0.0);
        glEnd();
        glPopMatrix();
    }
    glutSwapBuffers();
    glDisable(GL_TEXTURE_2D);
}

void reshape(int w, int h)
{
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 50.0);
    glMatrixMode(GL_MODELVIEW);
}

void anim()
{
    glutPostRedisplay();
    theta += 1;
}

Debugging results:

Guess you like

Origin www.cnblogs.com/strive-sun/p/11497219.html