I am looking for / writing C ++ - an implementation of a 16-bit floating point number that will be used with OpenGL vertex buffers (texture coordinates, normal, etc.). Here are my requirements:
- There must be 16 bits (obviously).
- Must be loaded into the OpenGL vertex buffer using GL_HALF_FLOAT.
- Should be able to represent numbers outside of -1.0 - +1.0 (otherwise I would just use GL_SHORT normalized).
- It should be possible to convert to and from a normal 32-bit float.
- Arithmetic does not matter - I only care about storage.
- Speed ββis not a primary concern, but correctness.
Here is what I have used so far for the interface:
class half { public: half(void) : data(0) {} half(const half& h) : data(h.data) {} half(const unsigned short& s) : data(s) {} half(const float& f) : data(fromFloat(f)) {} half(const double& d) : data(fromDouble(d)) {} inline operator const float() { return toFloat(data); } inline operator const double() { return toDouble(data); } inline const half operator=(const float& rhs) { data = fromFloat(rhs); return *this; } inline const half operator=(const double& rhs) { data = fromDouble(rhs); return *this; } private: unsigned short data; static unsigned short fromFloat(float f); static float toFloat(short h); inline static unsigned short fromDouble(double d) { return fromFloat((float)d); } inline static double toDouble(short h) { return (double)toFloat(h); } }; std::ostream& operator<<(std::ostream& os, half h) { os << (float)h; } std::istream& operator>>(std::istream& is, half& h) { float f; is >> f; h = f; }
Ultimately, the real meat of the class lies in the toFloat() and fromFloat() functions, with which I need help. I managed to find many examples of 16-bit float implementations, but none of them mention whether they can be loaded into OpenGL or not.
What are some issues I should know about when loading a 16-bit float in OpenGL? Is there a half-float implementation that specifically solves these problems?
EDIT: for a popular query, here's how my vertex data is generated, loaded and displayed.
Here's how the data is defined in the WireCubeEntity class:
VertexHalf vertices[8] = { vec3(-1.0f, -1.0f, -1.0f), vec3(1.0f, -1.0f, -1.0f), vec3(1.0f, 1.0f, -1.0f), vec3(-1.0f, 1.0f, -1.0f), vec3(-1.0f, -1.0f, 1.0f), vec3(1.0f, -1.0f, 1.0f), vec3(1.0f, 1.0f, 1.0f), vec3(-1.0f, 1.0f, 1.0f) }; unsigned char indices[24] = { 0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7 }; va.load(GL_LINES, VF_BASICHALF, 8, vertices, GL_UNSIGNED_BYTE, 24, indices);
where va is an instance of VertexArray. va.load defined as:
MappedBuffers VertexArray::load(GLenum primitive, VertexFormat vertexFormat, unsigned int vertexCount, void* vertices, GLenum indexFormat, unsigned int indexCount, void* indices) { MappedBuffers ret; if (primitive > GL_TRIANGLE_FAN) { error("in VertexFormat::load():\n"); errormore("Invalid enum '%i' passed to 'primitive'.\n", primitive); return ret; } clean(); glGenVertexArrays(1, &vao); bindArray(); glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, vertexSize(vertexFormat) * vertexCount, vertices, GL_STATIC_DRAW); if (!vertices) ret.vmap = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY); prim = primitive; vformat = vertexFormat; vcount = vertexCount; if (indexSize(indexFormat) != 0) { glGenBuffers(1, &ibo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexSize(indexFormat) * indexCount, indices, GL_STATIC_DRAW); if (!indices) ret.imap = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY); iformat = indexFormat; icount = indexCount; } switch (vformat) { case VF_BASIC: glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); break; case VF_32: glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glEnableVertexAttribArray(2); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, vertexSize(VF_32), (void*)0); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, vertexSize(VF_32), (void*)12); glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, vertexSize(VF_32), (void*)20); break; case VF_BASICHALF: glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_HALF_FLOAT, GL_FALSE, 0, (void*)0); break; case VF_WITHTANGENTS: glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glEnableVertexAttribArray(2); glEnableVertexAttribArray(3); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, vertexSize(VF_WITHTANGENTS), (void*)0); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, vertexSize(VF_WITHTANGENTS), (void*)12); glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, vertexSize(VF_WITHTANGENTS), (void*)20); glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, vertexSize(VF_WITHTANGENTS), (void*)32); break; default: error("In VertexFormat::load():\n"); errormore("Invalid enum '%i' passed to vertexFormat.\n", (int)vformat); clean(); return MappedBuffers(); } unbindArray(); if (vertices) ready = true; return ret; }
I know a pretty heavy function. MappedBuffers is just a structure containing 2 pointers, so if I pass NULL data to VertexArray::load() , I can use pointers to load data directly from a file into buffers (possibly from another stream). vertexSize is a function that returns sizeof() the particular format that I am passing, or 0 for an invalid format.
VertexHalf Structure:
struct VertexHalf { VertexHalf(void) {} VertexHalf(vec3 _pos) :x(_pos.x), y(_pos.y), z(_pos.z) {} VertexHalf(float _x, float _y, float _z) : x(_x), y(_y), z(_z) {} half x, y, z, padding; };
And finally, the data is displayed using VertexArray , which we downloaded earlier:
void VertexArray::draw(void) { if (ready == false) return; bindArray(); if (ibo == 0) glDrawArrays(prim, 0, vcount); else glDrawElements(prim, icount, iformat, NULL); unbindArray(); }