📄 main.cpp
字号:
#define WIN32_LEAN_AND_MEAN // trim the excess fat from Windows
/*******************************************************************
* Program: Chapter 7 Bitmap Example 4
* Author: Kevin Hawkins
* Description: Loads and displays a .BMP file in the window. When
* the user presses the 'S' or 's' key, the window
* contents are saved to disk.
********************************************************************/
////// Defines
#define BITMAP_ID 0x4D42 // the universal bitmap ID
////// Includes
#include <windows.h> // standard Windows app include
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <gl/gl.h> // standard OpenGL include
#include <gl/glu.h> // OpenGL utilties
#include <gl/glaux.h> // OpenGL auxiliary functions
////// Global Variables
HDC g_HDC; // global device context
bool fullScreen = false; // true = fullscreen; false = windowed
bool keyPressed[256]; // holds true for keys that are pressed
////// Bitmap Information
BITMAPINFOHEADER bitmapInfoHeader; // bitmap info header
unsigned char* bitmapData; // the bitmap data
void* imageData; // the screen image data
typedef struct
{
unsigned char imageTypeCode;
short int imageWidth;
short int imageHeight;
unsigned char bitCount;
unsigned char *imageData;
} TGAFILE;
TGAFILE *myTGA;
// DrawBitmap
// desc: draws the bitmap image data in bitmapImage at the location
// (200,200) in the window. (200,200) is the lower-left corner
// of the bitmap.
void DrawBitmap(long width, long height, unsigned char* bitmapImage)
{
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glRasterPos2i(200,200);
glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, bitmapImage);
}
// 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;
}
// LoadTGAFile()
// desc: loads the TGA file "filename" into the tgaFile data structure
int LoadTGAFile(char *filename, TGAFILE *tgaFile)
{
FILE *filePtr;
unsigned char ucharBad; // garbage unsigned char data
short int sintBad; // garbage short int data
long imageSize; // size of the TGA image
int colorMode; // 4 for RGBA or 3 for RGB
long imageIdx; // counter variable
unsigned char colorSwap; // swap variable
// open the TGA file
filePtr = fopen(filename, "rb");
if (!filePtr)
return 0;
// read first two bytes of garbage
fread(&ucharBad, sizeof(unsigned char), 1, filePtr);
fread(&ucharBad, sizeof(unsigned char), 1, filePtr);
// read in the image type
fread(&tgaFile->imageTypeCode, sizeof(unsigned char), 1, filePtr);
// for our purposes, the image type should be either a 2 (color) or a 3 (greyscale)
if ((tgaFile->imageTypeCode != 2) && (tgaFile->imageTypeCode != 3))
{
fclose(filePtr);
return 0;
}
// read 13 bytes of garbage data
fread(&sintBad, sizeof(short int), 1, filePtr);
fread(&sintBad, sizeof(short int), 1, filePtr);
fread(&ucharBad, sizeof(unsigned char), 1, filePtr);
fread(&sintBad, sizeof(short int), 1, filePtr);
fread(&sintBad, sizeof(short int), 1, filePtr);
// read image dimensions
fread(&tgaFile->imageWidth, sizeof(short int), 1, filePtr);
fread(&tgaFile->imageHeight, sizeof(short int), 1, filePtr);
// read image bit depth
fread(&tgaFile->bitCount, sizeof(unsigned char), 1, filePtr);
// read 1 byte of garbage data
fread(&ucharBad, sizeof(unsigned char), 1, filePtr);
// colorMode -> 3 = BGR, 4 = BGRA
colorMode = tgaFile->bitCount / 8;
imageSize = tgaFile->imageWidth * tgaFile->imageHeight * colorMode;
// allocate memory for image data
tgaFile->imageData = (unsigned char*)malloc(sizeof(unsigned char)*imageSize);
// read in image data
fread(tgaFile->imageData, sizeof(unsigned char), imageSize, filePtr);
// change BGR to RGB so OpenGL can read the image data
for (imageIdx = 0; imageIdx < imageSize; imageIdx += colorMode)
{
colorSwap = tgaFile->imageData[imageIdx];
tgaFile->imageData[imageIdx] = tgaFile->imageData[imageIdx + 2];
tgaFile->imageData[imageIdx + 2] = colorSwap;
}
// close the file
fclose(filePtr);
return 1;
}
// saves an array of pixels as a TGA image
int WriteTGAFile(char *filename, short int width, short int height, unsigned char* imageData)
{
unsigned char byteSkip; // used to fill in the data fields that we don't care about
short int shortSkip;
unsigned char imageType; // type of image we're writing to file
int colorMode;
unsigned char colorSwap;
int imageIdx;
unsigned char bitDepth;
long imageSize;
FILE *filePtr;
// create file for writing binary mode
filePtr = fopen(filename, "wb");
if (!filePtr)
{
fclose(filePtr);
return 0;
}
imageType = 2; // RGB, uncompressed
bitDepth = 24; // 24-bitdepth
colorMode = 3; // RGB color mode
byteSkip = 0;
shortSkip = 0;
// write 2 bytes of blank data
fwrite(&byteSkip, sizeof(unsigned char), 1, filePtr);
fwrite(&byteSkip, sizeof(unsigned char), 1, filePtr);
// write imageType
fwrite(&imageType, sizeof(unsigned char), 1, filePtr);
fwrite(&shortSkip, sizeof(short int), 1, filePtr);
fwrite(&shortSkip, sizeof(short int), 1, filePtr);
fwrite(&byteSkip, sizeof(unsigned char), 1, filePtr);
fwrite(&shortSkip, sizeof(short int), 1, filePtr);
fwrite(&shortSkip, sizeof(short int), 1, filePtr);
// write image dimensions
fwrite(&width, sizeof(short int), 1, filePtr);
fwrite(&height, sizeof(short int), 1, filePtr);
fwrite(&bitDepth, sizeof(unsigned char), 1, filePtr);
// write 1 byte of blank data
fwrite(&byteSkip, sizeof(unsigned char), 1, filePtr);
// calculate the image size
imageSize = width * height * colorMode;
// change image data from RGB to BGR
for (imageIdx = 0; imageIdx < imageSize ; imageIdx += colorMode)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -