Provide an integer for an array index in OpenGL ES 2.0?

I am developing an OpenGL application for the iPhone. In my vertex shader, I need a way to change the color of a large number (but not all) of my vertices right away, so I decided to index the color. This will allow me to leave the static VBO and change one homogeneous variable, rather than scrolling through each vertex and changing the color information between each frame.

My plan is to create a form with a color array, add an integer containing the index in the attributes. Here is my vertex shader:

uniform mat4 u_mvp_matrix; uniform vec4 u_color_array[]; attribute vec4 a_position; attribute int a_colorIndex; varying lowp vec4 v_color; void main() { v_color = u_color_array[a_colorIndex]; gl_Position = u_mvp_matrix * a_position; } 

An error occurs:

int cannot be in vertex shader

I did some research. The iPhone supports OpenGL ES 2.0, which means that it supports GLSL 1.2 at the latest, and obviously integers are only supported in GLSL 1.3 and later. I tried changing a_colorIndex to float. I did not expect this to work, and it is not.

How can I specify a color index for each vertex?

+6
source share
1 answer

Specify the attribute as a float. You can use float as indices in arrays.

+4
source

Source: https://habr.com/ru/post/919955/


All Articles