what's wrong with this code?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

what's wrong with this code?

Ususuus
The code is as blow, i created 3 triangles (a list in Class Object), and each has it's own data in Class Part,, my problem is,  all the 3 the triangles were drawn with the last triangle's vertex...

Please help to get me a trick...

public class Object {
        public GL2 gl;
        private ArrayList<JTPart> list = new ArrayList<>();
       
        public Object(GL2 gl) {
                this.gl = gl;
        }

        public boolean goLoad() {
                try {
                        int[]xx= {0,1,2};
                        float[]va= {
                                        3,0,0,
                                        0,3,0,
                                        -3,0,0};
                       
                        float[]vb= {
                                        20,0,10,
                                        0,20,10,
                                        -20,0,10};
                       
                        float[]vc= {
                                        30,0,20,
                                        0,30,20,
                                   -30,0,20};
                       
                        float []colora= {1,0,0,
                                        1,0,0,
                                        1,0,0};
                        float []colorb= {0,1,0,
                                        0,1,0,0,1,0};
                        float []colorc= {0,0,1,0,0,1
                                        ,0,0,1};
                       
                        float[]uv= {0,0,20,
                                        0,0,20,
                                        0,0,20};
                       
                        Part temp1 = new Part(gl);
                        temp1.genBuffers(xx, va, uv, colora);
                        list.add(temp1);
                       
                        JTPart temp2 = new Part(gl);
                        temp2.genBuffers(xx, vb, uv, colorb);
                        list.add(temp2);
                       
                        Part temp3 = new Part(gl);
                        temp3.genBuffers(xx, vc, uv, colorc);
                        list.add(temp3);
                       
                        return true;
                } catch (Exception e) {
                        e.printStackTrace();
                        return false;
                }
        }

        public void Draw() {
                for (int i=0;i<list.size();i++) {
                        list.get(i).Draw(0,x*(i/iMat),x*(i%iMat));
                }
        }
}

public class Part {
        public GL2 gl;
        private IntBuffer ibo, vbo;

        private int count = 0;

        public String partName = "";

        public Part(GL2 gl) {
                this.gl = gl;
        }

        public void genBuffers(int[] indicesArr, float[] verArr, float[] uvArr, float[] colorArr) {
                IntBuffer indicesBuf;
                FloatBuffer uvBuf, colorBuf, verBuf;

                vbo = Buffers.newDirectIntBuffer(3);
                ibo = Buffers.newDirectIntBuffer(1);

                indicesBuf = Buffers.newDirectIntBuffer(indicesArr);
                verBuf = Buffers.newDirectFloatBuffer(verArr);
                uvBuf = Buffers.newDirectFloatBuffer(uvArr);
                colorBuf = Buffers.newDirectFloatBuffer(colorArr);

                count = indicesArr.length * 3;
               
                gl.glGenBuffers(3, vbo);
                gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vbo.get(0));
                gl.glBufferData(GL2.GL_ARRAY_BUFFER, verBuf.capacity() * Buffers.SIZEOF_FLOAT, verBuf, GL2.GL_STATIC_DRAW);
                gl.glVertexPointer(3, GL2.GL_FLOAT, 0, 0);

                gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vbo.get(1));
                gl.glBufferData(GL2.GL_ARRAY_BUFFER, uvBuf.capacity() * Buffers.SIZEOF_FLOAT, uvBuf, GL2.GL_STATIC_DRAW);
                gl.glNormalPointer(GL2.GL_FLOAT, 0, 0);

                gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vbo.get(2));
                gl.glBufferData(GL2.GL_ARRAY_BUFFER, colorBuf.capacity() * Buffers.SIZEOF_FLOAT, colorBuf, GL2.GL_STATIC_DRAW);
                gl.glColorPointer(3, GL2.GL_FLOAT, 0, 0);

                gl.glGenBuffers(1, ibo);
                gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, ibo.get(0));
                gl.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, indicesBuf.capacity() * Buffers.SIZEOF_INT, indicesBuf, GL2.GL_STATIC_DRAW);
               
                verBuf = null;
                uvBuf = null;
                colorBuf = null;
                indicesBuf = null;
        }


        public void Draw(float x, float y, float z) {
                gl.glTranslatef(x, y, z);
                gl.glEnable(GL2.GL_COLOR_MATERIAL);

                gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
                gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vbo.get(0));
                gl.glVertexPointer(0, 3, GL2.GL_FLOAT, 0);

                gl.glEnableClientState(GL2.GL_NORMAL_ARRAY);
                gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vbo.get(1));
                gl.glNormalPointer(GL2.GL_FLOAT, 0, 0);

                gl.glEnableClientState(GL2.GL_COLOR_ARRAY);
                gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vbo.get(2));
                gl.glColorPointer(3, GL2.GL_FLOAT, 0, 0);

                gl.glEnableClientState(GL2.GL_ELEMENT_ARRAY_BUFFER);
                gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, ibo.get(0));
                gl.glDrawElements(GL2.GL_TRIANGLES, count, GL2.GL_UNSIGNED_INT, 0);

                gl.glDisable(GL2.GL_COLOR_MATERIAL);
                gl.glTranslatef(-x, -y, -z);
        }

}
Reply | Threaded
Open this post in threaded view
|

