openGL纹理初学者问题-1D纹理创建? [英] openGL textures beginner question - 1D Texture creation?

查看:98
本文介绍了openGL纹理初学者问题-1D纹理创建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑

好吧,我在纹理渲染中添加了一些更改,现在我看起来似乎不是我想要的,但是在尝试更改任何内容之前,我只想确保我位于正确的位置小路.我要解决的问题是:我有180000个顶点.它们每个都可以来自190个类"之一.每个类别可以在不同的时间分配不同的颜色.因此,我尝试创建具有190种颜色的纹理,并且对于180000个顶点中的每个顶点,都有一个coresponding类的textureCoord.因此,对于一些代码:

Ok I added some changes to my texture rendering, and I'm now at a point that it doesn't look how I want it but before I try to change anything I just want to be sure I'm on the right path. The problem I'm trying to fix is: I have 180000 vertices. Each of them can be from one of 190 "classes". Each class can have a different color assigned at a different time. So I'm trying to create a texture with 190 colors, and for each of the 180000 vertexes have a textureCoord to the coresponding class. So for some code:

    self.bufferTextureIndex = glGenBuffersARB(1)
    glBindBufferARB(GL_ARRAY_BUFFER_ARB, self.bufferTextureIndex)
    glBufferDataARB(GL_ARRAY_BUFFER_ARB, ADT.arrayByteCount(textureIndexes), ADT.voidDataPointer(textureIndexes), GL_STATIC_DRAW_ARB)               

    self.texture = glGenTextures(1)
    glBindTexture(GL_TEXTURE_1D, self.texture)
    glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
    glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 190, 0, GL_RGB , GL_FLOAT, textureArray)

因此textureIndexes是一个[0..1]浮点数的数组. len(textureIndexes)是我正在使用的顶点数(180000).对于纹理,textureArray包含190 * 3个浮点数,分别对应于每个类我想要的颜色的RBG.

So textureIndexes is an array of floats from [0..1]. len(textureIndexes) is the number of vertices I'm using (180000). For the texture, textureArray contains 190 * 3 floats coresponding to the RBG of the colors I want for each class.

绘图部分:

    glEnableClientState(GL_TEXTURE_COORD_ARRAY)
    glBindBufferARB(GL_ARRAY_BUFFER_ARB, self.bufferTextureIndex)
    glTexCoordPointer(1, GL_FLOAT, 0, None);
    glBindTexture(GL_TEXTURE_1D, self.texture)
    glEnable(GL_TEXTURE_1D)       
    if type == GL_POINTS:
        glDrawArrays( GL_POINTS, 0, len(self.vertices) / 3 ); 
    else: 
        glDrawElements(GL_TRIANGLES, len(self.triangles) , GL_UNSIGNED_SHORT, ADT.voidDataPointer(self.triangles))

那么这种方法看起来正确吗?结果不是我所期望的,但这可能是我选择的颜色编码的结果,如果主要方法是一种好的方法,我会进一步研究.我认为最有可能的索引构建错误.要构建它们,我有一个文件,其数字在0到190之间,与每个索引的类相对应.因此,到目前为止,我的索引构建只是读取索引,然后读取每个顶点的索引/190以获得[0..1]

So does this approach seem right ? The result isn't what I'm expecting but that might be for the color codification I chose and I cam further look on that if the main approach is a good one. I think that most likely the indexes are build wrong. To build them I have a file with a number between 0 and 190 corresponding to the class for each index. So my index building so far was just read index then index / 190 for each vertex to get a number in [0..1]

EDIT2 因此,我听取了您的建议,使用索引+ 0.5/190生成了我的索引.我正在打印indice数组的长度和值.它是60000,都是0到1之间的数字,大部分是0.3到0.95之间的数字.但是我所有的顶点仍然是相同的颜色.因此,我唯一没有检查过的就是1D纹理生成.也许这是我弄错了的地方:

EDIT2 So I took your advice, did the index + 0.5 / 190 to generate my indexes. I'm printing the length and values of the indice array. It's 60000 and all are numbers between 0 and 1 , mostly between 0.3 and 0.95. But still all my vertices are of the same color. So the only thing I haven't checked is the 1D Texture generation. Maybe here is where I got it wrong:

    i = 0
    while i < 30:
        textureArray.append([1,0,0])
        i = i + 1
    while i < 60:
        textureArray.append([1,1,0])
        i = i + 1
    while i < 90:
        textureArray.append([1,1,1])
        i = i + 1
    while i < 120:
        textureArray.append([0,1,1])
        i = i + 1
    while i < 150:
        textureArray.append([0,0,1])
        i = i + 1
    while i < 190:
        i = i + 1 
        textureArray.append([0,0,0])

