声明:因网易博客将关闭,移到此
/**
这里使用到的是opengl 和 glut 进行编写学习的笔记,一个简单的出口
使用到的开发库有 linux (gl glu glut) windows(opengl32.lib glu32.lib glut32.lib)
实现: 纹理映射
注意这个是直接从Nehe下载的代码里面进行了一些修改使用的,这里读取的是
一张bmp图片,图片的宽高要是2的倍数,图片的24位的
注意:此文件可以在linux和windows上进行编译。
提供ubuntu上的makefile
1.在opengl中贴图坐标是怎么样的?
2.如何给一个三角形贴上图呢?
3.或者怎么让贴图的坐标可以移动呢?
这些可以在网上找到,要是实在不知道,你在找我 :)
*/
#include <iostream>
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef _MSC_VER
#endif
#ifdef __GNUC__
#include <unistd.h>
#endif
using namespace std;
// 我们将使用的到的纹理
GLuint texture[1];
// 读取bmp图片部分
// 图片的类型 包含宽 高 数据
struct Image
{
unsigned long sizeX;
unsigned long sizeY;
char *data;
};
typedef struct Image Image;
int ImageLoad(char *filename, Image *image)
{
FILE *file;
unsigned long size; // 图片长度
unsigned long i; // 计数
unsigned short int planes;
unsigned short int bpp;
char temp; // bgr -rgb 变换
if ((file = fopen(filename, "rb")) == NULL)
{
printf("File Not Found: %s\n", filename);
return 0;
}
// 跳过文件的头部,准备读取宽度和高度
// 如果想自己具体的情况可以去看看bmp图片的相关信息
fseek(file, 18, SEEK_CUR);
// 读取宽度
if ((i = fread(&image->sizeX, 4, 1, file)) != 1)
{
printf("Error reading width from %s. \n", filename);
return 0;
}
printf("Width of %s: %lu\n", filename, image->sizeX);
// 读取宽度
if ((i = fread(&image->sizeY, 4, 1, file)) != 1)
{
printf("Error reading height from %s. \n", filename);
return 0;
}
printf("Height of %s: %lu\n", filename, image->sizeY);
// 计算长度(24bits或3bytes每个像素)
size = image->sizeX * image->sizeY * 3;
// 读取
if ((fread(&planes, 2, 1, file)) != 1)
{
printf("Error reading planes from %s. \n", filename);
return 0;
}
if (planes != 1)
{
printf("Planes from %s is not 1: %u\n", filename, planes);
return 0;
}
if ((i = fread(&bpp, 2, 1, file)) != 1)
{
printf("Error reading bpp from %s. \n", filename);
return 0;
}
if (bpp != 24)
{
printf("Bpp from %s is not 24: %u\n", filename, bpp);
return 0;
}
// 跳过余下的头文件数据
fseek(file, 24, SEEK_CUR);
// 读取数据
image->data = (char*) malloc (size);
if (image->data == NULL)
{
printf("Error allocating memory for color-corrected image data");
return 0;
}
if ((i = fread(image->data, size, 1, file)) != 1)
{
printf("Error reading image data from %s. \n", filename);
return 0;
}
// 交换颜色 bgr -> rgb
for (i = 0; i < size; i += 3)
{
temp = image->data[i];
image->data[i] = image->data[i+2];
image->data[i+2] = temp;
}
// 完成
return 1;
}
// 读取 bitmaps 并转化成纹理
void LoadGLTextures()
{
Image