Back to index

nux  3.0.0
Public Member Functions | Protected Member Functions | Protected Attributes | Private Attributes
nux::TemplateQuadBuffer Class Reference

#include <GLTemplatePrimitiveBuffer.h>

Collaboration diagram for nux::TemplateQuadBuffer:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 TemplateQuadBuffer (GpuDevice *, ShaderType Type=SHADER_TYPE_GLSL, int NumQuads=256)
 ~TemplateQuadBuffer ()
void BindAttribute (INT AttributeLocation, UINT AttributeIndex)
 Bind GLSL parameter.
void UnBindAttribute (INT AttributeLocation)
 Bind NVidia CG parameter.
void UnBind ()
void Render (INT NumPrimitives)
void SetPerQuadAttribute (UINT AttributeIndex, INT Num, Vector4 *)
 Set the Vertices's attribute on a per quad basis.
void SetPerVertexAttribute (UINT AttributeIndex, INT Num, Vector4 *pVector)
 Set the Vertices's attribute on a per vertex basis.
void UnSetQuadAttribute (UINT AttributeIndex)
void SetNumQuads (int NumQuads)
int GetNumQuads () const

Protected Member Functions

void FormatQuads ()

Protected Attributes

ObjectPtr< IOpenGLIndexBufferm_IB

Private Attributes

ShaderType m_ShaderType
ObjectPtr< IOpenGLVertexBufferVertexAttributeBuffer [16]
GpuDevicem_pDeviceFactory
INT m_NumVertex
INT m_NumQuad

Detailed Description

Definition at line 41 of file GLTemplatePrimitiveBuffer.h.


Constructor & Destructor Documentation

nux::TemplateQuadBuffer::TemplateQuadBuffer ( GpuDevice pDeviceFactory,
ShaderType  Type = SHADER_TYPE_GLSL,
int  NumQuads = 256 
)

Definition at line 29 of file GLTemplatePrimitiveBuffer.cpp.

    :   m_ShaderType(Type)
  {
    Memset(VertexAttributeBuffer, 0, 16);

    nuxAssert(pDeviceFactory);
    m_pDeviceFactory = pDeviceFactory;
    m_NumQuad = NumQuads;
    nuxAssert(m_NumQuad > 0);

    if (m_NumQuad < 0)
      m_NumQuad = 256;

    m_NumVertex = m_NumQuad * 6;

    FormatQuads();
  }

Here is the call graph for this function:

Definition at line 47 of file GLTemplatePrimitiveBuffer.cpp.

  {
    m_IB.Release();

    for (unsigned int i = 0; i < 16; i++)
    {
      VertexAttributeBuffer[i].Release();
    }
  }

Here is the call graph for this function:


Member Function Documentation

void nux::TemplateQuadBuffer::BindAttribute ( INT  AttributeLocation,
UINT  AttributeIndex 
)

Bind GLSL parameter.

