1

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 combining 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
4

1 回答 1

2

在 OpenGL ES 中,唯一的方法是连接着色器源,但在 OpenGL 中,有一些有趣的函数可以让你做你想做的事:

GL_ARB_shader_subroutine(OpenGL 4.0 核心的一部分) - 这几乎可以满足您的需求

GL_ARB_separate_shader_objects(OpenGL 4.1 核心的一部分)——这个扩展允许你在不同的程序中使用(混合)顶点和片段着色器,所以如果你有一个顶点着色器和几个片段着色器(例如用于不同的效果),那么这个扩展是给你的.

我承认这有点离题,但我认为很高兴知道(也可能对某人有用)。

于 2012-01-20T10:55:27.787 回答