# 计算机图形学 使用OpenGL的场景建模

8 篇文章 32 订阅
5 篇文章 6 订阅

## 虚拟场景建模

https://www.bilibili.com/video/BV13Z4y1w7TU?spm_id_from=333.999.0.0

### 代码解析

1.生成着色器程序program的函数
2.将mesh里保存到顶点信息送到着色器
4.绘制图像

#### 读取obj文件

void My_Mesh::load_obj(std::string obj_File)
{
this->clear_data();
//实现对含有UV坐标的obj文件的读取
//打开文件流
std::ifstream fin(obj_File);
std::string line;
if (!fin.is_open())
{
std::cout << "文件 " << obj_File << " 打开失败" << std::endl;
exit(-1);
}
std::vector<float> normals;//临时存储法线信息
std::vector<float> vtlist; //临时存储纹理信息

//计算各种类型的数据的数量
int v_num = 0;
int vt_num = 0;
int vn_num = 0;
int f_num = 0;

//保存vertices三个坐标下的最大值
float max_x = 0;
float max_y = 0;
float max_z = 0;

//按行读取
while (std::getline(fin, line))
{
std::istringstream sin(line);   //以一行的数据作为 string stream 解析并且读取
std::string type;
float x, y, z;
char slash;
//读取obj文件
sin >> type;
if (type == "v") {
sin >> x >> y >> z;
m_vertices_.push_back(x);
m_vertices_.push_back(y);
m_vertices_.push_back(z);
v_num++;

//更新最大值
if (max_x < fabs(x))max_x = fabs(x);
if (max_y < fabs(y))max_y = fabs(y);
if (max_z < fabs(z))max_z = fabs(z);

}
if (type == "vt") {
sin >> x >> y;
vtlist.push_back(x);
vtlist.push_back(y);
vt_num++;
}
if (type == "vn") {
sin >> x >> y >> z;
normals.push_back(x);
normals.push_back(y);
normals.push_back(z);
vn_num++;
}
if (type == "f") {
float a, b, c;
for (int i = 0; i < 3; i++) {
sin >> x >> slash >> y >> slash >> z;

//生成三角面片
m_faces_.push_back(x - 1);

//纹理坐标
a = vtlist[2 * (y - 1)];
b = vtlist[2 * (y - 1) + 1];
m_vt_list_.push_back(a);
m_vt_list_.push_back(b);

//法线由里向外
a = normals[3 * (z - 1)];
b = normals[3 * (z - 1) + 1];
c = normals[3 * (z - 1) + 2];
m_normals_.push_back(a);
m_normals_.push_back(b);
m_normals_.push_back(c);

//这里采用法线来生成颜色，学生可以自定义自己的颜色生成方式
float rr;
float gg;
float bb;
My_Mesh::normal_to_color(a, b, c, rr, gg, bb);
m_color_list_.push_back(rr);
m_color_list_.push_back(gg);
m_color_list_.push_back(bb);
}
f_num++;
}

}

//设置m_center_、>m_min_box_、>m_max_box_
this->m_center_ = point3f(0, 0, 0);
this->m_min_box_ = point3f(-max_x, -max_y, -max_z);
this->m_max_box_ = point3f(max_x, max_y, max_z);
};



（此处仅展示部分）

	My_Mesh* my_mesh1 = new My_Mesh;
my_mesh1->set_texture_file("texture/torso.png");
my_mesh1->set_translate(0, 0.25, 0);//设定初始位置
my_mesh1->set_theta(0, 0, 0);//设定人物初始的角度
my_mesh1->set_scale(3, 3, 3);//设定初始的规模大小
my_mesh1->set_theta_step(0, 0, 0);//设定自转的速度
my_meshs.push_back(my_mesh1);//将读入的物体放入存储物体的mesh容器



void My_Mesh::set_translate(float x, float y, float z)
{
vTranslation[0] = x;
vTranslation[1] = y;
vTranslation[2] = z;

};
void My_Mesh::get_translate(float& x, float& y, float& z)
{
x = vTranslation[0];
y = vTranslation[1];
z = vTranslation[2];
};

float x, y, z;
this->m_my_meshes_[i]->get_translate(x, y, z);
GLfloat  vTranslation[3] = { x, y, z };

mat4 model = Translate(vTranslation[0], vTranslation[1], vTranslation[2]);



#### 相机视角的移动

（实验三中有提到相机视角移动的实现）

vec3 cameraPosition(0.0, 8, 0.0);
vec3 cameraUp(0.0, 1.0, 0.0);



	mat4 viewMatrix = LookAt(vec4(cameraPosition, 1.0), vec4(cameraPosition, 1.0) + vec4(cameraDirection, 0.0), vec4(cameraUp, 1.0));//调用lookat函数来构造view矩阵


void
keyboard( unsigned char key, int x, int y )
{
float yPos;
switch (key)
{
case 'w':case 'W':
break;
case 's':case 'S':
break;
case 'a':case 'A':
break;
case 'd':case 'D':
break;
}
}



pitch 俯仰角
yaw 偏航角

	cameraDirection.x = cos(radians(pitch)) * sin(radians(yaw));


void mouse(int x, int y)
{
//此处是相机跟随鼠标的转动而发生转动，采用欧拉角来实现
yaw += 35 * (x - float(windowWidth) / 2.0) / windowWidth;
yaw = mod(yaw + 180.0f, 360.0f) - 180.0f;
pitch += -35 * (y - float(windowHeight) / 2.0) / windowHeight;
pitch = clamp(pitch, -89.0f, 89.0f);

glutWarpPointer(windowWidth / 2.0, windowHeight / 2.0);	// 将指针钉死在屏幕正中间

glutPostRedisplay();
return;
}



glutKeyboardFunc( keyboard );
glutPassiveMotionFunc( mouse );

#### 光照效果

vec3 lightPos(0, 15.0, 15.0);

glEnable(GL_LIGHTING);

mp_->lightPos = lightPos;

uniform vec3 lightPos;

		GLuint lightPosID = glGetUniformLocation(this->program_all[i], "lightPos");
glUniform3fv(lightPosID, 1, &lightPos[0]);


接下来仿照实验3.4的Phong光照模型，在顶点着色器的main函数中中写入这些语句，并且传给片元着色器

out vec3 N;
out vec3 V;
out vec3 lightPos_new;

// TODO 将顶点变换到相机坐标系下
vec4 vertPos_cameraspace = viewMatrix*modelMatrix * vec4(vPosition, 1.0);

// 对顶点坐标做透视投影
V = vertPos_cameraspace.xyz / vertPos_cameraspace.w;

// TODO 将光源位置变换到相机坐标系下
vec4 lightPos_cameraspace = viewMatrix *mat4(1.0)* vec4(lightPos, 1.0);

// 对光源坐标做透视投影
lightPos_new = lightPos_cameraspace.xyz / lightPos_cameraspace.w;

// TODO 将法向量变换到相机坐标系下并传入片元着色器
N = (viewMatrix*modelMatrix * vec4(vNormal, 0.0)).xyz;

//以下用来实现光照
fNormal = normal;

vec3 ambiColor = vec3(0.1, 0.1, 0.1);
vec3 diffColor = vec3(0.5, 0.5, 0.5);
vec3 specColor = vec3(0.3, 0.3, 0.3);

// TODO 计算N，L，V，R四个向量并归一化
vec3 N_norm = normalize(N);
vec3 L_norm = normalize(lightPos_new - V);
vec3 V_norm = normalize(-V);
vec3 R_norm = reflect(-L_norm, N_norm);

// TODO 计算漫反射系数和镜面反射系数
float lambertian = clamp(dot(L_norm, N_norm), 0.0, 1.0);
//float specular = clamp(dot(R_norm, V_norm), 0.0, 1.0);

//使用BLINN_PHONG模型
vec3 H_norm = normalize(L_norm + V_norm);
float specular = clamp(dot(N_norm, H_norm), 0.0, 1.0);

float shininess = 10.0;
float d = length(lightPos_new - V);
float a=1,b=0,c=0;
// TODO 计算最终每个片元的输出颜色
fColor*=0.5;
fColor += (vec4(ambiColor + diffColor * lambertian/(a+b*d+c*d*d) +specColor * pow(specular, 5.0), 1.0)/(a+b*d+c*d*d))*1.0;
fColor=vec4(0.0 , 0.0 , 0.0 , 1.0);
}
else{
fColor=fColor;
}



fColor = texture2D( texture, texCoord );


（如果绘制的是阴影，直接设置成黑色即可）

#### 阴影效果

float lx = lightPos[0];
float ly = lightPos[1];
float lz = lightPos[2];

// 计算阴影投影矩阵
lx, 0.0, lz, 1.0,
0.0, 0.0, -ly, 0.0,
0.0, 0.0, 0.0, -ly);

glDrawArrays(GL_TRIANGLES, 0, this->m_my_meshes_[i]->num_faces() * 3);



uniform bool drawShadow;


		GLuint drawShadowID = glGetUniformLocation(program_all[i], "drawShadow");


 vec4 v1 = projMatrix * viewMatrix*modelMatrix * vec4(vPosition, 1.0);
vec4 v2 = vec4(v1.xyz / v1.w, 1.0);
else gl_Position=v1;//如果绘制的不是阴影则不进行透视投影，防止出现大型物体显示的bug


	if(drawShadow){//如果绘制的是阴影，则需要颜色设置为黑色
fColor=vec4(0.0 , 0.0 , 0.0 , 1.0);
}
else{
fColor=fColor;
}



#### 层次建模及人物动画

void Mesh_Painter::draw_robot(int i, vec3 RotateCen) {

glUseProgram(this->program_all[i]);//指定使用渲染器，不同的模型可以指定不同的渲染器，这里我们使用的渲染器来自相同的文件，学生可以根据自己的爱好对不同的模型设定不同的渲染器
glBindVertexArray(this->vao_all[i]);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this->textures_all[i]);//该语句必须，否则将只使用同一个纹理进行绘制

float x, y, z;
this->m_my_meshes_[i]->get_theta(x, y, z);
GLfloat  Theta[3] = { x, y, z };

this->m_my_meshes_[i]->get_translate(x, y, z);
GLfloat  vTranslation[3] = { x, y, z };

this->m_my_meshes_[i]->get_scale(x, y, z);
GLfloat  vScale[3] = { x, y, z };

GLuint lightPosID = glGetUniformLocation(this->program_all[i], "lightPos");
glUniform3fv(lightPosID, 1, &lightPos[0]);

mat4 model = Translate(vTranslation[0], vTranslation[1], vTranslation[2]);
model *= RobotMat;
//model *= RotateY(180);
model *= RotateX(Theta[0]);
model *= Translate(RotateCen.x, 0, 0)*RotateY(Theta[1])*Translate(-RotateCen.x, 0, 0);
model *= Translate(0, 0, RotateCen.z)*RotateZ(Theta[2])*Translate(0, 0, -RotateCen.z);
model *= Scale(vScale[0], vScale[1], vScale[2]);

//model *= RobotMat;
GLuint mlocation = glGetUniformLocation(this->program_all[i], "modelMatrix");    // 名为model的uniform变量的位置索引
glUniformMatrix4fv(mlocation, 1, GL_TRUE, &model[0][0]);
GLuint vlocation = glGetUniformLocation(this->program_all[i], "viewMatrix");    // 名为model的uniform变量的位置索引
glUniformMatrix4fv(vlocation, 1, GL_TRUE, &viewMatrix[0][0]);
GLuint plocation = glGetUniformLocation(this->program_all[i], "projMatrix");    // 名为model的uniform变量的位置索引
glUniformMatrix4fv(plocation, 1, GL_TRUE, &projMatrix[0][0]);

glDrawArrays(GL_TRIANGLES, 0, this->m_my_meshes_[i]->num_faces() * 3);

绘制阴影

float lx = lightPos[0];
float ly = lightPos[1];
float lz = lightPos[2];

// 计算阴影投影矩阵
lx, 0.0, lz, 1.0,
0.0, 0.0, -ly, 0.0,
0.0, 0.0, 0.0, -ly);

glDrawArrays(GL_TRIANGLES, 0, this->m_my_meshes_[i]->num_faces() * 3);

glUseProgram(0);
}



class MatrixStack {
int    _index;
int    _size;
mat4*  _matrices;

public:
MatrixStack(int numMatrices = 100) :_index(0), _size(numMatrices)
{
_matrices = new mat4[numMatrices];
}

~MatrixStack()
{
delete[]_matrices;
}

void push(const mat4& m) {
assert(_index + 1 < _size);
_matrices[_index++] = m;

}

mat4& pop(void) {
assert(_index - 1 >= 0);
_index--;
return _matrices[_index];
}
};



（此处仅展示部分）

enum{
Torso,
Hat,
RightUpperArm,
RightLowerArm,
RightLowerArmZ,
LeftUpperArm,
LeftLowerArm,
LeftLowerArmZ,
RightUpperLeg,
RightLowerLeg,
LeftUpperLeg,
LeftLowerLeg,
Sword,
NumJointAngles,
Quit
};

// Joint angles with initial values
GLfloat
theta[NumJointAngles] = {
0.0,    // Torso
0.0,    // Hat
0.0,    // RightUpperArm
-30.0,    // RightLowerArm
0.0,  // RightLowerArmY
0.0,    // LeftUpperArm
-30.0,    // LeftLowerArm
0.0, // LeftLowerArmY
0.0,  // RightUpperLeg
20.0,    // RightLowerLeg
0.0,  // LeftUpperLeg
20.0,     // LeftLowerLeg
0.0 //Sword
};



//人物动画的函数，通过传入关键帧的参数，然后根据关键帧来改变人物的动作
void RobotArmAnim(int i) {
if (i % 20 == 1) {
mp_->theta[RightUpperArm] = 0;
mp_->theta[LeftUpperArm] = 0;
mp_->theta[LeftUpperLeg] = 0;
mp_->theta[RightUpperLeg] = 0;
mp_->theta[RightLowerArmZ] = 0;
mp_->theta[LeftLowerArmZ] = 0;
mp_->theta[Sword] = 0;
}
if (i%20 <= 5) {
mp_->theta[RightUpperArm] +=10;
mp_->theta[LeftUpperArm] -= 10;
mp_->theta[LeftUpperLeg] += 6;
mp_->theta[RightUpperLeg] -= 6;
}
if (i % 20 > 5 && i % 20 <= 15) {
mp_->theta[RightUpperArm] -= 10;
mp_->theta[LeftUpperArm] += 10;
mp_->theta[LeftUpperLeg] -= 6;
mp_->theta[RightUpperLeg] += 6;
}
if (i % 20 > 15 && i % 20 <= 20) {
mp_->theta[RightUpperArm] += 10;
mp_->theta[LeftUpperArm] -= 10;
mp_->theta[LeftUpperLeg] += 6;
mp_->theta[RightUpperLeg] -= 6;
}

}



case 'i':case 'I':
yPos = RobotPos.y;
RobotPos.y = yPos;//人物只在y轴上移动，因此y的值不应该发生改变
State++;//改变动画关键帧
RobotArmAnim(State);//人物移动时将会调用动画函数
break;


（此处我们让物体朝向相机的方向前进。

（为什么这里手臂握不到剑是因为建模的时候手臂设定太短了/(ㄒoㄒ)/~~）

void SwordAnim(int i) {
//剑的动画的函数
if (i%33 == 1) {
mp_->theta[RightUpperArm] = 0;
mp_->theta[LeftUpperArm] = 0;
mp_->theta[LeftUpperLeg] = 0;
mp_->theta[RightUpperLeg] = 0;
mp_->theta[RightLowerArmZ] = 0;
mp_->theta[LeftLowerArmZ] = 0;
mp_->theta[Sword] = 0;
}
if (i% 33 > 1 && i% 33 <= 5) {

}
if (i% 33 > 5 && i% 33 <= 8) {
mp_->theta[RightUpperArm] -=10 ;
mp_->theta[LeftUpperArm] -= 10;
mp_-> theta[Sword] += 15;

}
if (i% 33 > 8 && i% 33 <= 11) {
mp_->theta[RightLowerArmZ] += 15;
mp_->theta[LeftLowerArmZ] -= 15;
mp_->theta[Sword] += 15;
}
if (i % 33 > 11&& i % 33 <=21) {
mp_->theta[RightUpperArm] -= 10;
mp_->theta[LeftUpperArm] -= 10;
mp_->theta[Sword] += 7.5;

}
if (i % 33 > 21 ) {
mp_->theta[RightUpperArm] += 10;
mp_->theta[LeftUpperArm] += 10;
}

float yPos = RobotPos.y;
RobotPos.y = yPos;
if (i % 20 <= 5) {
mp_->theta[LeftUpperLeg] += 10;
mp_->theta[RightUpperLeg] -= 10;
}
if (i % 20 > 5 && i % 20 <= 15) {
mp_->theta[LeftUpperLeg] -= 10;
mp_->theta[RightUpperLeg] += 10;
}
if (i % 20 > 15 && i % 20 <= 20) {
mp_->theta[LeftUpperLeg] += 10;
mp_->theta[RightUpperLeg] -= 10;
}
}



### 使用说明操控方法：

Wasd控制相机前后左右移动，空格和c键实现相机上下移。
Ikjl实现人物前后左右移动（前后左右是相对相机的方向）
Z和x键可以调整人物转向的方向。

### 完整代码

#### 头文件

FreeImage.h

// ==========================================================
// FreeImage 3
//
// Design and implementation by
// - Floris van den Berg (flvdberg@wxs.nl)
// - Herv?Drolon (drolon@infonie.fr)
//
// Contributors:
// - see changes log named 'Whatsnew.txt', see header of each .h and .cpp file
//
// This file is part of FreeImage 3
//
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
// THIS DISCLAIMER.
//
// Use at your own risk!
// ==========================================================

#ifndef FREEIMAGE_H
#define FREEIMAGE_H

// Version information ------------------------------------------------------

#define FREEIMAGE_MAJOR_VERSION   3
#define FREEIMAGE_MINOR_VERSION   17
#define FREEIMAGE_RELEASE_SERIAL  0

// Compiler options ---------------------------------------------------------

#include <wchar.h>	// needed for UNICODE functions

#if defined(FREEIMAGE_LIB)
#define DLL_API
#define DLL_CALLCONV
#else
#if defined(_WIN32) || defined(__WIN32__)
#define DLL_CALLCONV __stdcall
// The following ifdef block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the FREEIMAGE_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// DLL_API functions as being imported from a DLL, wheras this DLL sees symbols
// defined with this macro as being exported.
#ifdef FREEIMAGE_EXPORTS
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif // FREEIMAGE_EXPORTS
#else
// try the gcc visibility support (see http://gcc.gnu.org/wiki/Visibility)
#if defined(__GNUC__) && ((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
#ifndef GCC_HASCLASSVISIBILITY
#define GCC_HASCLASSVISIBILITY
#endif
#endif // __GNUC__
#define DLL_CALLCONV
#if defined(GCC_HASCLASSVISIBILITY)
#define DLL_API __attribute__ ((visibility("default")))
#else
#define DLL_API
#endif
#endif // WIN32 / !WIN32
#endif // FREEIMAGE_LIB