Definition at line 177 of file GLTemplatePrimitiveBuffer.cpp.

  {
    if (AttributeLocation < 0)
    {
      // Exclude Attribute Location that are not valid. Not valid does not mean the program is wrong!
      return;
    }

    nuxAssert(AttributeIndex >= 0); // Index 0 is the vertex position attribute. This on is set in the constructor.
    nuxAssert(AttributeIndex < 16);
    nuxAssert(AttributeLocation >= 0);


    if (VertexAttributeBuffer[AttributeIndex].IsNull())
      return;

    if (m_ShaderType == SHADER_TYPE_GLSL)
    {
      if (AttributeIndex == 0)
      {
        VertexAttributeBuffer[AttributeIndex]->BindVertexBuffer();
        CHECKGL(glEnableVertexAttribArrayARB(AttributeLocation));
        CHECKGL(glVertexAttribPointerARB((GLuint) AttributeLocation, 4, GL_FLOAT, GL_FALSE, 0, NUX_BUFFER_OFFSET(0)));
      }
      else
      {
        VertexAttributeBuffer[AttributeIndex]->BindVertexBuffer();
        CHECKGL(glEnableVertexAttribArrayARB(AttributeLocation));
        CHECKGL(glVertexAttribPointerARB((GLuint) AttributeLocation, 4, GL_FLOAT, GL_FALSE, 0, NUX_BUFFER_OFFSET(0)));
      }
    }
    else
    {
      nuxAssertMsg(0, "[TemplateQuadBuffer::BindCgAttribute] Use BindCGAttribute for CG attributes.");
    }
  }

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 57 of file GLTemplatePrimitiveBuffer.cpp.

  {
    // create vertex buffers for quad rendering
    // NumQuads * (4 vertices per Quad) * (4 Bytes per vertices)
    VertexAttributeBuffer[0] = m_pDeviceFactory->CreateVertexBuffer(m_NumQuad * 4 * 16, VBO_USAGE_DYNAMIC);

    // fill quad vertex buffer
    unsigned idx = 0;
    //BYTE* data0;
    float *data0;
    VertexAttributeBuffer[0]->Lock(0, m_NumQuad * 4 * 16, (void **) &data0);

    for (INT i = 0; i < m_NumQuad; ++i)
    {
      //         (0, 0) _________(1, 0)
      //                |      /|
      //                |     / |
      //                |    /  |
      //                |   /   |
      //                |  /    |
      //                | /     |
      //                |/      |
      //         (0, 1) --------- (1, 1)


      ((float *) data0) [idx++] = 0; // x
      ((float *) data0) [idx++] = 0; // y
      ((float *) data0) [idx++] = 0; // z
      ((float *) data0) [idx++] = 1.0f; // w


      ((float *) data0) [idx++] = 0; // x
      ((float *) data0) [idx++] = 1.0f; // y
      ((float *) data0) [idx++] = 0; // z
      ((float *) data0) [idx++] = 1; // w

      ((float *) data0) [idx++] = 1.0f; // x
      ((float *) data0) [idx++] = 1.0f; // y
      ((float *) data0) [idx++] = 0; // z
      ((float *) data0) [idx++] = 1; // w


      ((float *) data0) [idx++] = 1.0f; // x
      ((float *) data0) [idx++] = 0; // y
      ((float *) data0) [idx++] = 0; // z
      ((float *) data0) [idx++] = 1; // w


//         ((DWORD*)data0)[idx++]=((255*(0&1)))|((255*((0&2)>>1))<<8)|(0<<16)|(255<<24);
//         ((DWORD*)data0)[idx++]=((255*(1&1)))|((255*((1&2)>>1))<<8)|(0<<16)|(255<<24);
//         ((DWORD*)data0)[idx++]=((255*(3&1)))|((255*((3&2)>>1))<<8)|(0<<16)|(255<<24);
//         ((DWORD*)data0)[idx++]=((255*(2&1)))|((255*((2&2)>>1))<<8)|(0<<16)|(255<<24);
    }

    VertexAttributeBuffer[0]->Unlock();

    // create index buffers for quad rendering
    // NumQuads * (6 index per Quad) * (2 Bytes per index) : each quad is 2 triangles => 6 indices.
    m_IB = m_pDeviceFactory->CreateIndexBuffer(m_NumQuad * 6 * 2, VBO_USAGE_DYNAMIC, INDEX_FORMAT_USHORT);

    // Fill quad index buffer
    // The index buffers defines 2 triangles for each quad.
    // Triangles are arranged as strips(that is 6 triangle index for a quad).
    WORD *data1;
    m_IB->Lock(0, m_NumQuad * 6 * 2, (void **) &data1);

    for (INT i = 0; i < m_NumQuad; ++i)
    {
      //
      //            (0) _________(1)
      //                |      /|
      //                |     / |
      //                |    /  |
      //                |   /   |
      //                |  /    |
      //                | /     |
      //                |/      |
      //            (3) --------- (2)
      //
      ((WORD *) data1) [i*6+0] = i * 4 + 0;
      ((WORD *) data1) [i*6+1] = i * 4 + 1;
      ((WORD *) data1) [i*6+2] = i * 4 + 3;
      ((WORD *) data1) [i*6+3] = i * 4 + 3;
      ((WORD *) data1) [i*6+4] = i * 4 + 1;
      ((WORD *) data1) [i*6+5] = i * 4 + 2;

    }

    m_IB->Unlock();
  }

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 166 of file GLTemplatePrimitiveBuffer.cpp.

  {
    return m_NumQuad;
  }
void nux::TemplateQuadBuffer::Render ( INT  NumPrimitives)

Definition at line 291 of file GLTemplatePrimitiveBuffer.cpp.

  {
    INT NumVertex = NumPrimitives * 6;  // each quad is 2 triangles = 6 indices

    if (NumVertex > m_NumVertex)
    {
      NumVertex = m_NumVertex;
    }

    m_IB->BindIndexBuffer();
    CHECKGL(glDrawElements( GL_TRIANGLES, NumVertex, GL_UNSIGNED_SHORT,  NUX_BUFFER_OFFSET(0)));
    m_pDeviceFactory->InvalidateIndexBuffer();
  }

Here is the call graph for this function:

Here is the caller graph for this function:

void nux::TemplateQuadBuffer::SetNumQuads ( int  NumQuads)

Definition at line 148 of file GLTemplatePrimitiveBuffer.cpp.

  {
    m_NumQuad = NumQuads;
    nuxAssert(m_NumQuad > 0);

    if (m_NumQuad < 0)
      m_NumQuad = 256;

    m_NumVertex = m_NumQuad * 6;

    m_IB = ObjectPtr<IOpenGLIndexBuffer> (0);;

    for (int i = 0; i < 16; i++)
      VertexAttributeBuffer[i] = ObjectPtr<IOpenGLVertexBuffer> (0);

    FormatQuads();
  }

Here is the call graph for this function:

void nux::TemplateQuadBuffer::SetPerQuadAttribute ( UINT  AttributeIndex,
INT  Num,
Vector4 pVector 
)

Set the Vertices's attribute on a per quad basis.

Set the Vertices's attribute on a per quad basis. All vertex of the quad will have the same value for the attribute index.

