任务:绘制有桌子茶壶,墙壁地板的简单三维场景,加载图片作为纹理,注意灯光的设置和场景的旋转。
#include<windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdio.h>
#include <malloc.h>
#include "FreeImage.h"
#define Height 16
#define Width 16
GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_pos[] = { 5,5,5,1 };
float eye[] = { -3,3, 7 };
float center[] = { 0, 0, 0 };
float place[] = { 0, 0, 5.6 };
#define ERROR(...) printf(__VA_ARGS__);exit(0)
float fRotate = 0.0f;
float fScale = 1.0f; //
float tRotate = 0.0f;
bool bPersp = false;
bool bAnim = false;
bool isRotate = false;
int wHeight = 0;
int wWidth = 0;
//int min(int x, int y)
//{
// return x < y ? x : y;
//}
typedef struct
{
int w;
int h;
unsigned char *buf;
GLuint rgb_mode;
}GLBITMAP;
static GLuint S_png_tex = 0;
static GLuint S_jpg_tex = 0;
static GLuint S_bmp_tex = 0;
static GLuint S_tga_tex = 0;
static GLuint S_ico_tex = 0;
GLBITMAP * FIBitmap2GLBitmap(FIBITMAP *fibmp)
{
int i, j, k;
int pitch = FreeImage_GetPitch(fibmp);
unsigned char *bits = FreeImage_GetBits(fibmp);
int bpp = FreeImage_GetBPP(fibmp);
GLBITMAP *glbmp = (GLBITMAP *)malloc(sizeof(GLBITMAP));
RGBQUAD *palette = NULL;
if (!glbmp) return NULL;
glbmp->w = FreeImage_GetWidth(fibmp);
glbmp->h = FreeImage_GetHeight(fibmp);
switch (bpp) {
case 8:
if (!(palette = FreeImage_GetPalette(fibmp))) return NULL;
if (!(glbmp->buf = (unsigned char *)malloc(glbmp->w*glbmp->h * 3))) return NULL;
glbmp->rgb_mode = GL_RGB;
for (i = 0; i < glbmp->h; ++i) {
for (j = 0; j < glbmp->w; ++j) {
k = bits[i*pitch + j];
glbmp->buf[(i*glbmp->w + j) * 3 + 0] = palette[k].rgbRed;
glbmp->buf[(i*glbmp->w + j) * 3 + 1] = palette[k].rgbGreen;
glbmp->buf[(i*glbmp->w + j) * 3 + 2] = palette[k].rgbBlue;
}
}
break;
case 24:
if (!(glbmp->buf = (unsigned char *)malloc(glbmp->w*glbmp->h * 3))) return NULL;
glbmp->rgb_mode = GL_RGB;
for (i = 0; i < glbmp->h; ++i) {
for (j = 0; j < glbmp->w; ++j) {
glbmp->buf[(i*glbmp->w + j) * 3 + 0] = bits[i*pitch + j * 3 + 2];
glbmp->buf[(i*glbmp->w + j) * 3 + 1] = bits[i*pitch + j * 3 + 1];
glbmp->buf[(i*glbmp->w + j) * 3 + 2] = bits[i*pitch + j * 3 + 0];
}
}
break;
case 32:
if (!(glbmp->buf = (unsigned char *)malloc(glbmp->w*glbmp->h * 4))) return NULL;
glbmp->rgb_mode = GL_RGBA;
for (i = 0; i < glbmp->h; ++i) {
for (j = 0; j < glbmp->w; ++j) {
glbmp->buf[(i*glbmp->w + j) * 4 + 0] = bits[i*pitch + j * 4 + 2];
glbmp->buf[(i*glbmp->w + j) * 4 + 1] = bits[i*pitch + j * 4 + 1];
glbmp->buf[(i*glbmp->w + j) * 4 + 2] = bits[i*pitch + j * 4 + 0];
glbmp->buf[