thanks for the suggestions, but the geometry shader still doesn’t work
As for your questions,
- the shader code compiled and linked succesfully.
- The shader program is in a single string with a “\n” after each line.
I didn’t mention it in my previously, but its only the geometry shader that is not working. The vertex and fragment shaders work fine.
(Actually when using the geometry shader, there is no output on the screen, not even the original line that i draw).
EDIT: When I attach only the geometry shader ( and not the vertex and fragment shader) there is a link error, but when all three shaders are attached then, the shader program successfully links. Also, the infostring is emty, so I can’t figure out what the error is.
I am attaching my entire code, may be something is wrong with it.
package hope.it.works;
import java.awt.BorderLayout;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.charset.Charset;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.swing.JFrame;
import com.sun.opengl.util.Animator;
import com.sun.opengl.util.BufferUtil;
public class Shader extends JFrame implements GLEventListener, WindowListener {
private static final long serialVersionUID = 1L;
boolean first = true;
int list;
int no;
double[] x, y, z, a;
public Shader() {
super("Test Shader");
setLayout(new BorderLayout());
addWindowListener(this);
setSize(850, 850);
setVisible(true);
setupJOGL();
String s = "0.00";
noFormat = new DecimalFormat(s);
}
private void setupJOGL() {
GLCapabilities caps = new GLCapabilities();
caps.setDoubleBuffered(true);
caps.setHardwareAccelerated(true);
GLCanvas canvas = new GLCanvas(caps);
canvas.addGLEventListener(this);
add(canvas, BorderLayout.CENTER);
Animator anim = new Animator(canvas);
anim.start();
}
/**
* @param args
*/
public static void main(String[] args) {
new Shader().setVisible(true);
}
public void display(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
gl.glLoadIdentity();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glColor3f(1, 0, 0);
gl.glBegin(GL.GL_LINES);
gl.glVertex2d(0, -d);
gl.glVertex2d(0, d);
gl.glEnd();
updateFPS();
}
public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {
// TODO Auto-generated method stub
}
public void init(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
gl.glClearColor(0, 0, 0, 0);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(0, 1, 0, 1, -1, 1);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
setupShaders(gl);
}
int p;
int v;
int f;
int g;
private void setupShaders(GL gl) {
v = gl.glCreateShader(GL.GL_VERTEX_SHADER);
f = gl.glCreateShader(GL.GL_FRAGMENT_SHADER);
g = gl.glCreateShader(GL.GL_GEOMETRY_SHADER_EXT);
String[] vs = new String[1];
vs[0] = readShaderFile("glsl/toon.vert");
String[] gs = new String[1];
gs[0] = readShaderFile("glsl/toon.geom");
String[] fs = new String[1];
fs[0] = readShaderFile("glsl/toon.frag");
int[] vb = new int[] { vs[0].length() };
gl.glShaderSource(v, 1, vs, vb, 0);
int[] fb = new int[] { fs[0].length() };
gl.glShaderSource(f, 1, fs, fb, 0);
int[] gb = new int[] { gs[0].length() };
gl.glShaderSource(g, 1, gs, gb, 0);
gl.glCompileShader(v);
checkCompileError(gl, v);
gl.glCompileShader(f);
checkCompileError(gl, f);
gl.glCompileShader(g);
checkCompileError(gl, g);
p = gl.glCreateProgram();
gl.glAttachShader(p, f);
gl.glAttachShader(p, v);
gl.glAttachShader(p, g);
gl
.glProgramParameteriEXT(g, GL.GL_GEOMETRY_INPUT_TYPE_EXT,
GL.GL_LINES);
gl.glProgramParameteriEXT(g, GL.GL_GEOMETRY_OUTPUT_TYPE_EXT,
GL.GL_LINE_STRIP);
int[] temp = new int[2];
gl.glGetIntegerv(GL.GL_MAX_GEOMETRY_OUTPUT_VERTICES_EXT, temp, 0);
gl.glProgramParameteriEXT(g, GL.GL_GEOMETRY_VERTICES_OUT_EXT, temp[0]);
gl.glLinkProgram(p);
checkLinkAndValidationErrors(gl, p);
gl.glUseProgram(p);
}
private void checkCompileError(GL gl, int id) {
IntBuffer status = BufferUtil.newIntBuffer(1);
gl.glGetShaderiv(id, GL.GL_COMPILE_STATUS, status);
if (status.get() == GL.GL_FALSE) {
getInfoLog(gl, id);
} else {
System.out.println("Successfully compiled shader " + id);
}
}
private void getInfoLog(GL gl, int id) {
IntBuffer infoLogLength = BufferUtil.newIntBuffer(1);
gl.glGetShaderiv(id, GL.GL_INFO_LOG_LENGTH, infoLogLength);
ByteBuffer infoLog = BufferUtil.newByteBuffer(infoLogLength.get(0));
gl.glGetShaderInfoLog(id, infoLogLength.get(0), null, infoLog);
String infoLogString = Charset.forName("US-ASCII").decode(infoLog)
.toString();
throw new Error("Shader compile error\n" + infoLogString);
}
private void checkLinkAndValidationErrors(GL gl, int id) {
IntBuffer status = BufferUtil.newIntBuffer(1);
gl.glGetProgramiv(id, GL.GL_LINK_STATUS, status);
if (status.get() == GL.GL_FALSE) {
getInfoLog(gl, id);
} else {
status.rewind();
gl.glValidateProgram(id);
gl.glGetProgramiv(id, GL.GL_VALIDATE_STATUS, status);
if (status.get() == GL.GL_FALSE) {
getInfoLog(gl, id);
} else {
System.out.println("Successfully linked program " + id);
}
}
}
private String readShaderFile(String file) {
String s = "";
try {
BufferedReader r = new BufferedReader(new FileReader(file));
String t = r.readLine();
while (t != null) {
s += t;
s += "\n";
t = r.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}
return s;
}
int w;
int h;
int d;
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
int height) {
GL gl = drawable.getGL();
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
w = width / 2;
h = height / 2;
gl.glOrtho(-1 * w, w, -1 * h, h, -600, 600);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
d = Math.min(w, h);
}
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent e) {
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
private double stTime;
private double enTime;
private double frameCt;
private NumberFormat noFormat;
private void updateFPS() {
enTime = System.currentTimeMillis();
double diff = enTime - stTime;
frameCt++;
if (diff > 1000) {
double fps = frameCt * 1000 / diff;
stTime = enTime;
String s = noFormat.format(fps);
this.setTitle("Name [FPS: " + s + "]");
frameCt = 0;
}
}
}
The shaders im using are as follows
The vertex shader
void main()
{
//Transform the vertex (ModelViewProj matrix)
gl_Position = ftransform();
}
The fragment shader
void main()
{
//Shade to blue!
gl_FragColor = vec4(0.0,0.0,1.0,1.0);
}
I had already posted the geometry shader code in my initial post.