OpenGL正方體紋理貼圖

貼圖出不來,請大佬糾錯。

image解碼:https://github.com/nothings/stb/blob/master/stb_image.h

#include <GL/glut.h>
#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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章