2 qq 34569022 qq_34569022 于 2016.05.01 07:56 提问

opengl纹理问题,纹理数组如何使用

作业是六面体做六个不同的纹理,想着用纹理数组做,看了网上的uv,有的是图片最左上是(0,0)有的是最左下
然后自己用纹理数组做六面体的时候只能上下两个面有图案,中间的都是过度的,如果是用画一个面贴一个图形是没问题的,用数组就是不行,以下是源码
#define GLUT_DISABLE_ATEXIT_HACK
#include
#include
#include
#include

#define BMP_Header_Length 54
float angle = 0;
GLuint texture_ID[6];
GLubyte* loadtexture(const char* file_name, GLint* width, GLint* height);
static GLdouble vertices[] =
{
-3,-3,-3, //0
3,-3,-3, //1
3,-3,3, //2
-3,-3,3, //立方体底面
-3,3,-3, //4
3,3,-3, //5
3,3,3, //6
-3,3,3, //顶部
};

static const GLint index[] =
{
3,2,6,7, //前面
5,6,2,1, //右侧面
4,5,1,0, //后面
7,4,0,3, //左侧面
5,4,7,6, //顶面
0,1,2,3, //底面
};

static GLfloat colors[] =
{
1,1,0,
0,1,1,
0,1,0,
1,0,1,
1,1,1,
1,0.3,1,
}; //颜色数组

static GLfloat texCoords[]
{
0,0, 1,0, 1,1, 0,1,
0,0, 1,0, 1,1, 0,1,
0,0, 1,0, 1,1, 0,1,
0,0, 1,0, 1,1, 0,1,
0,0, 1,0, 1,1, 0,1,
0,0, 1,0, 1,1, 0,1,
};

void init()
{
GLubyte* imagedata[6];
GLint width[6];
GLint height[6];
char a[6];
for (int i = 0;i < 6;i++)
{
_itoa(i+1, a, 10);
imagedata[i] = loadtexture(strcat(a,".BMP"), &width[i], &height[i]);
}

// 分配一个新的纹理编号 
glGenTextures(6, texture_ID);

for (int i = 0;i < 6;i++)
{
    glBindTexture(GL_TEXTURE_2D, texture_ID[i]);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width[i], height[i], 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, imagedata[i]);
    free(imagedata[i]);
}

}

void myDisplay(void)
{
glShadeModel(GL_SMOOTH);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清除缓存
glEnable(GL_DEPTH_TEST);

glMatrixMode(GL_PROJECTION);//投影矩阵
glLoadIdentity();     //初始化矩阵为单位矩阵,防止读取之前的矩阵。
gluPerspective(90, 540 / 480, 1, 20);

glMatrixMode(GL_MODELVIEW);        //模型视图矩阵
glLoadIdentity();
gluLookAt(0, 10, 10, 0, 0, 0, 0, 1, 0);     //视点矩阵 前三位分别是相机在世界的坐标,现在是z轴上的6个单位,假设物体离我们100个单位远或小于1个单位则观察不到。
                                             //中间三位是物体原始坐标
                                             //后面三位是相机以那边为正方向。假设这里为-1(与大小无关。正负).物体会以x轴倒过来
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture_ID[0]);
 //glRotatef(angle, 1, 1.0f, 1);        //围绕转
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
 glEnableClientState(GL_VERTEX_ARRAY);   //启用点数组
glVertexPointer(3, GL_DOUBLE, 0, vertices);
//glEnableClientState(GL_COLOR_ARRAY);  //启用颜色数组
 //glColorPointer(3, GL_FLOAT, 0, colors);
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_INT, index);




//glBindTexture(GL_TEXTURE_2D, texture_ID[0]);
//glPushMatrix();  //矩形进栈
//glBegin(GL_QUADS);
//glTexCoord2f(1.0f, 1.0f); glVertex3f(3.0f, 3.0f, -3.0f);
//glTexCoord2f(0.0f, 1.0f); glVertex3f(-3.0f, 3.0f, -3.0f);
//glTexCoord2f(0.0f, 0.0f); glVertex3f(-3.0f, 3.0f, 3.0f);
//glTexCoord2f(1.0f, 0.0f); glVertex3f(3.0f, 3.0f, 3.0f);
//glEnd();

//glBindTexture(GL_TEXTURE_2D, texture_ID[1]);
//glBegin(GL_QUADS);
//glTexCoord2f(1.0f, 1.0f); glVertex3f(3.0f, 3.0f, 3.0f);
//glTexCoord2f(0.0f, 1.0f); glVertex3f(-3.0f, 3.0f, 3.0f);
//glTexCoord2f(0.0f, 0.0f); glVertex3f(-3.0f, -3.0f, 3.0f);
//glTexCoord2f(1.0f, 0.0f); glVertex3f(3.0f, -3.0f, 3.0f);
//glEnd();

