opengl es2.0的着色器语言中,怎样设置片段着色器,使其既可以接收颜色值,又可以设置片段值?

kejian_clear 2016-12-12 06:09:06
QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this);
const char *vertexShaderSource = //顶点着色程序的源代码保存到一个字符数组里面;
"attribute highp vec4 posAttr;\n" //“attribute”声明了这个shader会接受一个传入位置变量“posAttr”。在后面的代码中,你会用它来传入顶点的位置数据。这个变量的类型是“vec4”,表示这是一个由4部分组成的矢量,highp表示最高精度
"attribute lowp vec4 colAttr;\n" //lowp表示最低精度 ,传入颜色变量 “colAttr”
"attribute mediump vec4 texCoordAttr;\n" //纹理坐标
"varying mediump vec4 texc;\n"
"varying lowp vec4 col;\n" //这个变量没有“attribute”的关键字。表明它是一个传出变量,它就是会传入片段着色器的参数。varying 关键字表示,依据顶点的颜色,平滑计算出顶点之间每个像素的颜色。
"uniform mat4 projMatrix;\n"
"uniform mat4 mvMatrix;\n"
"void main() {\n"
"col = colAttr;\n"
"gl_Position = projMatrix * mvMatrix * posAttr;\n"
"texc = texCoordAttr;\n"
//"gl_PointSize = 10.0f;\n" //线条粗细可以在这个地方控制
"}\n";
vshader->compileSourceCode(vertexShaderSource);

QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, this);
const char *fragmentShaderSource = //片段着色程序的源代码保存到一个字符数组里面;
""
"varying lowp vec4 col;\n"
"uniform sampler2D texture;\n"
"varying mediump vec4 texc;\n"
"void main() {\n"
"if()"
"gl_FragColor = col;\n" //gl_FragColor = vec4 ( 1.0, 0.0, 0.0, 1.0 );\n"
//"gl_FragColor = texture2D(texture, texc.st);\n"
"}\n";
fshader->compileSourceCode(fragmentShaderSource);

代码如上所示,现在gl_FragColor 要么就只能为col,或者只能为 texture2D(texture, texc.st)。请问怎样才能两者兼顾?因为我做的是一个类似CAD的项目,既要读取纹理贴图,又要画线条集合图形(线条颜色可控)。搞一周了,一直没调出来,请各位高手指点,感激不尽
...全文
108 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

8,304

社区成员

发帖
与我相关
我的任务
社区描述
游戏开发相关内容讨论专区
社区管理员
  • 游戏开发
  • 呆呆敲代码的小Y
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