Re: what's wrong with this code?

gouessej
Administrator
Hello

I advise you to start from a working example that doesn't mix the fixed pipeline with the programmable pipeline:
http://mail.jogamp.org/git/?p=jogl-demos.git;a=blob;f=src/demos/es2/RawGL2ES2demo.java;hb=HEAD

Moreover, if you stick to the fixed pipeline, rather use glPushMatrix, glPopMatrix and glLoadIdentity instead of calling twice glTranslate. Learn what the current matrix is...
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: what's wrong with this code?

Ususuus
i'm still stucked here, and it's good to see your reply...(reading the examples right now...^_^)

i just want to know why it doesn't work, my program is very simple in the case of 3D programs, needs very few functions, i'm planning to move to modern OPENGL after get this issue done.

public void display(GLAutoDrawable drawable) {
                GL2 gl = drawable.getGL().getGL2();
                setLight(gl, lightAmb, lightDif);
                gl.glMatrixMode(GL2.GL_PROJECTION);
                gl.glLoadIdentity();

                GLU glu = GLU.createGLU();
                float widthHeightRatio = (float) getWidth() / (float) getHeight();
                glu.gluPerspective(45, widthHeightRatio, 1, 1000);
                glu.gluLookAt(0, 0, 500, 0, 0, 0, 0, 1, 0);

                gl.glMatrixMode(GL2.GL_MODELVIEW);
                gl.glLoadIdentity();
                gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
                gl.glShadeModel(GL2.GL_SMOOTH);

                gl.glTranslatef(transf.x, transf.y, transf.z);
                gl.glRotatef(rotatef.y, 1f, 0f, 0f);
                gl.glRotatef(rotatef.x, 0f, 1f, 0f);
                gl.glRotatef(rotatef.z, 0f, 0f, 1f);
                gl.glScalef(fScaleRate, fScaleRate, fScaleRate);

                drawAsixes(gl);
                gl.glEnable(GL2.GL_BLEND);
                gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA);
                A.Draw();
                B.Draw();
                gl.glFlush();
                if (animator.isPaused() == false) {
                        animator.pause();
                }
        }

        public void init(GLAutoDrawable drawable) {

                GL2 gl = drawable.getGL().getGL2();
                gl.glEnable(GL2.GL_NORMALIZE); // 这行要看看是干什么用的
                // ----- Your OpenGL initialization code here -----
                gl.glClearColor(0.9f, 0.9f, 0.9f, 0.8f); // set background (clear) color
                gl.glClearDepth(1.0f); // set clear depth value to farthest

                gl.glEnable(GL2.GL_BLEND);
                gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA);
                gl.glEnable(GL2.GL_DEPTH_TEST); // enables depth testing
                gl.glDepthFunc(GL2.GL_LEQUAL); // the type of depth test to do
                gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT,GL2.GL_NICEST); /* best perspective correction */
                gl.glShadeModel(GL_SMOOTH); // blends colors nicely, and smoothes out
                                                                        // lighting

                gl.glEnable(GL2.GL_LIGHTING);
                gl.glEnable(GL2.GL_LIGHT0);

                loadTriangle(gl);
               
                GLU glu = GLU.createGLU();
                glu.gluPerspective(45, (double) getWidth() / getHeight(), 0.01, 5000);
                gl.glMatrixMode(GL2.GL_MODELVIEW);
        }

        public void loadTriangle(GL2 gl) {
                va = new float[] { -1f, -1f, 0.5f, 1f, -1f, 0.5f, 0f, 1f, 0.5f };
                vb = new float[] { -0.5f, -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0f, 0.5f, -0.5f };
                ca = new float[] { 1f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 1f };
                cb = new float[] { 1f, 1f, 1f, 0f, 1f, 0f, 0f, 0f, 0f };
                A=new Triangle(gl);
                B=new Triangle(gl);
//**********************************************************************************
// if reverse below two, it will show different triangles, what i'm trying to do is to display them together. but only show the last one (in right postion and with right color)
        B.genBuffers(vb, cb);
                A.genBuffers(va, ca);

        }