// Endianness:
// Some versions of gcc may have BYTE_ORDER or __BYTE_ORDER defined.
// If your big endian system isn't being detected, add an OS specific check
// or define any of FREEIMAGE_BIGENDIAN and FREEIMAGE_LITTLEENDIAN directly
// to specify the desired endianness.
#if (!defined(FREEIMAGE_BIGENDIAN) && !defined(FREEIMAGE_LITTLEENDIAN))
#if (defined(BYTE_ORDER) && BYTE_ORDER==BIG_ENDIAN) || (defined(__BYTE_ORDER) && __BYTE_ORDER==__BIG_ENDIAN) || defined(__BIG_ENDIAN__)
#define FREEIMAGE_BIGENDIAN
#endif // BYTE_ORDER
#endif // !FREEIMAGE_[BIG|LITTLE]ENDIAN

// Color-Order:
// The specified order of color components red, green and blue affects 24-
// and 32-bit images of type FIT_BITMAP as well as the colors that are part
// of a color palette. All other images always use RGB order. By default,
// color order is coupled to endianness:
// little-endian -> BGR
// big-endian    -> RGB
// However, you can always define FREEIMAGE_COLORORDER to any of the known
// orders FREEIMAGE_COLORORDER_BGR (0) and FREEIMAGE_COLORORDER_RGB (1) to
// specify your preferred color order.
#define FREEIMAGE_COLORORDER_BGR    0
#define FREEIMAGE_COLORORDER_RGB    1
#if (!defined(FREEIMAGE_COLORORDER)) || ((FREEIMAGE_COLORORDER != FREEIMAGE_COLORORDER_BGR) && (FREEIMAGE_COLORORDER != FREEIMAGE_COLORORDER_RGB))
#if defined(FREEIMAGE_BIGENDIAN)
#define FREEIMAGE_COLORORDER FREEIMAGE_COLORORDER_RGB
#else
#define FREEIMAGE_COLORORDER FREEIMAGE_COLORORDER_BGR
#endif // FREEIMAGE_BIGENDIAN
#endif // FREEIMAGE_COLORORDER

// Ensure 4-byte enums if we're using Borland C++ compilers
#if defined(__BORLANDC__)
#pragma option push -b
#endif

// For C compatibility --------------------------------------------------------

#ifdef __cplusplus
#define FI_DEFAULT(x)	= x
#define FI_ENUM(x)      enum x
#define FI_STRUCT(x)	struct x
#else
#define FI_DEFAULT(x)
#define FI_ENUM(x)      typedef int x; enum x
#define FI_STRUCT(x)	typedef struct x x; struct x
#endif

// Bitmap types -------------------------------------------------------------

FI_STRUCT (FIBITMAP) { void *data; };
FI_STRUCT (FIMULTIBITMAP) { void *data; };

// Types used in the library (directly copied from Windows) -----------------

#if defined(__MINGW32__) && defined(_WINDOWS_H)
#define _WINDOWS_	// prevent a bug in MinGW32
#endif // __MINGW32__

#ifndef _WINDOWS_
#define _WINDOWS_

#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
#ifndef NULL
#define NULL 0
#endif

#ifndef SEEK_SET
#define SEEK_SET  0
#define SEEK_CUR  1
#define SEEK_END  2
#endif

#ifndef _MSC_VER
// define portable types for 32-bit / 64-bit OS
#include <inttypes.h>
typedef int32_t BOOL;
typedef uint8_t BYTE;
typedef uint16_t WORD;
typedef uint32_t DWORD;
typedef int32_t LONG;
typedef int64_t INT64;
typedef uint64_t UINT64;
#else
// MS is not C99 ISO compliant
typedef long BOOL;
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned long DWORD;
typedef long LONG;
typedef signed __int64 INT64;
typedef unsigned __int64 UINT64;
#endif // _MSC_VER

#if (defined(_WIN32) || defined(__WIN32__))
#pragma pack(push, 1)
#else
#pragma pack(1)
#endif // WIN32

#if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
#else
BYTE rgbRed;
BYTE rgbGreen;
BYTE rgbBlue;
#endif // FREEIMAGE_COLORORDER
BYTE rgbReserved;

typedef struct tagRGBTRIPLE {
#if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR
BYTE rgbtBlue;
BYTE rgbtGreen;
BYTE rgbtRed;
#else
BYTE rgbtRed;
BYTE rgbtGreen;
BYTE rgbtBlue;
#endif // FREEIMAGE_COLORORDER
} RGBTRIPLE;

#if (defined(_WIN32) || defined(__WIN32__))
#pragma pack(pop)
#else
#pragma pack()
#endif // WIN32

DWORD biSize;
LONG  biWidth;
LONG  biHeight;
WORD  biPlanes;
WORD  biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG  biXPelsPerMeter;
LONG  biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;

typedef struct tagBITMAPINFO {
} BITMAPINFO, *PBITMAPINFO;

#endif // _WINDOWS_

// Types used in the library (specific to FreeImage) ------------------------

#if (defined(_WIN32) || defined(__WIN32__))
#pragma pack(push, 1)
#else
#pragma pack(1)
#endif // WIN32

/** 48-bit RGB
*/
typedef struct tagFIRGB16 {
WORD red;
WORD green;
WORD blue;
} FIRGB16;

/** 64-bit RGBA
*/
typedef struct tagFIRGBA16 {
WORD red;
WORD green;
WORD blue;
WORD alpha;
} FIRGBA16;

/** 96-bit RGB Float
*/
typedef struct tagFIRGBF {
float red;
float green;
float blue;
} FIRGBF;

/** 128-bit RGBA Float
*/
typedef struct tagFIRGBAF {
float red;
float green;
float blue;
float alpha;
} FIRGBAF;

/** Data structure for COMPLEX type (complex number)
*/
typedef struct tagFICOMPLEX {
/// real part
double r;
/// imaginary part
double i;
} FICOMPLEX;

#if (defined(_WIN32) || defined(__WIN32__))
#pragma pack(pop)
#else
#pragma pack()
#endif // WIN32

// Indexes for byte arrays, masks and shifts for treating pixels as words ---
// These coincide with the order of RGBQUAD and RGBTRIPLE -------------------

#ifndef FREEIMAGE_BIGENDIAN
#if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR
// Little Endian (x86 / MS Windows, Linux) : BGR(A) order
#define FI_RGBA_RED				2
#define FI_RGBA_GREEN			1
#define FI_RGBA_BLUE			0
#define FI_RGBA_ALPHA			3
#define FI_RGBA_RED_SHIFT		16
#define FI_RGBA_GREEN_SHIFT		8
#define FI_RGBA_BLUE_SHIFT		0
#define FI_RGBA_ALPHA_SHIFT		24
#else
// Little Endian (x86 / MaxOSX) : RGB(A) order
#define FI_RGBA_RED				0
#define FI_RGBA_GREEN			1
#define FI_RGBA_BLUE			2
#define FI_RGBA_ALPHA			3
#define FI_RGBA_RED_SHIFT		0
#define FI_RGBA_GREEN_SHIFT		8
#define FI_RGBA_BLUE_SHIFT		16
#define FI_RGBA_ALPHA_SHIFT		24
#endif // FREEIMAGE_COLORORDER
#else
#if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR
// Big Endian (PPC / none) : BGR(A) order
#define FI_RGBA_RED				2
#define FI_RGBA_GREEN			1
#define FI_RGBA_BLUE			0
#define FI_RGBA_ALPHA			3
#define FI_RGBA_RED_SHIFT		8
#define FI_RGBA_GREEN_SHIFT		16
#define FI_RGBA_BLUE_SHIFT		24
#define FI_RGBA_ALPHA_SHIFT		0
#else
// Big Endian (PPC / Linux, MaxOSX) : RGB(A) order
#define FI_RGBA_RED				0
#define FI_RGBA_GREEN			1
#define FI_RGBA_BLUE			2
#define FI_RGBA_ALPHA			3
#define FI_RGBA_RED_SHIFT		24
#define FI_RGBA_GREEN_SHIFT		16
#define FI_RGBA_BLUE_SHIFT		8
#define FI_RGBA_ALPHA_SHIFT		0
#endif // FREEIMAGE_COLORORDER
#endif // FREEIMAGE_BIGENDIAN

// The 16bit macros only include masks and shifts, since each color element is not byte aligned

#define FI16_555_RED_SHIFT		10
#define FI16_555_GREEN_SHIFT	5
#define FI16_555_BLUE_SHIFT		0
#define FI16_565_RED_SHIFT		11
#define FI16_565_GREEN_SHIFT	5
#define FI16_565_BLUE_SHIFT		0

// ICC profile support ------------------------------------------------------

#define FIICC_DEFAULT			0x00
#define FIICC_COLOR_IS_CMYK		0x01

FI_STRUCT (FIICCPROFILE) {
WORD    flags;	//! info flag
DWORD	size;	//! profile's size measured in bytes
void   *data;	//! points to a block of contiguous memory containing the profile
};

// Important enums ----------------------------------------------------------

/** I/O image format identifiers.
*/
FI_ENUM(FREE_IMAGE_FORMAT) {
FIF_UNKNOWN = -1,
FIF_BMP		= 0,
FIF_ICO		= 1,
FIF_JPEG	= 2,
FIF_JNG		= 3,
FIF_KOALA	= 4,
FIF_LBM		= 5,
FIF_IFF = FIF_LBM,
FIF_MNG		= 6,
FIF_PBM		= 7,
FIF_PBMRAW	= 8,
FIF_PCD		= 9,
FIF_PCX		= 10,
FIF_PGM		= 11,
FIF_PGMRAW	= 12,
FIF_PNG		= 13,
FIF_PPM		= 14,
FIF_PPMRAW	= 15,
FIF_RAS		= 16,
FIF_TARGA	= 17,
FIF_TIFF	= 18,
FIF_WBMP	= 19,
FIF_PSD		= 20,
FIF_CUT		= 21,
FIF_XBM		= 22,
FIF_XPM		= 23,
FIF_DDS		= 24,
FIF_GIF     = 25,
FIF_HDR		= 26,
FIF_FAXG3	= 27,
FIF_SGI		= 28,
FIF_EXR		= 29,
FIF_J2K		= 30,
FIF_JP2		= 31,
FIF_PFM		= 32,
FIF_PICT	= 33,
FIF_RAW		= 34,
FIF_WEBP	= 35,
FIF_JXR		= 36
};

/** Image type used in FreeImage.
*/
FI_ENUM(FREE_IMAGE_TYPE) {
FIT_UNKNOWN = 0,	//! unknown type
FIT_BITMAP  = 1,	//! standard image			: 1-, 4-, 8-, 16-, 24-, 32-bit
FIT_UINT16	= 2,	//! array of unsigned short	: unsigned 16-bit
FIT_INT16	= 3,	//! array of short			: signed 16-bit
FIT_UINT32	= 4,	//! array of unsigned long	: unsigned 32-bit
FIT_INT32	= 5,	//! array of long			: signed 32-bit
FIT_FLOAT	= 6,	//! array of float			: 32-bit IEEE floating point
FIT_DOUBLE	= 7,	//! array of double			: 64-bit IEEE floating point
FIT_COMPLEX	= 8,	//! array of FICOMPLEX		: 2 x 64-bit IEEE floating point
FIT_RGB16	= 9,	//! 48-bit RGB image			: 3 x 16-bit
FIT_RGBA16	= 10,	//! 64-bit RGBA image		: 4 x 16-bit
FIT_RGBF	= 11,	//! 96-bit RGB float image	: 3 x 32-bit IEEE floating point
FIT_RGBAF	= 12	//! 128-bit RGBA float image	: 4 x 32-bit IEEE floating point
};

/** Image color type used in FreeImage.
*/
FI_ENUM(FREE_IMAGE_COLOR_TYPE) {
FIC_MINISWHITE = 0,		//! min value is white
FIC_MINISBLACK = 1,		//! min value is black
FIC_RGB        = 2,		//! RGB color model
FIC_PALETTE    = 3,		//! color map indexed
FIC_RGBALPHA   = 4,		//! RGB color model with alpha channel
FIC_CMYK       = 5		//! CMYK color model
};

/** Color quantization algorithms.
Constants used in FreeImage_ColorQuantize.
*/
FI_ENUM(FREE_IMAGE_QUANTIZE) {
FIQ_WUQUANT = 0,		//! Xiaolin Wu color quantization algorithm
FIQ_NNQUANT = 1,		//! NeuQuant neural-net quantization algorithm by Anthony Dekker
FIQ_LFPQUANT = 2		//! Lossless Fast Pseudo-Quantization Algorithm by Carsten Klein
};

/** Dithering algorithms.
Constants used in FreeImage_Dither.
*/
FI_ENUM(FREE_IMAGE_DITHER) {
FID_FS			= 0,	//! Floyd & Steinberg error diffusion
FID_BAYER4x4	= 1,	//! Bayer ordered dispersed dot dithering (order 2 dithering matrix)
FID_BAYER8x8	= 2,	//! Bayer ordered dispersed dot dithering (order 3 dithering matrix)
FID_CLUSTER6x6	= 3,	//! Ordered clustered dot dithering (order 3 - 6x6 matrix)
FID_CLUSTER8x8	= 4,	//! Ordered clustered dot dithering (order 4 - 8x8 matrix)
FID_CLUSTER16x16= 5,	//! Ordered clustered dot dithering (order 8 - 16x16 matrix)
FID_BAYER16x16	= 6		//! Bayer ordered dispersed dot dithering (order 4 dithering matrix)
};

/** Lossless JPEG transformations
Constants used in FreeImage_JPEGTransform
*/
FI_ENUM(FREE_IMAGE_JPEG_OPERATION) {
FIJPEG_OP_NONE			= 0,	//! no transformation
FIJPEG_OP_FLIP_H		= 1,	//! horizontal flip
FIJPEG_OP_FLIP_V		= 2,	//! vertical flip
FIJPEG_OP_TRANSPOSE		= 3,	//! transpose across UL-to-LR axis
FIJPEG_OP_TRANSVERSE	= 4,	//! transpose across UR-to-LL axis
FIJPEG_OP_ROTATE_90		= 5,	//! 90-degree clockwise rotation
FIJPEG_OP_ROTATE_180	= 6,	//! 180-degree rotation
FIJPEG_OP_ROTATE_270	= 7		//! 270-degree clockwise (or 90 ccw)
};

/** Tone mapping operators.
Constants used in FreeImage_ToneMapping.
*/
FI_ENUM(FREE_IMAGE_TMO) {
FITMO_DRAGO03	 = 0,	//! Adaptive logarithmic mapping (F. Drago, 2003)
FITMO_REINHARD05 = 1,	//! Dynamic range reduction inspired by photoreceptor physiology (E. Reinhard, 2005)
FITMO_FATTAL02	 = 2	//! Gradient domain high dynamic range compression (R. Fattal, 2002)
};

/** Upsampling / downsampling filters.
Constants used in FreeImage_Rescale.
*/
FI_ENUM(FREE_IMAGE_FILTER) {
FILTER_BOX		  = 0,	//! Box, pulse, Fourier window, 1st order (constant) b-spline
FILTER_BICUBIC	  = 1,	//! Mitchell & Netravali's two-param cubic filter
FILTER_BILINEAR   = 2,	//! Bilinear filter
FILTER_BSPLINE	  = 3,	//! 4th order (cubic) b-spline
FILTER_CATMULLROM = 4,	//! Catmull-Rom spline, Overhauser spline
FILTER_LANCZOS3	  = 5	//! Lanczos3 filter
};

/** Color channels.
Constants used in color manipulation routines.
*/
FI_ENUM(FREE_IMAGE_COLOR_CHANNEL) {
FICC_RGB	= 0,	//! Use red, green and blue channels
FICC_RED	= 1,	//! Use red channel
FICC_GREEN	= 2,	//! Use green channel
FICC_BLUE	= 3,	//! Use blue channel
FICC_ALPHA	= 4,	//! Use alpha channel
FICC_BLACK	= 5,	//! Use black channel
FICC_REAL	= 6,	//! Complex images: use real part
FICC_IMAG	= 7,	//! Complex images: use imaginary part
FICC_MAG	= 8,	//! Complex images: use magnitude
FICC_PHASE	= 9		//! Complex images: use phase
};

/**
Tag data type information (based on TIFF specifications)

Note: RATIONALs are the ratio of two 32-bit integer values.
*/
FI_ENUM(FREE_IMAGE_MDTYPE) {
FIDT_NOTYPE		= 0,	//! placeholder
FIDT_BYTE		= 1,	//! 8-bit unsigned integer
FIDT_ASCII		= 2,	//! 8-bit bytes w/ last byte null
FIDT_SHORT		= 3,	//! 16-bit unsigned integer
FIDT_LONG		= 4,	//! 32-bit unsigned integer
FIDT_RATIONAL	= 5,	//! 64-bit unsigned fraction
FIDT_SBYTE		= 6,	//! 8-bit signed integer
FIDT_UNDEFINED	= 7,	//! 8-bit untyped data
FIDT_SSHORT		= 8,	//! 16-bit signed integer
FIDT_SLONG		= 9,	//! 32-bit signed integer
FIDT_SRATIONAL	= 10,	//! 64-bit signed fraction
FIDT_FLOAT		= 11,	//! 32-bit IEEE floating point
FIDT_DOUBLE		= 12,	//! 64-bit IEEE floating point
FIDT_IFD		= 13,	//! 32-bit unsigned integer (offset)
FIDT_PALETTE	= 14,	//! 32-bit RGBQUAD
FIDT_LONG8		= 16,	//! 64-bit unsigned integer
FIDT_SLONG8		= 17,	//! 64-bit signed integer
FIDT_IFD8		= 18	//! 64-bit unsigned integer (offset)
};

