資源簡介
opengl實現紋理貼圖,以地球為例子,但是實現得有點粗糙,地球上出現了一條裂縫
代碼片段和文件信息
#?include?
#?include?
#?include?
#?include?
#?include?
#define?Pi?3.141592653?
#define?X?.525731112119133606
#define?Z?.850650808352039932
//二十面體的十二個頂點坐標
static?GLfloat?vdata[12][3]?=?{???????????
{-X?0.0?Z}?{X?0.0?Z}?{-X?0.0?-Z}?{X?0.0?-Z}
{0.0?Z?X}?{0.0?Z?-X}?{0.0?-Z?X}?{0.0?-Z?-X}
{Z?X?0.0}?{-Z?X?0.0}?{Z?-X?0.0}?{-Z?-X?0.0}
};
//二十個三角形面的頂點索引
static?GLuint?tindices[20][3]?=?{
{1?4?0}?{4?9?0}?{4?5?9}?{8?5?4}?{1?8?4}
{1?10?8}?{10?3?8}?{8?3?5}?{3?2?5}?{3?7?2}
{3?10?7}?{10?6?7}?{6?11?7}?{6?0?11}?{6?1?0}
{10?1?6}?{11?0?9}?{2?11?9}?{5?2?9}?{11?2?7}
};
GLint?ImageWidth?ImageHeight?PixelLength;
GLubyte?*pixeldata;??//存儲圖像信息
char?name[20]?=?“earth.bmp“;
GLuint?texname;
GLint?px?py;
GLfloat?rx?ry;
bool?rotate;
GLfloat?rot[3]?depth;
GLsizei?width?height;
//記錄旋轉矩陣
GLdouble?current_rotate_matrix[16]?=?{
1.0?0.0?0.0?0.0
0.0?1.0?0.0?0.0
0.0?0.0?1.0?0.0
0.0?0.0?0.0?1.0
};
void?loadtexture(char?*filename)???//讀入圖像文件
{
FILE?*pFile;
pFile?=?fopen(filename?“r“);
if(pFile?==?0)
{
printf(“picture?open?error\n“);
exit(0);
}
fseek(pFile?0x0012?SEEK_SET);???//從第18個字節開始的8個字節存儲圖像的寬和高!!!!
????fread(&ImageWidth?sizeof(ImageWidth)?1?pFile);
????fread(&ImageHeight?sizeof(ImageHeight)?1?pFile);
?????//?計算像素數據長度
?????PixelLength?=?ImageWidth?*?3;???//一個像素有R、G、B成分,所以乘以3
?????while(?PixelLength?%?4?!=?0?)??//四字節對齊
?????????++PixelLength;
?????PixelLength?*=?ImageHeight;
?????//?讀取像素數據
?????pixeldata?=?(GLubyte*)malloc(PixelLength);
?????if(?pixeldata?==?0?)
?????????exit(0);
?????fseek(pFile?54?SEEK_SET);??//從第54個字節往后是圖像的真正內容
?????fread(pixeldata?PixelLength?1?pFile);
?????//?關閉文件
?????fclose(pFile);
}
void?init()
{
glClearColor(0.0?0.0?0.0?0.0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
loadtexture(name);??//載入紋理圖像
glPixelStorei(GL_UNPACK_ALIGNMENT?4);
glGenTextures(1?&texname);
glBindTexture(GL_TEXTURE_2D?texname);
glTexParameteri(GL_TEXTURE_2D?GL_TEXTURE_WRAP_S?GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D?GL_TEXTURE_WRAP_T?GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D?GL_TEXTURE_MAG_FILTER?GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D?GL_TEXTURE_MIN_FILTER?GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D?0?3?ImageWidth?ImageHeight
0?GL_BGR_EXT?GL_UNSIGNED_BYTE?pixeldata);
}
void?reshape(int?w?int?h)
{
width?=?w;
height?=?h;
glViewport(0?0?(GLsizei)w?(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();?
gluPerspective(60.0?(GLfloat)w/(GLfloat)h?0.1?100.0);?
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void?normalize(float?v[])???//把一個向量規范化
{
GLfloat?d?=?sqrt(v[0]*v[0]?+?v[1]*v[1]?+?v[2]*v[2]);
if(d?==?0.0)
{
printf(“zero?length?vector\n“);
return;
}
v[0]?/=?d;
v[1]?/=?d;
v[2]?/=?d;
}
void
- 上一篇:簡單通訊錄
- 下一篇:VC6.0完全卸載工具.exe
評論
共有 條評論