简体   繁体   中英

How do you display a 3D object using indices in jogl 2 with OpenGL 3.3

im tryign to write a script to display basic 3D objects/polygon triangles using JOGL 2 with OpenGL 3.3 however when the item compiles i receive no error and get an blank window of where the object appears. So my question is, is there anything in specific im missing in adding to make the object to appear.. my code is as follows...

    public void init(GL3 gl)
{





    gl.glGenVertexArrays(1, IntBuffer.wrap(temp));

    //create vertice buffers
    int vao = temp[0];

    gl.glBindVertexArray(vao);

    gl.glGenBuffers(1, IntBuffer.wrap(temp));

    int[] temp2 = new int[]{1,1};

    gl.glGenBuffers(2, IntBuffer.wrap(temp2));
    vbo = temp2[0]; 

    ebo = temp2[1];

    //creates vertex array
    float vertices[] = {
                -0.5f,  0.5f, 0.0f,//1,0,0, // Top-left
                 0.5f,  0.5f, 0.0f,//0,1,0, // Top-right
                 0.5f, -0.5f, 0.0f,//0,0,1, // Bottom-right
                -0.5f, -0.5f, 0.0f//1,1,0  // Bottom-left
                        };

    gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo);
    gl.glBufferData(GL.GL_ARRAY_BUFFER, vertices.length * 4,
             FloatBuffer.wrap(vertices), GL.GL_STATIC_DRAW);

    //creates element array

    int elements[] = {
            0,1,2,
            2,3,0
            };
    gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, ebo);
    gl.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER, elements.length * 4,
             IntBuffer.wrap(elements), GL.GL_STATIC_DRAW);

    gl.glVertexAttribPointer(0, 3, GL.GL_FLOAT, false, 3*4, 0* 4);

    gl.glEnableVertexAttribArray(0);


}

public void draw(GL3 gl) 
{
    gl.glBindVertexArray(vao);
    gl.glDrawElements(GL.GL_TRIANGLES, 2, GL.GL_UNSIGNED_INT, 0);
}

As for where my shaders being initiated, its in a different class, which is as follows..

        //Matrix4 view = new Matrix4(MatrixFactory.perspective(scene.camera.getHeightAngle(),scene.camera.getAspectRatio(),scene.camera.getPosition());
    projection = MatrixFactory.perspective(scene.camera.getHeightAngle(), scene.camera.getAspectRatio(), 0.01f, 100f);
    view = MatrixFactory.lookInDirection(scene.camera.getPosition(), scene.camera.getDirection(), scene.camera.getUp());
    try {
         shader = new Shader(new File("shaders/Transform.vert"), new File("shaders/Transform.frag"));
         shader.compile(gl);
         shader.enable(gl);
            shader.setUniform("projection", projection, gl);
            shader.setUniform("view", view, gl);
        }
        catch (Exception e) {
         System.out.println("message " + e.getMessage());
        }
    for (Shape s : scene.shapes) {
        s.init(gl);
    }

And finally, my shader files

#version 330


out vec4 fragColour;
//in vec3 outColour;
void main() {
 fragColour = vec4(1,0,0,1);
}

#version 330
uniform mat4 projection;
uniform mat4 view;
layout(location=0) in vec3 pos;
//layout(location=2) in vec2 texCoord;
//layout(location=1) in vec3 colours;
out vec2 fragTex;
out vec3 outColour;
vec4 newPos;

void main() {
newPos = vec4(pos,1.0);
gl_Position = projection * view * newPos;
//fragTex = texCoord;
//outColour = colours;
}

i am unsure on where i am going wrong, whether it is the shader files, or the actualy code itself..

I am not experienced in JOGL, I am used to c++ GL. However there are several problems: First, as Reto Koradi stated you are using the same value to ebo, vbo and vao . It should be like,

gl.glGenVertexArrays(1, IntBuffer.wrap(tempV));
int vao = tempV[0];


gl.glGenBuffers(2, IntBuffer.wrap(tempB));
int vbo = tempB[0]; 
int ebo = tempB[1];

Lastly, your draw seems a bit problematic, you seem to skip a step."bind the array to want to draw." Then draw.

gl.glBindVertexArray (vao);
gl.glDrawElements(GL.GL_TRIANGLES, 2, GL.GL_UNSIGNED_INT, 0);

I hope these help.

Ok, after many frustrating hours. someone helped me with the solution. The issue wasnt making seperate buffers, but rather not clearing them each time, meaning i needed to do

gl.glGenVertexArrays(1, IntBuffer.wrap(temp));

    //create vertice buffers
    vao = temp[0];


    gl.glGenBuffers(1, IntBuffer.wrap(temp));
    vbo = temp[0]; 
    gl.glGenBuffers(1, IntBuffer.wrap(temp));
    ebo = temp[0];

which is similar to how Hakes however i didnt need a seperate temp, i just needed to clear the buffer each time. one other thing i needed to do was to also put

gl.glBindVertexArray(vao); in the init as well as the draw.

(edit) im actually not too sure gl.glBindVertexArray(vao); needed to be in the draw method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM