c++ - OpenglES 2.0 Vertex Shader Attributes -
i've read in tutorials bare minimum vertex shader needs define below:
attribute vec3 v3_variable; attribute vec3 v3pos; void main() { gl_position = v3pos; }
opengles passes shader vertex data v3pos
name of variable can anything, other tutorials name a_position
, a_pos
, whatever~.
so question is, how opengles know when call glvertexattribpointer()
knows should put data v3pos
instead of v3_variable
?
there 2 methods of identifying attributes in shaders 'paired' data provide glvertexattribpointer
. identifier glvertexattribpointer
index
(the first parameter), must done index somehow.
in first method, obtain index of attribute shader after compiled , linked using glgetattriblocation, , use first parameter, eg.
glint program = ...; glint v3posattributeindex = glgetattriblocation(program, "v3pos"); glint v3varattributeindex = glgetattriblocation(program, "v3_variable"); glvertexattribpointer(v3posattributeindex, ...); glvertexattribpointer(v3varattributeindex, ...);
you can explicitly set layout of attributes when authoring shader (as @j-p's comment suggests), modify shader so:
layout(location = 1) in vec3 v3_variable; layout(location = 0) in vec3 v3pos;
and in code set them explicitly:
glvertexattribpointer(0, ...); // v3pos data glvertexattribpointer(1, ...); // v3_variable data
this second method works in gles 3.0+ (and opengl 3.0+), may not applicable case if using gles 2.0. also, note can use both of these methods together. eg. if explicitly define layouts, doesn't restrict querying them later.
Comments
Post a Comment