Ah, that seems to have cleared up a lot of confusion. Thank you very much.
I am attempting to use GLSL shaders to map the texture but believe I’m missing something:
init:
// Allocate my buffer to hold my two triangles to make up a quad
FloatBuffer buffer = FloatBuffer.allocate(18); {
buffer.put(-1.0f); // Top-Left
buffer.put(1.0f);
buffer.put(0.0f);
buffer.put(1.0f);
buffer.put(1.0f);
buffer.put(0.0f);
buffer.put(-1.0f);
buffer.put(-1.0f);
buffer.put(0.0f);
buffer.put(-1.0f);
buffer.put(-1.0f);
buffer.put(0.0f);
buffer.put(1.0f);
buffer.put(-1.0f);
buffer.put(0.0f);
buffer.put(1.0f);
buffer.put(1.0f);
buffer.put(0.0f);
buffer.rewind();
}
vertexes = new int[1];
gl.glGenBuffersARB(1, vertexes, 0); // Generate a buffer id
gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, vertexes[0]); // Bind the buffer
gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, buffer.capacity() * 4, buffer, GL.GL_STATIC_DRAW_ARB); // Send the vertex buffer data to the video card
// Create and bind texture
textures = new int[1];
gl.glGenTextures(1, textures, 0);
gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);
try {
BufferedImage image = ImageIO.read(getClass().getClassLoader().getResource("resource/crate.png"));
DataBufferByte dbb = (DataBufferByte)image.getRaster().getDataBuffer();
byte[] data = dbb.getData();
ByteBuffer pixels = BufferUtil.newByteBuffer(data.length);
pixels.put(data);
pixels.flip();
gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, 256, 256, 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, pixels);
} catch(Throwable t) {
t.printStackTrace();
}
// Load the GLSL texturing functionality
programId = gl.glCreateProgramObjectARB();
// Vertex Shader
vertexShaderId = gl.glCreateShaderObjectARB(GL.GL_VERTEX_SHADER_ARB);
String source = getShaderSource("texture_coordinates.vert");
gl.glShaderSourceARB(vertexShaderId, 1, new String[] {source}, new int[] {source.length()}, 0);
gl.glCompileShaderARB(vertexShaderId);
int[] result = new int[1];
gl.glGetShaderiv(vertexShaderId, GL.GL_COMPILE_STATUS, result, 0);
if (result[0] != GL.GL_TRUE) {
throw new RuntimeException("Compile of GLSL Shader failed!");
}
gl.glAttachObjectARB(programId, vertexShaderId);
// Fragment Shader
fragmentShaderId = gl.glCreateShaderObjectARB(GL.GL_FRAGMENT_SHADER_ARB);
source = getShaderSource("texture_coordinates.frag");
gl.glShaderSourceARB(fragmentShaderId, 1, new String[] {source}, new int[] {source.length()}, 0);
gl.glCompileShaderARB(fragmentShaderId);
result = new int[1];
gl.glGetShaderiv(fragmentShaderId, GL.GL_COMPILE_STATUS, result, 0);
if (result[0] != GL.GL_TRUE) {
throw new RuntimeException("Compile of GLSL Shader failed!");
}
gl.glAttachObjectARB(programId, fragmentShaderId);
// Link program
gl.glLinkProgramARB(programId);
gl.glGetProgramiv(programId, GL.GL_LINK_STATUS, result, 0);
if (result[0] != GL.GL_TRUE) {
throw new RuntimeException("Linking of GLSL Shader failed!");
}
gl.glUseProgramObjectARB(programId);
int texture0 = gl.glGetUniformLocationARB(programId, "texture0");
gl.glUniform1iARB(texture0, 0);
draw:
gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f); // Make it white
gl.glEnableClientState(GL.GL_VERTEX_ARRAY); // Enable vertex arrays
gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, vertexes[0]); // Bind the vertex buffer by id
gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0); // Tell where to start on the vertex buffer
gl.glDrawArrays(GL.GL_TRIANGLES, 0, 6); // Draw the vertex buffer data to the screen
gl.glDisableClientState(GL.GL_VERTEX_ARRAY); // Disable vertex arrays
Everything loads and the GLSL doesn’t throw any errors. The shaders are as follows:
texture_coordinates.vert:
#version 130
uniform mat4 projection_matrix;
uniform mat4 modelview_matrix;
in vec3 a_Vertex;
in vec3 a_Color;
in vec2 a_TexCoord0;
out vec4 color;
out vec2 texCoord0;
void main(void) {
texCoord0 = a_TexCoord0;
color = vec4(a_Color, 1.0);
vec4 pos = modelview_matrix * vec4(a_Vertex, 1.0);
gl_Position = projection_matrix * pos;
}
texture_coordinates.frag:
#version 130
uniform sampler2D texture0;
in vec4 color;
in vec2 texCoord0;
out vec4 outColor;
void main(void) {
outColor = color * texture(texture0, texCoord0.st);
}