这就是我生成纹理数组的方式.这不是实际的解决方案,而是出于测试的原因.所以我的纹理应该是1/6红色-1/6....上面的纹理生成:

This is how I generate my texture array. This will not be the actual solution but for testing reasons. So my texture should be 1/6 red - 1/6 ... . The texture generation as above:

    self.texture = glGenTextures(1)
    glBindTexture(GL_TEXTURE_1D, self.texture)
    glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT)
    glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_REPEAT)
    glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
    glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 190, 0, GL_RGB , GL_FLOAT, textureArray)

此纹理生成正确吗?因为即使我的索引范围如我之前提到的,我的所有顶点的颜色仍是纹理中第一个颜色的颜色.有趣的是,如果我省略"了glBindBufferARB(GL_ARRAY_BUFFER_ARB,self.bufferTextureIndex)形成图形并让法线代替了纹理索引,我确实会得到一些不同的颜色,但是textureIndexes似乎都指向了第一种颜色从我的质感.我已经上传了两个带有textureIndices和法线实际值的示例文件.没有想法为什么第一个不起作用,但是第二个似乎起作用(无法真正验证它们是否是正确的颜色,但至少它们是不同的). http://www.megafileupload.com/en/file/315895/textureIndices -txt.html http://www.megafileupload.com/en/file/315894/normalsTest -txt.html

Is this texture generating correct ? Because even though my indices range is like I mentioned before, all my vertices have the color of the very first color from the texture. Funny thing is that if I "omit" the glBindBufferARB(GL_ARRAY_BUFFER_ARB, self.bufferTextureIndex) form the drawing and let the normals take the place of the texture indices I do get some different colors, but textureIndexes seem to all point to the very first color from my texture. I've uploaded two sample files with the actual values of textureIndices and normals. No ideea why the first one doesn't work but the second seems to work(can't really verify if they are the correct colors but at least they are different). http://www.megafileupload.com/en/file/315895/textureIndices-txt.html http://www.megafileupload.com/en/file/315894/normalsTest-txt.html

EDIT3

所以现在我的索引似乎正常了.这是一个示例图像:

So now my indices seems to work. Here is a sample image:

http://i.stack.imgur.com/yvlV3.png

但是奇怪的是,如您所见,边框的定义不正确.这可能会受到传递给纹理的某些参数的任何影响,还是应该对我的textureIndex创建进行三重检查?

However the odd part is that as you can see the borders are not defined properly. Could this be influenced in any way by some of the parameters I pass to the texture or should I triple check my textureIndex creation ?

推荐答案

  1. 此刻,您将法线用作纹理坐标,因为在调用glTexCoordPointer时绑定了self.bufferNormals.一维纹理不仅限于每个顶点颜色.可以使用每个顶点的纹理坐标(例如2D纹理)来访问它们,否则它们将无法替代每个顶点的颜色.如果您不理解,请阅读一些有关OpenGL纹理化或一般纹理化的介绍性材料.

  1. At the moment you use your normals as texture coordinates, because self.bufferNormals was bound when calling glTexCoordPointer. 1D textures aren't just per vertex colors. they are accessed by per-vertex texture coordinates, like 2D textures, otherwise they would be a useless substitute for per-vertex colors. Read some introductory material on OpenGL texturing or texturing in general if you don't uderstand that.

如上所述,绝对不是.

编辑:根据您最新的问题(带有截图的问题),请记住,当单个三角形的顶点具有不同的纹理坐标时(在您的情况下,它们将属于不同的纹理坐标)类(我猜应该不会发生),将texCoords插入整个三角形,然后用于获取纹理颜色.因此,如果您不希望发生这种情况(我想是这样),则必须确保三角形的所有顶点都具有相同的texCoord.因此,您必须沿材质类"边界复制顶点.也许只需设置glShadeModel(GL_FLAT)即可获得一种快速而肮脏的解决方案,但是这也将使照明变平,并且不确定边界三角形属于哪个类别.

According to you newest question (the one with the screenshot), keep in mind, that when the vertices of a single triangle have different texture coordinates (in your case they would belong to different classes, which I suppose shouldn't happen), the texCoords are interpolated accross the triangle and then used to get the texture color. So you have to make sure all vertices of a triangle have the same texCoord if you don't want this to happen (which I suppose). So you have to duplicate vertices along the "material class" borders. Perhaps you could get a quick and dirty solution by just setting glShadeModel(GL_FLAT), but that would also flatten lighting and it is not determined to which class a border triangle belongs then.

这篇关于openGL纹理初学者问题-1D纹理创建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