GLSL shader for every situation

In my game, I want to create separate GLSL shaders for each situation. In the example, if I would have 3 models character, shiny swordand blury ghost, I would like to install renderShader, animationShaderand lightingShaderin character, then renderShader, lightingShaderand specularShaderbefore shiny sword, and, finally, I would like to install renderShader, lightingShaderand blurShaderin blury ghost.

renderShader should multiply the positions of the vertices by projection, world and other matrices, and this fragmet shader should simply specify the texture of the model.

animationShader must transform vertices using bone transforms.

lightingShadershould do lighting, and specularLightingshould perform mirror lighting.

blurShader should perform a blur effect.

Now, first of all, how can I perform several vertex transformations on different shaders? Because I animationShaderhave to calculate the animated positions of the vertices, and then I renderShaderhave to get this position and convert it to some matrices.

Secondly, how can I change the color of fragments on different shaders?

The main idea is that I want to be able to use different shaders for each game / effects, and I don’t know how to achieve it.

I need to know how I should use these shaders in opengl, and how I should use GLSL so that all shaders complete each other, and it would not matter to the shaders whether another shader would be used.

+5
source share
3 answers

. , , .

. ( , OpenGL, , OpenGL-4 - ). . , , . .

-

, . , glShaderSource , . .

uniform vec3 light_positions[N_LIGHT_SOURCES];
out vec3 light_directions[N_LIGHT_SOURCES];
out vec3 light_halfdirections[N_LIGHT_SOURCES];

void illum_calculation()
{
    for(int i = 0; i < N_LIGHT_SOURCES; i++) {
        light_directions[i] = ...;
        light_halfdirections[i] = ...;
    }
}

illum_calculation.vs.glslmod ( ). ,

uniform vec4 armature_pose[N_ARMATURE_BONES];
uniform vec3 armature_bones[N_ARMATURE_BONES];

in vec3 vertex_position;

void skeletal_animation()
{
    /* ...*/
}

illum_skeletal_anim.vs.glslmod.

#version 330
uniform ...;
in ...;

, ,

void main() {
    skeletal_animation();
    illum_calculation();
}

.. . . , ( OpenGL ). . , framebuffer. .

+2

, , , , "", .

, , , . #define d GLSL, , , GLSL. , .

. , , . , . , , , .

, , , . .

, , GLSL, . , .

:

INPUT vec4 modelSpacePosition;
OUTPUT vec4 clipSpacePosition;

uniform mat4 modelToClipMatrix;

void main()
{
  clipSpacePosition = modelToClipMatrix * modelSpacePosition;
}

"" , modelSpacePosition , , . , clipSpacePosition gl_Position, clipSpacePosition gl_Position. , .

, .

, . . , "lightingShader" , . , , , - , - .

+2

, . glUseProgram. , - , .

, , , , , , , ..

, Vertex.

, .

If you want to share common code between different shader programs, for example, illuminateDiffuse, you must outsource this function and provide it to your shader by simply inserting a string literal that represents the function in your shader code, which is nothing more than a string literal. This way you can achieve a kind of modularity or enable behavior using string manipulations with your shader code.

In any case, the shader compiler tells you what is wrong.

Regards

+1
source

All Articles