//**********************************************************************************
/*
Triangle class
*/

import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import com.jogamp.common.nio.Buffers;
import com.jogamp.opengl.GL2;

public class Triangle {

        public GL2 gl;
        private IntBuffer vbo,vao;
        public int count = 0;
        public Triangle(GL2 gl) {
                this.gl = gl;
        }

        public void genBuffers(float[] verArr, float[] colorArr) {
                FloatBuffer colorBuf, verBuf;

                verBuf = FloatBuffer.allocate(verArr.length);
                colorBuf = FloatBuffer.allocate(colorArr.length);
               
                for (int j = 0; j < verArr.length; j++) {
                        verBuf.put((float)verArr[j]);
                        colorBuf.put(colorArr[j]);
                }

                verBuf.flip();
                colorBuf.flip();
               
                count = 3;
                vao=Buffers.newDirectIntBuffer(1);
                gl.glGenBuffers(1, vao);
                gl.glBindVertexArray(vao.get(0));
               
                vbo = Buffers.newDirectIntBuffer(2);
                gl.glGenBuffers(2, vbo);
               
                gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
                gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vbo.get(0));
                gl.glBufferData(GL2.GL_ARRAY_BUFFER, verArr.length * Buffers.SIZEOF_FLOAT, verBuf, GL2.GL_STATIC_DRAW);
                gl.glVertexPointer(3, GL2.GL_FLOAT, 0, 0);
               
                gl.glEnableClientState(GL2.GL_COLOR_ARRAY);
                gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vbo.get(1));
                gl.glBufferData(GL2.GL_ARRAY_BUFFER, colorArr.length * Buffers.SIZEOF_FLOAT, colorBuf, GL2.GL_STATIC_DRAW);
                gl.glColorPointer(3, GL2.GL_FLOAT, 0, 0);

                verBuf = null;
                colorBuf = null;
                gl.glBindVertexArray(0);
        }

        public void Draw() {
                gl.glEnable(GL2.GL_COLOR_MATERIAL);
                gl.glBindVertexArray(vao.get(0));
                gl.glDrawArrays(GL2.GL_TRIANGLES, 0, count);
                gl.glBindVertexArray(0);
                gl.glDisable(GL2.GL_COLOR_MATERIAL);
        }
}
Reply | Threaded
Open this post in threaded view
|

Re: what's wrong with this code?

gouessej
Administrator
This post was updated on .
Passing indirect NIO buffers to JOGL is a very bad idea as it will have to convert them to direct ones anyway.

I don't see any call to glVertexAttribPointer and glEnableVertexAttribArray :s

You have to call glBindbuffer, glVertexAttribPointer and glEnableVertexAttribArray in this order exactly like in the example. Maybe this would help you:
https://stackoverflow.com/a/17150414

When you don't create a VAO, JOGL does it for you and your VBOs are bound to this implicitly created VAO.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: what's wrong with this code?

Ususuus
finally comes the good news!

i just changed the code to bind the VBO first before VAO, and then drawing. drawing with VAO binding only ,will draw the vetexes, but by default, the VAO was bind to the last VBO which was bound...
Reply | Threaded
Open this post in threaded view
|

Re: what's wrong with this code?

gouessej
Administrator
I'm glad for you :) Feel free to post your source code, maybe it could help someone else doing the same mistake.
Julien Gouesse | Personal blog | Website