简体   繁体   中英

Drawing successive triangles in a strip with a monotonically increasing number

I would like to draw many triangles, where the 'color' of each is really in index into an internal CPU-side data structure.

Is there any easy (fast way) to change the color (index) of every triangle rendered? I thought of using an uninterpolated texture, but I need indices in the 10,000's.

I realise the pixel shader can't reference a value from another one, but I thought a vertex type of (x, y, z, i) would work.

I've tried Ubuntu, OSX with OpenGL and ended up with the complexity of DX11/12 on Windows.

I tried with Unity3d and failed (needed C++ plugin so why bother) and unsure how to do this in OpenGL.

It is not practical to embed the ID of a triangle into a indexed vertex buffer, because vertices (presumably) will be used by more than one triangle. This would be possible if you were using unindexed data.

However, there is an easier way. Assuming you have GLSL 1.50 (OpenGL 3.2+), you can use gl_PrimitiveID in your fragment shader. Likewise, HLSL has the SV_PrimitiveID semantic (shader model 4.0+). This will be the 'index' of the triangle being drawn. You can use this index to determine an index into appropriate data. The data could be either supplied to your shader as a texture, uniform data, or a forumla for generating the color within the shader.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM