简体   繁体   English

在OpenGL中将纹理映射到顶点

[英]Mapping a texture to vertices in OpenGL

I'm trying to render textured meshes with OpenGL. 我正在尝试使用OpenGL渲染纹理化网格。 Currently, my main class holds a state consisting of : 当前,我的主类的状态为:

  • std::vector<vec3d> vertices
  • std::vector<face> mesh
  • std::vector<vec3d> colors

vec3d is an implementation of 3D vectors - nothing particular - and face a class holding 3 integers pointing to the index of a vertice in vertices . vec3d是3D向量的一种实现(没什么特别的),并且face一个类,其中包含3个整数,这些整数指向顶点中的vertices索引。

So far, I rendered my meshes without a texture with the following code (working fine) : 到目前为止,我使用以下代码渲染了没有纹理的网格物体(工作正常):

glShadeModel(params.smooth ? GL_SMOOTH : GL_FLAT);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);

/*This is my attempt to add the texture
 *
 *if (_colors.size() != 0) {
 *  cout << "Hello" << endl;
 *  glClientActiveTexture(GL_TEXTURE0);
 *  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 *  glTexCoordPointer(3,GL_FLOAT,0,&_colors[0].x);
}*/
glNormalPointer(GL_FLOAT,0,&normals[0].x);
glVertexPointer(3,GL_FLOAT,0,&vertices[0].x);

glDrawElements(GL_TRIANGLES,mesh.size()*3,GL_UNSIGNED_INT,&mesh[0].v1);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);

My texture is stored in colors as a list of triples of floats between 0 and 1. However, colors are not applied. 我的纹理以colors形式存储为0到1之间的三重浮点列表。但是,没有应用颜色。 I read many examples of texture mapping and tried to do the same, with no luck. 我阅读了许多纹理映射的示例,并尝试这样做,但是没有运气。 Any idea of what I'm doing wrong ? 有什么想法我做错了吗?

As seen from your comments, you are using the wrong OpenGL feature to achieve what you want. 从您的评论可以看出,您使用了错误的OpenGL功能来实现所需的功能。 Texturing means to stick a 2d image onto a mesh by using eg uv-coordinates. 纹理化是指通过使用例如uv坐标将2d图像粘贴到网格上。

What you are doing is to specify a color on each vertex, so you will need to enable GL_COLOR_ARRAY instead of GL_TEXTURE_COORD_ARRAY and use the respective functions for that. 您要做的是在每个顶点上指定一种颜色,因此您需要启用GL_COLOR_ARRAY而不是GL_TEXTURE_COORD_ARRAY并为此使用相应的功能。

One additional hint: If you are learning OpenGL from scratch you should consider using only modern OpenGL (3.2+) 另一提示:如果要从头开始学习OpenGL,则应考虑仅使用现代OpenGL(3.2+)

To answer the last comment: 要回答最后一条评论:

Well, I read those colors from a texture file, that's what I meant. 好吧,我从纹理文件中读取了这些颜色,这就是我的意思。 Is there a way to use such an array to display my mesh in color ? 有没有办法使用这样的数组以彩色显示我的网格?

Yes and no: You will most probably not get the result you expect when doing this. 是和否:执行此操作时,您极有可能无法获得预期的结果。 In general there will be multiple pixels in a texture that should be mapped to a face. 通常,纹理中将有多个像素应映射到一张脸。 With vertex-colors you can only apply one color-value per vertex which gets interpolated over the triangle. 使用顶点颜色时,每个顶点只能应用一种颜色值,该值会插值到三角形上。 Have a look on how to apply textures to a mesh, you should be able to find a lot of resources on the internet. 看看如何将纹理应用于网格,您应该能够在Internet上找到很多资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM