Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using GLshort instead of GLfloat for vertices

I'm attempting to convert my program from GLfloat to GLshort for vertex positions and I'm not sure how to represent this in the shader. I was using a vec3 datatype in the shader but vec3 represents 3 floats. Now I need to represent 3 shorts. As far as I can tell OpenGL doesn't have a vector for shorts so what am I supposed to do in this case?

like image 714
Xavier Avatar asked Jul 26 '12 22:07

Xavier


People also ask

Can I use glfloat instead of float in OpenGL?

If I recall correctly, OpenGL uses GLfloat internally. Thus passing any type other than GLfloat to OpenGL will require conversion. If you want complete portability, use GLfloat instead of float. However, I cannot think of a major system that would be running OpenGL where GLfloat is not the same as float.

Are int and float the same as glint and glfloat?

On the off chance int and float aren’t the same thing as GLint and GLfloat. Your compiler might use 16 or 64 bit ints, for example. But no matter what compiler you use, GLint and GLfloat will always be the right size. Use GLint and GLfloat to avoid surprises down the road. it’s the same, they’re just typedefs It’s the same on YOUR system TODAY.

What is the best practice for storing vertex data in Open GL?

Bookmark this question. Show activity on this post. What is the best practice in regards to storing vertex data in Open GL? I.e: Show activity on this post. There isn't any best practice.

What is the use of vertex array in OpenGL?

Using vertex arrays reduces the number of function calls and redundant usage of shared vertices. Therefore, you may increase the performance of rendering. Here, 3 different OpenGL functions are explained to use vertex arrays; glDrawArrays(), glDrawElements()and glDrawRangeElements().


1 Answers

I'm not sure how to represent this in the shader.

That's because this information doesn't live in the shader.

All values provided by glVertexAttribPointer will be converted to floating-point GLSL values (if they're not floats already). This conversion is essentially free. So you can use any of these glVertexAttribPointer definitions with a vec4 attribute type:

glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, ...);
glVertexAttribPointer(0, 4, GL_UNSIGNED_SHORT, GL_TRUE, ...);
glVertexAttribPointer(0, 4, GL_SHORT, GL_TRUE, ...);
glVertexAttribPointer(0, 4, GL_SHORT, GL_FALSE, ...);

All of these will be converted into a vec4 automatically. Your shader doesn't have to know or care that it's being fed shorts, bytes, integers, floats, etc.

The first one will be used as is. The second one will convert the unsigned short range [0, 65535] to the [0.0, 1.0] floating-point range. The third will convert the signed short range [-32768, 32767] to the [-1.0, 1.0] range (though the conversion is a bit odd and differs for certain OpenGL versions, so as to allow the integer 0 to map to the floating point 0.0). The fourth will convert [-32768, 32767] to [-32768.0, 32767.0], as a floating-point range.

The GLSL type you use for attributes only changes if you use glVertexAttribIPointer or glVertexAttribLPointer, neither of which is available in OpenGL ES 2.0.

In short: you should always use float GLSL types for attributes. OpenGL will do the conversion for you.

like image 138
Nicol Bolas Avatar answered Dec 18 '22 15:12

Nicol Bolas