glVertexAttribPointer 出现 JOGL 错误

Posted

技术标签:

【中文标题】glVertexAttribPointer 出现 JOGL 错误【英文标题】:JOGL error with glVertexAttribPointer 【发布时间】:2014-06-30 09:47:57 【问题描述】:

我正在处理下面的代码(使用 VAO(Vertex Buffer Object) 和 glDrawArraysInstanced 方法)

如果我设置 useVao = false,就可以了。但是当我将其设置为 true 时,我遇到了以下异常:

javax.media.opengl.GLException: Thread[AWT-EventQueue-0,6,main] glGetError() 在调用 glVertexAttribPointer( GLArrayDataWrapper[mgl_MultiTexCoord, index -1, location 1, isVertexAttribute true , dataType 0x1406, bufferClazz 类 java.nio.FloatBuffer, 元素 4, 组件 2, stride 20b 5c, mappedElementCount 0, 缓冲区 java.nio.DirectFloatBufferU[pos=0 lim=20 cap=40], vboEnabled true, vboName 1, vboUsage 0x88e4, vboTarget 0x8892, vboOffset 12, alive true]): GL_INVALID_OPERATION (1282 0x502),

我认为 glVertexAttribPointer() 的最后一个参数(0 和 Buffers.SIZEOF_FLOAT * 3)是可疑的,但我无法纠正它。

我的试验:

    将变量 arg0 更改为 0。 将变量步幅更改为 0。 将变量 stride 更改为 0,并将 arg0 更改为 0。

我不确定是否可以同时使用 VAO 和 glDrawArraysInstanced 方法。

有什么建议吗?

package demos.triangle;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.nio.FloatBuffer;

import javax.media.opengl.DebugGL3;
import javax.media.opengl.GL3;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLJPanel;
import javax.media.opengl.fixedfunc.GLMatrixFunc;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import com.jogamp.common.nio.Buffers;
import com.jogamp.opengl.util.FPSAnimator;
import com.jogamp.opengl.util.PMVMatrix;

public class MultTriangleVAOInstancedExperiment implements GLEventListener 
    private final JFrame frame;
    private final FPSAnimator animator;
    private final GLJPanel panel;
    private final Dimension dim = new Dimension(1024, 768);
    private double t0 = System.currentTimeMillis();
    private float theta;

    private int shaderProgram;
    private int vertShader;
    private int fragShader;
    private int modelViewProjectionMatrixLocation;
    private int transformMatrixLocation;

    private static final int locPos = 1;
    private static final int locCol = 2;
    private static final boolean useVao = false;
