So I have been creating a game and I want to support opengl version 2.1 with shaders. I implemented what I could according to tutorials online and when I run the game nothing shows up, Note: If the shaders are changed to the latest version I support everything works fine.. (I use VBOs)
Here are the shaders:
Fragment shader:
uniform sampler2D texture1;
varying vec4 pass_Color;
varying vec2 pass_TextureCoord;
void main(void) {
gl_FragColor = pass_Color;
vec2 texcoord = vec2(pass_TextureCoord.xy);
vec4 color = texture2D(texture1, texcoord) * pass_Color ;
gl_FragColor = color;
}
*Vertex shader: *
attribute vec4 in_Position;
attribute vec4 in_Color;
attribute vec2 in_TextureCoord;
varying vec4 pass_Color;
varying vec2 pass_TextureCoord;
uniform vec4 cameraPos;
uniform mat4 projection;
void main(void) {
gl_Position = ( (vec4(cameraPos.x*projection[0][0],cameraPos.y*projection[1][1],cameraPos.z*projection[0][0],cameraPos.w*projection[0][0])) + (in_Position * projection)) ;
pass_Color = in_Color;
pass_TextureCoord = in_TextureCoord;
}
Note: In the vertex shader I calculate the position, this is correct for sure because I use this exact line to calculate position in the newer shader and it works great.
How I create the VBOs:
vboId = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vboData, GL15.GL_STATIC_DRAW);
// Put the position coordinates in attribute list 0
GL20.glVertexAttribPointer(GL20.glGetAttribLocation(game.getResourceManager().getShaderProgramID(), "in_Position"), TexturedVertex.positionElementCount, GL11.GL_FLOAT,
false, TexturedVertex.stride, TexturedVertex.positionByteOffset);
// Put the color components in attribute list 1
GL20.glVertexAttribPointer(GL20.glGetAttribLocation(game.getResourceManager().getShaderProgramID(), "in_Color"), TexturedVertex.colorElementCount, GL11.GL_FLOAT,
false, TexturedVertex.stride, TexturedVertex.colorByteOffset);
// Put the texture coordinates in attribute list 2
GL20.glVertexAttribPointer(GL20.glGetAttribLocation(game.getResourceManager().getShaderProgramID(), "in_TextureCoord"), TexturedVertex.textureElementCount, GL11.GL_FLOAT,
false, TexturedVertex.stride, TexturedVertex.textureByteOffset);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
This is how I render: (Left out useless things)
GL20.glUseProgram(game.getResourceManager().getShaderProgramID());
//Send the camera location and the projection matrix
int loc3 = GL20.glGetUniformLocation(game.getResourceManager().getShaderProgramID(), "projection");
FloatBuffer buf2 = BufferUtils.createFloatBuffer(16);
GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX,buf2);
GL20.glUniformMatrix4(loc3,false,buf2);
int loc4 = GL20.glGetUniformLocation(game.getResourceManager().getShaderProgramID(), "cameraPos");
GL20.glUniform4f(loc4, game.getGameCamera().getCameraLocation().x*-1*Constants.PHYS_PIXEL_TO_METER_RATIO, game.getGameCamera().getCameraLocation().y*-1*Constants.PHYS_PIXEL_TO_METER_RATIO, 0,1);
////////////////////////////RENDERING\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1);
GL20.glEnableVertexAttribArray(2);
// Bind to the index VBO that has all the information about the order of the vertices
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, VBOIndeciesID);
// Draw the vertices
GL11.glDrawElements(GL11.GL_TRIANGLES,6 , GL11.GL_UNSIGNED_INT, 0);
// Put everything back to default (deselect)
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
GL20.glDisableVertexAttribArray(0);
GL20.glDisableVertexAttribArray(1);
GL20.glDisableVertexAttribArray(2);
GL20.glUseProgram(0);
EDIT:
I check for an error on almost everything I left it out because it gave me no output. This is my method for checking for an error:
private void exitOnGLError(String errorMessage) { //Method to check if any opengl errors occured
int errorValue = GL11.glGetError();
if (errorValue != GL11.GL_NO_ERROR) {
String errorString = GLU.gluErrorString(errorValue);
System.err.println("ERROR - " + errorMessage + ": " + errorString);
if (Display.isCreated()) Display.destroy();
System.exit(-1);
}
}
Linking shaders:
loadShader("res/shaders/vert21.glsl",GL20.GL_VERTEX_SHADER);
loadShader("res/shaders/frag21.glsl",GL20.GL_FRAGMENT_SHADER);
shaderProgramID = GL20.glCreateProgram(); //Create a new shader program
for (Integer id : shaders) {
GL20.glAttachShader(shaderProgramID, id); //attach all the custom shaders to the program
}
// Position information will be attribute 0
GL20.glBindAttribLocation(shaderProgramID, 0, "in_Position");
// Color information will be attribute 1
GL20.glBindAttribLocation(shaderProgramID, 1, "in_Color");
// Textute information will be attribute 2
GL20.glBindAttribLocation(shaderProgramID, 2, "in_TextureCoord");
GL20.glLinkProgram(shaderProgramID); //Link the program to lwjgl
GL20.glValidateProgram(shaderProgramID); //Compile and make sure program was setup correctly
GL20.glUseProgram(shaderProgramID); //Use the program so we can pick the texture unit before continuing
setTextureUnit0(shaderProgramID); //pick the unit and set it in the shader to use
Loading and compiling:
StringBuilder shaderSource = new StringBuilder();
int shaderID = 0;
try { //Read shader
BufferedReader reader = new BufferedReader(new InputStreamReader(ClassLoader.getSystemResourceAsStream(filename)));
String line;
while ((line = reader.readLine()) != null) {
shaderSource.append(line).append("\n");
}
reader.close();
} catch (IOException e) {
System.err.println("Could not read file.");
Logger.getGlobal().log(Level.WARNING, e.getMessage(), e);
System.exit(-1);
} catch (NullPointerException e) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader( new FileInputStream(filename)));
String line;
while ((line = reader.readLine()) != null) {
shaderSource.append(line).append("\n");
}
reader.close();
} catch (IOException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
shaderID = GL20.glCreateShader(type); //Create shader
GL20.glShaderSource(shaderID, shaderSource); //Link source
GL20.glCompileShader(shaderID); //Compile
//Check if compiled correctly
if (GL20.glGetShaderi(shaderID, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
System.err.println(shaderID+" | Shader wasn't able to be compiled correctly.");
System.out.println(GL20.glGetShaderInfoLog(shaderID,GL20.glGetShaderi(shaderID,GL20.GL_INFO_LOG_LENGTH)));
}
this.exitOnGLError("loadShader");
#version
directives? Where do you check the shader compilation/link statuses and logs?#version 120
at the top of your shader, a compliant GLSL compiler is not supposed to treat this as an OpenGL 2.1 shader (GLSL version 1.2). Instead, they are supposed to assume you wrote an OpenGL 2.0 shader (GLSL version 1.1). Syntactically, nothing seems out of place, but you need to learn to get into the habit of using#version ...
as the first thing in a GLSL shader so the GLSL compiler does not limit you to GLSL 1.1 capabilities.