Work on new OpenGL example - Splitting into several files.
This commit is contained in:
98
examples/OpenGL/OpenGLVertexBufferObjectSupport.cpp
Normal file
98
examples/OpenGL/OpenGLVertexBufferObjectSupport.cpp
Normal file
@ -0,0 +1,98 @@
|
||||
#include "OpenGLVertexBufferObjectSupport.h"
|
||||
|
||||
#include "PolyVoxCore/IndexedSurfacePatch.h"
|
||||
|
||||
using namespace PolyVox;
|
||||
using namespace std;
|
||||
|
||||
OpenGLSurfacePatch BuildOpenGLSurfacePatch(IndexedSurfacePatch& isp)
|
||||
{
|
||||
OpenGLSurfacePatch result;
|
||||
|
||||
const vector<SurfaceVertex>& vecVertices = isp.getVertices();
|
||||
const vector<uint32>& vecIndices = isp.getIndices();
|
||||
|
||||
glGenBuffers(1, &result.indexBuffer);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, result.indexBuffer);
|
||||
int s = vecIndices.size() * sizeof(GLint);
|
||||
if(s != 0)
|
||||
{
|
||||
GLvoid* blah = (GLvoid*)(&(vecIndices[0]));
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, s, blah, GL_STATIC_DRAW);
|
||||
}
|
||||
|
||||
result.noOfIndices = vecIndices.size();
|
||||
|
||||
glGenBuffers(1, &result.vertexBuffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, result.vertexBuffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, vecVertices.size() * sizeof(GLfloat) * 9, 0, GL_STATIC_DRAW);
|
||||
GLfloat* ptr = (GLfloat*)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
|
||||
|
||||
for(vector<SurfaceVertex>::const_iterator iterVertex = vecVertices.begin(); iterVertex != vecVertices.end(); ++iterVertex)
|
||||
{
|
||||
const SurfaceVertex& vertex = *iterVertex;
|
||||
const Vector3DFloat& v3dVertexPos = vertex.getPosition();
|
||||
//const Vector3DFloat v3dRegionOffset(uRegionX * g_uRegionSideLength, uRegionY * g_uRegionSideLength, uRegionZ * g_uRegionSideLength);
|
||||
const Vector3DFloat v3dFinalVertexPos = v3dVertexPos + static_cast<Vector3DFloat>(isp.m_v3dRegionPosition);
|
||||
|
||||
*ptr = v3dFinalVertexPos.getX();
|
||||
ptr++;
|
||||
*ptr = v3dFinalVertexPos.getY();
|
||||
ptr++;
|
||||
*ptr = v3dFinalVertexPos.getZ();
|
||||
ptr++;
|
||||
|
||||
*ptr = vertex.getNormal().getX();
|
||||
ptr++;
|
||||
*ptr = vertex.getNormal().getY();
|
||||
ptr++;
|
||||
*ptr = vertex.getNormal().getZ();
|
||||
ptr++;
|
||||
|
||||
GLfloat red = 0.0f;
|
||||
GLfloat green = 0.0f;
|
||||
GLfloat blue = 0.0f;
|
||||
|
||||
uint8 material = vertex.getMaterial() + 0.5;
|
||||
|
||||
switch(material)
|
||||
{
|
||||
case 1:
|
||||
red = 1.0;
|
||||
green = 0.0;
|
||||
blue = 0.0;
|
||||
break;
|
||||
case 2:
|
||||
red = 0.0;
|
||||
green = 1.0;
|
||||
blue = 0.0;
|
||||
break;
|
||||
case 3:
|
||||
red = 0.0;
|
||||
green = 0.0;
|
||||
blue = 1.0;
|
||||
break;
|
||||
case 4:
|
||||
red = 1.0;
|
||||
green = 1.0;
|
||||
blue = 0.0;
|
||||
break;
|
||||
case 5:
|
||||
red = 1.0;
|
||||
green = 0.0;
|
||||
blue = 1.0;
|
||||
break;
|
||||
}
|
||||
|
||||
*ptr = red;
|
||||
ptr++;
|
||||
*ptr = green;
|
||||
ptr++;
|
||||
*ptr = blue;
|
||||
ptr++;
|
||||
}
|
||||
|
||||
glUnmapBuffer(GL_ARRAY_BUFFER);
|
||||
|
||||
return result;
|
||||
}
|
Reference in New Issue
Block a user