我正在尝试在Mac上的java中创建一个GLSL纹理着色器,但是mac不支持#version 120之上的任何版本。我将如何移植这个330着色器:
//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;
}
这是我的片段着色器
#version 330
in vec2 texCoord0;
uniform sampler2D sampler;
out vec4 gl_FragColor;
void main()
{
gl_FragColor=texture2D(sampler,texCoord0.xy);
}
如何在不丢失功能的情况下将这些着色器转换为glsl 120?
答案 0 :(得分:2)
你必须:
_删除layout
限定符(包括位置分配)。您必须使用
void glBindAttribLocation(GLuint program,
GLuint index,
const GLchar *name);
对于顶点着色器的每个输入(索引设置为0,位置0和1设置为1,变量的名称如果你没有'在链接你的程序之前
_在顶点着色器中,将in
替换为attribute
,将out
替换为varying
_在片段着色器中,将in
替换为varying
并使用gl_FragColor
但不声明
在您的情况下,您可以在链接程序之前使用它:
//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);
你的顶点着色器:
#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;
}
你的片段着色器:
#version 120
varying vec2 texCoord0;
uniform sampler2D sampler;
void main()
{
gl_FragColor = texture2D(sampler, texCoord0.xy);
}