/**
*/
FI_ENUM(FREE_IMAGE_MDMODEL) {
FIMD_NODATA			= -1,
FIMD_COMMENTS		= 0,	//! single comment or keywords
FIMD_EXIF_MAIN		= 1,	//! Exif-TIFF metadata
FIMD_EXIF_EXIF		= 2,	//! Exif-specific metadata
FIMD_EXIF_GPS		= 3,	//! Exif GPS metadata
FIMD_EXIF_MAKERNOTE = 4,	//! Exif maker note metadata
FIMD_EXIF_INTEROP	= 5,	//! Exif interoperability metadata
FIMD_IPTC			= 6,	//! IPTC/NAA metadata
FIMD_XMP			= 7,	//! Abobe XMP metadata
FIMD_GEOTIFF		= 8,	//! GeoTIFF metadata
FIMD_ANIMATION		= 9,	//! Animation metadata
FIMD_CUSTOM			= 10,	//! Used to attach other metadata types to a dib
FIMD_EXIF_RAW		= 11	//! Exif metadata as a raw buffer
};

/**
*/
FI_STRUCT (FIMETADATA) { void *data; };

/**
Handle to a FreeImage tag
*/
FI_STRUCT (FITAG) { void *data; };

// File IO routines ---------------------------------------------------------

#ifndef FREEIMAGE_IO
#define FREEIMAGE_IO

typedef void* fi_handle;
typedef unsigned (DLL_CALLCONV *FI_ReadProc) (void *buffer, unsigned size, unsigned count, fi_handle handle);
typedef unsigned (DLL_CALLCONV *FI_WriteProc) (void *buffer, unsigned size, unsigned count, fi_handle handle);
typedef int (DLL_CALLCONV *FI_SeekProc) (fi_handle handle, long offset, int origin);
typedef long (DLL_CALLCONV *FI_TellProc) (fi_handle handle);

#if (defined(_WIN32) || defined(__WIN32__))
#pragma pack(push, 1)
#else
#pragma pack(1)
#endif // WIN32

FI_STRUCT(FreeImageIO) {
FI_WriteProc write_proc;    //! pointer to the function used to write data
FI_SeekProc  seek_proc;     //! pointer to the function used to seek
FI_TellProc  tell_proc;     //! pointer to the function used to aquire the current position
};

#if (defined(_WIN32) || defined(__WIN32__))
#pragma pack(pop)
#else
#pragma pack()
#endif // WIN32

/**
Handle to a memory I/O stream
*/
FI_STRUCT (FIMEMORY) { void *data; };

#endif // FREEIMAGE_IO

// Plugin routines ----------------------------------------------------------

#ifndef PLUGINS
#define PLUGINS

typedef const char *(DLL_CALLCONV *FI_FormatProc)(void);
typedef const char *(DLL_CALLCONV *FI_DescriptionProc)(void);
typedef const char *(DLL_CALLCONV *FI_ExtensionListProc)(void);
typedef const char *(DLL_CALLCONV *FI_RegExprProc)(void);
typedef void *(DLL_CALLCONV *FI_OpenProc)(FreeImageIO *io, fi_handle handle, BOOL read);
typedef void (DLL_CALLCONV *FI_CloseProc)(FreeImageIO *io, fi_handle handle, void *data);
typedef int (DLL_CALLCONV *FI_PageCountProc)(FreeImageIO *io, fi_handle handle, void *data);
typedef int (DLL_CALLCONV *FI_PageCapabilityProc)(FreeImageIO *io, fi_handle handle, void *data);
typedef FIBITMAP *(DLL_CALLCONV *FI_LoadProc)(FreeImageIO *io, fi_handle handle, int page, int flags, void *data);
typedef BOOL (DLL_CALLCONV *FI_SaveProc)(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flags, void *data);
typedef BOOL (DLL_CALLCONV *FI_ValidateProc)(FreeImageIO *io, fi_handle handle);
typedef const char *(DLL_CALLCONV *FI_MimeProc)(void);
typedef BOOL (DLL_CALLCONV *FI_SupportsExportBPPProc)(int bpp);
typedef BOOL (DLL_CALLCONV *FI_SupportsExportTypeProc)(FREE_IMAGE_TYPE type);
typedef BOOL (DLL_CALLCONV *FI_SupportsICCProfilesProc)(void);
typedef BOOL (DLL_CALLCONV *FI_SupportsNoPixelsProc)(void);

FI_STRUCT (Plugin) {
FI_FormatProc format_proc;
FI_DescriptionProc description_proc;
FI_ExtensionListProc extension_proc;
FI_RegExprProc regexpr_proc;
FI_OpenProc open_proc;
FI_CloseProc close_proc;
FI_PageCountProc pagecount_proc;
FI_PageCapabilityProc pagecapability_proc;
FI_SaveProc save_proc;
FI_ValidateProc validate_proc;
FI_MimeProc mime_proc;
FI_SupportsExportBPPProc supports_export_bpp_proc;
FI_SupportsExportTypeProc supports_export_type_proc;
FI_SupportsICCProfilesProc supports_icc_profiles_proc;
FI_SupportsNoPixelsProc supports_no_pixels_proc;
};

typedef void (DLL_CALLCONV *FI_InitProc)(Plugin *plugin, int format_id);

#endif // PLUGINS

// Load / Save flag constants -----------------------------------------------

#define BMP_DEFAULT         0
#define BMP_SAVE_RLE        1
#define CUT_DEFAULT         0
#define DDS_DEFAULT			0
#define EXR_DEFAULT			0		//! save data as half with piz-based wavelet compression
#define EXR_FLOAT			0x0001	//! save data as float instead of as half (not recommended)
#define EXR_NONE			0x0002	//! save with no compression
#define EXR_ZIP				0x0004	//! save with zlib compression, in blocks of 16 scan lines
#define EXR_PIZ				0x0008	//! save with piz-based wavelet compression
#define EXR_PXR24			0x0010	//! save with lossy 24-bit float compression
#define EXR_B44				0x0020	//! save with lossy 44% float compression - goes to 22% when combined with EXR_LC
#define EXR_LC				0x0040	//! save images with one luminance and two chroma channels, rather than as RGB (lossy compression)
#define FAXG3_DEFAULT		0
#define GIF_DEFAULT			0
#define GIF_LOAD256			1		//! load the image as a 256 color image with ununsed palette entries, if it's 16 or 2 color
#define GIF_PLAYBACK		2		//! 'Play' the GIF to generate each frame (as 32bpp) instead of returning raw frame data when loading
#define HDR_DEFAULT			0
#define ICO_DEFAULT         0
#define ICO_MAKEALPHA		1		//! convert to 32bpp and create an alpha channel from the AND-mask when loading
#define IFF_DEFAULT         0
#define J2K_DEFAULT			0		//! save with a 16:1 rate
#define JP2_DEFAULT			0		//! save with a 16:1 rate
#define JPEG_FAST           0x0001	//! load the file as fast as possible, sacrificing some quality
#define JPEG_ACCURATE       0x0002	//! load the file with the best quality, sacrificing some speed
#define JPEG_CMYK			0x0004	//! load separated CMYK "as is" (use | to combine with other load flags)
#define JPEG_EXIFROTATE		0x0008	//! load and rotate according to Exif 'Orientation' tag if available
#define JPEG_GREYSCALE		0x0010	//! load and convert to a 8-bit greyscale image
#define JPEG_QUALITYSUPERB  0x80	//! save with superb quality (100:1)
#define JPEG_QUALITYGOOD    0x0100	//! save with good quality (75:1)
#define JPEG_QUALITYNORMAL  0x0200	//! save with normal quality (50:1)
#define JPEG_QUALITYAVERAGE 0x0400	//! save with average quality (25:1)
#define JPEG_PROGRESSIVE	0x2000	//! save as a progressive-JPEG (use | to combine with other save flags)
#define JPEG_SUBSAMPLING_411 0x1000		//! save with high 4x1 chroma subsampling (4:1:1)
#define JPEG_SUBSAMPLING_420 0x4000		//! save with medium 2x2 medium chroma subsampling (4:2:0) - default value
#define JPEG_SUBSAMPLING_422 0x8000		//! save with low 2x1 chroma subsampling (4:2:2)
#define JPEG_SUBSAMPLING_444 0x10000	//! save with no chroma subsampling (4:4:4)
#define JPEG_OPTIMIZE		0x20000		//! on saving, compute optimal Huffman coding tables (can reduce a few percent of file size)
#define JPEG_BASELINE		0x40000		//! save basic JPEG, without metadata or any markers
#define KOALA_DEFAULT       0
#define LBM_DEFAULT         0
#define MNG_DEFAULT         0
#define PCD_DEFAULT         0
#define PCD_BASE            1		//! load the bitmap sized 768 x 512
#define PCD_BASEDIV4        2		//! load the bitmap sized 384 x 256
#define PCD_BASEDIV16       3		//! load the bitmap sized 192 x 128
#define PCX_DEFAULT         0
#define PFM_DEFAULT         0
#define PICT_DEFAULT        0
#define PNG_DEFAULT         0
#define PNG_Z_BEST_SPEED			0x0001	//! save using ZLib level 1 compression flag (default value is 6)
#define PNG_Z_DEFAULT_COMPRESSION	0x0006	//! save using ZLib level 6 compression flag (default recommended value)
#define PNG_Z_BEST_COMPRESSION		0x0009	//! save using ZLib level 9 compression flag (default value is 6)
#define PNG_Z_NO_COMPRESSION		0x0100	//! save without ZLib compression
#define PNG_INTERLACED				0x0200	//! save using Adam7 interlacing (use | to combine with other save flags)
#define PNM_DEFAULT         0
#define PNM_SAVE_RAW        0       //! if set the writer saves in RAW format (i.e. P4, P5 or P6)
#define PNM_SAVE_ASCII      1       //! if set the writer saves in ASCII format (i.e. P1, P2 or P3)
#define PSD_DEFAULT         0
#define PSD_CMYK			1		//! reads tags for separated CMYK (default is conversion to RGB)
#define PSD_LAB				2		//! reads tags for CIELab (default is conversion to RGB)
#define RAS_DEFAULT         0
#define RAW_DEFAULT         0		//! load the file as linear RGB 48-bit
#define RAW_PREVIEW			1		//! try to load the embedded JPEG preview with included Exif Data or default to RGB 24-bit
#define RAW_DISPLAY			2		//! load the file as RGB 24-bit
#define RAW_HALFSIZE		4		//! output a half-size color image
#define RAW_UNPROCESSED		8		//! output a FIT_UINT16 raw Bayer image
#define SGI_DEFAULT			0
#define TARGA_DEFAULT       0
#define TARGA_LOAD_RGB888   1       //! if set the loader converts RGB555 and ARGB8888 -> RGB888.
#define TARGA_SAVE_RLE		2		//! if set, the writer saves with RLE compression
#define TIFF_DEFAULT        0
#define TIFF_CMYK			0x0001	//! reads/stores tags for separated CMYK (use | to combine with compression flags)
#define TIFF_PACKBITS       0x0100  //! save using PACKBITS compression
#define TIFF_DEFLATE        0x0200  //! save using DEFLATE compression (a.k.a. ZLIB compression)
#define TIFF_NONE           0x0800  //! save without any compression
#define TIFF_CCITTFAX3		0x1000  //! save using CCITT Group 3 fax encoding
#define TIFF_CCITTFAX4		0x2000  //! save using CCITT Group 4 fax encoding
#define TIFF_LZW			0x4000	//! save using LZW compression
#define TIFF_JPEG			0x8000	//! save using JPEG compression
#define TIFF_LOGLUV			0x10000	//! save using LogLuv compression
#define WBMP_DEFAULT        0
#define XBM_DEFAULT			0
#define XPM_DEFAULT			0
#define WEBP_DEFAULT		0		//! save with good quality (75:1)
#define WEBP_LOSSLESS		0x100	//! save in lossless mode
#define JXR_DEFAULT			0		//! save with quality 80 and no chroma subsampling (4:4:4)
#define JXR_LOSSLESS		0x0064	//! save lossless
#define JXR_PROGRESSIVE		0x2000	//! save as a progressive-JXR (use | to combine with other save flags)

// Background filling options ---------------------------------------------------------
// Constants used in FreeImage_FillBackground and FreeImage_EnlargeCanvas

#define FI_COLOR_IS_RGB_COLOR			0x00	//! RGBQUAD color is a RGB color (contains no valid alpha channel)
#define FI_COLOR_IS_RGBA_COLOR			0x01	//! RGBQUAD color is a RGBA color (contains a valid alpha channel)
#define FI_COLOR_FIND_EQUAL_COLOR		0x02	//! For palettized images: lookup equal RGB color from palette
#define FI_COLOR_ALPHA_IS_INDEX			0x04	//! The color's rgbReserved member (alpha) contains the palette index to be used
#define FI_COLOR_PALETTE_SEARCH_MASK	(FI_COLOR_FIND_EQUAL_COLOR | FI_COLOR_ALPHA_IS_INDEX)	// No color lookup is performed

// RescaleEx options ---------------------------------------------------------
// Constants used in FreeImage_RescaleEx

#define FI_RESCALE_DEFAULT			0x00    //! default options; none of the following other options apply
#define FI_RESCALE_TRUE_COLOR		0x01	//! for non-transparent greyscale images, convert to 24-bit if src bitdepth <= 8 (default is a 8-bit greyscale image).
#define FI_RESCALE_OMIT_METADATA	0x02	//! do not copy metadata to the rescaled image

