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). I guess the problem is the method “glDrawArrays()”. Please take a look at the code below. Great thanks for any help!!
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" +
" gl_FragColor = 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
}
});
}
}