I am currently trying to get a shder to create a brick like texture on a teapot.
I can render a teapot fine using the GLUnit tools but as soon as I add the shaders the teapot just disapears.
The frag and vertex shaders were a copy paste from the orange book so they should be fine and I dont get any errors in the shader log, so at the moment I hit a wall and not sure what I have done wrong.
public class SimpleJOGL implements GLEventListener {
private int shaderprogram = 0;
private GLUT glut;
private String vertsrc = "VertexShader.vert";
private String fragsrc = "FragmentShader.frag";
public static void main(String[] args) {
Frame frame = new Frame("Simple JOGL Application");
GLCanvas canvas = new GLCanvas();
[....]
// Center frame
frame.setLocationRelativeTo(null);
frame.setVisible(true);
animator.start();
}
public void init(GLAutoDrawable drawable) {
// Use debug pipeline
drawable.setGL(new DebugGL(drawable.getGL()));
GL gl = drawable.getGL();
glut = new GLUT();
System.err.println("INIT GL IS: " + gl.getClass().getName());
// Enable VSync
gl.setSwapInterval(1);
// Setup the drawing area and shading mode
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
//gl.glShadeModel(GL.GL_SMOOTH); // try setting this to GL_FLAT and see what happens.
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glDepthFunc(GL.GL_LESS);
gl.glEnable(GL.GL_TEXTURE_GEN_S);
gl.glEnable(GL.GL_TEXTURE_1D);
gl.glEnable(GL.GL_CULL_FACE);
gl.glEnable(GL.GL_LIGHTING);
gl.glEnable(GL.GL_LIGHT0);
gl.glEnable(GL.GL_AUTO_NORMAL);
gl.glEnable(GL.GL_NORMALIZE);
gl.glFrontFace(GL.GL_CW);
gl.glCullFace(GL.GL_BACK);
gl.glMaterialf(GL.GL_FRONT, GL.GL_SHININESS, 64.0f);
setupShaders(gl);// comment this out and u can see the teapot
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
[.. usual ..]
}
public void display(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
// Clear the drawing area
//gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
// Reset the current matrix to the "identity"
//gl.glLoadIdentity();
// Move the "drawing cursor" around
gl.glTranslatef(-1.5f, 0.0f, -6.0f);
//gl.glPushMatrix();
glut.glutSolidTeapot(1.0f);
// Flush all drawing operations to the graphics card
gl.glFlush();
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
}
private void setupShaders(GL gl) {
int v = gl.glCreateShader(GL.GL_VERTEX_SHADER);
int f = gl.glCreateShader(GL.GL_FRAGMENT_SHADER);
if (shaderprogram == 0) {
System.out.println("initializing shaders");
try {
String[] vertexsource = {stringifyfile(vertsrc)};
String[] fragmentsource = {stringifyfile(fragsrc)};
gl.glShaderSource(v, 1, vertexsource, (int[]) null, 0);
gl.glCompileShader(v);
checkLogInfo(gl, v);
gl.glShaderSource(f, 1, fragmentsource, (int[]) null, 0);
gl.glCompileShader(f);
checkLogInfo(gl, f);
shaderprogram = gl.glCreateProgram();
gl.glAttachShader(shaderprogram, v);
gl.glAttachShader(shaderprogram, f);
gl.glLinkProgram(shaderprogram);
gl.glValidateProgram(shaderprogram);
checkLogInfo(gl, shaderprogram);
} catch (FileNotFoundException e) {
System.err.println("Shader source file not found." + e);
} catch (IOException e) {
System.err.println("Could not read shader file.");
}
}
gl.glUseProgram(shaderprogram);
checkLogInfo(gl, shaderprogram);
checkLogInfo(gl, v);
checkLogInfo(gl, f);
}
private String stringifyfile(String filename) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(filename));
String res = "";
String line;
while ((line = br.readLine()) != null) {
res += line + "\n";
}
return res;
}
private void checkLogInfo(GL gl, int obj) {
IntBuffer iVal = BufferUtil.newIntBuffer(1);
gl.glGetObjectParameterivARB(obj, GL.GL_OBJECT_INFO_LOG_LENGTH_ARB, iVal);
int length = iVal.get();
if (length <= 1) {
return;
}
ByteBuffer infoLog = BufferUtil.newByteBuffer(length);
iVal.flip();
gl.glGetInfoLogARB(obj, length, iVal, infoLog);
byte[] infoBytes = new byte[length];
infoLog.get(infoBytes);
System.out.println("GLSL Validation >> " + new String(infoBytes));
}
}