#ifdef __cplusplus
extern "C" {
#endif

// Init / Error routines ----------------------------------------------------

DLL_API void DLL_CALLCONV FreeImage_Initialise(BOOL load_local_plugins_only FI_DEFAULT(FALSE));
DLL_API void DLL_CALLCONV FreeImage_DeInitialise(void);

// Version routines ---------------------------------------------------------

DLL_API const char *DLL_CALLCONV FreeImage_GetVersion(void);

// Message output functions -------------------------------------------------

typedef void (*FreeImage_OutputMessageFunction)(FREE_IMAGE_FORMAT fif, const char *msg);
typedef void (DLL_CALLCONV *FreeImage_OutputMessageFunctionStdCall)(FREE_IMAGE_FORMAT fif, const char *msg);

DLL_API void DLL_CALLCONV FreeImage_SetOutputMessageStdCall(FreeImage_OutputMessageFunctionStdCall omf);
DLL_API void DLL_CALLCONV FreeImage_SetOutputMessage(FreeImage_OutputMessageFunction omf);
DLL_API void DLL_CALLCONV FreeImage_OutputMessageProc(int fif, const char *fmt, ...);

// Allocate / Clone / Unload routines ---------------------------------------

DLL_API FIBITMAP *DLL_CALLCONV FreeImage_Allocate(int width, int height, int bpp, unsigned red_mask FI_DEFAULT(0), unsigned green_mask FI_DEFAULT(0), unsigned blue_mask FI_DEFAULT(0));
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_AllocateT(FREE_IMAGE_TYPE type, int width, int height, int bpp FI_DEFAULT(8), unsigned red_mask FI_DEFAULT(0), unsigned green_mask FI_DEFAULT(0), unsigned blue_mask FI_DEFAULT(0));
DLL_API FIBITMAP * DLL_CALLCONV FreeImage_Clone(FIBITMAP *dib);

DLL_API BOOL DLL_CALLCONV FreeImage_HasPixels(FIBITMAP *dib);

// Load / Save routines -----------------------------------------------------

DLL_API FIBITMAP *DLL_CALLCONV FreeImage_Load(FREE_IMAGE_FORMAT fif, const char *filename, int flags FI_DEFAULT(0));
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_LoadU(FREE_IMAGE_FORMAT fif, const wchar_t *filename, int flags FI_DEFAULT(0));
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_LoadFromHandle(FREE_IMAGE_FORMAT fif, FreeImageIO *io, fi_handle handle, int flags FI_DEFAULT(0));
DLL_API BOOL DLL_CALLCONV FreeImage_Save(FREE_IMAGE_FORMAT fif, FIBITMAP *dib, const char *filename, int flags FI_DEFAULT(0));
DLL_API BOOL DLL_CALLCONV FreeImage_SaveU(FREE_IMAGE_FORMAT fif, FIBITMAP *dib, const wchar_t *filename, int flags FI_DEFAULT(0));
DLL_API BOOL DLL_CALLCONV FreeImage_SaveToHandle(FREE_IMAGE_FORMAT fif, FIBITMAP *dib, FreeImageIO *io, fi_handle handle, int flags FI_DEFAULT(0));

// Memory I/O stream routines -----------------------------------------------

DLL_API FIMEMORY *DLL_CALLCONV FreeImage_OpenMemory(BYTE *data FI_DEFAULT(0), DWORD size_in_bytes FI_DEFAULT(0));
DLL_API void DLL_CALLCONV FreeImage_CloseMemory(FIMEMORY *stream);
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_LoadFromMemory(FREE_IMAGE_FORMAT fif, FIMEMORY *stream, int flags FI_DEFAULT(0));
DLL_API BOOL DLL_CALLCONV FreeImage_SaveToMemory(FREE_IMAGE_FORMAT fif, FIBITMAP *dib, FIMEMORY *stream, int flags FI_DEFAULT(0));
DLL_API long DLL_CALLCONV FreeImage_TellMemory(FIMEMORY *stream);
DLL_API BOOL DLL_CALLCONV FreeImage_SeekMemory(FIMEMORY *stream, long offset, int origin);
DLL_API BOOL DLL_CALLCONV FreeImage_AcquireMemory(FIMEMORY *stream, BYTE **data, DWORD *size_in_bytes);
DLL_API unsigned DLL_CALLCONV FreeImage_ReadMemory(void *buffer, unsigned size, unsigned count, FIMEMORY *stream);
DLL_API unsigned DLL_CALLCONV FreeImage_WriteMemory(const void *buffer, unsigned size, unsigned count, FIMEMORY *stream);

DLL_API FIMULTIBITMAP *DLL_CALLCONV FreeImage_LoadMultiBitmapFromMemory(FREE_IMAGE_FORMAT fif, FIMEMORY *stream, int flags FI_DEFAULT(0));
DLL_API BOOL DLL_CALLCONV FreeImage_SaveMultiBitmapToMemory(FREE_IMAGE_FORMAT fif, FIMULTIBITMAP *bitmap, FIMEMORY *stream, int flags);

// Plugin Interface ---------------------------------------------------------

DLL_API FREE_IMAGE_FORMAT DLL_CALLCONV FreeImage_RegisterLocalPlugin(FI_InitProc proc_address, const char *format FI_DEFAULT(0), const char *description FI_DEFAULT(0), const char *extension FI_DEFAULT(0), const char *regexpr FI_DEFAULT(0));
DLL_API FREE_IMAGE_FORMAT DLL_CALLCONV FreeImage_RegisterExternalPlugin(const char *path, const char *format FI_DEFAULT(0), const char *description FI_DEFAULT(0), const char *extension FI_DEFAULT(0), const char *regexpr FI_DEFAULT(0));
DLL_API int DLL_CALLCONV FreeImage_GetFIFCount(void);
DLL_API int DLL_CALLCONV FreeImage_SetPluginEnabled(FREE_IMAGE_FORMAT fif, BOOL enable);
DLL_API int DLL_CALLCONV FreeImage_IsPluginEnabled(FREE_IMAGE_FORMAT fif);
DLL_API FREE_IMAGE_FORMAT DLL_CALLCONV FreeImage_GetFIFFromFormat(const char *format);
DLL_API FREE_IMAGE_FORMAT DLL_CALLCONV FreeImage_GetFIFFromMime(const char *mime);
DLL_API const char *DLL_CALLCONV FreeImage_GetFormatFromFIF(FREE_IMAGE_FORMAT fif);
DLL_API const char *DLL_CALLCONV FreeImage_GetFIFExtensionList(FREE_IMAGE_FORMAT fif);
DLL_API const char *DLL_CALLCONV FreeImage_GetFIFDescription(FREE_IMAGE_FORMAT fif);
DLL_API const char *DLL_CALLCONV FreeImage_GetFIFRegExpr(FREE_IMAGE_FORMAT fif);
DLL_API const char *DLL_CALLCONV FreeImage_GetFIFMimeType(FREE_IMAGE_FORMAT fif);
DLL_API FREE_IMAGE_FORMAT DLL_CALLCONV FreeImage_GetFIFFromFilename(const char *filename);
DLL_API FREE_IMAGE_FORMAT DLL_CALLCONV FreeImage_GetFIFFromFilenameU(const wchar_t *filename);
DLL_API BOOL DLL_CALLCONV FreeImage_FIFSupportsWriting(FREE_IMAGE_FORMAT fif);
DLL_API BOOL DLL_CALLCONV FreeImage_FIFSupportsExportBPP(FREE_IMAGE_FORMAT fif, int bpp);
DLL_API BOOL DLL_CALLCONV FreeImage_FIFSupportsExportType(FREE_IMAGE_FORMAT fif, FREE_IMAGE_TYPE type);
DLL_API BOOL DLL_CALLCONV FreeImage_FIFSupportsICCProfiles(FREE_IMAGE_FORMAT fif);
DLL_API BOOL DLL_CALLCONV FreeImage_FIFSupportsNoPixels(FREE_IMAGE_FORMAT fif);

// Multipaging interface ----------------------------------------------------

DLL_API FIMULTIBITMAP * DLL_CALLCONV FreeImage_OpenMultiBitmap(FREE_IMAGE_FORMAT fif, const char *filename, BOOL create_new, BOOL read_only, BOOL keep_cache_in_memory FI_DEFAULT(FALSE), int flags FI_DEFAULT(0));
DLL_API FIMULTIBITMAP * DLL_CALLCONV FreeImage_OpenMultiBitmapFromHandle(FREE_IMAGE_FORMAT fif, FreeImageIO *io, fi_handle handle, int flags FI_DEFAULT(0));
DLL_API BOOL DLL_CALLCONV FreeImage_SaveMultiBitmapToHandle(FREE_IMAGE_FORMAT fif, FIMULTIBITMAP *bitmap, FreeImageIO *io, fi_handle handle, int flags FI_DEFAULT(0));
DLL_API BOOL DLL_CALLCONV FreeImage_CloseMultiBitmap(FIMULTIBITMAP *bitmap, int flags FI_DEFAULT(0));
DLL_API int DLL_CALLCONV FreeImage_GetPageCount(FIMULTIBITMAP *bitmap);
DLL_API void DLL_CALLCONV FreeImage_AppendPage(FIMULTIBITMAP *bitmap, FIBITMAP *data);
DLL_API void DLL_CALLCONV FreeImage_InsertPage(FIMULTIBITMAP *bitmap, int page, FIBITMAP *data);
DLL_API void DLL_CALLCONV FreeImage_DeletePage(FIMULTIBITMAP *bitmap, int page);
DLL_API FIBITMAP * DLL_CALLCONV FreeImage_LockPage(FIMULTIBITMAP *bitmap, int page);
DLL_API void DLL_CALLCONV FreeImage_UnlockPage(FIMULTIBITMAP *bitmap, FIBITMAP *data, BOOL changed);
DLL_API BOOL DLL_CALLCONV FreeImage_MovePage(FIMULTIBITMAP *bitmap, int target, int source);
DLL_API BOOL DLL_CALLCONV FreeImage_GetLockedPageNumbers(FIMULTIBITMAP *bitmap, int *pages, int *count);

// Filetype request routines ------------------------------------------------

DLL_API FREE_IMAGE_FORMAT DLL_CALLCONV FreeImage_GetFileType(const char *filename, int size FI_DEFAULT(0));
DLL_API FREE_IMAGE_FORMAT DLL_CALLCONV FreeImage_GetFileTypeU(const wchar_t *filename, int size FI_DEFAULT(0));
DLL_API FREE_IMAGE_FORMAT DLL_CALLCONV FreeImage_GetFileTypeFromHandle(FreeImageIO *io, fi_handle handle, int size FI_DEFAULT(0));
DLL_API FREE_IMAGE_FORMAT DLL_CALLCONV FreeImage_GetFileTypeFromMemory(FIMEMORY *stream, int size FI_DEFAULT(0));

// Image type request routine -----------------------------------------------

DLL_API FREE_IMAGE_TYPE DLL_CALLCONV FreeImage_GetImageType(FIBITMAP *dib);

// FreeImage helper routines ------------------------------------------------

DLL_API BOOL DLL_CALLCONV FreeImage_IsLittleEndian(void);
DLL_API BOOL DLL_CALLCONV FreeImage_LookupX11Color(const char *szColor, BYTE *nRed, BYTE *nGreen, BYTE *nBlue);
DLL_API BOOL DLL_CALLCONV FreeImage_LookupSVGColor(const char *szColor, BYTE *nRed, BYTE *nGreen, BYTE *nBlue);

// Pixel access routines ----------------------------------------------------

DLL_API BYTE *DLL_CALLCONV FreeImage_GetBits(FIBITMAP *dib);
DLL_API BYTE *DLL_CALLCONV FreeImage_GetScanLine(FIBITMAP *dib, int scanline);

DLL_API BOOL DLL_CALLCONV FreeImage_GetPixelIndex(FIBITMAP *dib, unsigned x, unsigned y, BYTE *value);
DLL_API BOOL DLL_CALLCONV FreeImage_GetPixelColor(FIBITMAP *dib, unsigned x, unsigned y, RGBQUAD *value);
DLL_API BOOL DLL_CALLCONV FreeImage_SetPixelIndex(FIBITMAP *dib, unsigned x, unsigned y, BYTE *value);
DLL_API BOOL DLL_CALLCONV FreeImage_SetPixelColor(FIBITMAP *dib, unsigned x, unsigned y, RGBQUAD *value);

// DIB info routines --------------------------------------------------------

DLL_API unsigned DLL_CALLCONV FreeImage_GetColorsUsed(FIBITMAP *dib);
DLL_API unsigned DLL_CALLCONV FreeImage_GetBPP(FIBITMAP *dib);
DLL_API unsigned DLL_CALLCONV FreeImage_GetWidth(FIBITMAP *dib);
DLL_API unsigned DLL_CALLCONV FreeImage_GetHeight(FIBITMAP *dib);
DLL_API unsigned DLL_CALLCONV FreeImage_GetLine(FIBITMAP *dib);
DLL_API unsigned DLL_CALLCONV FreeImage_GetPitch(FIBITMAP *dib);
DLL_API unsigned DLL_CALLCONV FreeImage_GetDIBSize(FIBITMAP *dib);
DLL_API unsigned DLL_CALLCONV FreeImage_GetMemorySize(FIBITMAP *dib);

DLL_API unsigned DLL_CALLCONV FreeImage_GetDotsPerMeterX(FIBITMAP *dib);
DLL_API unsigned DLL_CALLCONV FreeImage_GetDotsPerMeterY(FIBITMAP *dib);
DLL_API void DLL_CALLCONV FreeImage_SetDotsPerMeterX(FIBITMAP *dib, unsigned res);
DLL_API void DLL_CALLCONV FreeImage_SetDotsPerMeterY(FIBITMAP *dib, unsigned res);

DLL_API BITMAPINFO *DLL_CALLCONV FreeImage_GetInfo(FIBITMAP *dib);
DLL_API FREE_IMAGE_COLOR_TYPE DLL_CALLCONV FreeImage_GetColorType(FIBITMAP *dib);

DLL_API unsigned DLL_CALLCONV FreeImage_GetTransparencyCount(FIBITMAP *dib);
DLL_API BYTE * DLL_CALLCONV FreeImage_GetTransparencyTable(FIBITMAP *dib);
DLL_API void DLL_CALLCONV FreeImage_SetTransparent(FIBITMAP *dib, BOOL enabled);
DLL_API void DLL_CALLCONV FreeImage_SetTransparencyTable(FIBITMAP *dib, BYTE *table, int count);
DLL_API BOOL DLL_CALLCONV FreeImage_IsTransparent(FIBITMAP *dib);
DLL_API void DLL_CALLCONV FreeImage_SetTransparentIndex(FIBITMAP *dib, int index);
DLL_API int DLL_CALLCONV FreeImage_GetTransparentIndex(FIBITMAP *dib);

DLL_API BOOL DLL_CALLCONV FreeImage_HasBackgroundColor(FIBITMAP *dib);
DLL_API BOOL DLL_CALLCONV FreeImage_GetBackgroundColor(FIBITMAP *dib, RGBQUAD *bkcolor);
DLL_API BOOL DLL_CALLCONV FreeImage_SetBackgroundColor(FIBITMAP *dib, RGBQUAD *bkcolor);

DLL_API FIBITMAP *DLL_CALLCONV FreeImage_GetThumbnail(FIBITMAP *dib);
DLL_API BOOL DLL_CALLCONV FreeImage_SetThumbnail(FIBITMAP *dib, FIBITMAP *thumbnail);

// ICC profile routines -----------------------------------------------------

DLL_API FIICCPROFILE *DLL_CALLCONV FreeImage_GetICCProfile(FIBITMAP *dib);
DLL_API FIICCPROFILE *DLL_CALLCONV FreeImage_CreateICCProfile(FIBITMAP *dib, void *data, long size);
DLL_API void DLL_CALLCONV FreeImage_DestroyICCProfile(FIBITMAP *dib);

// Line conversion routines -------------------------------------------------

DLL_API void DLL_CALLCONV FreeImage_ConvertLine1To4(BYTE *target, BYTE *source, int width_in_pixels);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine8To4(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine16To4_555(BYTE *target, BYTE *source, int width_in_pixels);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine16To4_565(BYTE *target, BYTE *source, int width_in_pixels);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine24To4(BYTE *target, BYTE *source, int width_in_pixels);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine32To4(BYTE *target, BYTE *source, int width_in_pixels);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine1To8(BYTE *target, BYTE *source, int width_in_pixels);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine4To8(BYTE *target, BYTE *source, int width_in_pixels);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine16To8_555(BYTE *target, BYTE *source, int width_in_pixels);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine16To8_565(BYTE *target, BYTE *source, int width_in_pixels);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine24To8(BYTE *target, BYTE *source, int width_in_pixels);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine32To8(BYTE *target, BYTE *source, int width_in_pixels);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine1To16_555(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine4To16_555(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine8To16_555(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine16_565_To16_555(BYTE *target, BYTE *source, int width_in_pixels);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine24To16_555(BYTE *target, BYTE *source, int width_in_pixels);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine32To16_555(BYTE *target, BYTE *source, int width_in_pixels);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine1To16_565(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine4To16_565(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine8To16_565(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine16_555_To16_565(BYTE *target, BYTE *source, int width_in_pixels);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine24To16_565(BYTE *target, BYTE *source, int width_in_pixels);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine32To16_565(BYTE *target, BYTE *source, int width_in_pixels);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine1To24(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine4To24(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine8To24(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine16To24_555(BYTE *target, BYTE *source, int width_in_pixels);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine16To24_565(BYTE *target, BYTE *source, int width_in_pixels);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine32To24(BYTE *target, BYTE *source, int width_in_pixels);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine1To32(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine4To32(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine8To32(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine16To32_555(BYTE *target, BYTE *source, int width_in_pixels);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine16To32_565(BYTE *target, BYTE *source, int width_in_pixels);
DLL_API void DLL_CALLCONV FreeImage_ConvertLine24To32(BYTE *target, BYTE *source, int width_in_pixels);

// Smart conversion routines ------------------------------------------------

DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertTo4Bits(FIBITMAP *dib);
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertTo8Bits(FIBITMAP *dib);
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertToGreyscale(FIBITMAP *dib);
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertTo16Bits555(FIBITMAP *dib);
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertTo16Bits565(FIBITMAP *dib);
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertTo24Bits(FIBITMAP *dib);
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertTo32Bits(FIBITMAP *dib);
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ColorQuantize(FIBITMAP *dib, FREE_IMAGE_QUANTIZE quantize);
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ColorQuantizeEx(FIBITMAP *dib, FREE_IMAGE_QUANTIZE quantize FI_DEFAULT(FIQ_WUQUANT), int PaletteSize FI_DEFAULT(256), int ReserveSize FI_DEFAULT(0), RGBQUAD *ReservePalette FI_DEFAULT(NULL));
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_Threshold(FIBITMAP *dib, BYTE T);
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_Dither(FIBITMAP *dib, FREE_IMAGE_DITHER algorithm);

DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertFromRawBits(BYTE *bits, int width, int height, int pitch, unsigned bpp, unsigned red_mask, unsigned green_mask, unsigned blue_mask, BOOL topdown FI_DEFAULT(FALSE));
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertFromRawBitsEx(BOOL copySource, BYTE *bits, FREE_IMAGE_TYPE type, int width, int height, int pitch, unsigned bpp, unsigned red_mask, unsigned green_mask, unsigned blue_mask, BOOL topdown FI_DEFAULT(FALSE));
DLL_API void DLL_CALLCONV FreeImage_ConvertToRawBits(BYTE *bits, FIBITMAP *dib, int pitch, unsigned bpp, unsigned red_mask, unsigned green_mask, unsigned blue_mask, BOOL topdown FI_DEFAULT(FALSE));

DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertToFloat(FIBITMAP *dib);
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertToRGBF(FIBITMAP *dib);
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertToRGBAF(FIBITMAP *dib);
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertToUINT16(FIBITMAP *dib);
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertToRGB16(FIBITMAP *dib);
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertToRGBA16(FIBITMAP *dib);

DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertToStandardType(FIBITMAP *src, BOOL scale_linear FI_DEFAULT(TRUE));
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertToType(FIBITMAP *src, FREE_IMAGE_TYPE dst_type, BOOL scale_linear FI_DEFAULT(TRUE));

// Tone mapping operators ---------------------------------------------------

DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ToneMapping(FIBITMAP *dib, FREE_IMAGE_TMO tmo, double first_param FI_DEFAULT(0), double second_param FI_DEFAULT(0));
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_TmoDrago03(FIBITMAP *src, double gamma FI_DEFAULT(2.2), double exposure FI_DEFAULT(0));
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_TmoReinhard05(FIBITMAP *src, double intensity FI_DEFAULT(0), double contrast FI_DEFAULT(0));
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_TmoReinhard05Ex(FIBITMAP *src, double intensity FI_DEFAULT(0), double contrast FI_DEFAULT(0), double adaptation FI_DEFAULT(1), double color_correction FI_DEFAULT(0));

DLL_API FIBITMAP *DLL_CALLCONV FreeImage_TmoFattal02(FIBITMAP *src, double color_saturation FI_DEFAULT(0.5), double attenuation FI_DEFAULT(0.85));

// ZLib interface -----------------------------------------------------------

DLL_API DWORD DLL_CALLCONV FreeImage_ZLibCompress(BYTE *target, DWORD target_size, BYTE *source, DWORD source_size);
DLL_API DWORD DLL_CALLCONV FreeImage_ZLibUncompress(BYTE *target, DWORD target_size, BYTE *source, DWORD source_size);
DLL_API DWORD DLL_CALLCONV FreeImage_ZLibGZip(BYTE *target, DWORD target_size, BYTE *source, DWORD source_size);
DLL_API DWORD DLL_CALLCONV FreeImage_ZLibGUnzip(BYTE *target, DWORD target_size, BYTE *source, DWORD source_size);
DLL_API DWORD DLL_CALLCONV FreeImage_ZLibCRC32(DWORD crc, BYTE *source, DWORD source_size);

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------

// tag creation / destruction
DLL_API FITAG *DLL_CALLCONV FreeImage_CreateTag(void);
DLL_API void DLL_CALLCONV FreeImage_DeleteTag(FITAG *tag);
DLL_API FITAG *DLL_CALLCONV FreeImage_CloneTag(FITAG *tag);

// tag getters and setters
DLL_API const char *DLL_CALLCONV FreeImage_GetTagKey(FITAG *tag);
DLL_API const char *DLL_CALLCONV FreeImage_GetTagDescription(FITAG *tag);
DLL_API WORD DLL_CALLCONV FreeImage_GetTagID(FITAG *tag);
DLL_API FREE_IMAGE_MDTYPE DLL_CALLCONV FreeImage_GetTagType(FITAG *tag);
DLL_API DWORD DLL_CALLCONV FreeImage_GetTagCount(FITAG *tag);
DLL_API DWORD DLL_CALLCONV FreeImage_GetTagLength(FITAG *tag);
DLL_API const void *DLL_CALLCONV FreeImage_GetTagValue(FITAG *tag);

DLL_API BOOL DLL_CALLCONV FreeImage_SetTagKey(FITAG *tag, const char *key);
DLL_API BOOL DLL_CALLCONV FreeImage_SetTagDescription(FITAG *tag, const char *description);
DLL_API BOOL DLL_CALLCONV FreeImage_SetTagID(FITAG *tag, WORD id);
DLL_API BOOL DLL_CALLCONV FreeImage_SetTagType(FITAG *tag, FREE_IMAGE_MDTYPE type);
DLL_API BOOL DLL_CALLCONV FreeImage_SetTagCount(FITAG *tag, DWORD count);
DLL_API BOOL DLL_CALLCONV FreeImage_SetTagLength(FITAG *tag, DWORD length);
DLL_API BOOL DLL_CALLCONV FreeImage_SetTagValue(FITAG *tag, const void *value);

// iterator

DLL_API BOOL DLL_CALLCONV FreeImage_SetMetadata(FREE_IMAGE_MDMODEL model, FIBITMAP *dib, const char *key, FITAG *tag);
DLL_API BOOL DLL_CALLCONV FreeImage_GetMetadata(FREE_IMAGE_MDMODEL model, FIBITMAP *dib, const char *key, FITAG **tag);
DLL_API BOOL DLL_CALLCONV FreeImage_SetMetadataKeyValue(FREE_IMAGE_MDMODEL model, FIBITMAP *dib, const char *key, const char *value);

// helpers
DLL_API unsigned DLL_CALLCONV FreeImage_GetMetadataCount(FREE_IMAGE_MDMODEL model, FIBITMAP *dib);
DLL_API BOOL DLL_CALLCONV FreeImage_CloneMetadata(FIBITMAP *dst, FIBITMAP *src);

// tag to C string conversion
DLL_API const char* DLL_CALLCONV FreeImage_TagToString(FREE_IMAGE_MDMODEL model, FITAG *tag, char *Make FI_DEFAULT(NULL));

// --------------------------------------------------------------------------
// JPEG lossless transformation routines
// --------------------------------------------------------------------------

DLL_API BOOL DLL_CALLCONV FreeImage_JPEGTransform(const char *src_file, const char *dst_file, FREE_IMAGE_JPEG_OPERATION operation, BOOL perfect FI_DEFAULT(TRUE));
DLL_API BOOL DLL_CALLCONV FreeImage_JPEGTransformU(const wchar_t *src_file, const wchar_t *dst_file, FREE_IMAGE_JPEG_OPERATION operation, BOOL perfect FI_DEFAULT(TRUE));
DLL_API BOOL DLL_CALLCONV FreeImage_JPEGCrop(const char *src_file, const char *dst_file, int left, int top, int right, int bottom);
DLL_API BOOL DLL_CALLCONV FreeImage_JPEGCropU(const wchar_t *src_file, const wchar_t *dst_file, int left, int top, int right, int bottom);
DLL_API BOOL DLL_CALLCONV FreeImage_JPEGTransformFromHandle(FreeImageIO* src_io, fi_handle src_handle, FreeImageIO* dst_io, fi_handle dst_handle, FREE_IMAGE_JPEG_OPERATION operation, int* left, int* top, int* right, int* bottom, BOOL perfect FI_DEFAULT(TRUE));
DLL_API BOOL DLL_CALLCONV FreeImage_JPEGTransformCombined(const char *src_file, const char *dst_file, FREE_IMAGE_JPEG_OPERATION operation, int* left, int* top, int* right, int* bottom, BOOL perfect FI_DEFAULT(TRUE));
DLL_API BOOL DLL_CALLCONV FreeImage_JPEGTransformCombinedU(const wchar_t *src_file, const wchar_t *dst_file, FREE_IMAGE_JPEG_OPERATION operation, int* left, int* top, int* right, int* bottom, BOOL perfect FI_DEFAULT(TRUE));
DLL_API BOOL DLL_CALLCONV FreeImage_JPEGTransformCombinedFromMemory(FIMEMORY* src_stream, FIMEMORY* dst_stream, FREE_IMAGE_JPEG_OPERATION operation, int* left, int* top, int* right, int* bottom, BOOL perfect FI_DEFAULT(TRUE));

// --------------------------------------------------------------------------
// Image manipulation toolkit
// --------------------------------------------------------------------------

// rotation and flipping
/// @deprecated see FreeImage_Rotate
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_RotateClassic(FIBITMAP *dib, double angle);
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_Rotate(FIBITMAP *dib, double angle, const void *bkcolor FI_DEFAULT(NULL));
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_RotateEx(FIBITMAP *dib, double angle, double x_shift, double y_shift, double x_origin, double y_origin, BOOL use_mask);
DLL_API BOOL DLL_CALLCONV FreeImage_FlipHorizontal(FIBITMAP *dib);
DLL_API BOOL DLL_CALLCONV FreeImage_FlipVertical(FIBITMAP *dib);

// upsampling / downsampling
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_Rescale(FIBITMAP *dib, int dst_width, int dst_height, FREE_IMAGE_FILTER filter FI_DEFAULT(FILTER_CATMULLROM));
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_MakeThumbnail(FIBITMAP *dib, int max_pixel_size, BOOL convert FI_DEFAULT(TRUE));
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_RescaleRect(FIBITMAP *dib, int dst_width, int dst_height, int left, int top, int right, int bottom, FREE_IMAGE_FILTER filter FI_DEFAULT(FILTER_CATMULLROM), unsigned flags FI_DEFAULT(0));

// color manipulation routines (point operations)
DLL_API BOOL DLL_CALLCONV FreeImage_AdjustCurve(FIBITMAP *dib, BYTE *LUT, FREE_IMAGE_COLOR_CHANNEL channel);
DLL_API BOOL DLL_CALLCONV FreeImage_AdjustGamma(FIBITMAP *dib, double gamma);
DLL_API BOOL DLL_CALLCONV FreeImage_AdjustBrightness(FIBITMAP *dib, double percentage);
DLL_API BOOL DLL_CALLCONV FreeImage_AdjustContrast(FIBITMAP *dib, double percentage);
DLL_API BOOL DLL_CALLCONV FreeImage_Invert(FIBITMAP *dib);
DLL_API BOOL DLL_CALLCONV FreeImage_GetHistogram(FIBITMAP *dib, DWORD *histo, FREE_IMAGE_COLOR_CHANNEL channel FI_DEFAULT(FICC_BLACK));
DLL_API int DLL_CALLCONV FreeImage_GetAdjustColorsLookupTable(BYTE *LUT, double brightness, double contrast, double gamma, BOOL invert);
DLL_API BOOL DLL_CALLCONV FreeImage_AdjustColors(FIBITMAP *dib, double brightness, double contrast, double gamma, BOOL invert FI_DEFAULT(FALSE));
DLL_API unsigned DLL_CALLCONV FreeImage_ApplyColorMapping(FIBITMAP *dib, RGBQUAD *srccolors, RGBQUAD *dstcolors, unsigned count, BOOL ignore_alpha, BOOL swap);
DLL_API unsigned DLL_CALLCONV FreeImage_ApplyPaletteIndexMapping(FIBITMAP *dib, BYTE *srcindices,	BYTE *dstindices, unsigned count, BOOL swap);
DLL_API unsigned DLL_CALLCONV FreeImage_SwapPaletteIndices(FIBITMAP *dib, BYTE *index_a, BYTE *index_b);

// channel processing routines
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_GetChannel(FIBITMAP *dib, FREE_IMAGE_COLOR_CHANNEL channel);
DLL_API BOOL DLL_CALLCONV FreeImage_SetChannel(FIBITMAP *dst, FIBITMAP *src, FREE_IMAGE_COLOR_CHANNEL channel);
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_GetComplexChannel(FIBITMAP *src, FREE_IMAGE_COLOR_CHANNEL channel);
DLL_API BOOL DLL_CALLCONV FreeImage_SetComplexChannel(FIBITMAP *dst, FIBITMAP *src, FREE_IMAGE_COLOR_CHANNEL channel);

// copy / paste / composite routines
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_Copy(FIBITMAP *dib, int left, int top, int right, int bottom);
DLL_API BOOL DLL_CALLCONV FreeImage_Paste(FIBITMAP *dst, FIBITMAP *src, int left, int top, int alpha);
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_CreateView(FIBITMAP *dib, unsigned left, unsigned top, unsigned right, unsigned bottom);

DLL_API FIBITMAP *DLL_CALLCONV FreeImage_Composite(FIBITMAP *fg, BOOL useFileBkg FI_DEFAULT(FALSE), RGBQUAD *appBkColor FI_DEFAULT(NULL), FIBITMAP *bg FI_DEFAULT(NULL));
DLL_API BOOL DLL_CALLCONV FreeImage_PreMultiplyWithAlpha(FIBITMAP *dib);

// background filling routines
DLL_API BOOL DLL_CALLCONV FreeImage_FillBackground(FIBITMAP *dib, const void *color, int options FI_DEFAULT(0));
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_EnlargeCanvas(FIBITMAP *src, int left, int top, int right, int bottom, const void *color, int options FI_DEFAULT(0));
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_AllocateEx(int width, int height, int bpp, const RGBQUAD *color, int options FI_DEFAULT(0), const RGBQUAD *palette FI_DEFAULT(NULL), unsigned red_mask FI_DEFAULT(0), unsigned green_mask FI_DEFAULT(0), unsigned blue_mask FI_DEFAULT(0));
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_AllocateExT(FREE_IMAGE_TYPE type, int width, int height, int bpp, const void *color, int options FI_DEFAULT(0), const RGBQUAD *palette FI_DEFAULT(NULL), unsigned red_mask FI_DEFAULT(0), unsigned green_mask FI_DEFAULT(0), unsigned blue_mask FI_DEFAULT(0));

// miscellaneous algorithms
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_MultigridPoissonSolver(FIBITMAP *Laplacian, int ncycle FI_DEFAULT(3));

// restore the borland-specific enum size option
#if defined(__BORLANDC__)
#pragma option pop
#endif

#ifdef __cplusplus
}
#endif

#endif // FREEIMAGE_H



mesh.h

#pragma once

#include <vector>
#include <string>
#include "Angel.h"
struct  point3f
{
float x;
float y;
float z;
point3f()
{
x = 0;
y = 0;
z = 0;
};
point3f(float xx, float yy, float zz)
{
x = xx;
y = yy;
z = zz;
};

float distance(const point3f& p)
{
float d = (x - p.x)*(x - p.x);
d += (y - p.y)*(y - p.y);
d += (z - p.z)*(z - p.z);

return sqrt(d);
};
};

typedef std::vector<float> VertexList;
typedef std::vector<float> NormalList;
typedef std::vector<float> VtList;

typedef std::vector<float> STLVectorf;
typedef std::vector<int> STLVectori;

typedef std::vector<std::pair<int, int> > Edges;
typedef std::vector<unsigned int> FaceList;

class My_Mesh
{
public:
My_Mesh();
~My_Mesh();

void set_texture_file(std::string s);
std::string get_texture_file();

const VertexList& get_vertices();
const NormalList& get_normals();
const FaceList&   get_faces();
const STLVectorf&   get_colors();
const VtList&   get_vts();

int num_faces();
int num_vertices();

const point3f& get_center();
void get_boundingbox(point3f& min_p, point3f& max_p) const;

void generate_disk(int num_division = 100);
void generate_cone(int num_division = 100, float height = 2);

void generate_cylinder(int num_division = 100, float height = 2);

static void normal_to_color(float, float, float, float&, float&, float&);
void set_translate(float, float, float);
void get_translate(float& x, float& y, float& z);
private:
void clear_data();
private:

VertexList m_vertices_;
NormalList m_normals_;
FaceList   m_faces_;
STLVectorf m_color_list_;
VtList	m_vt_list_;
point3f m_center_;
point3f m_min_box_;
point3f m_max_box_;

public:
void set_theta_step(float x, float y, float z);
void set_theta(float x, float y, float z);

void get_theta(float& x, float& y, float& z);
void set_scale(float x, float y, float z);

void get_scale(float& x, float& y, float& z);

private:

std::string texture_file_name;

enum { Xaxis = 0, Yaxis = 1, Zaxis = 2, NumAxes = 3 };
int      Axis = Xaxis;
GLfloat  Theta[3];
GLfloat  Theta_step[3];
GLfloat vTranslation[3];
GLfloat vscale[3];

};



Mesh_Painter.h

#pragma once
#include "mesh.h"
class Mesh_Painter
{
public:
Mesh_Painter();
~Mesh_Painter();

void draw_meshes();
void draw_robot(int i,vec3 RotateCen);
void draw_torso();
void draw_left_upper_arm();
void draw_right_upper_arm();
void draw_right_lower_arm();
void draw_left_lower_arm();
void draw_right_upper_leg();
void draw_left_upper_leg();
void draw_right_lower_leg();
void draw_left_lower_leg();
void draw_hat();
void draw_sword();

void update_vertex_buffer();
void update_texture();

void clear_mehs();

void updateMatrix(mat4 view);

mat4 modelMatrix;//需要修改的物体的矩阵
mat4 viewMatrix;
mat4 projMatrix;

vec3 lightPos;//光源位置
mat4 RobotTrans;
//层次建模各个部件的角度
enum{
Torso,
Hat,
RightUpperArm,
RightLowerArm,
RightLowerArmZ,
LeftUpperArm,
LeftLowerArm,
LeftLowerArmZ,
RightUpperLeg,
RightLowerLeg,
LeftUpperLeg,
LeftLowerLeg,
Sword,
NumJointAngles,
Quit
};

// Joint angles with initial values
GLfloat
theta[NumJointAngles] = {
0.0,    // Torso
0.0,    // Hat
0.0,    // RightUpperArm
-30.0,    // RightLowerArm
0.0,  // RightLowerArmY
0.0,    // LeftUpperArm
-30.0,    // LeftLowerArm
0.0, // LeftLowerArmY
0.0,  // RightUpperLeg
20.0,    // RightLowerLeg
0.0,  // LeftUpperLeg
20.0,     // LeftLowerLeg
0.0 //Sword
};

bool dirForward;
bool dirBack;
bool dirLeft;
bool dirRight;

private:

std::vector<GLuint> textures_all;
std::vector<GLuint> program_all;
std::vector<GLuint> vao_all;
std::vector<GLuint> buffer_all;
std::vector<GLuint> vPosition_all;
std::vector<GLuint> vColor_all;
std::vector<GLuint> vTexCoord_all;
std::vector<GLuint> vNormal_all;
std::vector<GLuint>  theta_all;
std::vector<GLuint>  trans_all;

std::vector<GLuint>  model_all;
std::vector<GLuint>  view_all;
std::vector<GLuint>  proj_all;

std::vector<My_Mesh*> m_my_meshes_;
};



#### 源文件

InitShder.cpp

#include "Angel.h"

namespace Angel {

// Create a NULL-terminated string by reading the provided file
static char*
{
FILE *fp;

if ( fp == NULL ) { return NULL; }

fseek(fp, 0L, SEEK_END);
long size = ftell(fp);

fseek(fp, 0L, SEEK_SET);
char* buf = new char[size + 1];
memset(buf, 0, size + 1);

//while (buf[size - 1] != '}')size--;
buf[size] = '\0';
fclose(fp);

return buf;
}

// Create a GLSL program object from vertex and fragment shader files
GLuint
{
const char*  filename;
GLenum       type;
GLchar*      source;
};

GLuint program = glCreateProgram();

for ( int i = 0; i < 2; ++i ) {
if ( shaders[i].source == NULL ) {
std::cerr << "Failed to read " << s.filename << std::endl;
exit( EXIT_FAILURE );
}

GLint  compiled;
if ( !compiled ) {
std::cerr << s.filename << " failed to compile:" << std::endl;
GLint  logSize;
char* logMsg = new char[logSize];
std::cerr << logMsg << std::endl;
delete [] logMsg;

exit( EXIT_FAILURE );
}

delete [] s.source;

}

/* link  and error check */

GLint  logSize;
glGetProgramiv( program, GL_INFO_LOG_LENGTH, &logSize);
char* logMsg = new char[logSize];
glGetProgramInfoLog( program, logSize, NULL, logMsg );
std::cerr << logMsg << std::endl;
delete [] logMsg;

exit( EXIT_FAILURE );
}

/* use program object */
glUseProgram(program);

return program;
}

}  // Close namespace Angel block



mesh.cpp

#include "mesh.h"
#include<sstream>
#include <fstream>
#include <iosfwd>
#include <algorithm>
#include <gl/GL.h>
#include <math.h>
#include <algorithm>

#pragma comment(lib, "glew32.lib")

My_Mesh::My_Mesh()
{

vTranslation[0] = Theta[0] = 0;
vTranslation[1] = Theta[1] = 0;
vTranslation[2] = Theta[2] = 0;
Theta[0] = 45;
}

My_Mesh::~My_Mesh()
{

}

void My_Mesh::normal_to_color(float nx, float ny, float nz, float& r, float& g, float& b)
{
r = float(std::min(std::max(0.5 * (nx + 1.0), 0.0), 1.0));
g = float(std::min(std::max(0.5 * (ny + 1.0), 0.0), 1.0));
b = float(std::min(std::max(0.5 * (nz + 1.0), 0.0), 1.0));
};

//读取obj的函数
{
this->clear_data();
//实现对含有UV坐标的obj文件的读取
//打开文件流
std::ifstream fin(obj_File);
std::string line;
if (!fin.is_open())
{
std::cout << "文件 " << obj_File << " 打开失败" << std::endl;
exit(-1);
}
std::vector<float> normals;//临时存储法线信息
std::vector<float> vtlist; //临时存储纹理信息

//计算各种类型的数据的数量
int v_num = 0;
int vt_num = 0;
int vn_num = 0;
int f_num = 0;

//保存vertices三个坐标下的最大值
float max_x = 0;
float max_y = 0;
float max_z = 0;

//按行读取
while (std::getline(fin, line))
{
std::istringstream sin(line);   //以一行的数据作为 string stream 解析并且读取
std::string type;
float x, y, z;
char slash;
//读取obj文件
sin >> type;
if (type == "v") {
sin >> x >> y >> z;
m_vertices_.push_back(x);
m_vertices_.push_back(y);
m_vertices_.push_back(z);
v_num++;

//更新最大值
if (max_x < fabs(x))max_x = fabs(x);
if (max_y < fabs(y))max_y = fabs(y);
if (max_z < fabs(z))max_z = fabs(z);

}
if (type == "vt") {
sin >> x >> y;
vtlist.push_back(x);
vtlist.push_back(y);
vt_num++;
}
if (type == "vn") {
sin >> x >> y >> z;
normals.push_back(x);
normals.push_back(y);
normals.push_back(z);
vn_num++;
}
if (type == "f") {
float a, b, c;
for (int i = 0; i < 3; i++) {
sin >> x >> slash >> y >> slash >> z;

//生成三角面片
m_faces_.push_back(x - 1);

//纹理坐标
a = vtlist[2 * (y - 1)];
b = vtlist[2 * (y - 1) + 1];
m_vt_list_.push_back(a);
m_vt_list_.push_back(b);

//法线由里向外
a = normals[3 * (z - 1)];
b = normals[3 * (z - 1) + 1];
c = normals[3 * (z - 1) + 2];
m_normals_.push_back(a);
m_normals_.push_back(b);
m_normals_.push_back(c);

//这里采用法线来生成颜色，学生可以自定义自己的颜色生成方式
float rr;
float gg;
float bb;
My_Mesh::normal_to_color(a, b, c, rr, gg, bb);
m_color_list_.push_back(rr);
m_color_list_.push_back(gg);
m_color_list_.push_back(bb);
}
f_num++;
}

}

//设置m_center_、>m_min_box_、>m_max_box_
this->m_center_ = point3f(0, 0, 0);
this->m_min_box_ = point3f(-max_x, -max_y, -max_z);
this->m_max_box_ = point3f(max_x, max_y, max_z);
};

const VtList&  My_Mesh::get_vts()
{
return this->m_vt_list_;
};
void My_Mesh::clear_data()
{
m_vertices_.clear();
m_normals_.clear();
m_faces_.clear();
m_color_list_.clear();
m_vt_list_.clear();
};
void My_Mesh::get_boundingbox(point3f& min_p, point3f& max_p) const
{
min_p = this->m_min_box_;
max_p = this->m_max_box_;
};
const STLVectorf&  My_Mesh::get_colors()
{
return this->m_color_list_;
};
const VertexList& My_Mesh::get_vertices()
{
return this->m_vertices_;
};
const NormalList& My_Mesh::get_normals()
{
return this->m_normals_;
};
const FaceList&   My_Mesh::get_faces()
{
return this->m_faces_;
};

int My_Mesh::num_faces()
{
return this->m_faces_.size()/3;
};
int My_Mesh::num_vertices()
{
return this->m_vertices_.size()/3;
};

const point3f& My_Mesh::get_center()
{
return this->m_center_;
};
void My_Mesh::generate_disk(int num_division)
{
this->clear_data();
this->m_center_ = point3f(0, 0, 0);
this->m_min_box_ = point3f(-1, -1, 0);
this->m_max_box_ = point3f(1, 1, 0);

int num_samples = num_division;

float pi = 3.14159265;
float step = 1.0 * 360 / num_samples;
float rr = pi / 180;

float z = 0;
for (int i = 0; i < num_samples; i++)
{
float r_r_r = i * step * rr;
float x = cos(r_r_r);
float y = sin(r_r_r);
m_vertices_.push_back(x);
m_vertices_.push_back(y);
m_vertices_.push_back(z);

m_normals_.push_back(x);
m_normals_.push_back(y);
m_normals_.push_back(0);

float r;
float g;
float b;
My_Mesh::normal_to_color(x, y, 0, r, g, b);

m_color_list_.push_back(r);
m_color_list_.push_back(g);
m_color_list_.push_back(b);
}

m_vertices_.push_back(0);
m_vertices_.push_back(0);
m_vertices_.push_back(0);

m_normals_.push_back(0);
m_normals_.push_back(0);
m_normals_.push_back(1);

float r;
float g;
float b;
My_Mesh::normal_to_color(0, 0, 1, r, g, b);
m_color_list_.push_back(r);
m_color_list_.push_back(g);
m_color_list_.push_back(b);

for (int i = 0; i < (num_samples); i++)
{
m_faces_.push_back(num_samples);
m_faces_.push_back((i) % (num_samples));
m_faces_.push_back((i + 1) % (num_samples));

m_vt_list_.push_back(0.5);
m_vt_list_.push_back(0.5);

float r_r_r = (i)* step * rr;
float x = cos(r_r_r);
float y = sin(r_r_r);

m_vt_list_.push_back((x + 1) / 2);
m_vt_list_.push_back((y + 1) / 2);

r_r_r = (i + 1) % num_samples * step * rr;
x = cos(r_r_r);
y = sin(r_r_r);

m_vt_list_.push_back((x + 1) / 2);
m_vt_list_.push_back((1 + y) / 2);
}
};
void My_Mesh::generate_cone(int num_division, float height)
{
//请在此添加代码生成圆锥体

this->clear_data();
this->m_center_ = point3f(0, 0, height / 2);
this->m_min_box_ = point3f(-1, -1, 0);
this->m_max_box_ = point3f(1, 1, height);

int num_samples = num_division;

float pi = 3.14159265;
float step = 1.0 * 360 / num_samples;
float rr = pi / 180;

float z = 0;
for (int i = 0; i < num_samples; i++)
{
float r_r_r = i * step * rr;
float x = cos(r_r_r);
float y = sin(r_r_r);
m_vertices_.push_back(x);
m_vertices_.push_back(y);
m_vertices_.push_back(z);

m_normals_.push_back(x);
m_normals_.push_back(y);
m_normals_.push_back(0);

float r;
float g;
float b;
My_Mesh::normal_to_color(x, y, 0, r, g, b);
m_color_list_.push_back(r);
m_color_list_.push_back(g);
m_color_list_.push_back(b);
}

m_vertices_.push_back(0);
m_vertices_.push_back(0);
m_vertices_.push_back(height);

m_normals_.push_back(0);
m_normals_.push_back(0);
m_normals_.push_back(1);

float r;
float g;
float b;
My_Mesh::normal_to_color(0, 0, 1, r, g, b);
m_color_list_.push_back(r);
m_color_list_.push_back(g);
m_color_list_.push_back(b);

for (int i = 0; i < (num_samples); i++)
{
m_faces_.push_back(num_samples);
m_faces_.push_back(i);
m_faces_.push_back((i + 1) % (num_samples));

m_vt_list_.push_back(0.5);
m_vt_list_.push_back(1);

m_vt_list_.push_back(1.0 * (i) / num_samples);
m_vt_list_.push_back(0);

m_vt_list_.push_back(1.0 * (i + 1) / num_samples);
m_vt_list_.push_back(0);

}
};

void My_Mesh::generate_cylinder(int num_division, float height)
{
this->clear_data();
this->m_center_ = point3f(0, 0, 0);
this->m_min_box_ = point3f(-1, -1, -height);
this->m_max_box_ = point3f(1, 1, height);

int num_samples = num_division;
float z = -height;
float pi = 3.14159265;
float step = 1.0 * 360 / num_samples;
float rr = pi / 180;
//圆柱体Z轴向上，按cos和sin生成x，y坐标
for (int i = 0; i < num_samples; i++)
{
float r_r_r = i * step * rr;
float x = cos(r_r_r);
float y = sin(r_r_r);
m_vertices_.push_back(x);
m_vertices_.push_back(y);
m_vertices_.push_back(z);

m_normals_.push_back(x);
m_normals_.push_back(y);
m_normals_.push_back(0);
//法线由里向外
float r;
float g;
float b;
My_Mesh::normal_to_color(x, y, z, r, g, b);
//这里采用法线来生成颜色，学生可以自定义自己的颜色生成方式
m_color_list_.push_back(r);
m_color_list_.push_back(g);
m_color_list_.push_back(b);
}

z = height;
//圆柱体Z轴向上，按cos和sin生成x，y坐标，
for (int i = 0; i < num_samples; i++)
{
float r_r_r = i * step * rr;
float x = cos(r_r_r);
float y = sin(r_r_r);
m_vertices_.push_back(x);
m_vertices_.push_back(y);
m_vertices_.push_back(z);

m_normals_.push_back(x);
m_normals_.push_back(y);
m_normals_.push_back(0);
//法线由里向外
float r;
float g;
float b;
My_Mesh::normal_to_color(x, y, z, r, g, b);
//这里采用法线来生成颜色，学生可以自定义自己的颜色生成方式
m_color_list_.push_back(r);
m_color_list_.push_back(g);
m_color_list_.push_back(b);

}
//生成三角面片
for (int i = 0; i < num_samples; i++)
{
m_faces_.push_back(i);
m_faces_.push_back((i + 1) % num_samples);
m_faces_.push_back((i) +num_samples);

m_faces_.push_back((i ) +num_samples);
m_faces_.push_back((i + 1) % num_samples);
m_faces_.push_back((i + 1) % (num_samples)+num_samples);
//生成三角面片

m_vt_list_.push_back(1.0 * i / num_samples);
m_vt_list_.push_back(0.0);
//纹理坐标
m_vt_list_.push_back(1.0 * ((i + 1)) / num_samples);
m_vt_list_.push_back(0.0);
//纹理坐标
m_vt_list_.push_back(1.0 * i / num_samples);
m_vt_list_.push_back(1.0);
//纹理坐标

m_vt_list_.push_back(1.0 * i / num_samples);
m_vt_list_.push_back(1.0);
//纹理坐标
m_vt_list_.push_back(1.0 * ((i + 1)) / num_samples);
m_vt_list_.push_back(0.0);
//纹理坐标
m_vt_list_.push_back(1.0 * ((i + 1)) / num_samples);
m_vt_list_.push_back(1.0);
//纹理坐标
}

};

void My_Mesh::set_texture_file(std::string s)
{
this->texture_file_name = s;
};
std::string My_Mesh::get_texture_file()
{
return this->texture_file_name;
};

void My_Mesh::set_translate(float x, float y, float z)
{
vTranslation[0] = x;
vTranslation[1] = y;
vTranslation[2] = z;

};
void My_Mesh::get_translate(float& x, float& y, float& z)
{
x = vTranslation[0];
y = vTranslation[1];
z = vTranslation[2];
};

void My_Mesh::set_theta(float x, float y, float z)
{
Theta[0] = x;
Theta[1] = y;
Theta[2] = z;
};
void My_Mesh::get_theta(float& x, float& y, float& z)
{
x = Theta[0];
y = Theta[1];
z = Theta[2];
};

void My_Mesh::set_scale(float x, float y, float z)
{
vscale[0] = x;
vscale[1] = y;
vscale[2] = z;

};
void My_Mesh::get_scale(float& x, float& y, float& z)
{
x = vscale[0];
y = vscale[1];
z = vscale[2];
};

void My_Mesh::set_theta_step(float x, float y, float z)
{
Theta_step[0] = x;
Theta_step[1] = y;
Theta_step[2] = z;
};

{
Theta[0] = Theta[0] + Theta_step[0];
Theta[1] = Theta[1] + Theta_step[1];
Theta[2] = Theta[2] + Theta_step[2];
};


Mesh_Painter.cpp

#include "Mesh_Painter.h"
#include "FreeImage.h"
#include <assert.h>
Mesh_Painter::Mesh_Painter()
{
}

Mesh_Painter::~Mesh_Painter()
{
}

class MatrixStack {
int    _index;
int    _size;
mat4*  _matrices;

public:
MatrixStack(int numMatrices = 100) :_index(0), _size(numMatrices)
{
_matrices = new mat4[numMatrices];
}

~MatrixStack()
{
delete[]_matrices;
}

void push(const mat4& m) {
assert(_index + 1 < _size);
_matrices[_index++] = m;

}

mat4& pop(void) {
assert(_index - 1 >= 0);
_index--;
return _matrices[_index];
}
};

mat4 RobotMat;
MatrixStack  mvstack;
//这个函数专门用来绘制机器人的部件，第一个参数是要绘制的物体的下标，第二个参数是旋转中心
void Mesh_Painter::draw_robot(int i, vec3 RotateCen) {

glUseProgram(this->program_all[i]);//指定使用渲染器，不同的模型可以指定不同的渲染器，这里我们使用的渲染器来自相同的文件，学生可以根据自己的爱好对不同的模型设定不同的渲染器
glBindVertexArray(this->vao_all[i]);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this->textures_all[i]);//该语句必须，否则将只使用同一个纹理进行绘制

float x, y, z;
this->m_my_meshes_[i]->get_theta(x, y, z);
GLfloat  Theta[3] = { x, y, z };

this->m_my_meshes_[i]->get_translate(x, y, z);
GLfloat  vTranslation[3] = { x, y, z };

this->m_my_meshes_[i]->get_scale(x, y, z);
GLfloat  vScale[3] = { x, y, z };

GLuint lightPosID = glGetUniformLocation(this->program_all[i], "lightPos");
glUniform3fv(lightPosID, 1, &lightPos[0]);

mat4 model = Translate(vTranslation[0], vTranslation[1], vTranslation[2]);
model *= RobotMat;
//model *= RotateY(180);
model *= RotateX(Theta[0]);
model *= Translate(RotateCen.x, 0, 0)*RotateY(Theta[1])*Translate(-RotateCen.x, 0, 0);
model *= Translate(0, 0, RotateCen.z)*RotateZ(Theta[2])*Translate(0, 0, -RotateCen.z);
model *= Scale(vScale[0], vScale[1], vScale[2]);

//model *= RobotMat;
GLuint mlocation = glGetUniformLocation(this->program_all[i], "modelMatrix");    // 名为model的uniform变量的位置索引
glUniformMatrix4fv(mlocation, 1, GL_TRUE, &model[0][0]);
GLuint vlocation = glGetUniformLocation(this->program_all[i], "viewMatrix");    // 名为model的uniform变量的位置索引
glUniformMatrix4fv(vlocation, 1, GL_TRUE, &viewMatrix[0][0]);
GLuint plocation = glGetUniformLocation(this->program_all[i], "projMatrix");    // 名为model的uniform变量的位置索引
glUniformMatrix4fv(plocation, 1, GL_TRUE, &projMatrix[0][0]);

glDrawArrays(GL_TRIANGLES, 0, this->m_my_meshes_[i]->num_faces() * 3);

绘制阴影

float lx = lightPos[0];
float ly = lightPos[1];
float lz = lightPos[2];

// 计算阴影投影矩阵
lx, 0.0, lz, 1.0,
0.0, 0.0, -ly, 0.0,
0.0, 0.0, 0.0, -ly);

glDrawArrays(GL_TRIANGLES, 0, this->m_my_meshes_[i]->num_faces() * 3);

glUseProgram(0);
}
void Mesh_Painter::draw_torso() {
draw_robot(0,vec3(0,0,0));
}
void Mesh_Painter::draw_left_upper_arm() {
draw_robot(1, vec3(0.395, 0, 0));
//draw_robot(1, vec3(0, 0, 0));
}
void Mesh_Painter::draw_right_upper_arm() {
draw_robot(2, vec3(-0.395, 0, 0));
//draw_robot(2, vec3(0, 0, 0));

}
void Mesh_Painter::draw_right_lower_arm() {
draw_robot(3, vec3(-0.345, 0, 0));
//draw_robot(3, vec3(0, 0, 0));

}
void Mesh_Painter::draw_left_lower_arm() {
draw_robot(4, vec3(0.345, 0, 0));
//draw_robot(4, vec3(0, 0, 0));

}
void Mesh_Painter::draw_right_upper_leg() {
draw_robot(5, vec3(-0.2, 0, 0));
//draw_robot(5, vec3(0, 0, 0));
}
void Mesh_Painter::draw_left_upper_leg() {
draw_robot(6, vec3(0.2, 0, 0));
//draw_robot(6, vec3(0, 0, 0));

}
void Mesh_Painter::draw_right_lower_leg() {
draw_robot(7, vec3(-0.17, 0, 0));
//draw_robot(7, vec3(0, 0, 0));
}
void Mesh_Painter::draw_left_lower_leg() {
draw_robot(8, vec3(0.17, 0, 0));
//draw_robot(8, vec3(0, 0, 0));
}
draw_robot(9, vec3(0, 0, 0));
}
void Mesh_Painter::draw_hat() {
draw_robot(10, vec3(0, 0, 0));
}
void Mesh_Painter::draw_sword() {
draw_robot(11, vec3(0, 0, 0));
}

void Mesh_Painter::draw_meshes()
{

//以下为层次建模来调用各个部位的函数并且绘制
RobotMat = RobotTrans;

/

mvstack.push(RobotMat);//保存父节点矩阵
RobotMat *= RotateY(theta[Torso]);
draw_torso();
RobotMat = mvstack.pop();//恢复父节点矩阵

/
mvstack.push(RobotMat);//保存父节点矩阵
RobotMat*=RotateY(theta[Torso]);
//由于采用绘制软件来生成obj模型，导致物体的旋转中心并不太一致，只能手动微调旋转中心
RobotMat *= Translate(0, 0.8, 0)*RotateX(theta[LeftUpperArm])*Translate(0,-0.8,0);
draw_left_upper_arm();
RobotMat = mvstack.pop();//恢复父节点矩阵
/
mvstack.push(RobotMat);//保存父节点矩阵
RobotMat *= Translate(-0.065, 0, 0)*RotateY(theta[Torso])*Translate(0.065, 0, 0);
RobotMat *= Translate(0, 0.7, 0)*RotateX(theta[LeftUpperArm])*Translate(0, -0.7, 0);
RobotMat *= Translate(0, 0.5, 0)*RotateX(theta[LeftLowerArm])*Translate(0, -0.5, 0);
RobotMat *= Translate(0.3, 0.5, 0)*RotateZ(theta[LeftLowerArmZ])*Translate(-0.3, -0.5, 0);

draw_left_lower_arm();
RobotMat = mvstack.pop();//恢复父节点矩阵

/
mvstack.push(RobotMat);//保存父节点矩阵
RobotMat *= RotateY(theta[Torso]);
RobotMat *= Translate(0, 0.8, 0)*RotateX(theta[RightUpperArm])*Translate(0, -0.8, 0);
draw_right_upper_arm();
RobotMat = mvstack.pop();//恢复父节点矩阵

/
mvstack.push(RobotMat);//保存父节点矩阵
RobotMat *= Translate(0.065,0,0)*RotateY(theta[Torso])*Translate(-0.065,0,0);
RobotMat *= Translate(0, 0.7, 0)*RotateX(theta[RightUpperArm])*Translate(0, -0.7, 0);
RobotMat *= Translate(0, 0.5, 0)*RotateX(theta[RightLowerArm])*Translate(0, -0.5, 0);
RobotMat *= Translate(-0.3, 0.5, 0)*RotateZ(theta[RightLowerArmZ])*Translate(0.3, -0.5, 0);

draw_right_lower_arm();
RobotMat = mvstack.pop();//恢复父节点矩阵

/

mvstack.push(RobotMat);//保存父节点矩阵
RobotMat *= RotateY(theta[Torso]);
RobotMat *= Translate(0, 0.5, 0)*RotateX(theta[RightUpperLeg])*Translate(0, -0.5, 0);
draw_right_upper_leg();
RobotMat = mvstack.pop();//恢复父节点矩阵
/
mvstack.push(RobotMat);//保存父节点矩阵
RobotMat *= Translate(0.05, 0, 0)*RotateY(theta[Torso])*Translate(-0.05, 0, 0);
RobotMat *= Translate(0, 0.5, 0)*RotateX(theta[RightUpperLeg])*Translate(0, -0.5, 0);
RobotMat *= Translate(0, 0.2, 0)*RotateX(theta[RightLowerLeg])*Translate(0, -0.2, 0);

draw_right_lower_leg();
RobotMat = mvstack.pop();//恢复父节点矩阵

/
mvstack.push(RobotMat);//保存父节点矩阵
RobotMat *= RotateY(theta[Torso]);
RobotMat *= Translate(0, 0.5, 0)*RotateX(theta[LeftUpperLeg])*Translate(0, -0.5, 0);
draw_left_upper_leg();
RobotMat = mvstack.pop();//恢复父节点矩阵

/
mvstack.push(RobotMat);//保存父节点矩阵
RobotMat *= Translate(-0.05, 0, 0)*RotateY(theta[Torso])*Translate(0.05, 0, 0);
RobotMat *= Translate(0, 0.5, 0)*RotateX(theta[LeftUpperLeg])*Translate(0, -0.5, 0);
RobotMat *= Translate(0, 0.2, 0)*RotateX(theta[LeftLowerLeg])*Translate(0, -0.2, 0);
draw_left_lower_leg();
RobotMat = mvstack.pop();//恢复父节点矩阵
/
mvstack.push(RobotMat);//保存父节点矩阵
RobotMat *= RotateY(theta[Torso]);
RobotMat = mvstack.pop();//恢复父节点矩阵
/
mvstack.push(RobotMat);//保存父节点矩阵
RobotMat *= RotateY(theta[Torso]);
draw_hat();
RobotMat = mvstack.pop();//恢复父节点矩阵
/
mvstack.push(RobotMat);//保存父节点矩阵
RobotMat *= Translate(0.125, 0, 0)*RotateY(theta[Torso])*Translate(-0.125, 0, 0);
RobotMat *= Translate(0, 0.7, 0)*RotateX(theta[RightUpperArm])*Translate(0, -0.7, 0);
RobotMat *= Translate(0, 0.5, 0)*RotateX(theta[RightLowerArm])*Translate(0, -0.5, 0);
RobotMat *= Translate(0, 0, 0)*RotateZ(theta[LeftLowerArmZ])*Translate(0, 0, 0);
RobotMat *= Translate(-0.2, 0, 0)*RotateY(theta[Sword])*Translate(0.2, 0, 0);

draw_sword();
RobotMat = mvstack.pop();//恢复父节点矩阵

//层次建模绘制完毕后，开始对其他物体进行绘制
for (unsigned int i = 12; i < this->m_my_meshes_.size(); i++)
{
//if (i == 6)continue;
glUseProgram(this->program_all[i]);//指定使用渲染器，不同的模型可以指定不同的渲染器，这里我们使用的渲染器来自相同的文件，学生可以根据自己的爱好对不同的模型设定不同的渲染器
glBindVertexArray(this->vao_all[i]);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this->textures_all[i]);//该语句必须，否则将只使用同一个纹理进行绘制

float x, y, z;
this->m_my_meshes_[i]->get_theta(x, y, z);
GLfloat  Theta[3] = {x, y, z};

this->m_my_meshes_[i]->get_translate(x, y, z);
GLfloat  vTranslation[3] = { x, y, z };

this->m_my_meshes_[i]->get_scale(x, y, z);
GLfloat  vScale[3] = { x, y, z };

GLuint lightPosID = glGetUniformLocation(this->program_all[i], "lightPos");
glUniform3fv(lightPosID, 1, &lightPos[0]);

mat4 model = Translate(vTranslation[0], vTranslation[1], vTranslation[2]);
model *= RotateX(Theta[0]);
model *= RotateY(Theta[1]);
if (i != 21) {
model *= RotateZ(Theta[2]);//21是风车，风车自旋的旋转中心需要稍作调整
}
else model *= Translate(-0.2, 2.95, 0)*RotateZ(Theta[2])*Translate(0.2, -2.95, 0);
model *= Scale(vScale[0], vScale[1], vScale[2]);

GLuint mlocation = glGetUniformLocation(this->program_all[i], "modelMatrix");
glUniformMatrix4fv(mlocation, 1, GL_TRUE, &model[0][0]);
GLuint vlocation = glGetUniformLocation(this->program_all[i], "viewMatrix");
glUniformMatrix4fv(vlocation, 1, GL_TRUE, &viewMatrix[0][0]);
GLuint plocation = glGetUniformLocation(this->program_all[i], "projMatrix");
glUniformMatrix4fv(plocation, 1, GL_TRUE, &projMatrix[0][0]);

glDrawArrays(GL_TRIANGLES, 0, this->m_my_meshes_[i]->num_faces() * 3);

if (i==17||i==22||i==23) {
//某些物体我们并不需要绘制其阴影，例如地面和太阳，所以我们在此作出区分
continue;
}
绘制阴影

float lx = lightPos[0];
float ly = lightPos[1];
float lz = lightPos[2];

// 计算阴影投影矩阵
lx, 0.0, lz, 1.0,
0.0, 0.0, -ly, 0.0,
0.0, 0.0, 0.0, -ly);

glDrawArrays(GL_TRIANGLES, 0, this->m_my_meshes_[i]->num_faces() * 3);

glUseProgram(0);
}

};

void Mesh_Painter::update_texture()
{
this->textures_all.clear();

for (unsigned int i = 0; i < this->m_my_meshes_.size(); i++)
{
GLuint textures;

glGenTextures(1, &textures);
//调用FreeImage生成纹理

//指定最大最小过滤方法，此两行代码必须添加，否则将无法显示纹理
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glBindTexture(GL_TEXTURE_2D, textures);
glUniform1i(glGetUniformLocation(this->program_all[i], "texture"), 0);
this->textures_all.push_back(textures);
}

};

{
//1 获取图片格式
FREE_IMAGE_FORMAT fifmt = FreeImage_GetFileType(file_name.c_str(), 0);

//2 加载图片
FIBITMAP *dib = FreeImage_Load(fifmt, file_name.c_str(), 0);

//3 转化为rgb 24色;
dib = FreeImage_ConvertTo24Bits(dib);

//4 获取数据指针
BYTE *pixels = (BYTE*)FreeImage_GetBits(dib);

int width = FreeImage_GetWidth(dib);
int height = FreeImage_GetHeight(dib);

/**
* 产生一个纹理Id,可以认为是纹理句柄，后面的操作将书用这个纹理id
*/

/**
* 使用这个纹理id,或者叫绑定(关联)
*/
glBindTexture(GL_TEXTURE_2D, m_texName);
/**
* 指定纹理的放大,缩小滤波，使用线性方式，即当图片放大的时候插值方式
*/
/**
* 将图片的rgb数据上传给opengl.
*/
glTexImage2D(
GL_TEXTURE_2D, //! 指定是二维图片
0, //! 指定为第一级别，纹理可以做mipmap,即lod,离近的就采用级别大的，远则使用较小的纹理
GL_RGB, //! 纹理的使用的存储格式
width, //! 宽度，老一点的显卡，不支持不规则的纹理，即宽度和高度不是2^n。
height, //! 宽度，老一点的显卡，不支持不规则的纹理，即宽度和高度不是2^n。
0, //! 是否的边
GL_BGR_EXT, //! 数据的格式，bmp中，windows,操作系统中存储的数据是bgr格式
GL_UNSIGNED_BYTE, //! 数据是8bit数据
pixels
);
/**
* 释放内存
*/
};

void Mesh_Painter::update_vertex_buffer()
{

this->vao_all.clear();
this->buffer_all.clear();
this->vPosition_all.clear();
this->vColor_all.clear();
this->vTexCoord_all.clear();
this->vNormal_all.clear();

for (unsigned int m_i = 0; m_i < this->m_my_meshes_.size(); m_i++)
{
int num_face = this->m_my_meshes_[m_i]->num_faces();
int num_vertex = this->m_my_meshes_[m_i]->num_vertices();

const VertexList& vertex_list = this->m_my_meshes_[m_i]->get_vertices();
const NormalList& normal_list = this->m_my_meshes_[m_i]->get_normals();
const FaceList&  face_list = this->m_my_meshes_[m_i]->get_faces();
const STLVectorf& color_list = this->m_my_meshes_[m_i]->get_colors();
const VtList& vt_list = this->m_my_meshes_[m_i]->get_vts();

// Create a vertex array object
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER,
sizeof(vec3)*num_face * 3
+ sizeof(vec3)*num_face * 3
+ sizeof(vec3)*num_face * 3
+ sizeof(vec2)*num_face * 3
,
NULL, GL_STATIC_DRAW)
;
//获得足够的空间存储坐标，颜色，法线以及纹理坐标等等
// Specify an offset to keep track of where we're placing data in our
//   vertex array buffer.  We'll use the same technique when we
//   associate the offsets with vertex attribute pointers.

vec3* points = new vec3[num_face * 3];
point3f  p_center_ = this->m_my_meshes_[m_i]->get_center();
point3f p_min_box_, p_max_box_;
this->m_my_meshes_[m_i]->get_boundingbox(p_min_box_, p_max_box_);
float d = p_min_box_.distance(p_max_box_);

for (int i = 0; i < num_face; i++)
{
int index = face_list[3 * i];
points[3 * i] = vec3(
(vertex_list[index * 3 + 0] - p_center_.x) / (1.5 * d),
(vertex_list[index * 3 + 1] - p_center_.y) / (1.5 * d),
(vertex_list[index * 3 + 2] - p_center_.z) / (1.5 * d)
);

index = face_list[3 * i + 1];
points[3 * i + 1] = vec3(
(vertex_list[index * 3 + 0] - p_center_.x) / (1.5 * d),
(vertex_list[index * 3 + 1] - p_center_.y) / (1.5 * d),
(vertex_list[index * 3 + 2] - p_center_.z) / (1.5 * d)
);

index = face_list[3 * i + 2];
points[3 * i + 2] = vec3(
(vertex_list[index * 3 + 0] - p_center_.x) / (1.5 * d),
(vertex_list[index * 3 + 1] - p_center_.y) / (1.5 * d),
(vertex_list[index * 3 + 2] - p_center_.z) / (1.5 * d)
);

}
GLintptr offset = 0;
glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(vec3)*num_face * 3, points);
offset += sizeof(vec3)*num_face * 3;
delete[] points;
/************************************************************************/
/*                                                                      */
/************************************************************************/
points = new vec3[num_face * 3];
for (int i = 0; i < num_face; i++)
{
int index = face_list[3 * i];
points[3 * i] = vec3(
(normal_list[index * 3 + 0]),
(normal_list[index * 3 + 1]),
(normal_list[index * 3 + 2])
);

index = face_list[3 * i + 1];
points[3 * i + 1] = vec3(
(normal_list[index * 3 + 0]),
(normal_list[index * 3 + 1]),
(normal_list[index * 3 + 2])
);

index = face_list[3 * i + 2];
points[3 * i + 2] = vec3(
(normal_list[index * 3 + 0]),
(normal_list[index * 3 + 1]),
(normal_list[index * 3 + 2])
);
}
glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(vec3) * num_face * 3, points);
offset += sizeof(vec3) * num_face * 3;
delete[] points;

/************************************************************************/
/*                                                                      */
/************************************************************************/
points = new vec3[num_face * 3];
for (int i = 0; i < num_face; i++)
{
int index = face_list[3 * i];
points[3 * i] = vec3(
(color_list[index * 3 + 0]),
(color_list[index * 3 + 1]),
(color_list[index * 3 + 2])
);

index = face_list[3 * i + 1];
points[3 * i + 1] = vec3(
(color_list[index * 3 + 0]),
(color_list[index * 3 + 1]),
(color_list[index * 3 + 2])
);

index = face_list[3 * i + 2];
points[3 * i + 2] = vec3(
(color_list[index * 3 + 0]),
(color_list[index * 3 + 1]),
(color_list[index * 3 + 2])
);
}
glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(vec3) * num_face * 3, points);
offset += sizeof(vec3) * num_face * 3;
delete[] points;

/************************************************************************/
/*                                                                      */
/************************************************************************/
vec2* points_2 = new vec2[num_face * 3];
for (int i = 0; i < num_face; i++)
{
points_2[i * 3] = vec2(vt_list[i * 6 + 0], vt_list[i * 6 + 1]);
points_2[i * 3 + 1] = vec2(vt_list[i * 6 + 2], vt_list[i * 6 + 3]);
points_2[i * 3 + 2] = vec2(vt_list[i * 6 + 4], vt_list[i * 6 + 5]);
}
glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(vec2) * num_face * 3, points_2);
offset += sizeof(vec2) * num_face * 3;
delete points_2;
/************************************************************************/
/*                                                                      */
/************************************************************************/

// set up vertex arrays
offset = 0;
GLuint vPosition;
vPosition = glGetAttribLocation(this->program_all[m_i], "vPosition");
glEnableVertexAttribArray(vPosition);
glVertexAttribPointer(vPosition, 3, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET(offset));
offset += sizeof(vec3) * num_face * 3;

GLuint vNormal;
vNormal = glGetAttribLocation(this->program_all[m_i], "vNormal");
glEnableVertexAttribArray(vNormal);
glVertexAttribPointer(vNormal, 3, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET(offset));
offset += sizeof(vec3) * num_face * 3;

GLuint vColor;
vColor = glGetAttribLocation(this->program_all[m_i], "vColor");
glEnableVertexAttribArray(vColor);
glVertexAttribPointer(vColor, 3, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET(offset));
offset += sizeof(vec3) * num_face * 3;

GLuint vTexCoord;
vTexCoord = glGetAttribLocation(this->program_all[m_i], "vTexCoord");
glEnableVertexAttribArray(vTexCoord);
glVertexAttribPointer(vTexCoord, 2, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET(offset));

/************************************************************************/
/*                                                                      */
/************************************************************************/

this->vao_all.push_back(vao);
this->buffer_all.push_back(buffer);
this->vPosition_all.push_back(vPosition);
this->vColor_all.push_back(vColor);
this->vTexCoord_all.push_back(vTexCoord);
this->vNormal_all.push_back(vNormal);
}
};

void Mesh_Painter::updateMatrix(mat4 view) {
viewMatrix = view;
std::cout << "update view" << std::endl;
viewMatrix.display();
}

{
this->program_all.clear();
this->theta_all.clear();
this->trans_all.clear();
for (unsigned int i = 0; i < this->m_my_meshes_.size(); i++)
{
//指定使用的渲染器，不同的模型可以指定不同的渲染器，这里我们使用的渲染器来自相同的文件，学生可以根据自己的爱好对不同的模型设定不同的渲染器
this->program_all.push_back(program);

GLuint 	theta = glGetUniformLocation(program, "theta");
GLuint  trans = glGetUniformLocation(program, "translation");

theta_all.push_back(theta);
trans_all.push_back(trans);
}

};
{
this->m_my_meshes_.push_back(m);
};
void Mesh_Painter::clear_mehs()
{
this->m_my_meshes_.clear();

this->textures_all.clear();
this->program_all.clear();
this->vao_all.clear();
this->buffer_all.clear();
this->vPosition_all.clear();
this->vColor_all.clear();
this->vTexCoord_all.clear();
this->vNormal_all.clear();
};



Texture_Mapping.cpp

// rotating cube with two texture objects
// change textures with 1 and 2 keys

#include "Angel.h"
#include "mesh.h"
#include "FreeImage.h"
#include "Mesh_Painter.h"

#pragma comment(lib, "glew32.lib")
#pragma comment(lib, "FreeImage.lib")

typedef Angel::vec4 point4;
typedef Angel::vec4 color4;
//躯干各个部位，这里是theta的下标，我们将其用英文取代数字来标识
enum {
Torso,
Hat,
RightUpperArm,
RightLowerArm,
RightLowerArmZ,
LeftUpperArm,
LeftLowerArm,
LeftLowerArmZ,
RightUpperLeg,
RightLowerLeg,
LeftUpperLeg,
LeftLowerLeg,
Sword,
NumJointAngles,
Quit
};

int windowWidth = 800;  // 窗口宽
int windowHeight = 800;

float pitch = 9.6f;
float roll = 0.0f;
float yaw = 158.4f;

vec3 lightPos(0, 15.0, 15.0);

std::vector<My_Mesh*> my_meshs;
Mesh_Painter*			mp_;
// Texture objects and storage for texture image

vec3 cameraPosition(-4.4, 2.13, -6.40);
vec3 cameraUp(0.0, 1.0, 0.0);

vec3 RobotPos(0, 0, 0);

// Vertex data arrays

// Array of rotation angles (in degrees) for each coordinate axis

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------

//弧度转角度
float PI = 3.1415926;
return PI / 180 * degree;
}

