c++ - Opengl - instanced attributes -


i use oglplus - it's c++ wrapper opengl.

i have problem defining instanced data particle renderer - positions work fine goes wrong when want instance bunch of ints same vbo.

i going skip of implementation details not make problem more complicated. assume bind vao , vbo before described operations.

i have array of structs (called "particle") upload this:

glbufferdata(gl_array_buffer, sizeof(particle) * numinstances, newdata, gl_dynamic_draw); 

definition of struct:

struct particle {     float3 position;             //some more attributes, 9 floats in total     //(...)     int fluidid;         }; 

i use helper function define opengl attributes this:

void addinstancedattrib(const instancedattribdescriptor& attribdesc, glslprogram& program, int offset=0)         {             //binding , implementation details             //(...)              oglplus::vertexarrayattrib attrib(program, attribdesc.getname().c_str());             attrib.pointer(attribdesc.getpervertvals(), attribdesc.gettype(), false, sizeof(particle), (void*)offset);              attrib.divisor(1);             attrib.enable();         } 

i add attributes positions , fluidids this:

            instancedattribdescriptor posdesc(3, "instancetranslation", oglplus::datatype::float);             this->instanceddata.addinstancedattrib(posdesc, this->program);              instancedattribdescriptor fluiddesc(1, "fluidid", oglplus::datatype::int);             this->instanceddata.addinstancedattrib(fluiddesc, this->program, (int)offsetof(particle,fluidid)); 

vertex shader code:

uniform vec3 fluidcolors[2];  in vec3 instancetranslation; in vec3 vertexposition; in vec3 n;  in int fluidid;  out float lightintensity; out vec3 spherecolor;  void main() {         //some typical mvp transformations     //(...)     spherecolor = fluidcolors[fluidid];     gl_position = projection * vertexposeye;  } 

this code whole produces output:

opengl-problem

as can see, particles arranged in way wanted them be, means "instancetranslation" property setup correctly. group of particles left have fluidid value of 0 , ones right equal 1. second set of particles have proper positions index improperly fluidcolors array.

what know:

  • it's not problem way set fluidcolors uniform. if hard-code color selection in shader this:

    spherecolor = fluidid == 0? fluidcolors[0] : fluidcolors1;

i get:

enter image description here

  • opengl returns gl_no_error glgeterror there's no problem enums/values provide
  • it's not problem offsetof macro. tried using hard-coded values , didn't work either.
  • it's not compatibility issue glint, use simple 32bit ints (checked sizeof(int))
  • i need use fluidid instanced attrib indexes color array because otherwise, if set color particle group simple vec3 uniform, i'd have batch same particle types (with same fluidid) first means sorting them , it'd costly of operation.

to me, seems issue of how set fluidid attribute pointer. since use type int in shader, must use glvertexattribipointer() set attribute pointer. attributes set normal glvertexattribpointer() function work float-based attribute types. accept integer input, data converted float when shader accesses them.

in oglplus, apparently have use vertexarrayattrib::ipointer() instead of vertexarrayattrib::pointer() if want work integer attributes.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -