Java 纹理缓冲区和多重元素

Java 纹理缓冲区和多重元素,java,opengl,glsl,jogl,Java,Opengl,Glsl,Jogl,背景故事: 我正在尝试使用一次绘制调用在屏幕上绘制尽可能多的正方形。我正在使用一个定制的glsl顶点着色器,它专门用于2D绘图,应该是从采样缓冲区中提取正方形顶点的位置数据。因为我不需要担心旋转或缩放正方形,所以我需要做的就是将位置数据加载到缓冲区,将纹理绑定到该缓冲区,然后使用采样器获取每个顶点在着色器中的位置。为了得到纹理的索引,我将每个元素索引存储为顶点的z分量 对于一千多个方块来说,一切似乎都很好,但在那之后,我开始奇怪地眨眼。它似乎不是在每个绘制步骤都绘制所有的正方形,或者可能没有使用

背景故事: 我正在尝试使用一次绘制调用在屏幕上绘制尽可能多的正方形。我正在使用一个定制的glsl顶点着色器,它专门用于2D绘图,应该是从采样缓冲区中提取正方形顶点的位置数据。因为我不需要担心旋转或缩放正方形,所以我需要做的就是将位置数据加载到缓冲区,将纹理绑定到该缓冲区,然后使用采样器获取每个顶点在着色器中的位置。为了得到纹理的索引,我将每个元素索引存储为顶点的z分量

对于一千多个方块来说,一切似乎都很好,但在那之后,我开始奇怪地眨眼。它似乎不是在每个绘制步骤都绘制所有的正方形,或者可能没有使用所有的位置,所以许多正方形是重叠的

奇怪的是,如果我使用抽屉元素而不是抽屉元素multi,闪烁就会消失(当然,所有的正方形都被画成一个单独的对象,这是我不想要的)

我的一个问题是,我的位置数据是否限制为“最大纹理大小”或“最大纹理缓冲区大小”。如果我被限制在更小的最大纹理大小,我该如何绕过它呢?所有的纹理缓冲空间都在那里肯定是有原因的,但我显然不知道如何正确使用它

我也在想,也许GLmultipaurements在做一些我没有用采样器解释的事情。Idk,我在这一点上真的迷路了,但是..它对较小的正方形非常有效,所以我必须做一些正确的事情

[编辑]代码已更改以反映以下建议(以及可读性),但问题仍然存在

好的,这里有一些代码。首先是顶点着色器:

uniform mat3 projection;
attribute vec3 vertex;

uniform samplerBuffer positionSampler;

attribute vec4 vertex_color;
varying vec4 color;

float positionFetch(int index)
{
    // I've tried texelFetch here as well, same effect
    float value = texelFetchBuffer(positionSampler, index).r;
    return value;
}

void main(void)  
{ 
    color = vec4(1, 1, 1, 1);
    // use the z-component of the vertex to look up the position of this instance in the texture
    vec3 real_position = vec3(vertex.x + positionFetch(int(vertex.z)*2), vertex.y + positionFetch(int(vertex.z)*2+1), 1); 
    gl_Position = vec4(projection * real_position, 1); 
}
现在我要说的是,对不起,代码太多了,我只想确定这里有足够的信息来得到答案。这真的让我抓狂,java的例子似乎很难找到(也许这段代码可以帮助其他人完成他们的任务):