//  private static final boolean useVao = true;

    private int[] vbo;
    private int[] vao;

    public static void main(String[] s)
        SwingUtilities.invokeLater(new Runnable() 
            @Override
            public void run() 
                new MultTriangleVAOInstancedExperiment();
            
        );
    

    public MultTriangleVAOInstancedExperiment() 
        frame = new JFrame(this.getClass().getSimpleName());
        frame.setLayout(new BorderLayout());
        frame.addWindowListener(new WindowAdapter() 
            @Override
            public void windowClosing(WindowEvent e) 
                animator.stop();
                System.exit(0);
            
        );

        GLProfile profile = GLProfile.get(GLProfile.GL3);
            panel = new GLJPanel(new GLCapabilities(profile));
        panel.addGLEventListener(this);
        panel.setPreferredSize(dim);
        frame.add(panel, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
        animator = new FPSAnimator(panel, 60, true);
        animator.start();
    

    @Override
    public void init(GLAutoDrawable drawable) 
        GL3 gl = drawable.getGL().getGL3();
        drawable.setGL(new DebugGL3(gl));

        gl.glClearColor(1, 1, 1, 1);
        gl.glClearDepth(1.0f);

        System.err.println("Chosen GLCapabilities: " + drawable.getChosenGLCapabilities());
        System.err.println("INIT GL IS: " + gl.getClass().getName());
        System.err.println("GL_VENDOR: " + gl.glGetString(GL3.GL_VENDOR));
        System.err.println("GL_RENDERER: " + gl.glGetString(GL3.GL_RENDERER));
        System.err.println("GL_VERSION: " + gl.glGetString(GL3.GL_VERSION));

        vertShader = gl.glCreateShader(GL3.GL_VERTEX_SHADER);
        fragShader = gl.glCreateShader(GL3.GL_FRAGMENT_SHADER);

        String[] vlines = new String[]  vertexShaderString ;
        int[] vlengths = new int[]  vlines[0].length() ;
        gl.glShaderSource(vertShader, vlines.length, vlines, vlengths, 0);
        gl.glCompileShader(vertShader);

        int[] compiled = new int[1];
        gl.glGetShaderiv(vertShader, GL3.GL_COMPILE_STATUS, compiled, 0);
        if(compiled[0] != 0) 
            System.out.println("Vertex shader compiled");
         else 
            int[] logLength = new int[1];
            gl.glGetShaderiv(vertShader, GL3.GL_INFO_LOG_LENGTH, logLength, 0);

            byte[] log = new byte[logLength[0]];
            gl.glGetShaderInfoLog(vertShader, logLength[0], (int[])null, 0, log, 0);

            System.err.println("Error compiling the vertex shader: " + new String(log));
            System.exit(1);
        

        String[] flines = new String[]  fragmentShaderString ;
        int[] flengths = new int[]  flines[0].length() ;
        gl.glShaderSource(fragShader, flines.length, flines, flengths, 0);
        gl.glCompileShader(fragShader);

        gl.glGetShaderiv(fragShader, GL3.GL_COMPILE_STATUS, compiled, 0);
        if(compiled[0] != 0)
            System.out.println("Fragment shader compiled.");
         else 
            int[] logLength = new int[1];
            gl.glGetShaderiv(fragShader, GL3.GL_INFO_LOG_LENGTH, logLength, 0);

            byte[] log = new byte[logLength[0]];
            gl.glGetShaderInfoLog(fragShader, logLength[0], (int[])null, 0, log, 0);

            System.err.println("Error compiling the fragment shader: " + new String(log));
            System.exit(1);
        

        shaderProgram = gl.glCreateProgram();
        gl.glAttachShader(shaderProgram, vertShader);
        gl.glAttachShader(shaderProgram, fragShader);

        gl.glBindAttribLocation(shaderProgram, locPos, "VertexPosition");
        gl.glBindAttribLocation(shaderProgram, locCol, "VertexColor");

        gl.glLinkProgram(shaderProgram);

        modelViewProjectionMatrixLocation = gl.glGetUniformLocation(shaderProgram, "uniform_Projection");
        System.out.println("modelViewProjectionMatrixLocation:" + modelViewProjectionMatrixLocation);
        transformMatrixLocation = gl.glGetUniformLocation(shaderProgram, "uniform_Transform");
        System.out.println("transformMatrixLocation:" + transformMatrixLocation);

        FloatBuffer interleavedBuffer = Buffers.newDirectFloatBuffer(vertices.length + colors.length);
        for(int i = 0; i < vertices.length/3; i++) 
            for(int j = 0; j < 3; j++) 
                interleavedBuffer.put(vertices[i*3 + j]);
            
            for(int j = 0; j < 4; j++) 
                interleavedBuffer.put(colors[i*4 + j]);
            
        
        interleavedBuffer.flip();

        vao = new int[1];
        gl.glGenVertexArrays(1, vao , 0); // was 2
        gl.glBindVertexArray(vao[0]);
        vbo = new int[1];
        gl.glGenBuffers(1, vbo, 0); //was 2
        gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vbo[0]);
        gl.glBufferData(GL3.GL_ARRAY_BUFFER, interleavedBuffer.limit() * Buffers.SIZEOF_FLOAT, interleavedBuffer, GL3.GL_STATIC_DRAW);

        gl.glEnableVertexAttribArray(locPos);
        gl.glEnableVertexAttribArray(locCol);

            // 
        int stride = Buffers.SIZEOF_FLOAT * (3+4);

        long arg0 = Buffers.SIZEOF_FLOAT * 3;

            //suspicious 
        gl.glVertexAttribPointer( locPos, 3, GL3.GL_FLOAT, false, stride, 0);
        gl.glVertexAttribPointer( locCol, 4, GL3.GL_FLOAT, false, stride, arg0); 

        if(!useVao)  //added
          gl.glDisableVertexAttribArray(locPos);
          gl.glDisableVertexAttribArray(locCol);
        
    

    @Override
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) 
        System.out.println("Window resized to  #version 330 \n" +
                    "\n" +
                    "uniform mat4 uniform_Projection; \n" + //not used
                    "uniform mat4 uniform_Transform[2]; \n" +
                    "in vec4  VertexPosition; \n" +
                    "in vec4  VertexColor; \n" +
                    "out vec4 tmpColor; \n" +
                    "void main(void) \n" +
                    " \n" +
                    "  tmpColor = VertexColor; \n" +
                    //"  gl_Position = uniform_Projection * VertexPosition; \n" +
                    "  gl_Position = uniform_Transform[gl_InstanceID] * VertexPosition; \n" +
                    " ";

    private final String fragmentShaderString =
            "#version 330\n" +
                    "\n" +
                    "in vec4    tmpColor; \n" +
                    "out vec4   outColor; \n" +
                    "void main (void) \n" +
                    " \n" +
                    "  outColor = tmpColor; \n" +
                    " ";

    private final float[] vertices = 
            1.0f, 0.0f, 0,
            -0.5f, 0.866f, 0,
            -0.5f, -0.866f, 0
    ;

    private final float[] colors = 
            1.0f, 0.0f, 0.0f, 1.0f,
            0.0f, 1.0f, 0.0f, 1.0f,
            0f, 0f, 1.0f, 1f
    ;


【问题讨论】:

删除“gl.glBindVertexArray(0);”行后在 display() 方法结束时,异常消失了。我觉得很奇怪。 【参考方案1】:

我不清楚具体的异常消息,但这是您肯定遇到的一个问题:在您的 init() 方法结束时,您为您的两个顶点属性调用 glDisableVertexAttribArray()。此时,您的 VAO 已绑定。属性启用/禁用状态是 VAO 状态的一部分。因此,当您稍后在 display() 方法中调用 glBindVertexArray() 时,顶点属性将被禁用,并且您在没有任何启用属性的情况下进行 glDrawArraysInstanced() 调用。

要解决此问题,您应该在 init() 结束时进行 glDisableVertexAttribArray() 调用:

if (!useVao) 
    gl.glDisableVertexAttribArray(locPos);
    gl.glDisableVertexAttribArray(locCol);

【讨论】:

以上是关于glVertexAttribPointer 出现 JOGL 错误的主要内容,如果未能解决你的问题,请参考以下文章

glVertexAttribPointer和stride参数要求

glVertexAttribPointer() - 啥时候应该调用它?

缓冲区和 glVertexAttribPointer 之间的关系

如何使用 glVertexAttribPointer() 添加非数组值

Opengl顶点着色器为每个顶点设置布尔值(glVertexAttribPointer)

glVertexAttribPointer 返回 GL_INVALID_OPERATION