开发者

openGL textures beginner question - 1D Texture creation?

开发者 https://www.devze.com 2023-03-09 22:08 出处:网络
EDIT 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. Th

EDIT

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)

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.

The drawing part:

    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))

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 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])

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)

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

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. 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.

  2. As said above, definitely not.

EDIT: 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号