void
init()
{
glEnable(GL_DEPTH_TEST);
glEnable( GL_DEPTH_TEST );
glEnable(GL_LIGHTING);
glClearColor( 0.596, 0.96, 1.0, 1.0 );
}

void
display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

mp_->theta[0] = -yaw+180;
//这条语句可以用来使得角色朝向相机正对的方向

mat4 modelMatrix = mat4(1.0);
mat4 viewMatrix = LookAt(vec4(cameraPosition, 1.0), vec4(cameraPosition, 1.0) + vec4(cameraDirection, 0.0), vec4(cameraUp, 1.0));//调用lookat函数来构造view矩阵
mat4 projMatrix = Perspective(45.0, 1.0, 1, 100.0);
mp_->modelMatrix = modelMatrix;//修改传入mp的矩阵
mp_->viewMatrix = viewMatrix;
mp_->projMatrix = projMatrix;

mp_->draw_meshes();
glutSwapBuffers();
};

//----------------------------------------------------------------------------

//----------------------------------------------------------------------------
void
idle(void)
{
glutPostRedisplay();
}

//----------------------------------------------------------------------------

//相机视角改变所需要的函数
float clamp(float x, float min, float max)
{
if (x > max)
return max;
if (x < min)
return min;
return x;
}
float mod(float a, float b) {
while (a > b) {
a = a - b;
}
return a;
}

