无法创建将纹理应用于OpenGL中的线条

时间:2017-06-16 14:32:14

标签: opengl jogl

我的目标是使用着色器创建虚线。我已经定义了一维纹理,我认为它应该被应用,但我的所有线条都以黑色结束。我希望有一些简单的东西,我只是想念。

我的线顶点着色器:

#version 330 core

layout (location = 0) in vec2 iPosition;

uniform mat4 transform;

void main() {
    gl_Position = transform * vec4(iPosition, 0.0, 1.0);
}

我的几何着色器:

#version 330 core

layout(lines) in;
layout(line_strip, max_vertices=2) out;

out float texCoord;

void main()
{
    gl_Position = gl_in[0].gl_Position;
    texCoord = 0.0f;
    EmitVertex();
    gl_Position = gl_in[1].gl_Position;
    texCoord = 1.0f;
    EmitVertex();
}

我的片段着色器:

#version 330 core

uniform sampler1D pattern;

layout (location = 0) out vec4 fragColor;
in float texCoord;

void main() {
    fragColor = texture(pattern, texCoord);
}

我的代码:

int[] textures = new int[1];
gl4.glGenTextures(1, textures, 0);
int textureId = textures[0];
gl4.glActiveTexture(GL4.GL_TEXTURE0);
gl4.glBindTexture(GL4.GL_TEXTURE_1D, textureId);

ByteBuffer pixels = Buffers.newDirectByteBuffer(64 * 4 * 4);
float full = Float.MAX_VALUE;
float empty = 0f;

//opaque red.
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);    

//transparent green.
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);

//opaque red
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);

//transparent green.
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);

pixels.flip();
gl4.glTexParameteri(GL4.GL_TEXTURE_1D, GL4.GL_TEXTURE_WRAP_S, GL4.GL_REPEAT);
gl4.glTexParameteri(GL4.GL_TEXTURE_1D, GL4.GL_TEXTURE_WRAP_T, GL4.GL_REPEAT);

gl4.glTexParameteri(GL4.GL_TEXTURE_1D, GL4.GL_TEXTURE_MIN_FILTER, GL4.GL_NEAREST);
gl4.glTexParameteri(GL4.GL_TEXTURE_1D, GL4.GL_TEXTURE_MAG_FILTER, GL4.GL_NEAREST);
gl4.glTexImage1D(GL4.GL_TEXTURE_1D, 0, 4, 32, 0, GL.GL_RGBA, GL.GL_FLOAT, pixels);

  for (MapLayerData mapLayerData : mapLayerDataList) {
     List<Line> lines = mapLayerData.getLines(); 
     for (int x = 0; x < lines.size(); x++){
        Line line = lines.get(x);
        ArrayList<Point2D.Float> points = line.getPoints();

        for (Point2D.Float point : points) {
            vertBuffer.putFloat((float) point.x); 
            vertBuffer.putFloat((float) point.y); 
        }
        vertBuffer.flip();

        int[] bufferPointers = new int[1];
        gl4.glGenBuffers(1, bufferPointers, 0);

        gl4.glBindBuffer(GL.GL_ARRAY_BUFFER, bufferPointers[0]);
        gl4.glBufferData(GL.GL_ARRAY_BUFFER, (points.size() * 8), vertBuffer, GL.GL_DYNAMIC_DRAW);

        gl4.glVertexAttribPointer(0, 2, GL.GL_FLOAT, false, 8, 0);
        gl4.glEnableVertexAttribArray(0);

        gl4.glUniformMatrix4fv(lineTransformPointer, 1, false, transform.getMatrix(), 0);
        gl4.glUniform1i(patternPointer, 0);
        gl4.glDrawArrays(GL.GL_LINE_STRIP, 0, points.size());

        gl4.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
        gl4.glDeleteBuffers(bufferPointers.length, bufferPointers, 0);
        vertBuffer.clear();
     }
  }   
  gl4.glDeleteTextures(1, textures,0);

1 个答案:

答案 0 :(得分:0)

  • 4在核心情境中不是internalFormat的有效glTexImage1D()
  • 没有证据表明您有VAO约束。这些在Core环境中是必需的。
  • 没有理由在{1}纹理上GL_TEXTURE_WRAP_T
  • 不知道patternPointer是什么。