Definition at line 305 of file GLTemplatePrimitiveBuffer.cpp.

  {
    nuxAssert(AttributeIndex > 0); // Index 0 is the vertex position attribute. This on is set in the constructor.
    nuxAssert(AttributeIndex < 16);

    // Destroy the vertex buffer by setting it to NULL;
    VertexAttributeBuffer[AttributeIndex] = ObjectPtr<IOpenGLVertexBuffer> (0);;

    VertexAttributeBuffer[AttributeIndex] = m_pDeviceFactory->CreateVertexBuffer(m_NumQuad * 4 * sizeof(Vector4), VBO_USAGE_DYNAMIC);

    FLOAT *data;
    VertexAttributeBuffer[AttributeIndex]->Lock(0, m_NumQuad * 4 * sizeof(Vector4), (void **) &data);

    INT i;

    for (i = 0; (i < m_NumQuad) && (i <  Num); i++)
    {
      for (INT j = 0; j < 4; j++)
      {
        data[16*i + 4*j + 0] = pVector[i].x;
        data[16*i + 4*j + 1] = pVector[i].y;
        data[16*i + 4*j + 2] = pVector[i].z;
        data[16*i + 4*j + 3] = pVector[i].w;
      }
    }

    while (i < m_NumQuad)
    {
      // this happens if Num < m_NumQuad.
      // Fill the rest with the last element of the parameter array passed as argument of the function.
      for (INT j = 0; j < 4; j++)
      {
        data[16*i + 4*j + 0] = pVector[Num-1].x;
        data[16*i + 4*j + 1] = pVector[Num-1].y;
        data[16*i + 4*j + 2] = pVector[Num-1].z;
        data[16*i + 4*j + 3] = pVector[Num-1].w;
      }

      i++;
    }

    VertexAttributeBuffer[AttributeIndex]->Unlock();
  }

Here is the call graph for this function:

void nux::TemplateQuadBuffer::SetPerVertexAttribute ( UINT  AttributeIndex,
INT  Num,
Vector4 pVector 
)

Set the Vertices's attribute on a per vertex basis.

Set the vertex attribute on a per vertex basis.

Definition at line 349 of file GLTemplatePrimitiveBuffer.cpp.

  {
    nuxAssert(AttributeIndex > 0); // Index 0 is the vertex position attribute. This on is set in the constructor.
    nuxAssert(AttributeIndex < 16);

    // Destroy the vertex buffer by setting it to NULL;
    VertexAttributeBuffer[AttributeIndex] = ObjectPtr<IOpenGLVertexBuffer> (0);;

    VertexAttributeBuffer[AttributeIndex] = m_pDeviceFactory->CreateVertexBuffer(m_NumQuad * 4 * sizeof(Vector4), VBO_USAGE_DYNAMIC);

    FLOAT *data;
    VertexAttributeBuffer[AttributeIndex]->Lock(0, m_NumQuad * 4 * sizeof(Vector4), (void **) &data);

    INT i;

    for (i = 0; (i < m_NumQuad * 4) && (i <  Num); i++)
    {
      data[4*i+0] = pVector[i].x;
      data[4*i+1] = pVector[i].y;
      data[4*i+2] = pVector[i].z;
      data[4*i+3] = pVector[i].w;
    }

    while (i < m_NumQuad * 4)
    {
      // this happens if Num < m_NumQuad.
      // Fill the rest with the last element of the parameter array passed as argument of the function.
      data[4*i+0] = pVector[Num-1].x;
      data[4*i+1] = pVector[Num-1].y;
      data[4*i+2] = pVector[Num-1].z;
      data[4*i+3] = pVector[Num-1].w;
      i++;
    }

    VertexAttributeBuffer[AttributeIndex]->Unlock();
  }

Here is the call graph for this function:

Definition at line 171 of file GLTemplatePrimitiveBuffer.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void nux::TemplateQuadBuffer::UnBindAttribute ( INT  AttributeLocation)

Bind NVidia CG parameter.

Definition at line 253 of file GLTemplatePrimitiveBuffer.cpp.

  {
    if (AttributeLocation < 0)
    {
      // Exclude Attribute Location that are not valid. Not valid does not mean the program is wrong!
      return;
    }

    if (m_ShaderType == SHADER_TYPE_GLSL)
    {
      CHECKGL(glDisableVertexAttribArrayARB(AttributeLocation));
    }
    else
    {
      nuxAssertMsg(0, "[TemplateQuadBuffer::UnBindAttribute] Use UnBindCGAttribute for CG attributes.");
    }
  }

Here is the caller graph for this function:

Definition at line 386 of file GLTemplatePrimitiveBuffer.cpp.

  {

  }

Member Data Documentation

Definition at line 76 of file GLTemplatePrimitiveBuffer.h.

Definition at line 85 of file GLTemplatePrimitiveBuffer.h.

Definition at line 84 of file GLTemplatePrimitiveBuffer.h.

Definition at line 83 of file GLTemplatePrimitiveBuffer.h.

Definition at line 81 of file GLTemplatePrimitiveBuffer.h.

Definition at line 82 of file GLTemplatePrimitiveBuffer.h.


The documentation for this class was generated from the following files: