2012-08-04 02:51:27 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// Copyright 2013 Pixar
|
2012-08-04 02:51:27 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
|
|
|
// with the following modification; you may not use this file except in
|
|
|
|
// compliance with the Apache License and the following modification to it:
|
|
|
|
// Section 6. Trademarks. is deleted and replaced with:
|
2012-08-04 02:51:27 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// 6. Trademarks. This License does not grant permission to use the trade
|
|
|
|
// names, trademarks, service marks, or product names of the Licensor
|
|
|
|
// and its affiliates, except as required to comply with Section 4(c) of
|
|
|
|
// the License and to reproduce the content of the NOTICE file.
|
2012-08-04 02:51:27 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// You may obtain a copy of the Apache License at
|
2012-08-04 02:51:27 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2013-07-18 21:19:50 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the Apache License with the above modification is
|
|
|
|
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
// KIND, either express or implied. See the Apache License for the specific
|
|
|
|
// language governing permissions and limitations under the Apache License.
|
2012-08-04 02:51:27 +00:00
|
|
|
//
|
2012-06-21 00:11:17 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
#include "../osd/cpuGLVertexBuffer.h"
|
2012-06-21 00:11:17 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
#include <string.h>
|
2012-06-21 00:11:17 +00:00
|
|
|
|
2015-04-27 18:27:05 +00:00
|
|
|
#include "../osd/opengl.h"
|
|
|
|
|
2012-06-21 00:11:17 +00:00
|
|
|
namespace OpenSubdiv {
|
|
|
|
namespace OPENSUBDIV_VERSION {
|
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
namespace Osd {
|
|
|
|
|
|
|
|
CpuGLVertexBuffer::CpuGLVertexBuffer(int numElements, int numVertices)
|
2012-12-11 01:15:13 +00:00
|
|
|
: _numElements(numElements), _numVertices(numVertices),
|
2013-02-04 18:57:19 +00:00
|
|
|
_vbo(0), _cpuBuffer(0), _dataDirty(true) {
|
2012-06-21 00:11:17 +00:00
|
|
|
}
|
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
CpuGLVertexBuffer::~CpuGLVertexBuffer() {
|
2012-12-11 01:15:13 +00:00
|
|
|
|
2013-02-03 18:42:18 +00:00
|
|
|
delete[] _cpuBuffer;
|
2015-04-27 18:27:05 +00:00
|
|
|
|
2014-01-31 01:29:14 +00:00
|
|
|
if (_vbo) {
|
|
|
|
glDeleteBuffers(1, &_vbo);
|
|
|
|
}
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
CpuGLVertexBuffer *
|
2015-04-27 18:27:05 +00:00
|
|
|
CpuGLVertexBuffer::Create(int numElements, int numVertices, void *) {
|
2014-09-05 22:07:46 +00:00
|
|
|
CpuGLVertexBuffer *instance =
|
|
|
|
new CpuGLVertexBuffer(numElements, numVertices);
|
2012-12-11 01:15:13 +00:00
|
|
|
if (instance->allocate()) return instance;
|
|
|
|
delete instance;
|
|
|
|
return NULL;
|
2012-06-21 00:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-27 18:27:05 +00:00
|
|
|
CpuGLVertexBuffer::UpdateData(const float *src,
|
|
|
|
int startVertex, int numVertices,
|
|
|
|
void * /*deviceContext*/) {
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2015-04-27 18:27:05 +00:00
|
|
|
memcpy(_cpuBuffer + startVertex * GetNumElements(), src,
|
|
|
|
GetNumElements() * numVertices * sizeof(float));
|
2013-02-04 18:57:19 +00:00
|
|
|
_dataDirty = true;
|
2012-06-21 00:11:17 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
int
|
2014-09-05 22:07:46 +00:00
|
|
|
CpuGLVertexBuffer::GetNumElements() const {
|
2012-12-11 01:15:13 +00:00
|
|
|
|
|
|
|
return _numElements;
|
|
|
|
}
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
int
|
2014-09-05 22:07:46 +00:00
|
|
|
CpuGLVertexBuffer::GetNumVertices() const {
|
2012-06-21 00:11:17 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
return _numVertices;
|
2012-06-21 00:11:17 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
float*
|
2014-09-05 22:07:46 +00:00
|
|
|
CpuGLVertexBuffer::BindCpuBuffer() {
|
2014-01-31 01:29:14 +00:00
|
|
|
|
2013-02-04 18:57:19 +00:00
|
|
|
_dataDirty = true; // caller might modify data
|
2012-12-11 01:15:13 +00:00
|
|
|
return _cpuBuffer;
|
2012-08-04 02:51:27 +00:00
|
|
|
}
|
2012-06-21 00:11:17 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
GLuint
|
2015-04-27 18:27:05 +00:00
|
|
|
CpuGLVertexBuffer::BindVBO(void * /*deviceContext*/) {
|
2014-01-31 01:29:14 +00:00
|
|
|
|
2016-02-19 20:57:56 +00:00
|
|
|
if (! _dataDirty)
|
2013-02-04 18:57:19 +00:00
|
|
|
return _vbo;
|
|
|
|
|
2013-02-04 20:10:36 +00:00
|
|
|
int size = GetNumElements() * GetNumVertices() * sizeof(float);
|
2013-02-03 18:42:18 +00:00
|
|
|
|
2016-02-19 20:57:56 +00:00
|
|
|
if (! _vbo) {
|
2014-01-31 01:29:14 +00:00
|
|
|
glGenBuffers(1, &_vbo);
|
|
|
|
}
|
|
|
|
|
2013-02-03 18:42:18 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, size, _cpuBuffer, GL_STATIC_DRAW);
|
2013-03-08 01:50:15 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
2012-12-11 01:15:13 +00:00
|
|
|
|
2013-02-04 18:57:19 +00:00
|
|
|
_dataDirty = false;
|
2012-12-11 01:15:13 +00:00
|
|
|
return _vbo;
|
2012-06-21 00:11:17 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
bool
|
2014-09-05 22:07:46 +00:00
|
|
|
CpuGLVertexBuffer::allocate() {
|
2014-01-31 01:29:14 +00:00
|
|
|
|
2013-02-04 20:10:36 +00:00
|
|
|
_cpuBuffer = new float[GetNumElements() * GetNumVertices()];
|
2013-02-04 18:57:19 +00:00
|
|
|
_dataDirty = true;
|
2013-11-06 18:46:36 +00:00
|
|
|
return true;
|
2012-06-21 00:11:17 +00:00
|
|
|
}
|
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
} // end namespace Osd
|
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
} // end namespace OPENSUBDIV_VERSION
|
|
|
|
} // end namespace OpenSubdiv
|