glsl - The difference between a color attribute and using gl_Color -
most glsl shaders using attribute color in vertex shader, forwarded varying fragment shader. this:
attribute vec4 position; attribute vec4 color; uniform mat4 mvp; varying vec4 destinationcolor; void main(){ destinationcolor = color; gl_position = mvp * position; };
setting color can done glvertexatribpointer()
pass 1 color per vertex or glvertexattrib4fv()
pass global color vertexes. try understand difference predefined variable gl_color
in vertex shader (if there difference @ all). i.e.
attribute vec4 position; uniform mat4 mvp; varying vec4 destinationcolor; void main(){ destinationcolor = gl_color; gl_position = mvp * position; };
and using glcolorpointer()
pass 1 color per vertex or glcolor4fv()
use global color vertexes. me second shader looks better (= more efficient?), because uses less attributes. tutorials & online resources using first approach - wonder if missed or if there no difference @ all. better practice when writing glsl shaders?
to me second shader looks better (= more efficient?), because uses less attributes.
it not use fewer attributes. uses fewer explicit attribute declarations. of work needed color value opengl still there. it's still being done. hardware still fetching data buffer object or getting glcolor
context value or whatever.
you don't see in shader's text. because don't see doesn't mean happens free.
user-defined attributes preferred following reasons:
- user-defined attributes make clear how many resources shaders using. if want know how many attributes need provide shader, @ global declarations. predefined attributes, can't this; have scan through entire vertex shader
gl_*
names name predefined attribute. - user-defined attributes can more things. if want pass integer values integers vertex shader, must use user-defined attribute. if need pass double-precision float vertex shader, again, predefined attribute cannot you.
- predefined attributes removed core opengl contexts. osx, example, not allow compatibility profile. can still use opengl 2.1, if want use opengl version 3.2 or greater on osx, cannot use removed functionality. , built-in vertex attributes removed in opengl 3.1.
- predefined attributes never part of opengl es 2.0+. if want write shaders can work in opengl es, again cannot use them.
so basically, there's no reason use them these days.
Comments
Post a Comment