贴图出不来,请大佬纠错。
image解码:https://github.com/nothings/stb/blob/master/stb_image.h
#include
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
int WinWidth = 800, WinHeight = 800;
// 旋转角度,初始可以随便设置
float rotateX = 20.0f;
float rotateY = 30.0f;
GLuint texture;
GLuint textures[6];
int LoadGLTextures() // 根据加载的位图创建纹理
{
const char *pictures[] = {// 创建一个位图名称数组,对应6幅位图
"side1.png",
"side2.png",
"side3.png",
"side4.png",
"side5.png",
"side6.png" };
int w, h, ch;
/*
unsigned char *data = stbi_load("side1.png", &w, &h, &ch, 0);
printf("%s:%d: %d X %d ,%d\n", __FILE__, __LINE__, w, h, ch);
glGenTextures(1, &texture); // 创建纹理
glBindTexture(GL_TEXTURE_2D, texture);// 将生成的纹理的名称绑定到指定的纹理上
if(ch==3){
//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, w, h, GL_RGB, GL_UNSIGNED_BYTE, data);
}else if(ch==4){
//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data);
}
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
*/
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); // 设置纹理和物体表面颜色处理方式
for(int i=0; i<6; i++)// 遍历位图名称数组,根据位图名称分别生成
{
unsigned char *data = stbi_load(pictures[i], &w, &h, &ch, 0);
printf("%s:%d: %s %d X %d ,%d\n", __FILE__, __LINE__, pictures[i], w, h, ch);
glGenTextures(1, &textures[i]); // 为第i个位图创建纹理
glBindTexture(GL_TEXTURE_2D, textures[i]);// 将生成的纹理的名称绑定到指定的纹理上
if(ch==3){
//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, w, h, GL_RGB, GL_UNSIGNED_BYTE, data);
}else if(ch==4){
//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data);
}
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);//抗锯齿
//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}
return texture;
}
void Draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// 立方体的8个顶点坐标
GLfloat vertex_list[][3] = {
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
};
GLint index_list[][4] = {
0, 2, 3, 1,
0, 4, 6, 2,
0, 1, 5, 4,
4, 5, 7, 6,
1, 3, 7, 5,
2, 6, 7, 3,
};
#if 0
// 只有正面,并且只显示边线,不进行填充
//glFrontFace(GL_CCW);
//glCullFace(GL_BACK);
//glEnable(GL_CULL_FACE);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
#endif
// 设置旋转
glRotatef(rotateX, 1.0f, 0.0f, 0.0f);
glRotatef(rotateY, 0.0f, 1.0f, 0.0f);
// 定义不同的颜色
GLfloat colors[][3] = { { 0.0, 0.0, 1.0 },
{ 0.0, 1.0, 0.0 },
{ 1.0, 0.0, 0.0 },
{ 1.0, 0.0, 1.0 },
{ 1.0, 1.0, 0.0 },
{ 0.0, 1.0, 1.0 },
{ 1.0, 0.5, 0.5 },
{ 0.0, 0.5, 0.5 } };
int i, j;
glBegin(GL_QUADS); // 绘制四边形
for (i = 0; i < 6; i++) // 有六个面,循环六次
{
//glColor3f(colors[i][0], colors[i][1], colors[i][2]);
glBindTexture(GL_TEXTURE_2D, textures[i]);
//for (j = 0; j < 4; ++j) // 每个面有四个顶点,循环四次
//{
glTexCoord2f(0.0f, 0.0f); glVertex3fv(vertex_list[index_list[i][0]]);
glTexCoord2f(1.0f, 0.0f); glVertex3fv(vertex_list[index_list[i][1]]);
glTexCoord2f(1.0f, 1.0f); glVertex3fv(vertex_list[index_list[i][2]]);
glTexCoord2f(0.0f, 1.0f); glVertex3fv(vertex_list[index_list[i][3]]);
//}
}
glEnd();
glutSwapBuffers();
}
void Reshape(int w, int h)
{
WinWidth = w;
WinHeight = h;
//改变显示区域,起始位置为客户端窗口左下角(非坐标原点)
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//宽高比改为当前值,视线区域与屏幕大小一致;
gluPerspective(45, 1.0*WinWidth / WinHeight, 1, 1000);
//开启更新深度缓冲区的功能
glEnable(GL_DEPTH_TEST);
//摄像机视图观看,从 (0,5,20) 往(0,0,0)处看,(0,1,0)为正方向
gluLookAt(0, 5, 20, 0, 0, 0, 0, 1, 0);
}
void SpecialKeys(int key, int x, int y)
{
if (key == GLUT_KEY_UP) rotateX += 5.0f;
if (key == GLUT_KEY_DOWN) rotateX -= 5.0f;
if (key == GLUT_KEY_LEFT) rotateY += 5.0f;
if (key == GLUT_KEY_RIGHT) rotateY -= 5.0f;
if (rotateX > 356.0f) rotateX = 0.0f;
if (rotateX < -1.0f) rotateX = 355.0f;
if (rotateY > 356.0f) rotateY = 0.0f;
if (rotateY < -1.0f) rotateY = 355.0f;
//刷新窗口
glutPostRedisplay();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowPosition(200, 100);
glutInitWindowSize(WinWidth, WinHeight);
glutCreateWindow("OpenGL");
const char* version = (const char*)glGetString(GL_VERSION);
//const char* extensions = (const char*)glGetString(GL_EXTENSIONS);
printf("%s:%d: OpenGL Version:%s\n", __FILE__, __LINE__, version);
glEnable(GL_TEXTURE_2D); // 启用纹理
LoadGLTextures();
/*
目的:当窗口尺寸改变时,图形比例不发生变化
思路:窗口宽高比改变时,通过改变窗口显示区域大小,并利用投影矩阵改变观测物体大小使之适应变化。
*/
glutReshapeFunc(&Reshape);
glutDisplayFunc(&Draw);
//glutKeyboardFunc(processNormalKeys);
glutSpecialFunc(SpecialKeys);
glutMainLoop();
return 0;
}