多纹理,贴到一个面

由于一张纹理的大小不能超过30M,把文理分开贴在模型上面,我应该怎么来设置纹理的坐标,可以采用以下函数能实现! 

实现方法
1 把图片分割为多个,同时把面也分割多个。
2 使用纹理融合

对第2种举例(测试通过)

 //texture1
 SoGroup *tg1=new SoGroup;
 SoTextureUnit *texUnit1 = new SoTextureUnit ;
 texUnit1->unit.setValue(0);
 texUnit1->mappingMethod = SoTextureUnit::IMAGE_MAPPING ;

 SoTexture2Transform *texturetrans1 = new SoTexture2Transform;
 texturetrans1->scaleFactor.setValue(2.0,1.0);
 texturetrans1->center.setValue(0.0,0.0);
 SoTexture2  *texture1 = new SoTexture2;
 texture1->filename="./1.bmp";
 texture1->wrapS =SoTexture2::CLAMP_TO_BORDER  ;
 texture1->wrapT =SoTexture2::CLAMP_TO_BORDER  ;
 texture1->enableBorder.setValue(FALSE);
 texture1->borderColor.setValue(1.0f,1.0f,1.0f,0.0f);

 tg1->addChild(texUnit1);
 tg1->addChild(texturetrans1);
 tg1->addChild(texture1);
 root->addChild(tg1);

 //texture2
 SoGroup *tg2=new SoGroup;
 SoTextureUnit *texUnit2 = new SoTextureUnit ;
 texUnit2->unit.setValue(1);
 texUnit2->mappingMethod = SoTextureUnit::IMAGE_MAPPING ;

 SoTexture2Transform *texturetrans2 = new SoTexture2Transform;
 texturetrans2->scaleFactor.setValue(2.0,1.0);
 texturetrans2->center.setValue(1.0,0.0);
 SoTexture2  *texture2 = new SoTexture2;
 texture2->filename="./2.bmp";
 texture2->wrapS =SoTexture2::CLAMP_TO_BORDER ;
 texture2->wrapT =SoTexture2::CLAMP_TO_BORDER ;
 texture2->enableBorder.setValue(FALSE);
 texture2->borderColor.setValue(1.0f,1.0f,1.0f,1.0f);

 tg2->addChild(texUnit2);
 tg2->addChild(texturetrans2);
 tg2->addChild(texture2);
 root->addChild(tg2);

 SoCoordinate3 *coord=new SoCoordinate3;
 coord->point.set1Value(0,0,0,0);
 coord->point.set1Value(1,0,1,0);
 coord->point.set1Value(2,1,1,0);
 coord->point.set1Value(3,1,0,0);

 coord->point.set1Value(4,2,1,0);
 coord->point.set1Value(5,2,0,0);
 coord->point.set1Value(6,1,0,0);
 coord->point.set1Value(7,1,1,0);
 root->addChild(coord);


 SoFaceSet *face=new SoFaceSet;
 face->numVertices.set1Value(0,4);
 face->numVertices.set1Value(1,4);
 root->addChild(face);

 