int State=0;
//人物动画的函数，通过传入关键帧的参数，然后根据关键帧来改变人物的动作
void RobotArmAnim(int i) {
if (i % 20 == 1) {
mp_->theta[RightUpperArm] = 0;
mp_->theta[LeftUpperArm] = 0;
mp_->theta[LeftUpperLeg] = 0;
mp_->theta[RightUpperLeg] = 0;
mp_->theta[RightLowerArmZ] = 0;
mp_->theta[LeftLowerArmZ] = 0;
mp_->theta[Sword] = 0;
}
if (i%20 <= 5) {
mp_->theta[RightUpperArm] +=10;
mp_->theta[LeftUpperArm] -= 10;
mp_->theta[LeftUpperLeg] += 6;
mp_->theta[RightUpperLeg] -= 6;
}
if (i % 20 > 5 && i % 20 <= 15) {
mp_->theta[RightUpperArm] -= 10;
mp_->theta[LeftUpperArm] += 10;
mp_->theta[LeftUpperLeg] -= 6;
mp_->theta[RightUpperLeg] += 6;
}
if (i % 20 > 15 && i % 20 <= 20) {
mp_->theta[RightUpperArm] += 10;
mp_->theta[LeftUpperArm] -= 10;
mp_->theta[LeftUpperLeg] += 6;
mp_->theta[RightUpperLeg] -= 6;
}

}
int SwordState = 0;
void SwordAnim(int i) {
//剑的动画的函数
if (i%33 == 1) {
mp_->theta[RightUpperArm] = 0;
mp_->theta[LeftUpperArm] = 0;
mp_->theta[LeftUpperLeg] = 0;
mp_->theta[RightUpperLeg] = 0;
mp_->theta[RightLowerArmZ] = 0;
mp_->theta[LeftLowerArmZ] = 0;
mp_->theta[Sword] = 0;
}
if (i% 33 > 1 && i% 33 <= 5) {

}
if (i% 33 > 5 && i% 33 <= 8) {
mp_->theta[RightUpperArm] -=10 ;
mp_->theta[LeftUpperArm] -= 10;
mp_-> theta[Sword] += 15;

}
if (i% 33 > 8 && i% 33 <= 11) {
mp_->theta[RightLowerArmZ] += 15;
mp_->theta[LeftLowerArmZ] -= 15;
mp_->theta[Sword] += 15;
}
if (i % 33 > 11&& i % 33 <=21) {
mp_->theta[RightUpperArm] -= 10;
mp_->theta[LeftUpperArm] -= 10;
mp_->theta[Sword] += 7.5;

}
if (i % 33 > 21 ) {
mp_->theta[RightUpperArm] += 10;
mp_->theta[LeftUpperArm] += 10;
}

float yPos = RobotPos.y;
RobotPos.y = yPos;
if (i % 20 <= 5) {
mp_->theta[LeftUpperLeg] += 10;
mp_->theta[RightUpperLeg] -= 10;
}
if (i % 20 > 5 && i % 20 <= 15) {
mp_->theta[LeftUpperLeg] -= 10;
mp_->theta[RightUpperLeg] += 10;
}
if (i % 20 > 15 && i % 20 <= 20) {
mp_->theta[LeftUpperLeg] += 10;
mp_->theta[RightUpperLeg] -= 10;
}
}
void
keyboard( unsigned char key, int x, int y )
{
float yPos;
switch (key)
{
case 033:	// ESC键 和 'q' 键退出游戏
exit(EXIT_SUCCESS);
break;
case 'q':case 'Q':
exit(EXIT_SUCCESS);
break;
case 'w':case 'W':
break;
case 's':case 'S':
break;
case 'a':case 'A':
break;
case 'd':case 'D':
break;
case ' ':
cameraPosition.y += 0.10f;
break;
case 'c':case 'C':
cameraPosition.y -= 0.10f;
break;
case 'i':case 'I':
yPos = RobotPos.y;
RobotPos.y = yPos;//人物只在y轴上移动，因此y的值不应该发生改变
State++;//改变动画关键帧
RobotArmAnim(State);//人物移动时将会调用动画函数
break;
case 'k':case 'K':
yPos = RobotPos.y;
RobotPos.y = yPos;
State++;
RobotArmAnim(State);
break;
case 'j':case 'J':
yPos = RobotPos.y;
RobotPos.y = yPos;
State++;
RobotArmAnim(State);
break;
case 'l':case 'L':
yPos = RobotPos.y;
RobotPos.y = yPos;
State++;
RobotArmAnim(State);
break;
case 'z':case'Z':
mp_->theta[0] += 5;//调整物体朝向的方向
break;
case 'x':case'X':
mp_->theta[0] -= 5;//调整物体朝向的方向
break;
case 'r': case 'R':
mp_->theta[LeftUpperLeg] -= 10;
break;
case 't': case 'T':
mp_->theta[LeftLowerLeg] -= 10;
break;
case 'y': case 'Y':
mp_->theta[Sword] += 10;
break;

case 'o': case 'O':
mp_->theta[LeftLowerArmZ] -= 10;
mp_->theta[RightLowerArmZ] += 10;
break;
case 'p': case'P':
SwordState++;
SwordAnim(SwordState);

}

mp_->RobotTrans = Translate(RobotPos.x, RobotPos.y, RobotPos.z);
//改变机器人的位置
glutPostRedisplay();
}