public类GLRenderer扩展了GLCanvas实现了GLEventListener、WindowListener
{
私有静态最终长serialVersionUID=-8513201172428486833L;
私有静态final int bytesperflot=Float.SIZE/Byte.SIZE;
私有静态final int bytesPerShort=Short.SIZE/Byte.SIZE;
公共浮动viewWidth、viewHeight;
公共浮动屏幕宽度、屏幕高度;
私人动画师;
私有布尔didInit=false;
JFrame_框架;
正方形几何;
//虽然可能需要2的幂,但似乎没有什么区别
私有静态final int NUM_THINGS=2*2*2*2*2*2*2*2*2*2*2*2*2*2*2;
float[]位置=新的float[NUM_THINGS*2];
//着色器属性
私有int着色器程序、projectionAttribute、vertexAttribute、positionAttribute;
公共静态void main(字符串[]args)
{
新GLRenderer();
}
公营部门()
{
//安装OpenGL版本2
super(新的GLCapabilities(GLProfile.get(GLProfile.GL2));
addGLEventListener(本);
设置大小(18001000);
_框架=新的JFrame(“你好世界”);
_frame.getContentPane().add(这个);
_frame.setSize(_frame.getContentPane().getPreferredSize());
_frame.setVisible(true);
_frame.addWindowListener(这个);
animator=新的FPS animator(本,60);
animator.start();
}
//gl上下文首次可用时由驱动程序调用
公共void init(GLAutoDrawable d)
{        
最终GL2 gl=d.getGL().getGL2();
IntBuffer asd=IntBuffer.allocate(1);
gl.glGetIntegerv(GL2.gl\u MAX\u TEXTURE\u BUFFER\u SIZE,asd);
System.out.println(asd.get(0));
asd=IntBuffer.allocate(1);
gl.glGetIntegerv(GL2.gl\u MAX\u纹理大小,asd);
System.out.println(asd.get(0));
shaderProgram=ShaderLoader.compileProgram(gl,“默认”);
gl.glLinkProgram(着色器程序);
_getShaderAttributes(德国劳埃德船级社);
gl.glUseProgram(着色器程序);
_检查能力(gl);
_初始设置(gl);
//从污垢几何体计算一批顶点数据
几何体=新的方形几何体(.1f);
几何体。构建几何体(视图宽度、视图高度);
几何学.几何测量学(数字事物);
geometry.vertexBufferID=_generateBufferID(gl);
_loadVertexBuffer(总图、几何图形);
geometry.indexBufferID=_generateBufferID(gl);
_loadIndexBuffer(总图,几何图形);
geometry.positionBufferID=\u generateBufferID(gl);
//初始化缓冲区对象
int size=NUM_THINGS*2*字节perfloat;
系统输出打印项次(尺寸);
IntBuffer bla=IntBuffer.allocate(1);
gl.glGenTextures(1,bla);
geometry.positionTextureID=bla.get(0);
gl.glUniform1i(positionAttribute,0);
gl.glActiveTexture(GL2.gl_TEXTURE0);
gl.glBindTexture(GL2.gl_TEXTURE_BUFFER,geometry.positionTextureID);
gl.glBindBuffer(GL2.gl\u纹理\u缓冲区,geometry.positionBufferID);
gl.glBufferData(GL2.gl\u纹理\u缓冲区,大小,null,GL2.gl\u动态\u绘制);
gl.glTexBuffer(GL2.gl_纹理_BUFFER,GL2.gl_R32F,geometry.positionBufferID);
}
私有无效设置(GL2 gl)
{
gl.glClearColor(0f、0f、0f、1f);
}
专用空隙_loadIndexBuffer(GL2 gl,方形几何)
{
gl.glBindBuffer(GL2.gl_元素_数组_缓冲区,geometry.indexBufferID);
gl.glBufferData(GL2.gl_元素_数组_缓冲区,字节短*NUM_事物*geometry.getNumPoints(),geometry.indexBuffer,GL2.gl_静态_绘制);
}
私有void_loadVertexBuffer(GL2 gl,SquareGeometry)
{
int numBytes=geometry.getNumPoints()*3*bytesperflot*NUM\u THINGS;
gl.glBindBuffer(GL2.gl_数组_BUFFER,geometry.vertex
public class GLRenderer extends GLCanvas implements GLEventListener, WindowListener
{

    private static final long serialVersionUID = -8513201172428486833L;

    private static final int bytesPerFloat = Float.SIZE / Byte.SIZE;
    private static final int bytesPerShort = Short.SIZE / Byte.SIZE;

    public float viewWidth, viewHeight;
    public float screenWidth, screenHeight;

    private FPSAnimator animator;

    private boolean didInit = false;

    JFrame the_frame;
    SquareGeometry geometry;

    // Thought power of 2 might be required, doesn't seem to make a difference
    private static final int NUM_THINGS = 2*2*2*2*2*2*2*2*2*2*2*2*2*2; 

    float[] position = new float[NUM_THINGS*2];

    // Shader attributes
    private int shaderProgram, projectionAttribute, vertexAttribute, positionAttribute;

    public static void main(String[] args) 
    {
        new GLRenderer();
    }

    public GLRenderer()
    {
        // setup OpenGL Version 2
        super(new GLCapabilities(GLProfile.get(GLProfile.GL2)));

        addGLEventListener(this);
        setSize(1800, 1000);

        the_frame = new JFrame("Hello World");
        the_frame.getContentPane().add(this);
        the_frame.setSize(the_frame.getContentPane().getPreferredSize());
        the_frame.setVisible(true);
        the_frame.addWindowListener(this);

        animator = new FPSAnimator(this, 60);
        animator.start();
    }

    // Called by the drivers when the gl context is first made available
    public void init(GLAutoDrawable d)
    {        
        final GL2 gl = d.getGL().getGL2();
        IntBuffer asd = IntBuffer.allocate(1);
        gl.glGetIntegerv(GL2.GL_MAX_TEXTURE_BUFFER_SIZE, asd);
        System.out.println(asd.get(0));
        asd = IntBuffer.allocate(1);
        gl.glGetIntegerv(GL2.GL_MAX_TEXTURE_SIZE, asd);
        System.out.println(asd.get(0));
        shaderProgram = ShaderLoader.compileProgram(gl, "default");
        gl.glLinkProgram(shaderProgram);

        _getShaderAttributes(gl);

        gl.glUseProgram(shaderProgram);
        _checkGLCapabilities(gl);
        _initGLSettings(gl);

        // Calculate batch of vertex data from dirt geometry
        geometry = new SquareGeometry(.1f);
        geometry.buildGeometry(viewWidth, viewHeight);
        geometry.finalizeGeometry(NUM_THINGS);

        geometry.vertexBufferID = _generateBufferID(gl);
        _loadVertexBuffer(gl, geometry);

        geometry.indexBufferID = _generateBufferID(gl);
        _loadIndexBuffer(gl, geometry);

        geometry.positionBufferID = _generateBufferID(gl);

        // initialize buffer object
        int size = NUM_THINGS * 2 * bytesPerFloat;
        System.out.println(size);

    IntBuffer bla = IntBuffer.allocate(1);
    gl.glGenTextures(1, bla);
    geometry.positionTextureID = bla.get(0);

        gl.glUniform1i(positionAttribute, 0);

        gl.glActiveTexture(GL2.GL_TEXTURE0);
        gl.glBindTexture(GL2.GL_TEXTURE_BUFFER, geometry.positionTextureID);
        gl.glBindBuffer(GL2.GL_TEXTURE_BUFFER, geometry.positionBufferID);
        gl.glBufferData(GL2.GL_TEXTURE_BUFFER, size, null, GL2.GL_DYNAMIC_DRAW);
        gl.glTexBuffer(GL2.GL_TEXTURE_BUFFER, GL2.GL_R32F, geometry.positionBufferID);
    }

    private void _initGLSettings(GL2 gl)
    {
        gl.glClearColor(0f, 0f, 0f, 1f);
    }

    private void _loadIndexBuffer(GL2 gl, SquareGeometry geometry)
    {
        gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, geometry.indexBufferID);
        gl.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, bytesPerShort*NUM_THINGS*geometry.getNumPoints(), geometry.indexBuffer, GL2.GL_STATIC_DRAW);
    }

    private void _loadVertexBuffer(GL2 gl, SquareGeometry geometry)
    {
        int numBytes = geometry.getNumPoints() * 3 * bytesPerFloat * NUM_THINGS;

        gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, geometry.vertexBufferID);
        gl.glBufferData(GL2.GL_ARRAY_BUFFER, numBytes, geometry.vertexBuffer, GL2.GL_STATIC_DRAW);
        gl.glEnableVertexAttribArray(vertexAttribute);
        gl.glVertexAttribPointer(vertexAttribute, 3, GL2.GL_FLOAT, false, 0, 0);
    }

    private int _generateBufferID(GL2 gl)
    {
        IntBuffer bufferIDBuffer = IntBuffer.allocate(1);
        gl.glGenBuffers(1, bufferIDBuffer);

        return bufferIDBuffer.get(0);
    }

    private void _checkGLCapabilities(GL2 gl)
    {
        // TODO: Respond to this information in a meaningful way.
        boolean VBOsupported = gl.isFunctionAvailable("glGenBuffersARB") && gl.isFunctionAvailable("glBindBufferARB")
                && gl.isFunctionAvailable("glBufferDataARB") && gl.isFunctionAvailable("glDeleteBuffersARB");

        System.out.println("VBO Supported: " + VBOsupported);
    }

    private void _getShaderAttributes(GL2 gl)
    {
        vertexAttribute = gl.glGetAttribLocation(shaderProgram, "vertex");
        projectionAttribute = gl.glGetUniformLocation(shaderProgram, "projection");
        positionAttribute = gl.glGetUniformLocation(shaderProgram, "positionSampler");
    }

    // Called by me on the first resize call, useful for things that can't be initialized until the screen size is known
    public void viewInit(GL2 gl)
    {
        for(int i = 0; i < NUM_THINGS; i++)
        {
            position[i*2] = (float) (Math.random()*viewWidth);
            position[i*2+1] = (float) (Math.random()*viewHeight);
        }

        gl.glUniformMatrix3fv(projectionAttribute, 1, false, Matrix.projection3f, 0);

        // Load position data into a texture buffer
        gl.glBindBuffer(GL2.GL_TEXTURE_BUFFER, geometry.positionBufferID);
        ByteBuffer textureBuffer = gl.glMapBuffer(GL2.GL_TEXTURE_BUFFER, GL2.GL_WRITE_ONLY);
        FloatBuffer textureFloatBuffer = textureBuffer.order(ByteOrder.nativeOrder()).asFloatBuffer();

        for(int i = 0; i < position.length; i++)
        {
            textureFloatBuffer.put(position[i]);
        }

        gl.glUnmapBuffer(GL2.GL_TEXTURE_BUFFER);
        gl.glBindBuffer(GL2.GL_TEXTURE_BUFFER, 0);
    }

    public void display(GLAutoDrawable d)
    {

        if (!didInit || geometry.vertexBufferID == 0)
        {
            return;
        }

        //long startDrawTime = System.currentTimeMillis();
        final GL2 gl = d.getGL().getGL2();

        gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);


        // If we were drawing any other buffers here we'd need to set this every time
        // but instead we just leave them bound after initialization, saves a little render time
        // No combination of these seems to fix the problem
        //gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, geometry.vertexBufferID);
        //gl.glVertexAttribPointer(vertexAttribute, 3, GL2.GL_FLOAT, false, 0, 0);
        //gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, geometry.indexBufferID);
        gl.glBindBuffer(GL2.GL_TEXTURE_BUFFER, geometry.positionBufferID);
        //gl.glActiveTexture(GL2.GL_TEXTURE0);
        //gl.glTexBuffer(GL2.GL_TEXTURE_BUFFER, GL2.GL_R32F, geometry.positionBufferID);

        _render(gl, geometry);

        // Also tried these
        //gl.glFlush();
        //gl.glFinish();
    }

    public void _render(GL2 gl, SquareGeometry geometry)
    {
        gl.glMultiDrawElements(geometry.drawMode, geometry.countBuffer, GL2.GL_UNSIGNED_SHORT, geometry.offsetBuffer, NUM_THINGS);
        // This one works, but isn't what I want
        //gl.glDrawElements(GL2.GL_LINE_LOOP, count, GL2.GL_UNSIGNED_SHORT, 0);
    }

    public void reshape(GLAutoDrawable d, int x, int y, int width, int height)
    {
        final GL2 gl = d.getGL().getGL2();
        gl.glViewport(0, 0, width, height);
        float ratio = (float) height / width;

        screenWidth = width;
        screenHeight = height;
        viewWidth = 100;
        viewHeight = viewWidth * ratio;

        Matrix.ortho3f(0, viewWidth, 0, viewHeight);

        if (!didInit)
        {
            viewInit(gl);
            didInit = true;
        } 
        else
        {
            // respond to view size changing
        }
    }
}
public class SquareGeometry
{
    public float[] vertices = null;

