开发者

How to draw plain colored surfaces in OpenGL ES?

开发者 https://www.devze.com 2023-03-22 12:57 出处:网络
I have a curved surface defined by a number of triangular faces. The entire surface has single color.

I have a curved surface defined by a number of triangular faces. The entire surface has single color.

So far I was drawing each triangle separately. For each triangle I was passing 3 vertices, 3 indices, 3 color values (of same color) and 3 normal values (all equal to surface normal of triangle). Then I would push all these arrays to VBO and render the curved surface.

Recently I realized this is pretty wasteful compared to the texture method of rendering. Now I create a texture out of a 2x2 array of pixels (all of the same color that I want for the curved surface). Then I calculate texture coordinates (s,t) for all the vertices on the curved surface. Thus I effectively stretch the 2x2 pixel texture to fit to my curved surface. This works pretty OK, for some surfaces. Also this requires much smaller size buffers (because now the vertices have only single normal and color value, therefore they have to be mentioned only once). However by this method, sometimes the curved surface doesn't render at all. My suspicion is in the way I calculate the texture coordinates. It's alright if a surface is slightly cur开发者_JAVA技巧ved, but when the surface revolves around an axis and forms a closed boundary then the texture coordinates seem to confuse the graphic card.

My question is, what's a recommended way in OpenGL ES, of drawing surfaces with a single color?

UPDATE - Explanation on accepting the answer

It took me a while to understand how genpfault's answer will work. So here's an explanation. Above I have described two methods of drawing plain colored surfaces - 1. using rgba color values, 2. using textures that have single color. genpfault's answer explains how to do method 1 efficiently.

I had implemented first method by passing the same rgba color values per vertex multiple times. Therefore I was reading this color value in the vertex shader code as an attribute. In genpfault's method, we set the color as a uniform variable before calling drawArray/Elements. This will require change in vertex shader to read the value from a uniform variable, instead of an attribute.


Just set the current color via glColor() before rendering each patch.

That way you don't have to pass in per-vertex color components.

0

精彩评论

暂无评论...
验证码 换一张
取 消