简单的立方体与纹理JOGL / GLSL

时间:2014-04-24 15:35:00

标签: glsl jogl

我对这个简单的代码有疑问。它是一个C ++程序,我试图适应JAVA。我用顶点数组绘制一个立方体,但结果是一个三角形,我不知道为什么。我希望有一个人可以帮助我 !

这是我的代码:

  public class gl_cube_03 implements GLEventListener
    {
        IntBuffer texName = IntBuffer.allocate(1);         
        IntBuffer vao = IntBuffer.allocate(1);             
        IntBuffer vbo_positions = IntBuffer.allocate(1);   
        IntBuffer vbo_colors = IntBuffer.allocate(1);      
        IntBuffer vbo_texCoords = IntBuffer.allocate(1);   
        IntBuffer vbo_indices = IntBuffer.allocate(1);     
        GLSLShader shader;


        @Override
        public void init(GLAutoDrawable drawable)
        {
            GL2 gl = drawable.getGL().getGL2();
            shader = new GLSLShader();

            // Load texture
            BufferedImage img = null;
            ByteBuffer pixels = null;
            try
            {
                img = ImageIO.read(new File("img/brick_grise.jpg"));
                DataBufferByte dbb = (DataBufferByte) img.getRaster().getDataBuffer();
                byte[] data = dbb.getData();
                pixels = Buffers.newDirectByteBuffer(data.length);
                pixels.put(data);
                pixels.flip();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }

              // Generate one texture objects
              gl.glGenTextures(1, texName);
              if (img != null) 
              {
                gl.glBindTexture(GL2.GL_TEXTURE_2D, texName.get(0));
                gl.glTexParameteri(GL2.GL_TEXTURE_2D,GL2.GL_TEXTURE_MAG_FILTER,GL2.GL_LINEAR);
                gl.glTexParameteri(GL2.GL_TEXTURE_2D,GL2.GL_TEXTURE_MIN_FILTER,GL2.GL_LINEAR);
                gl.glTexImage2D(GL2.GL_TEXTURE_2D,0,GL2.GL_RGB, img.getWidth(),img.getHeight(),0, GL2.GL_RGB,GL2.GL_UNSIGNED_BYTE, pixels);
              }

              // Création des shaders
              shader.LoadFromString(gl, GL2.GL_VERTEX_SHADER, "vertex.glsl");
              shader.LoadFromString(gl, GL2.GL_FRAGMENT_SHADER, "fragment.glsl");
              shader.CreateAndLinkProgram(gl);
              shader.AddAttribute(gl,"position");
              shader.AddAttribute(gl,"v_texcoords");
              shader.AddUniform(gl,"texture0");
              shader.Use(gl);

              // Créations des buffers
              float positions[] = {0,0,1, 1,0,1, 1,1,1, 0,1,1,  
                                     0,1,1, 1,1,1, 1,1,0, 0,1,0, 
                                     1,0,0, 0,0,0, 0,1,0, 1,1,0,  
                                     0,0,0, 1,0,0, 1,0,1, 0,0,1,  
                                     0,0,0, 0,0,1, 0,1,1, 0,1,0, 
                                     1,0,1, 1,0,0, 1,1,0, 1,1,1}; 
              FloatBuffer bufPos = Buffers.newDirectFloatBuffer(positions);

              int    indices[] = {    0, 1, 2,   2, 3, 0, 
                                      4,  5, 6,   6, 7, 4, 
                                      8,  9,10,  10,11, 8,  
                                      12,13,14,  14,15,12,  
                                      16,17,18,  18,19,16,  
                                      20,21,22,  22,23,20}; 
              IntBuffer bufInd = Buffers.newDirectIntBuffer(indices);

              float texcoords[] = {0,0, 1,0, 1,1, 0,1, 
                                    0,0, 1,0, 1,1, 0,1,
                                    0,0, 1,0, 1,1, 0,1,
                                    0,0, 1,0, 1,1, 0,1, 
                                    0,0, 1,0, 1,1, 0,1,
                                    0,0, 1,0, 1,1, 0,1};
              FloatBuffer bufCoord = Buffers.newDirectFloatBuffer(texcoords);




              gl.glGenBuffers(1, vbo_positions);
              gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vbo_positions.get(0));
              gl.glBufferData(GL2.GL_ARRAY_BUFFER, bufPos.capacity(), bufPos, GL2.GL_STATIC_DRAW);

              gl.glGenBuffers(1, vbo_texCoords);
              gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vbo_texCoords.get(0));
              gl.glBufferData(GL2.GL_ARRAY_BUFFER, bufCoord.capacity(), bufCoord, GL2.GL_STATIC_DRAW);


              gl.glGenBuffers(1, vbo_indices);
              gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, vbo_indices.get(0));
              gl.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, bufInd.capacity(), bufInd, GL2.GL_STATIC_DRAW);

              gl.glGenVertexArrays(1, vao);
              gl.glBindVertexArray(vao.get(0));

              gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vbo_positions.get(0));
              gl.glVertexAttribPointer(shader.getAttribute("position"), 3, GL2.GL_FLOAT, false, 0, 0);
              gl.glEnableVertexAttribArray(shader.getAttribute("position"));

              gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vbo_texCoords.get(0));
              gl.glEnableVertexAttribArray(shader.getAttribute("v_texcoords"));
              gl.glVertexAttribPointer(shader.getAttribute("v_texcoords"), 2, GL2.GL_FLOAT, false, 0, 0);

              gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, vbo_indices.get(0));


              gl.glBindVertexArray(0);
              gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0);
              gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, 0);

              gl.glClearColor(0.5f, 0.3f, 0.3f, 1.0f);
              gl.glEnable(GL2.GL_DEPTH_TEST);
        }
        @Override
        public void dispose(GLAutoDrawable drawable)
        {
            /* nothing */
        }

        @Override
        public void display(GLAutoDrawable drawable)
        {
              GL2 gl = drawable.getGL().getGL2();

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

              gl.glUniform1ui(shader.getUniform("texture0"), 0);

              gl.glBindVertexArray(vao.get(0));

              gl.glActiveTexture(GL2.GL_TEXTURE0);
              gl.glBindTexture(GL2.GL_TEXTURE_2D, texName.get(0));
              gl.glDrawElements(GL2.GL_TRIANGLES, 36, GL2.GL_UNSIGNED_INT, 0);

              gl.glBindVertexArray(0);
              drawable.swapBuffers();
        }
        @Override
        public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
        {
              GL2 gl = drawable.getGL().getGL2();
              gl.glViewport(0, 0, width, height);
        }

    }

感谢' S

0 个答案:

没有答案