I am trying to create a GLSL texture shader in java on a mac, but mac doesn't support any version above #version 120. How would I port this 330 shader:
//this is my Vertex Shader
#version 330
layout (location = 0) in vec3 position;
layout (location = 1) in vec2 texCoord;
out vec2 texCoord0;
uniform mat4 transform;
void main()
{
gl_Position=transform*vec4(position,1.0);
texCoord0=texCoord;
}
This is my Fragment Shader
#version 330
in vec2 texCoord0;
uniform sampler2D sampler;
out vec4 gl_FragColor;
void main()
{
gl_FragColor=texture2D(sampler,texCoord0.xy);
}
How do I convert these shaders to glsl 120 without losing functionality?
You just have to:
_Remove the layout qualifiers (including location assignment). You'll the have to use
void glBindAttribLocation(GLuint program,
GLuint index,
const GLchar *name);
for each input of your vertex shader (with index set to 0 and 1 for location 0 and 1 and the name of the variable in case you didn't understand) before linking your program
_In the vertex shader, replace in by attribute and out by varying
_In the fragment shader, replace in by varying and use gl_FragColor but without declaring it
In your case, you would have this just before linking your program:
//program is your shader program of course...
//Bind vertex shader attributes (ins) instead of using layout
glBindAttribLocation(program, 0, "position");
glBindAttribLocation(program, 1, "texCoord");
//Then the program should be linked (or relinked, it doesn't matter)
glLinkProgram(program);
Your vertex shader:
#version 120
attribute vec3 position;
attribute vec2 texCoord;
varying vec2 texCoord0;
uniform mat4 transform;
void main()
{
gl_Position = transform*vec4(position, 1.0);
texCoord0 = texCoord;
}
And your fragment shader:
#version 120
varying vec2 texCoord0;
uniform sampler2D sampler;
void main()
{
gl_FragColor = texture2D(sampler, texCoord0.xy);
}
Related
Why are my uniform vector and float not being initialized? My shader code compiles correctly, my shader is compiling properly, but when I try to get the uniform location of my vec4 lightDirection, and floats specularFactor and diffuseFactor, it gives me an error. Note I haven't actually used these uniforms for anything yet, however, that shouldn't matter.
Here is my vertex shader, which gets all uniform locations properly:
#version 330
layout (location = 0) in vec3 position;
layout (location = 1) in vec2 texture_coord;
layout (location = 2) in vec3 normal;
layout (location = 3) in vec3 fNormal;
uniform mat4 worldMat;
uniform mat4 projection;
uniform mat4 transform;
out vec2 tex;
out vec3 n;
out vec3 fn;
void main() {
fn=fNormal;
n=normal;
tex = texture_coord;
gl_Position = projection * worldMat * transform * vec4(position, 1);
}
Here is my fragment shader, which only gets the texture sample uniform location:
#version 330
uniform sampler2D sampleTexture;
uniform vec4 lightDirection;
uniform float specularFactor;
uniform float diffuseFactor;
in vec2 tex;
in vec3 n;
in vec3 fn;
out vec4 fragColor;
void main() {
fragColor=texture(sampleTexture, tex);
}
Here is the method I use to get uniform locations (I am using Java):
public int loadUniform(String uniformName)throws Exception{
int iD= glGetUniformLocation(program,uniformName);
System.out.println("PROGRAM: "+program+" UNIFORM: "+uniformName+" "+iD);
if(iD==-1) {
throw new Exception("uniform:"+uniformName+" not initialized");
}
return iD;
}
Now, here is what is printed in console by print statment/exception. Am confusion. Numbers definitely seem wrong, but whatevs. I don't understaaaaand.
java.lang.Exception: uniform:lightDirection not initialized
at shader_src.Shader.loadUniform(Shader.java:273)
at shader_src.StaticShader.initValues(StaticShader.java:51)
at application.Main.main(Main.java:94)
PROGRAM: 4 UNIFORM: worldMat 9
PROGRAM: 4 UNIFORM: projection 5
PROGRAM: 4 UNIFORM: transform 0
PROGRAM: 4 UNIFORM: sampleTexture 4
PROGRAM: 4 UNIFORM: lightDirection -1
The glsl compiler and linker optimizes the code. Unnecessary code will be removed, and unnecessary uniforms and attributes will not become active program resources. If uniform variables are not used or are only used in a part of the code that is itself optimized out, they do not become active program resources. lightDirection, specularFactor and diffuseFactor are unused variables and therefore not active resources thus no active resources and therefore you cannot get a uniform location for these variables.
I would like to have some custom data stored inside the shader to be re-used for multiple frame during rendering.
At first what I try to do is to store a color buffer, to see is this can be done.
vertex shader:
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;
uniform mat4 u_projTrans;
varying vec4 v_color;
varying vec2 v_texCoords;
vec2 u_mouse;
uniform float u_time;
attribute vec3 my_data;
varying vec3 frag_data;
void main() {
v_color = a_color;
v_texCoords = a_texCoord0;
gl_Position = u_projTrans * a_position;
frag_data = my_data;
}
fragment shader:
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
varying vec3 frag_data;
void main(){
gl_FragColor = vec4(vec3(frag_data),1.0);
}
The shaders compile and work as intended with the exception of the usage of vec3 my_data:
ShaderProgram.pedantic = false;
vertex_shader = Gdx.files.internal("vertex_shader.glsl").readString();
fragment_shader = Gdx.files.internal("fragment_shader.glsl").readString();
shader_program = new ShaderProgram(vertex_shader, fragment_shader);
I try to set the variable from a button-click, like so:
goBtn = new TextButton("Reset", textButtonStyle);
goBtn.setSize(128,128);
goBtn.addListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
FloatBuffer myArray = BufferUtils.newFloatBuffer(3);
myArray.put(1.0f);myArray.put(1.0f);myArray.put(1.0f);
shader_program.begin();
shader_program.setVertexAttribute("my_data", 3, GL20.GL_FLOAT, false, 0, myArray);
shader_program.end();
}
});
But it has no effect..
What am I doing wrong here?
What would be the correct way to do this?
I've never tried setting a vertex attribute via the ShaderProgram. I'm not even sure why that exists as an option, because vertex data is supposed to be per-vertex. Since you are setting this for the whole mesh, it should be a uniform. And since it's a uniform, you don't have to pass it via a varying either.
So remove the varying and attribute from your vertex shader. And change the varying in the fragment shader to a uniform.
And to pass the data:
public void changed(ChangeEvent event, Actor actor) {
shader_program.begin();
shader_program.setUniformf("my_data", 1.0f, 1.0f, 1.0f);
shader_program.end();
}
But you should also call this code to set the default values for this uniform when you are first setting things up. Behavior might be undefined before you set their values.
I've run into the strangest problem I've ever had in my shader experience. I created some test shader code (see below) and ran it on a simple texture.
Basically what I was trying to do is make my shader check the color of a fragment, and if it was within a certain range it would color that fragment according to a uniform color variable. The problem I'm having is that my shader does not correctly recognize the color of a fragment. I even went as far as to check if the red portion of the color is equal to one and it always returns true for every fragment. Yet if I use the same shader to draw the original texture it works just fine.
Why is this happening? The shader compiles without any errors what so ever. I feel like I'm missing something obvious...
Code (if you have access to LibGDX you can run this code for yourself).
// Vertex
#version 330
in vec4 a_position;
in vec4 a_color;
in vec2 a_texCoord0;
out vec4 v_color;
out vec2 v_texCoord0;
uniform mat4 u_projTrans;
void main() {
v_color = a_color;
v_texCoord0 = a_texCoord0;
gl_Position = u_projTrans * a_position;
}
// Fragment
#version 330
in vec4 v_color;
in vec2 v_texCoord0;
out vec4 outColor;
uniform vec4 color;
uniform sampler2D u_texture;
void main() {
// This is always true for some reason...
if(v_color.r == 1.0) {
outColor = color;
} else {
// But if I run this code it draws the original texture just fine with the correct colors.
outColor = v_color * texture2D(u_texture, v_texCoord0);
}
}
// Java code.
// Creating sprite, shader and sprite batch.
SpriteBatch batch = new SpriteBatch();
Sprite sprite = new Sprite(new Texture("testTexture.png"));
ShaderProgram shader = new ShaderProgram(Gdx.files.internal("vertex.vert"),
Gdx.files.internal("fragment.frag"));
// Check to see if the shader has logged any errors. Prints nothing.
System.out.println(shader.getLog());
// We start the rendering.
batch.begin();
// We begin the shader so we can load uniforms into it.
shader.begin();
// Set the uniform (works fine).
shader.setUniformf("color", Color.RED);
// We end the shader to tell it we've finished loading uniforms.
shader.end();
// We then tell our renderer to use this shader.
batch.setShader(shader);
// Then we draw our sprite.
sprite.draw(batch);
// And finally we tell our renderer that we're finished drawing.
batch.end();
// Dispose to release resources.
shader.dispose();
batch.dispose();
sprite.getTexture().dispose();
The texture:
You have 2 input colors for your fragment shader: one sent as a vertex attribute and one as a texture. You intend to check the texture color but instead you check for the color value sent as the vertex attribute.
// Vertex
#version 330
in vec4 a_position;
in vec4 a_color; // <--- A color value is passed as a vertex attribute
in vec2 a_texCoord0;
out vec4 v_color;
out vec2 v_texCoord0;
uniform mat4 u_projTrans;
void main() {
v_color = a_color; // <--- you are sending it to fragment shader
v_texCoord0 = a_texCoord0;
gl_Position = u_projTrans * a_position;
}
// Fragment
#version 330
in vec4 v_color; // <--- coming from vertex buffer
in vec2 v_texCoord0;
out vec4 outColor;
uniform vec4 color;
uniform sampler2D u_texture;
void main() {
// This is always true for some reason...
if(v_color.r == 1.0) { // <--- and then checking this vertex attribute color
outColor = color; // instead of what you send from texture
} else {
...
}
}
I was working on a project, and for that project I had to walk through a book called "OpenGL ES 2 For Android: A quick start guide".
So when I got to texturing, I got the error of:
'texture2D' : No matching overloaded function found
...when I compile the shader.
The shader code:
// Fragment shader
precision mediump float;
uniform sampler2D u_TextureUnit;
varying vec4 v_TextureCoordinates;
void main()
{
gl_FragColor = texture2D(u_TextureUnit, v_TextureCoordinates);
}
// Vertex shader
uniform mat4 u_Matrix;
attribute vec4 a_Position;
attribute vec4 a_TextureCoordinates;
varying vec4 v_TextureCoordinates;
void main()
{
gl_Position = u_Matrix * a_Position;
v_TextureCoordinates = a_TextureCoordinates;
}
I tried the same shaders for my project and for exactly the same code as in the book but it still gives me the same error when I compile the shader, and the viewport on the android device is blank, just the clear color I set is shown.
varying vec4 v_TextureCoordinates;
^^^^
There are exactly two texture2D() overloads in ES 2.0:
vec4 texture2D(sampler2D sampler, vec2 coord)
vec4 texture2D(sampler2D sampler, vec2 coord, float bias)
...neither of which accept a vec4 for coord.
Slice off the last two vector components of v_TextureCoordinates using a swizzle:
gl_FragColor = texture2D(u_TextureUnit, v_TextureCoordinates.xy );
I'm trying to understand shaders using libgdx, coming from an XNA/HLSL background. I'm trying to get a vert/frag shader pair to reproduce the output I get without a shader, but it's not displaying anything.
Shader creation:
void SetupShader()
{
ShaderProgram.pedantic = false;
shader = new ShaderProgram(
Gdx.files.internal("assets/default.vert").readString(),
Gdx.files.internal("assets/default.frag").readString());
if(!shader.isCompiled()) {
Gdx.app.log("Problem loading shader:", shader.getLog());
}
batch.setShader(shader);
}
default.vert:
attribute vec4 a_Position;
attribute vec4 a_Normal;
attribute vec2 a_TexCoord;
attribute vec4 a_Color;
uniform mat4 u_projTrans;
varying vec2 v_texCoords;
varying vec4 v_color;
void main() {
v_color = a_Color;
v_texCoords = a_TexCoord;
gl_Position = u_projTrans * a_Position;
}
default.frag:
#ifdef GL_ES
precision mediump float;
#endif
varying vec2 v_texCoords;
varying vec4 v_color;
void main() {
gl_FragColor = v_color;
}
Rendering:
batch.begin();
for (GameObject gObj : gameObjects)
gObj.Draw(batch);
batch.end();
Any suggestions here? I'm new to OpenGL-ES as well, so I may be missing something obvious. I looked around a bit before posting, and the doc for SpriteBatch.setShader(ShaderProgram) was as follows:
Sets the shader to be used in a GLES 2.0 environment. Vertex position
attribute is called "a_position", the texture coordinates attribute is
called called "a_texCoords0", the color attribute is called "a_color".
See ShaderProgram.POSITION_ATTRIBUTE, ShaderProgram.COLOR_ATTRIBUTE
and ShaderProgram.TEXCOORD_ATTRIBUTE which gets "0" appened to
indicate the use of the first texture unit. The projection matrix is
uploaded via a mat4 uniform called "u_proj", the transform matrix is
uploaded via a uniform called "u_trans", the combined transform and
projection matrx is is uploaded via a mat4 uniform called
"u_projTrans". The texture sampler is passed via a uniform called
"u_texture". Call this method with a null argument to use the default
shader.
After looking at the code in SpriteBatch it seems that I had a few mistakes in my code. Below are the correct shaders:
default.vert:
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;
uniform mat4 u_projTrans;
varying vec4 v_color;
varying vec2 v_texCoords;
void main() {
v_color = a_color;
v_texCoords = a_texCoord0;
gl_Position = u_projTrans * a_position;
}
default.frag:
#ifdef GL_ES
precision mediump float;
#endif
varying vec4 v_color;
varying vec2 v_texCoords;
uniform sampler2D u_texture;
void main() {
gl_FragColor = v_color * texture2D(u_texture, v_texCoords);
}