This demo is exercise #2 in the book “Computer Graphics Programming” by Gordon & Clevenger, Third Edition. It uses openGL source code and is supposed to create a single point, 30 pixels in size, and shade it a blue color. Unfortunately it does not do this, for reasons unknown to me. I can do the same thing in Processing using a shader with a lot less code, but would like to get this demo to run correctly. There is a very long error message of unknown significance which the Processing editor will not allow me to copy/paste so I’m also attaching a screen shot of the first few lines. The code does run and I see a window with a black background but no blue point. Any enlightenment would be appreciated.
import javax.swing.*;
import processing.opengl.PGraphicsOpenGL;
import com.jogamp.opengl.GL4;
import com.jogamp.opengl.*;
import com.jogamp.opengl.awt.GLCanvas;
GLAutoDrawable drawable;
GLCanvas myCanvas;
final int GL_COLOR_BUFFER_BIT = 0x00004000;
final int GL_POINTS = 0x0000;
final int GL_VERTEX_SHADER = 35633;
final int GL_FRAGMENT_SHADER = 35632;
String[] vshaderSource = new String[3];
String[] fshaderSource = new String[4];
class Wnd implements GLEventListener {
int renderingProgram;
int vao[] = new int[1];
Wnd() {
JFrame frame = new JFrame("OpenGL Demo");
frame.setBounds(200, 200, 600, 400);
myCanvas = new GLCanvas();
myCanvas.addGLEventListener(this);
frame.add(myCanvas);
frame.setVisible(true);
}
void display(GLAutoDrawable drawable) {
GL4 gl = (GL4)GLContext.getCurrentGL();
gl.glUseProgram(renderingProgram);
gl.glDrawArrays(GL_POINTS, 0, 1);
// gl.glClearColor(1.0, 0.0, 0.0, 1.0);
// gl.glClear(GL_COLOR_BUFFER_BIT);
gl.glPointSize(30.0f);
}
void init(GLAutoDrawable drawable) {
GL4 gl = (GL4)GLContext.getCurrentGL();
renderingProgram = createShaderProgram();
gl.glGenVertexArrays(vao.length, vao, 0);
gl.glBindVertexArray(vao[0]);
}
void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
}
void dispose(GLAutoDrawable drawable) {
}
int createShaderProgram() {
GL4 gl = (GL4)GLContext.getCurrentGL();
String vshaderSource[] = {
"#version 410 \n",
"void main(void) \n",
"{gl_Position = vec4(0.0,0.0,0.0,1.0);} \n",
};
String fshaderSource[] = {
"#version 410 \n",
"out vec4 color; \n",
"void main(void) \n",
"{color = vec4(0.0,0.0,1.0,1.0);} \n",
};
int vShader = gl.glCreateShader(GL_VERTEX_SHADER);
gl.glShaderSource(vShader, 3, vshaderSource, null, 0);
gl.glCompileShader(vShader);
int fShader = gl.glCreateShader(GL_FRAGMENT_SHADER);
gl.glShaderSource(fShader, 4, fshaderSource, null, 0);
gl.glCompileShader(fShader);
int vfProgram = gl.glCreateProgram();
gl.glAttachShader(vfProgram, vShader);
gl.glAttachShader(vfProgram, fShader);
gl.glLinkProgram(vfProgram);
gl.glDeleteShader(vShader);
gl.glDeleteShader(fShader);
return vfProgram;
}
}
void setup() {
surface.setVisible(false);
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
Wnd wnd = new Wnd();
wnd.display(drawable); // Builds components on EventDispatchThread
}
}
);
}
Part of Error Message: