MysteryPancake MysteryPancake - 5 days ago 5
Swift Question

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 a z 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.

Answer

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.

Comments