Я хочу использовать затенение на своих объектах OpenGL, но не могу получить доступ к функциям GLSL в моем пакете opengl. Есть ли доступный пакет GLSL для OpenGL ES в Eclipse?
EDIT: Как заметил Тим. Шейдеры записываются в виде текстовых файлов, а затем загружаются с использованием glShaderSoure, у меня есть файл шейдера C++, который я написал один раз для приложения трассировки лучей. Но я действительно смущен тем, как я буду реализовывать Java. Предположим, что у меня есть 2D-квадрат, нарисованный в моем классе Renderer
с использованием объекта gl MySquare
, как я буду использовать реализацию java-эквивалента файла шейдера ниже.
Shader.vert
varying vec3 N;
varying vec3 v;
void main()
{
// Need to transform the normal into eye space.
N = normalize(gl_NormalMatrix * gl_Normal);
// Always have to transform vertex positions so they end
// up in the right place on the screen.
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
// Fragment shader for per-pixel Phong interpolation and shading.
Shader.Frag
// The "varying" keyword means that the parameter value is interpolated
// between the nearby vertices.
varying vec3 N;
varying vec3 v;
//Used for Environmental mapping shader calculations
const vec3 xUnitVec=vec3(1.0, 0.0, 0.0), yUnitVec=vec3(1.0, 1.0, 0.0);
uniform vec3 BaseColor, MixRatio;
uniform sampler2D EnvMap;
void main()
{
// The scene ambient light.
vec4 ambient = gl_LightModel.ambient * gl_FrontMaterial.ambient;
// The normal vectors is generally not normalized after being
// interpolated across a triangle. Here we normalize it.
vec3 Normal = normalize(N);
// Since the vertex is in eye space, the direction to the
// viewer is simply the normalized vector from v to the
// origin.
vec3 Viewer = -normalize(v);
// Get the lighting direction and normalize it.
vec3 Light = normalize(gl_LightSource[0].position.xyz);
// Compute halfway vector
vec3 Half = normalize(Viewer+Light);
// Compute factor to prevent light leakage from below the
// surface
float B = 1.0;
if(dot(Normal, Light)<0.0) B = 0.0;
// Compute geometric terms of diffuse and specular
float diffuseShade = max(dot(Normal, Light), 0.0);
float specularShade =
B * pow(max(dot(Half, Normal), 0.0), gl_FrontMaterial.shininess);
// Compute product of geometric terms with material and
// lighting values
vec4 diffuse = diffuseShade * gl_FrontLightProduct[0].diffuse;
vec4 specular = specularShade * gl_FrontLightProduct[0].specular;
ambient += gl_FrontLightProduct[0].ambient;
// Assign final color
gl_FragColor= ambient + diffuse + specular + gl_FrontMaterial.emission;
}
Ознакомьтесь с учебными пособиями на сайте learnopengles.com. Они ответят на все ваши вопросы.
Там нет java-эквивалента файла шейдера. Шейдер написан в GLSL. Шейдер будет таким же, будет ли ваш opengl обернут в java, или c++, или python, или что-то еще. Помимо небольших различий API между OpenGL и OpenGLES, вы можете загрузить тот же самый шейдер в Java, что и в c++, символ для символа.