//glBindTexture(GL_TEXTURE_2D, texture_ID[2]);
//glBegin(GL_QUADS);
//glTexCoord2f(1.0f, 1.0f); glVertex3f(-3.0f, 3.0f, 3.0f);
//glTexCoord2f(0.0f, 1.0f); glVertex3f(-3.0f, 3.0f, -3.0f);
//glTexCoord2f(0.0f, 0.0f); glVertex3f(-3.0f, -3.0f, -3.0f);
//glTexCoord2f(1.0f, 0.0f); glVertex3f(-3.0f, -3.0f, 3.0f);
//glEnd();

//glBindTexture(GL_TEXTURE_2D, texture_ID[3]);
//glBegin(GL_QUADS);
//glTexCoord2f(1.0f, 1.0f); glVertex3f(3.0f, 3.0f, -3.0f);
//glTexCoord2f(0.0f, 1.0f); glVertex3f(3.0f, 3.0f, 3.0f);
//glTexCoord2f(0.0f, 0.0f); glVertex3f(3.0f, -3.0f, 3.0f);
//glTexCoord2f(1.0f, 0.0f); glVertex3f(3.0f, -3.0f, -3.0f);
//glEnd();

//glBindTexture(GL_TEXTURE_2D, texture_ID[4]);
//glBegin(GL_QUADS);
//glTexCoord2f(1.0f, 1.0f); glVertex3f(-3.0f, 3.0f, -3.0f);
//glTexCoord2f(0.0f, 1.0f); glVertex3f(3.0f, 3.0f, -3.0f);
//glTexCoord2f(0.0f, 0.0f); glVertex3f(3.0f, -3.0f,-3.0f);
//glTexCoord2f(1.0f, 0.0f); glVertex3f(-3.0f, -3.0f,-3.0f);
//glEnd();

//glBindTexture(GL_TEXTURE_2D, texture_ID[5]);
//glBegin(GL_QUADS);
//glTexCoord2f(1.0f, 1.0f); glVertex3f(3.0f, -3.0f, 3.0f);
//glTexCoord2f(0.0f, 1.0f); glVertex3f(-3.0f, -3.0f, 3.0f);
//glTexCoord2f(0.0f, 0.0f); glVertex3f(-3.0f, -3.0f, -3.0f);
//glTexCoord2f(1.0f, 0.0f); glVertex3f(3.0f, -3.0f, -3.0f);
//glEnd();
//glPopMatrix();

glutSwapBuffers();

}

void Update()
{
angle += 0.005f;
if (angle > 360.0f)
angle = 0.0f;
myDisplay();
}

int main(int argc, char *argv[])
{
glutInit(&argc, argv); //初始化
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE|GLUT_DEPTH); //rgb格式,单缓存
glutInitWindowPosition(100, 100); //窗口初始坐标
glutInitWindowSize(540, 480); //窗口长宽
glutCreateWindow("openGL纹理");
init();
glutDisplayFunc(&myDisplay);
glutIdleFunc(&Update);
glutMainLoop();
return 0;
}

GLubyte* loadtexture(const char* file_name, GLint* width, GLint* height)
{
GLint total_bytes;
GLubyte* pixels = 0;

// 打开文件,如果失败,返回 
FILE* pFile = fopen(file_name, "rb");
if (pFile == 0)
    return 0;

// 读取文件中图象的宽度和高度 
fseek(pFile, 0x0012, SEEK_SET);
fread(width, 4, 1, pFile);
fread(height, 4, 1, pFile);
fseek(pFile, BMP_Header_Length, SEEK_SET);

// 计算每行像素所占字节数,并根据此数据计算总像素字节数 
{
    GLint line_bytes = *width * 3;
    while (line_bytes % 4 != 0)
        ++line_bytes;
    total_bytes = line_bytes * *height;
}

// 根据总像素字节数分配内存 
pixels = (GLubyte*)malloc(total_bytes);
if (pixels == 0)
{
    fclose(pFile);
    return 0;
}

// 读取像素数据 
if (fread(pixels, total_bytes, 1, pFile) <= 0)
{
    free(pixels);
    fclose(pFile);
    return 0;
}
return pixels;
free(pixels);

}

1个回答

CSDNXIAOD
CSDNXIAOD   2016.05.01 08:01

opengl纹理的使用
OpenGL纹理使用
Qt OpenGL 使用纹理
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!