So, after borrowing the shader code from http://www.cse.ohio-state.edu/~kerwin/refraction.html (I asked for permission already), I attempted to implement it via OpenGL.
Bam! I hit snag 1. I cannot figure out how to get OpenGL to render correctly. I can use the shader code in rendermonkey just fine, which tells me that the problem is at my end. I’m probably passing the textures incorrectly.
my init:
GL2 gl = drawable.getGL().getGL2();
gl.glShadeModel(GL2.GL_SMOOTH);
gl.glEnable(GL2.GL_DEPTH_TEST);
gl.glClearColor(0, 0, 0, 1);
glu = new GLU();
glut = new GLUT();
textures = new Texture[6];
TextureData[] data = new TextureData[6];
try {
for (int i = 0; i < textures.length; i++) {
data[i] = TextureIO.newTextureData(drawable.getGLProfile(), new URL(base + textnames[i]), false, "jpg");
textures[i] = TextureIO.newTexture(data[i]);
}
int[] temp = new int[1];
gl.glGenTextures(1, temp, 0);
texname = temp[0];
gl.glBindTexture(GL2.GL_TEXTURE_CUBE_MAP, texname);
gl.glTexParameteri(GL2.GL_TEXTURE_CUBE_MAP, GL2.GL_TEXTURE_WRAP_S, GL2.GL_REPEAT);
gl.glTexParameteri(GL2.GL_TEXTURE_CUBE_MAP, GL2.GL_TEXTURE_WRAP_T, GL2.GL_REPEAT);
gl.glTexParameteri(GL2.GL_TEXTURE_CUBE_MAP, GL2.GL_TEXTURE_WRAP_R, GL2.GL_REPEAT);
gl.glTexParameteri(GL2.GL_TEXTURE_CUBE_MAP, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_NEAREST);
gl.glTexParameteri(GL2.GL_TEXTURE_CUBE_MAP, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_NEAREST);
int[] type = { GL2.GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL2.GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
GL2.GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL2.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
GL2.GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL2.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
};
for (int i = 0; i < data.length; i++)
gl.glTexImage2D(type[i], 0, data[i].getInternalFormat(), data[i].getWidth(),
data[i].getHeight(), 0, data[i].getPixelFormat(), data[i].getPixelType(),
data[i].getBuffer());
gl.glTexGeni(GL2.GL_S, GL2.GL_TEXTURE_GEN_MODE, GL2.GL_NORMAL_MAP);
gl.glTexGeni(GL2.GL_T, GL2.GL_TEXTURE_GEN_MODE, GL2.GL_NORMAL_MAP);
gl.glTexGeni(GL2.GL_R, GL2.GL_TEXTURE_GEN_MODE, GL2.GL_NORMAL_MAP);
gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_MODULATE);
gl.glGenerateMipmap(GL2.GL_TEXTURE_CUBE_MAP);
} catch (IOException exc) {
textures = null;
exc.printStackTrace();
}
int v = gl.glCreateShader(GL2.GL_VERTEX_SHADER);
int f = gl.glCreateShader(GL2.GL_FRAGMENT_SHADER);
try {
TextureData text = TextureIO.newTextureData(drawable.getGLProfile(), new File("glassbump.png"), false, "png");
bumpMap = TextureIO.newTexture(text);
BufferedReader brv = new BufferedReader(new FileReader("water.vert.txt"));
String vsrc = "";
String line;
while ((line=brv.readLine()) != null) {
vsrc += line + "
";
}
String[] src = new String[1];
src[0] = vsrc;
gl.glShaderSource(v, 1, src, null);
gl.glCompileShader(v);
BufferedReader brf = new BufferedReader(new FileReader("regular_water.frag.txt"));
String fsrc = "";
while ((line=brf.readLine()) != null) {
fsrc += line + "
";
}
src = new String[1];
src[0] = fsrc;
gl.glShaderSource(f, 1, src, null);
gl.glCompileShader(f);
checkLogInfo(gl, shaderprogram);
shaderprogram = gl.glCreateProgram();
gl.glAttachShader(shaderprogram, v);
gl.glAttachShader(shaderprogram, f);
gl.glLinkProgram(shaderprogram);
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
//enviro = new Cube(textures);
gl.glUseProgram(shaderprogram);
gl.glActiveTexture(GL2.GL_TEXTURE0);
int loc = gl.glGetUniformLocation(shaderprogram, "Texture");
gl.glBindTexture(GL2.GL_TEXTURE_2D, bumpMap.getTextureObject());
gl.glUniform1i(loc, 0);
gl.glActiveTexture(GL2.GL_TEXTURE1);
gl.glBindTexture(GL2.GL_TEXTURE_2D, textures[0].getTextureObject());
loc = gl.glGetUniformLocation(shaderprogram, "Environment");
gl.glUniform1i(loc, 1);
gl.glUniform(new GLUniformData("refraction_index", (float)1.4));
gl.glValidateProgram(shaderprogram);
checkLogInfo(gl, shaderprogram);
gl.glUseProgram(shaderprogram);
My drawing func:
GL2 gl = drawable.getGL().getGL2();
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(90, width/height, near, far);
glu.gluLookAt(object_pos*java.lang.Math.sin(theta), 0, object_pos*java.lang.Math.cos(theta), 0, 0, 0, 0, 1, 0);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
glut.glutSolidSphere(1, 50, 50);
gl.glFlush();
drawable.swapBuffers();
gl.glFinish();
The Environment variable is of type samplerCube and the Texture variable is of type sampler2D.
What I have done so far:
- I tested the textures: that is I checked if the textures were read in to opengl correctly by texturing some simple objects. The textures were fine.
- I checked if the files were read in correctly by modifying the frag shader to auto assign the pixel to red. It worked. So the shaders were read in and validated fine.
I’m at an impasse. I’m fairly certain that I either initialized the shader incorrectly or mapped the texture incorrectly or something to that effect. I just can’t seem to figure that quite out.
Also, I’m fairly confident that to return to the fixed pipeline, I specify gl.glUseProgram(0). But that still disables texturing for objects even when I try gl.glEnable(GL2.GL_TEXTURE_2D) again!
Is there a step I’m missing?
I’m sorry for the long post, and would be grateful for any help you may be able to provide.