Browse Source

MeshTools: return pointer to buffer from fullScreenTriangle().

On systems without VAOs and without gl_VertexID the buffer needs to be
bound before every draw call. This way the buffer reference would become
dangling one, because the buffer is moved out somewhere else. Now
creating it on heap and returning pointer instead.

This multiple code path implementation is really starting to pay out,
yay!
pull/23/head
Vladimír Vondruš 13 years ago
parent
commit
d579e0e2c6
  1. 11
      src/MeshTools/FullScreenTriangle.cpp
  2. 6
      src/MeshTools/FullScreenTriangle.h

11
src/MeshTools/FullScreenTriangle.cpp

@ -32,25 +32,28 @@
namespace Magnum { namespace MeshTools {
std::pair<Buffer, Mesh> fullScreenTriangle() {
std::pair<Buffer*, Mesh> fullScreenTriangle() {
Mesh mesh;
mesh.setPrimitive(Mesh::Primitive::Triangles)
.setVertexCount(3);
Buffer buffer;
Buffer* buffer = nullptr;
#ifndef MAGNUM_TARGET_GLES
if(!Context::current()->isVersionSupported(Version::GL300))
#else
if(!Context::current()->isVersionSupported(Version::GLES300))
#endif
{
buffer = new Buffer;
constexpr Vector2 triangle[] = {
Vector2(-1.0, 1.0),
Vector2(-1.0, -3.0),
Vector2( 3.0, 1.0)
};
buffer.setData(triangle, Buffer::Usage::StaticDraw);
mesh.addVertexBuffer(buffer, 0, AbstractShaderProgram::Attribute<0, Vector2>());
buffer->setData(triangle, Buffer::Usage::StaticDraw);
/** @todo Is it possible to attach moveable buffer here to avoid heap
allocation? OTOH this is more effective in most (modern) cases */
mesh.addVertexBuffer(*buffer, 0, AbstractShaderProgram::Attribute<0, Vector2>());
}
return {std::move(buffer), std::move(mesh)};

6
src/MeshTools/FullScreenTriangle.h

@ -52,8 +52,8 @@ computed from them. The vertex positions are, in order:
On OpenGL 2.1 and OpenGL ES 2.0 the vertex positions are passed explicitly as
attribute `0`, contained in the buffer. On OpenGL 3.0+ and OpenGL ES 3.0+ the
mesh is attribute-less and the vertex positions can be computed using
`gl_VertexID` builtin shader variable, thus the returned vertex buffer is empty
and basically useless.
`gl_VertexID` builtin shader variable, thus `nullptr` is returned instead of
vertex buffer.
Computing positions in vertex shader in a portable way might be done like this.
For OpenGL 2.1 and OpenGL ES 2.0 you then need to bind the location of `position`
@ -80,7 +80,7 @@ void main() {
@attention The implementation needs to check OpenGL version, so it expects
active context.
*/
std::pair<Buffer, Mesh> MAGNUM_MESHTOOLS_EXPORT fullScreenTriangle();
std::pair<Buffer*, Mesh> MAGNUM_MESHTOOLS_EXPORT fullScreenTriangle();
}}

Loading…
Cancel
Save