http://tanghg678.blog.163.com/blog/static/1292320520070128244225/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用OpenGL绘制一个球体并纹理的基本步骤: 1. 定义球体顶点坐标 在球体的表面上定义一系列的顶点坐标,可以使用球面坐标系来实现。常见的球面坐标系包括球极坐标系和球面直角坐标系。在这里,我们使用球极坐标系来定义球体表面上的顶点坐标。 2. 定义纹理坐标 纹理坐标用于将纹理映射到球体表面上。通常情况下,我们将一个矩形纹理映射到球体表面上,因此需要在矩形的左下角定义纹理坐标原点,右上角定义纹理坐标最大值。在球体表面上的每个顶点上,我们需要定义一个对应的纹理坐标。 3. 加载纹理 加载纹理,将纹理绑定到OpenGL纹理对象上,并设置纹理参数。 4. 绘制球体 使用顶点坐标和纹理坐标绘制球体,并将纹理映射到球体表面上。 下面是一份参考代码: ```c++ #include <GL/glut.h> #include <cmath> #define PI 3.14159265358979323846 // 定义球体顶点坐标 GLfloat vertices[4][3] = { {0.0, 1.0, 0.0}, {-1.0, -1.0, 0.0}, {1.0, -1.0, 0.0}, {0.0, 0.0, 1.0} }; // 定义球体三角形面 GLint faces[4][3] = { {0, 1, 2}, {0, 2, 3}, {0, 3, 1}, {1, 3, 2} }; // 定义纹理坐标 GLfloat texcoords[4][2] = { {0.0, 1.0}, {0.0, 0.0}, {1.0, 0.0}, {1.0, 1.0} }; // 加载纹理 void loadTexture() { GLuint texture; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); 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_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); int width, height, nrChannels; unsigned char *data = stbi_load("texture.jpg", &width, &height, &nrChannels, 0); if (data) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); } else { std::cout << "Failed to load texture." << std::endl; } stbi_image_free(data); } // 绘制球体 void drawSphere() { glBegin(GL_TRIANGLES); for (int i = 0; i < 4; i++) { glTexCoord2f(texcoords[i][0], texcoords[i][1]); glVertex3f(vertices[i][0], vertices[i][1], vertices[i][2]); } for (int i = 0; i < 4; i++) { glTexCoord2f(texcoords[faces[i][0]][0], texcoords[faces[i][0]][1]); glVertex3f(vertices[faces[i][0]][0], vertices[faces[i][0]][1], vertices[faces[i][0]][2]); glTexCoord2f(texcoords[faces[i][1]][0], texcoords[faces[i][1]][1]); glVertex3f(vertices[faces[i][1]][0], vertices[faces[i][1]][1], vertices[faces[i][1]][2]); glTexCoord2f(texcoords[faces[i][2]][0], texcoords[faces[i][2]][1]); glVertex3f(vertices[faces[i][2]][0], vertices[faces[i][2]][1], vertices[faces[i][2]][2]); } glEnd(); } // 绘制函数 void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glEnable(GL_TEXTURE_2D); loadTexture(); drawSphere(); glFlush(); glDisable(GL_TEXTURE_2D); glDisable(GL_DEPTH_TEST); } // 初始化函数 void init() { glClearColor(1.0, 1.0, 1.0, 1.0); } // 窗口大小改变函数 void reshape(GLsizei w, GLsizei h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); } // 主函数 int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow("OpenGL Sphere with Texture"); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); return 0; } ``` 在代码中,我们使用了 `stb_image` 库来加载纹理图片。你需要下载并安装该库,然后在代码中引入头文件 `stb_image.h`。 在绘制球体时,我们先将纹理坐标和顶点坐标绑定起来,然后使用 `glBegin` 和 `glEnd` 函数之间的代码来绘制球体表面上的三角形面。其中,`glTexCoord2f` 函数用于指定纹理坐标,`glVertex3f` 函数用于指定顶点坐标。 在初始化函数中,我们使用 `glClearColor` 函数设置背景颜色。 在窗口大小改变函数中,我们使用 `glViewport` 函数设置视口大小,使用 `glMatrixMode` 和 `glLoadIdentity` 函数将矩阵模式设置为投影模式和模型视图模式,分别使用 `gluPerspective` 和 `gluLookAt` 函数设置投影矩阵和模型视图矩阵。 最后,在主函数中,我们使用 `glutInit` 函数初始化 GLUT 库,使用 `glutInitDisplayMode` 函数设置显示模式,使用 `glutInitWindowSize` 和 `glutInitWindowPosition` 函数设置窗口大小和位置,使用 `glutCreateWindow` 函数创建窗口,使用 `glutDisplayFunc` 和 `glutReshapeFunc` 函数分别设置绘制函数和窗口大小改变函数,使用 `glutMainLoop` 函数进入事件循环。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值