简体   繁体   中英

OpenGL ES: Do vertex structs need x, y, AND z?

This is probably a stupid question, but I've been wondering it - do vertex structs in OpenGL NEED to have x, y and z? I'm drawing in 2D, yet all the tutorials I've seen seem to have az variable in their struct even though it's always 0. Doesn't this slow down rendering or something?

Also, they always seem to have r, g, b and a for the color variables, but I don't require the alpha since it'll always be 1 for my project. Do I really need to say it's 1 every single time?

I'm just wondering since glVertexAttribPointer lets you set the second parameter to be anything, and I tried setting it to 2 since I'm only using x and y, and it worked.

From the spec section 2.8:

If size is one then the x component of the attribute is specified by the array; the y, z, and w components are implicitly set to zero, zero, and one, respectively. If size is two then the x and y components of the attribute are specified by the array; the z , and w components are implicitly set to zero, and one, respectively. If size is three then x, y, and z are specified, and w is implicitly set to one. If size is four then all components are specified.

So if you don't define z or w for your 2D vertices. The vertex assembler automatically fills them with z=0,w=1. Your code is safe and correct.

RGBA is the same as XYZW, so you don't have to specify alpha for colour components either assuming you want alpha to be 1.

Performance-wise you should generally expect a win by using smaller vertex components because less data has to be moved around and copied to the GPU. I would expect any performance gain to be tiny and probably unmeasurable.

One exception is that if you shave a few bytes off a vertex and reduce it say from a nice cache friendly 16-bytes per vertex to 12-bytes per vertex, you might reduce performance slightly because 8, 16, 32 byte sizes are a bit of a magic number as far as memory accesses go. Again, any performance loss here will be minute, so I wouldn't let it trouble you.

If you removed the z, you won't notice improvement because OpenGL will still do 3D rendering although you are just using it for 2D rendering. However, you gain memory. So you can remove the z and let the second parameter of glVertexAttribPointer be 2 as you said but watch out how you will manipulate the Vectors in the shaders.

This could be considered as over optimization but if you want, you can disable depth rendering so objects will be rendered back to front in the order they were rendered by calling glDisable (GL_DEPTH_TEST) during setup. In openGL, how can you get items to draw back to front?

For the non transparent image, you can load it to the GPU as GL_RGB type:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, each.w, each.h, 0, GL_RGB, GL_UNSIGNED_BYTE, each.image)

Yep always needs a Z, its a 3D renderer, and when you are doing 2D stuff you are just exploiting the 3D renderer to do 2D really (using orthographic projection instead of perspective), you can still use the Z to make certain objects be on top of others which is useful. As far as i know, the gl_Position variable in the shader is actually a vec4, so it will have a Z and W component, i think if you are using a vec2 so just X and Y, the Z and W will get default values or you will have to convert it to a vec4 in the shader, i cant remember.

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