void mouse(int x, int y)
{
//此处是相机跟随鼠标的转动而发生转动，采用欧拉角来实现
yaw += 35 * (x - float(windowWidth) / 2.0) / windowWidth;
yaw = mod(yaw + 180.0f, 360.0f) - 180.0f;
pitch += -35 * (y - float(windowHeight) / 2.0) / windowHeight;
pitch = clamp(pitch, -89.0f, 89.0f);

glutWarpPointer(windowWidth / 2.0, windowHeight / 2.0);	// 将指针钉死在屏幕正中间

glutPostRedisplay();
return;
}

//----------------------------------------------------------------------------

int
main( int argc, char **argv )
{

glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowSize( windowWidth, windowHeight );
glutInitContextVersion( 3, 2 );
glutInitContextProfile( GLUT_CORE_PROFILE );
int mainWindow=glutCreateWindow( "黄伟斌2018182011图形学大作业" );

glewExperimental = GL_TRUE;
glewInit();

init();

mp_ = new Mesh_Painter;
mp_->lightPos = lightPos;

//从以下开始绘制模型

My_Mesh* my_mesh1 = new My_Mesh;
my_mesh1->set_texture_file("texture/torso.png");
my_mesh1->set_translate(0, 0.25, 0);//设定初始位置
my_mesh1->set_theta(0, 0, 0);//设定人物初始的角度
my_mesh1->set_scale(3, 3, 3);//设定初始的规模大小
my_mesh1->set_theta_step(0, 0, 0);//设定自转的速度
my_meshs.push_back(my_mesh1);//将读入的物体放入存储物体的mesh容器

My_Mesh* my_mesh2 = new My_Mesh;
my_mesh2->set_texture_file("texture/left_upper_arm.png");
my_mesh2->set_translate(0, 0.25, 0);
my_mesh2->set_theta(0, 0, 0);
my_mesh2->set_scale(3, 3, 3);
my_mesh2->set_theta_step(0, 0, 0);
my_meshs.push_back(my_mesh2);

My_Mesh* my_mesh3 = new My_Mesh;
my_mesh3->set_texture_file("texture/right_upper_arm.png");
my_mesh3->set_translate(0, 0.25, 0);
my_mesh3->set_theta(0, 0, 0);
my_mesh3->set_scale(3, 3, 3);
my_mesh3->set_theta_step(0, 0, 0);
my_meshs.push_back(my_mesh3);

My_Mesh* my_mesh4 = new My_Mesh;
my_mesh4->set_texture_file("texture/right_lower_arm.png");
my_mesh4->set_translate(-0.06, 0.32, 0);
my_mesh4->set_theta(0, 0, 0);
my_mesh4->set_scale(2, 2, 2);
my_mesh4->set_theta_step(0, 0, 0);
my_meshs.push_back(my_mesh4);

My_Mesh* my_mesh5 = new My_Mesh;
my_mesh5->set_texture_file("texture/left_lower_arm.png");
my_mesh5->set_translate(0.06, 0.32, 0);
my_mesh5->set_theta(0, 0, 0);
my_mesh5->set_scale(2, 2, 2);
my_mesh5->set_theta_step(0, 0, 0);
my_meshs.push_back(my_mesh5);

My_Mesh* my_mesh6 = new My_Mesh;
my_mesh6->set_texture_file("texture/right_upper_leg.png");
my_mesh6->set_translate(0, -0.05, 0);
my_mesh6->set_theta(0, 0, 0);
my_mesh6->set_scale(2, 2, 2);
my_mesh6->set_theta_step(0, 0, 0);
my_meshs.push_back(my_mesh6);

My_Mesh* my_mesh7 = new My_Mesh;
my_mesh7->set_texture_file("texture/left_upper_leg.png");
my_mesh7->set_translate(0, -0.05, 0);
my_mesh7->set_theta(0, 0, 0);
my_mesh7->set_scale(2, 2, 2);
my_mesh7->set_theta_step(0, 0, 0);
my_meshs.push_back(my_mesh7);

My_Mesh* my_mesh8 = new My_Mesh;
my_mesh8->set_texture_file("texture/right_lower_leg.png");
my_mesh8->set_translate(-0.05, 0, 0);
my_mesh8->set_theta(0, 0, 0);
my_mesh8->set_scale(1, 1, 1);
my_mesh8->set_theta_step(0, 0, 0);
my_meshs.push_back(my_mesh8);

My_Mesh* my_mesh9 = new My_Mesh;
my_mesh9->set_texture_file("texture/left_lower_leg.png");
my_mesh9->set_translate(0.05, 0, 0);
my_mesh9->set_theta(0, 0, 0);
my_mesh9->set_scale(1, 1, 1);
my_mesh9->set_theta_step(0, 0, 0);
my_meshs.push_back(my_mesh9);

My_Mesh* my_mesh10 = new My_Mesh;
my_mesh10->set_translate(0, 0.52, 0);
my_mesh10->set_theta(0, 0, 0);
my_mesh10->set_scale(4, 4, 4);
my_mesh10->set_theta_step(0, 0, 0);
my_meshs.push_back(my_mesh10);

My_Mesh* my_mesh11 = new My_Mesh;
my_mesh11->generate_cone(100, 2);
my_mesh11->set_texture_file("texture/cone.jpg");
my_mesh11->set_translate(0, 1.8, -0.04);
my_mesh11->set_theta(-90, 0, 0);
my_mesh11->set_scale(1.7, 1.7, 1.7);//旋转轴
my_mesh11->set_theta_step(0.0, 0.0, 0);
my_meshs.push_back(my_mesh11);

My_Mesh* my_mesh12 = new My_Mesh;
my_mesh12->set_texture_file("texture/sword.png");
my_mesh12->set_translate(-0.12, 0.3, 0);
my_mesh12->set_theta(0, 0, 0);
my_mesh12->set_scale(4, 4, 4);
my_mesh12->set_theta_step(0, 0, 0);
my_meshs.push_back(my_mesh12);

My_Mesh* my_mesh13 = new My_Mesh;
my_mesh13->generate_cylinder(100, 3);//生成圆柱表面
my_mesh13->set_texture_file("texture/cylinder10.jpg");//指定纹理图像文件
my_mesh13->set_translate(2.5, 3, 2.5);
my_mesh13->set_theta(90, 0, 0);//旋转轴
my_mesh13->set_scale(3, 3, 10);//旋转轴
my_mesh13->set_theta_step(0, 0, 0);//旋转速度
my_meshs.push_back(my_mesh13);

My_Mesh* my_mesh14 = new My_Mesh;
my_mesh14->generate_cylinder(100, 3);//生成圆柱表面
my_mesh14->set_texture_file("texture/cylinder10.jpg");//指定纹理图像文件
my_mesh14->set_translate(7.5, 3, 2.5);
my_mesh14->set_theta(90, 0, 0);//旋转轴
my_mesh14->set_scale(3, 3, 10);//旋转轴
my_mesh14->set_theta_step(0, 0, 0);//旋转速度
my_meshs.push_back(my_mesh14);

My_Mesh* my_mesh15 = new My_Mesh;
my_mesh15->generate_cylinder(100, 3);//生成圆柱表面
my_mesh15->set_texture_file("texture/cylinder10.jpg");//指定纹理图像文件
my_mesh15->set_translate(2.5, 3, 7.5);
my_mesh15->set_theta(90, 0, 0);//旋转轴
my_mesh15->set_scale(3, 3, 10);//旋转轴
my_mesh15->set_theta_step(0, 0, 0);//旋转速度
my_meshs.push_back(my_mesh15);

My_Mesh* my_mesh16 = new My_Mesh;
my_mesh16->generate_cylinder(100, 3);//生成圆柱表面
my_mesh16->set_texture_file("texture/cylinder10.jpg");//指定纹理图像文件
my_mesh16->set_translate(7.5, 3, 7.5);
my_mesh16->set_theta(90, 0, 0);//旋转轴
my_mesh16->set_scale(3, 3, 10);//旋转轴
my_mesh16->set_theta_step(0, 0, 0);//旋转速度
my_meshs.push_back(my_mesh16);

My_Mesh* my_mesh17 = new My_Mesh;
my_mesh17->set_texture_file("texture/castle4.png");
my_mesh17->set_translate(5, 0,5);
my_mesh17->set_theta(0, 0, 0);
my_mesh17->set_scale(20, 20, 20);
my_mesh17->set_theta_step(0, 0, 0);
my_meshs.push_back(my_mesh17);

My_Mesh* my_mesh18 = new My_Mesh;
my_mesh18->set_texture_file("texture/grass.png");
my_mesh18->set_translate(3, -0.1, 3);
my_mesh18->set_theta(0, 0, 0);
my_mesh18->set_scale(80, 1, 80);
my_mesh18->set_theta_step(0, 0, 0);
my_meshs.push_back(my_mesh18);

My_Mesh* my_mesh19 = new My_Mesh;
my_mesh19->set_texture_file("texture/soldier.png");
my_mesh19->set_translate(2.0, 0, 5);
my_mesh19->set_theta(0, -90, 0);
my_mesh19->set_scale(8, 8, 8);
my_mesh19->set_theta_step(0, 0, 0);
my_meshs.push_back(my_mesh19);

My_Mesh* my_mesh20 = new My_Mesh;
my_mesh20->set_texture_file("texture/princess.png");
my_mesh20->set_translate(1, 0, 7.5);
my_mesh20->set_theta(0, -180, 0);
my_mesh20->set_scale(8, 8, 8);
my_mesh20->set_theta_step(0, 0, 0);
my_meshs.push_back(my_mesh20);

My_Mesh* my_mesh21 = new My_Mesh;
my_mesh21->set_texture_file("texture/pillar.png");
my_mesh21->set_translate(-1.5, 0, 8);
my_mesh21->set_theta(0, 180, 0);
my_mesh21->set_scale(7.5, 15, 7.5);
my_mesh21->set_theta_step(0, 0, 0);
my_meshs.push_back(my_mesh21);

My_Mesh* my_mesh22 = new My_Mesh;
my_mesh22->set_texture_file("texture/wind_mill.png");
my_mesh22->set_translate(-1.5, 1.7, 8.2);
my_mesh22->set_theta(0, 180, 0);
my_mesh22->set_scale(15, 15, 15);
my_mesh22->set_theta_step(0, 0, 0.5);
my_meshs.push_back(my_mesh22);

My_Mesh* my_mesh23 = new My_Mesh;
my_mesh23->set_texture_file("texture/sun.png");
my_mesh23->set_translate(0, 15, 15);
my_mesh23->set_theta(90, 0, 0);
my_mesh23->set_scale(15, 15, 15);
my_mesh23->set_theta_step(0, 0, 0);
my_meshs.push_back(my_mesh23);

My_Mesh* my_mesh24 = new My_Mesh;
my_mesh24->set_texture_file("texture/cloud.png");
my_mesh24->set_translate(8, 12, 10);
my_mesh24->set_theta(90, 0, -30);
my_mesh24->set_scale(15, 15, 15);
my_mesh24->set_theta_step(0, 0, 0);
my_meshs.push_back(my_mesh24);

mp_->update_vertex_buffer();//绑定vbo
mp_->update_texture();//设定纹理

glutDisplayFunc( display );
glutKeyboardFunc( keyboard );
glutPassiveMotionFunc( mouse );
glutIdleFunc( idle );

glutMainLoop();

for (unsigned int i = 0; i < my_meshs.size(); i++)
{
delete my_meshs[i];
}
delete mp_;

return 0;
}



v_texture.glsl

#version 330 core

in  vec3 vPosition;
in  vec3 vColor;
in  vec3 vNormal;
in  vec2 vTexCoord;
in  vec3 vFaceIndecies;

out vec4 color;
out vec2 texCoord;
out vec4 normal;

uniform vec3 theta;
uniform vec3 translation;

uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projMatrix;

uniform vec3 lightPos;
out vec3 N;
out vec3 V;
out vec3 lightPos_new;

void main()
{

color       = vec4(vColor, 0);
texCoord    = vTexCoord;
normal = vec4(vNormal, 0);

vec4 v1 = projMatrix * viewMatrix*modelMatrix * vec4(vPosition, 1.0);
vec4 v2 = vec4(v1.xyz / v1.w, 1.0);
else gl_Position=v1;//如果绘制的不是阴影则不进行透视投影，防止出现大型物体显示的bug

//光照

// TODO 将顶点变换到相机坐标系下
vec4 vertPos_cameraspace = viewMatrix*modelMatrix * vec4(vPosition, 1.0);

// 对顶点坐标做透视投影
V = vertPos_cameraspace.xyz / vertPos_cameraspace.w;

// TODO 将光源位置变换到相机坐标系下
vec4 lightPos_cameraspace = viewMatrix *mat4(1.0)* vec4(lightPos, 1.0);

// 对光源坐标做透视投影
lightPos_new = lightPos_cameraspace.xyz / lightPos_cameraspace.w;

// TODO 将法向量变换到相机坐标系下并传入片元着色器
N = (viewMatrix*modelMatrix * vec4(vNormal, 0.0)).xyz;

}



f_texture.glsl

#version 330 core

in vec4 color;
in vec2 texCoord;
in vec4 normal;
in vec3 faceIndecies;

out vec4 fColor;
out vec4 fNormal;

uniform sampler2D texture;//纹理

in vec3 N;
in vec3 V;
in vec3 lightPos_new;

void main()
{
fColor = texture2D( texture, texCoord );

//以下用来实现光照
fNormal = normal;

vec3 ambiColor = vec3(0.1, 0.1, 0.1);
vec3 diffColor = vec3(0.5, 0.5, 0.5);
vec3 specColor = vec3(0.3, 0.3, 0.3);

// TODO 计算N，L，V，R四个向量并归一化
vec3 N_norm = normalize(N);
vec3 L_norm = normalize(lightPos_new - V);
vec3 V_norm = normalize(-V);
vec3 R_norm = reflect(-L_norm, N_norm);

// TODO 计算漫反射系数和镜面反射系数
float lambertian = clamp(dot(L_norm, N_norm), 0.0, 1.0);
//float specular = clamp(dot(R_norm, V_norm), 0.0, 1.0);

//使用BLINN_PHONG模型
vec3 H_norm = normalize(L_norm + V_norm);
float specular = clamp(dot(N_norm, H_norm), 0.0, 1.0);

float shininess = 10.0;
float d = length(lightPos_new - V);
float a=1,b=0,c=0;
// TODO 计算最终每个片元的输出颜色
fColor*=0.5;
fColor += (vec4(ambiColor + diffColor * lambertian/(a+b*d+c*d*d) +specColor * pow(specular, 5.0), 1.0)/(a+b*d+c*d*d))*1.0;
fColor=vec4(0.0 , 0.0 , 0.0 , 1.0);
}
else{
fColor=fColor;
}
}

• 17
点赞
• 181
收藏
觉得还不错? 一键收藏
• 15
评论
06-26
08-22 4634
12-14 4848
09-19 1804
10-17 1567
01-14
12-26
01-15
02-05
11-30 2618

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。