superbible sample code ported to JOGL, yet not working

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

superbible sample code ported to JOGL, yet not working

a_shuang
This post was updated on .
I wrote some sample code from superbible to JOGL, but it did not work(there should be a triangle, yet there is nothing but only green background). Please take a look at the code below. Great thanks for any help!!

Original code from superbible: singletri.cpp

import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.nio.IntBuffer;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import com.jogamp.opengl.util.FPSAnimator;
import com.jogamp.opengl.util.GLBuffers;


public class TestVerAttributes extends GLCanvas implements GLEventListener
{
   private static final long serialVersionUID = 1L;
   
   // Define constants for the top-level container
   private static String TITLE = "Test glsl";
   private static final int CANVAS_WIDTH = 320;  // width of the drawable
   private static final int CANVAS_HEIGHT = 240; // height of the drawable
   private static final int FPS = 60; // animator's target frames per second
   
   private IntBuffer vaoNameBuff = GLBuffers.newDirectIntBuffer(1);
   private int shaderProgram;
   private float green[] = { 0.0f, 0.25f, 0.0f, 1.0f };
   
   //private float temp;
   
   public TestVerAttributes()
   {
      addGLEventListener(this);
   }
   
   public void init(GLAutoDrawable drawable)
   {
      GL2 gl = drawable.getGL().getGL2();
     
      initShaders(gl);
   }
   
   public void initShaders(GL2 gl)
   {
      int verShader = gl.glCreateShader(GL2.GL_VERTEX_SHADER);
      int fraShader = gl.glCreateShader(GL2.GL_FRAGMENT_SHADER);
     
      String[] vsrc =
                        {
                                "#version 400 core \n" +
                                "void main(void) \n" +
                                "{        \n" +
                                " const vec4 vertices[] = vec4[](vec4(0.25, -0.25, 0.5, 1), \n" +
                                " vec4(-0.25, -0.25, 0.5, 1), \n" +
                                " vec4( 0.25, 0.25, 0.5, 1)); \n" +
                                " gl_Position = vertices[gl_VertexID]; \n" +
                                "}"
                        };
     
      gl.glShaderSource(verShader, 1, vsrc, null);
      gl.glCompileShader(verShader);
     
      String[] fsrc =
                        {
                                "#version 400 core \n" +
                                "void main( void) \n" +
                                "{ \n" +
                                " color = vec4(0.0, 0.8, 1.0, 1.0) \n" +
                                "}"
                        };
     
      gl.glShaderSource(fraShader, 1, fsrc, null);
      gl.glCompileShader(fraShader);
     
      shaderProgram = gl.glCreateProgram();
      gl.glAttachShader(shaderProgram, verShader);
      gl.glAttachShader(shaderProgram, fraShader);
      gl.glLinkProgram(shaderProgram);
      gl.glValidateProgram(shaderProgram);
     
      gl.glGenVertexArrays(1, vaoNameBuff);
      gl.glBindVertexArray(vaoNameBuff.get(0));
   }
   
   public void dispose(GLAutoDrawable drawable)
   {
      GL2 gl = drawable.getGL().getGL2();
     
      gl.glDeleteVertexArrays(1, vaoNameBuff);
      gl.glDeleteProgram(shaderProgram);
   }
   
   public void display(GLAutoDrawable drawable)
   {
      GL2 gl = drawable.getGL().getGL2();
     
      /*float attrib[] = {
            (float) Math.sin(temp) * 0.5f,
            (float) Math.cos(temp) * 0.6f,
            0.0f, 0.0f
            };
     
      if (temp < 3.14f)
      {
         temp += 0.01f;
      }
      else
      {
         temp = 0f;
      }*/
     
      gl.glClearBufferfv(GL2.GL_COLOR, 0, green, 0);
      gl.glUseProgram(shaderProgram);
      //gl.glVertexAttrib4fv(0, attrib, 0);
      gl.glDrawArrays(GL.GL_TRIANGLES, 0, 3);
   }
   
   public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
   {
     
   }
   
   /** The entry main() method to setup the top-level container and animator */
   public static void main(String[] args)
   {
      // Run the GUI codes in the event-dispatching thread for thread safety
      SwingUtilities.invokeLater(new Runnable()
      {
         public void run()
         {
            // Create the OpenGL rendering canvas
               GLCanvas canvas = new TestVerAttributes();
               canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));

               // Create a animator that drives canvas' display() at the specified FPS.
               final FPSAnimator animator = new FPSAnimator(canvas, FPS, true);
               
               // Create the top-level container
               final JFrame frame = new JFrame(); // Swing's JFrame or AWT's Frame
               frame.getContentPane().add(canvas);
               frame.addWindowListener(new WindowAdapter()
               {
                  public void windowClosing(WindowEvent e)
                  {
                     // Use a dedicate thread to run the stop() to ensure that the
                     // animator stops before program exits.
                     new Thread()
                     {
                        public void run()
                        {
                           if (animator.isStarted())
                           {
                              animator.stop();
                           }
                           System.exit(0);
                        }
                     }.start();
                  }
               });
               frame.setTitle(TITLE);
               frame.pack();
               frame.setVisible(true);
               animator.start(); // start the animation loop
         }
      });
   }
}
Reply | Threaded
Open this post in threaded view
|

Re: superbible sample code ported to JOGL, yet not working

jmaasing
Look at the Arcsynthesis tutorial at
http://www.arcsynthesis.org/gltut/index.html

Specifically I think this will explain how it works:
http://www.arcsynthesis.org/gltut/Basics/Tut01%20Following%20the%20Data.html
Reply | Threaded
Open this post in threaded view
|

Re: superbible sample code ported to JOGL, yet not working

a_shuang
Thank you for your answer, I read that part you posted. But I still cannot find what's wrong. Maybe it's a stupid mistake, or some tiny thing is missed.
Reply | Threaded
Open this post in threaded view
|

Re: superbible sample code ported to JOGL, yet not working

jmaasing
Check the documentation for glDrawArrays:
https://www.opengl.org/sdk/docs/man/html/glDrawArrays.xhtml

"When glDrawArrays is called, it uses count sequential elements from each enabled array to construct a sequence of geometric primitives"

So you need to set up and enable vertex arrays, I don't see that in your code. Since the vertex shader has some vertices hard-coded I guess there might be some simplification in the example but that is not the usual way to draw elements.
It is all explained in the arcsynthesis tutorial. Sample 1.2 here : http://www.arcsynthesis.org/gltut/Basics/Tut01%20Following%20the%20Data.html
I don't know how to explain it better, there are explicit lines of code in the tutorial for glBufferData and glEnableVertexAttribArray
Reply | Threaded
Open this post in threaded view
|

Re: superbible sample code ported to JOGL, yet not working

a_shuang
Finally I fixed it. You are right. Thank you for your help.
By the way, I found some materials really useful for those who use JOGL + GL3,
at http://forum.jogamp.org/JOGL2-OpenGL-3-3-td4029102.html
where someone uploaded his JOGL source code converted from an openGL & C++ example, which really helps.
Reply | Threaded
Open this post in threaded view
|

Re: superbible sample code ported to JOGL, yet not working

a_shuang
In reply to this post by jmaasing
I just posted the original code from superbible 6th. It does not use any buffer, please take a look, thanks.