    ShortBuffer indexBuffer;
    IntBuffer countBuffer;
    PointerBuffer offsetBuffer;
    FloatBuffer vertexBuffer;

    public int vertexBufferID = 0;
    public int indexBufferID = 0;
    public int positionBufferID = 0;
    public int positionTextureID = 0;

    public int drawMode;

    protected float width = 0;
    protected float height = 0;

    public SquareGeometry(float size)
    {
        width = size;
        height = size;
    }

    public void buildGeometry(float viewWidth, float viewHeight)
    {
        vertices = new float[4 * 2];
        vertices[0] = -width/2;
        vertices[1] = -height/2;
        vertices[2] = -width/2;
        vertices[3] = height/2;
        vertices[4] = width/2;
        vertices[5] = height/2;
        vertices[6] = width/2;
        vertices[7] = -height/2;

        drawMode = GL2.GL_POLYGON;
    }

    public void finalizeGeometry(int numInstances)
    {
        if(vertices == null) return;

        int num_vertices = this.getNumPoints();
        int total_num_vertices = numInstances * num_vertices;

        // initialize vertex Buffer (# of coordinate values * 4 bytes per float)  
        ByteBuffer vbb = ByteBuffer.allocateDirect(total_num_vertices * 3 * Float.SIZE);
        vbb.order(ByteOrder.nativeOrder());
        vertexBuffer = vbb.asFloatBuffer();

        for(int i = 0; i < numInstances; i++)
        {
            for(int v = 0; v < num_vertices; v++)
            {
                int vertex_index = v * 2;
                vertexBuffer.put(vertices[vertex_index]);
                vertexBuffer.put(vertices[vertex_index+1]);
                vertexBuffer.put(i);
            }
        }
        vertexBuffer.rewind();

        // Create the indices
        vbb = ByteBuffer.allocateDirect(total_num_vertices * Short.SIZE);
        vbb.order(ByteOrder.nativeOrder());
        indexBuffer = vbb.asShortBuffer();

        for(int i = 0; i < total_num_vertices; i++)
        {
            indexBuffer.put((short) (i));
        }
        indexBuffer.rewind();

        // Create the counts
        vbb = ByteBuffer.allocateDirect(numInstances * Integer.SIZE);
        vbb.order(ByteOrder.nativeOrder());
        countBuffer = vbb.asIntBuffer();
        for(int i = 0; i < numInstances; i++)
        {
            countBuffer.put(num_vertices);
        }
        countBuffer.rewind();

        // create the offsets
        offsetBuffer = PointerBuffer.allocateDirect(numInstances);
        for(int i = 0; i < numInstances; i++)
        {
            offsetBuffer.put(num_vertices*i*2);
        }
        offsetBuffer.rewind();
    }

    public int getNumPoints() 
    {
        return vertices.length/2;
    }
}