~ubuntu-branches/ubuntu/oneiric/nux/oneiric

« back to all changes in this revision

Viewing changes to NuxGraphics/IOpenGLVertexBuffer.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2010-11-18 19:17:32 UTC
  • Revision ID: james.westby@ubuntu.com-20101118191732-rn35790vekj6o4my
Tags: upstream-0.9.4
ImportĀ upstreamĀ versionĀ 0.9.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2010 Inalogic Inc.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU Lesser General Public License version 3, as
 
6
 * published by the  Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 
10
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 
11
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
 
12
 * License for more details.
 
13
 *
 
14
 * You should have received a copy of both the GNU Lesser General Public
 
15
 * License version 3 along with this program.  If not, see
 
16
 * <http://www.gnu.org/licenses/>
 
17
 *
 
18
 * Authored by: Jay Taoko <jay.taoko_AT_gmail_DOT_com>
 
19
 *
 
20
 */
 
21
 
 
22
 
 
23
#include "GpuDevice.h"
 
24
#include "GLDeviceObjects.h"
 
25
#include "IOpenGLVertexBuffer.h"
 
26
 
 
27
namespace nux
 
28
{
 
29
 
 
30
  NUX_IMPLEMENT_OBJECT_TYPE (IOpenGLVertexBuffer);
 
31
 
 
32
  IOpenGLVertexBuffer::IOpenGLVertexBuffer (t_u32 Length, VBO_USAGE Usage, NUX_FILE_LINE_DECL)
 
33
    :   IOpenGLResource (RTVERTEXBUFFER, NUX_FILE_LINE_PARAM)
 
34
    ,   _Length (Length)
 
35
    ,   _Usage (Usage)
 
36
    ,   _MemMap (0)
 
37
    ,   _OffsetToLock (0)
 
38
    ,   _SizeToLock (0)
 
39
  {
 
40
    CHECKGL ( glGenBuffersARB (1, &_OpenGLID) );
 
41
    CHECKGL ( glBindBufferARB (GL_ARRAY_BUFFER_ARB, _OpenGLID) );
 
42
    CHECKGL ( glBufferDataARB (GL_ARRAY_BUFFER_ARB, _Length, NULL, Usage) );
 
43
    CHECKGL ( glBindBufferARB (GL_ARRAY_BUFFER_ARB, 0) );
 
44
    GRunTimeStats.Register (this);
 
45
  }
 
46
 
 
47
  IOpenGLVertexBuffer::~IOpenGLVertexBuffer()
 
48
  {
 
49
    CHECKGL ( glDeleteBuffersARB (1, &_OpenGLID) );
 
50
    _OpenGLID = 0;
 
51
    GRunTimeStats.UnRegister (this);
 
52
  }
 
53
 
 
54
  int IOpenGLVertexBuffer::Lock (
 
55
    t_u32 OffsetToLock,
 
56
    t_u32 SizeToLock,
 
57
    void **ppbData)
 
58
  {
 
59
    nuxAssert (SizeToLock <= _Length);
 
60
    nuxAssert (OffsetToLock + SizeToLock <= _Length);
 
61
 
 
62
    if (SizeToLock == 0)
 
63
    {
 
64
      if (OffsetToLock == 0)
 
65
      {
 
66
        // lock the entire buffer
 
67
        SizeToLock = _Length;
 
68
      }
 
69
      else
 
70
      {
 
71
        nuxDebugMsg (TEXT ("[IOpenGLVertexBuffer::Lock] Invalid parameters.") );
 
72
        return OGL_INVALID_CALL;
 
73
      }
 
74
    }
 
75
 
 
76
    // If _MemMap, _OffsetToLock and _SizeToLock are not equal to zero, then we have already mapped the buffer
 
77
    // Unlock it before locking again.
 
78
    nuxAssert (_MemMap == 0);
 
79
    nuxAssert (_OffsetToLock == 0);
 
80
    nuxAssert (_SizeToLock == 0);
 
81
 
 
82
    CHECKGL ( glBindBufferARB (GL_ARRAY_BUFFER_ARB, _OpenGLID) );
 
83
    // Map the Entire buffer into system memory
 
84
    _MemMap = (BYTE *) glMapBufferARB (GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB);
 
85
    CHECKGL_MSG (glMapBufferARB);
 
86
    *ppbData = (void *) (_MemMap + OffsetToLock);
 
87
 
 
88
    _OffsetToLock   = OffsetToLock;
 
89
    _SizeToLock     = SizeToLock;
 
90
 
 
91
    CHECKGL ( glBindBufferARB (GL_ARRAY_BUFFER_ARB, 0) );
 
92
 
 
93
    return OGL_OK;
 
94
  }
 
95
 
 
96
  int IOpenGLVertexBuffer::Unlock()
 
97
  {
 
98
    CHECKGL ( glBindBufferARB (GL_ARRAY_BUFFER_ARB, _OpenGLID) );
 
99
    //CHECKGL( glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, _OffsetToLock, _SizeToLock, _MemMap) );
 
100
 
 
101
    CHECKGL ( glUnmapBufferARB (GL_ARRAY_BUFFER_ARB) );
 
102
    CHECKGL ( glBindBufferARB (GL_ARRAY_BUFFER_ARB, 0) );
 
103
 
 
104
    _MemMap         = 0;
 
105
    _OffsetToLock   = 0;
 
106
    _SizeToLock     = 0;
 
107
    return OGL_OK;
 
108
  }
 
109
 
 
110
  void IOpenGLVertexBuffer::BindVertexBuffer()
 
111
  {
 
112
    CHECKGL (glBindBufferARB (GL_ARRAY_BUFFER_ARB, _OpenGLID) );
 
113
  }
 
114
 
 
115
  t_u32 IOpenGLVertexBuffer::GetSize()
 
116
  {
 
117
    return _Length;
 
118
  }
 
119
 
 
120
}