这里使用FreeImage加载图片,生成纹理,并且把这个纹理映射到正方体四个面。
免费的、开源的、跨平台,图像加载处理库,支持20多种图像格式。
官网:http://freeimage.sourceforge.net/
/*
* 使用FreeImage 加载图片并映射到立方体
*/
#define GLEW_STATIC
#include
#include
#include
#include
#include
#include "FreeImage.h"
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) //这行是取消显示控制台
char szTitle[64] = "opengl view";
// Window dimensions
const GLuint WIDTH = 800, HEIGHT = 600;
static void error_callback(int error, const char* description) {
fputs(description, stderr);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
//顶点数据
struct Vertex {
float x, y, z;
float u, v;
};
//纹理ID
GLuint _texture;
static unsigned createTexture(int w, int h, const void* data) {
unsigned texId;
glGenTextures(1, &texId);
glBindTexture(GL_TEXTURE_2D, texId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
return texId;
}
/**
* 使用FreeImage加载图片
*/
static unsigned createTextureFromImage(const char* fileName) {
//1 获取图片格式
FREE_IMAGE_FORMAT fifmt = FreeImage_GetFileType(fileName, 0);
if(fifmt == FIF_UNKNOWN) {
return 0;
}
//2 加载图片
FIBITMAP *dib = FreeImage_Load(fifmt, fileName, 0);
//获取加载图片格式
FREE_IMAGE_COLOR_TYPE type = FreeImage_GetColorType(dib);
// 获取数据指针
FIBITMAP* temp = dib;
dib = FreeImage_ConvertTo32Bits(dib);
FreeImage_Unload(temp);
BYTE* pixels = (BYTE*)FreeImage_GetBits(dib);
int width = FreeImage_GetWidth(dib);
int height = FreeImage_GetHeight(dib);
//默认加载出来的图片数据格式是bgra 这里要转换成rgba
for(int i = 0; i < width * height * 4; i+=4) {
BYTE temp = pixels[i];
pixels[i] = pixels[i + 2];
pixels[i + 2] = temp;
}
unsigned res = createTexture(width, height, pixels);
FreeImage_Unload(dib);
return res;
}
static void onInit() {
_texture = createTextureFromImage("../res/1.jpg");
}
static void onDestory() {
glDeleteTextures(1, &_texture); //销毁纹理 显存中释放纹理内存
}
// 绘制
static void render(GLFWwindow * window) {
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//指定数据 顶点坐标 对应 纹理坐标
Vertex cubeVertices[] =
{
{ -1.0f, -1.0f, 1.0f, 0.1f, 0.1f },
{ 1.0f, -1.0f, 1.0f, 0.9f, 0.1f },
{ 1.0f, 1.0f, 1.0f, 0.9f, 0.9f },
{ -1.0f, 1.0f, 1.0f, 0.1f, 0.9f },
{ -1.0f, -1.0f, -1.0f, 0, 0 },
{ -1.0f, 1.0f, -1.0f, 1, 0 },
{ 1.0f, 1.0f, -1.0f, 1, 1 },
{ 1.0f, -1.0f, -1.0f, 0, 1 },
{ -1.0f, 1.0f,-1.0f ,0, 0 },
{ -1.0f, 1.0f, 1.0f ,1, 0 },
{ 1.0f, 1.0f, 1.0f ,1, 1 },
{ 1.0f, 1.0f, -1.0f, 0, 1 },
{ -1.0f, -1.0f, -1.0f, 0, 0 },
{ 1.0f, -1.0f, -1.0f, 1, 0 },
{ 1.0f, -1.0f, 1.0f, 1, 1 },
{ -1.0f, -1.0f, 1.0f, 0, 1 },
{ 1.0f, -1.0f, -1.0f, 0, 0 },
{ 1.0f, 1.0f, -1.0f, 1, 0 },
{ 1.0f, 1.0f, 1.0f, 1, 1 },
{ 1.0f, -1.0f, 1.0f, 0, 1 },
{ -1.0f, -1.0f, -1.0f, 0, 0 },
{ -1.0f, -1.0f, 1.0f, 1, 0 },
{ -1.0f, 1.0f, 1.0f, 1, 1 },
{ -1.0f, 1.0f, -1.0f, 0, 1 },
};
glMatrixMode(GL_MODELVIEW);
glBindTexture(GL_TEXTURE_2D, _texture);
#if 0
glEnable(GL_DEPTH_TEST);
glInterleavedArrays(GL_T2F_V3F, sizeof(Vertex), cubeVertices);
#else
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &cubeVertices[0].x);
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &cubeVertices[0].u);
#endif
// 清成单位矩阵
glLoadIdentity();
// 产生一个矩阵
glTranslatef(0, 0, -10);
static float angle = 0;
glRotatef(angle,1,1,1);
angle += 1;
glDrawArrays(GL_QUADS, 0, 24);
glfwSwapBuffers(window);
glfwPollEvents();
}
int main(void) {
GLFWwindow * window;
glfwSetErrorCallback(error_callback);
if(!glfwInit()) return -1;
window = glfwCreateWindow(WIDTH, HEIGHT, szTitle, NULL, NULL);
if(!window) {
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, key_callback);
glewExperimental = GL_TRUE;
glewInit();
onInit();
glViewport(0, 0, WIDTH, HEIGHT); //设置opengl视口 即看到的显示区域
glMatrixMode(GL_PROJECTION); //设置当前操作的是投影矩阵队列
glLoadIdentity(); //将队列最上层的一个投影矩阵清空成单位矩阵
//gluPerspective (GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar);
//fovy 角度 视角
//aspect 宽高比
//zNear 近裁剪面
//zFar 远裁剪面
gluPerspective(60, double(WIDTH) / double(HEIGHT), 0.1, 1000);//产生一个新的投影矩阵并和上面的投影矩阵列表最上面单位矩阵相乘,实际上就是产生一个投影矩阵并放到投影矩阵列表的最上面
while(!glfwWindowShouldClose(window)) {
render(window);
}
onDestory();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}