开发者

Using multiple vertex shaders on the same program

I'm trying to implements projection, using a vertex shader.

Is there a way to have a separate vertex shader to handle set the gl_Position, and having another vertex shader to set the values required for the fragment shader?

The problem I have it that only the main() function of the first vertex shader is called.

Edit: I found a way to make it work, by combinin开发者_StackOverflow中文版g the shader sources instead of using multiple independant shaders. I'm not sure if this is the best way to do it, but it seems to work nicely.

main_shader.vsh

attribute vec4 src_color;

varying vec4 dst_color; // forward declaration

void transform(void);

void main(void)
{
    dst_color = src_color;
    transform();
}

transform_2d.vsh

attribute vec4 position;

void transform(void)
{
    gl_Position = position;
}

Then use it as such:

char merged[2048];
strcat(merged, main_shader_src);
strcat(merged, transform_shader_src);
// create and compile shader with merged as source


in OpenGL ES, the only way is to concatenate shader sources, but in OpenGL, there are some interesting functions that allow you to do what you want:

GL_ARB_shader_subroutine (part of OpenGL 4.0 core) - That does pretty much what you wanted

GL_ARB_separate_shader_objects (part of OpenGL 4.1 core) - This extension allows you to use (mix) vertex and fragment shaders in different programs, so if you have one vertex shader and several fragment shaders (e.g. for different effects), then this extensions is for you.

I admit this is slightly offtopic, but i think it's good to know (also, might be useful for someone).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