# Basic vbo mesh question

**URL:** <https://discourse.libcinder.org/t/basic-vbo-mesh-question/1279>\
**Category:** Using Cinder\
**Created:** [August 26, 2018, 2:49am UTC](https://discourse.libcinder.org/t/basic-vbo-mesh-question/1279 "2018-08-26T02:49:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marie](https://avatars.discourse-cdn.com/v4/letter/m/bc8723/32.png) [@marie](https://discourse.libcinder.org/u/marie)\
**Post date:** [August 26, 2018, 2:49am UTC](https://discourse.libcinder.org/t/basic-vbo-mesh-question/1279/1 "2018-08-26T02:49:39Z")

</div>

I’m trying to understand VboMesh by drawing a cube but am having difficulty with normals. I’ve got a camera set up, and I’m drawing a cube, here called Box. Below is my Box definition and a screenshot of the results. What might I be doing wrong?

```
#include "cinder/app/App.h"
#include "Box.h"

using namespace ci;
using namespace std;

Box::Box() {}

void Box::init() {
    
    vector < uint32_t > mIndices;
    vector < vec3 > mPositions;
    vector < vec3 > mNormals;
    
    gl::VboMeshRef mBox;
    gl::GlslProgRef mShader;
    
    float X = 1.0f;
    float Y = 1.0f;
    float Z = 1.0f;
    
    mPositions.push_back( vec3( -X, -Y, -Z ) );
    mPositions.push_back( vec3( -X, -Y, Z ) );
    mPositions.push_back( vec3( X, -Y, Z ) );
    mPositions.push_back( vec3( X, -Y, -Z ) );
    mPositions.push_back( vec3( -X, Y, -Z ) );
    mPositions.push_back( vec3( -X, Y, Z ) );
    mPositions.push_back( vec3( X, Y, Z ) );
    mPositions.push_back( vec3( X, Y, -Z ) );
    
    mIndices.push_back( 0 );
    mIndices.push_back( 1 );
    mIndices.push_back( 3 );
    
    mIndices.push_back( 1 );
    mIndices.push_back( 2 );
    mIndices.push_back( 3 );
    
    mIndices.push_back( 4 );
    mIndices.push_back( 7 );
    mIndices.push_back( 5 );
    
    mIndices.push_back( 7 );
    mIndices.push_back( 6 );
    mIndices.push_back( 5 );
    
    mIndices.push_back( 0 );
    mIndices.push_back( 4 );
    mIndices.push_back( 1 );
    
    mIndices.push_back( 4 );
    mIndices.push_back( 5 );
    mIndices.push_back( 1 );
    
    mIndices.push_back( 2 );
    mIndices.push_back( 6 );
    mIndices.push_back( 3 );
    
    mIndices.push_back( 6 );
    mIndices.push_back( 7 );
    mIndices.push_back( 3 );
    
    mIndices.push_back( 1 );
    mIndices.push_back( 5 );
    mIndices.push_back( 2 );
    
    mIndices.push_back( 5 );
    mIndices.push_back( 6 );
    mIndices.push_back( 2 );
    
    mIndices.push_back( 3 );
    mIndices.push_back( 7 );
    mIndices.push_back( 0 );
    
    mIndices.push_back( 7 );
    mIndices.push_back( 4 );
    mIndices.push_back( 0 );
    
    mNormals.push_back( vec3( 0, -1, 0 ) );
    mNormals.push_back( vec3( 0, 1, 0 ) );
    mNormals.push_back( vec3( -1, 0, 0 ) );
    mNormals.push_back( vec3( 1, 0, 0 ) );
    mNormals.push_back( vec3( 0, 0, 1 ) );
    mNormals.push_back( vec3( 0, 0, -1 ) );

    
    vector < gl::VboMesh::Layout > bufferLayout = {
    gl::VboMesh::Layout().usage( GL_STATIC_DRAW ).attrib( geom::Attrib::POSITION , 3 ),
    gl::VboMesh::Layout().usage( GL_STATIC_DRAW ).attrib( geom::Attrib::NORMAL , 3 )
    };
    
    mBox = gl::VboMesh::create( mPositions.size(), GL_TRIANGLES, bufferLayout, mIndices.size(), GL_UNSIGNED_INT );
    
    mBox->bufferAttrib( geom::Attrib::POSITION, mPositions );
    mBox->bufferAttrib( geom::Attrib::NORMAL, mNormals );
    mBox->bufferIndices( mIndices.size() * sizeof( uint32_t ), mIndices.data() );
    
    auto scale = geom::Scale(3,3,3); // scale from normalized space to viewing space
    mShader = gl::getStockShader( gl::ShaderDef().lambert() ); // using a Cinder generic shader, since we always need a shader to draw
    mBatch = gl::Batch::create( mBox , mShader );
    
}

void Box::draw() {
    mBatch->draw();
}

```

 ![09%20PM](https://canada1.discourse-cdn.com/flex030/uploads/libcinder/original/1X/5df318f84c7f447c10d68d73086b6a480ac4c558.jpeg)

---

<div class="post-metadata">

**Author:** ![lithium](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/lithium/32/38_2.png) [@lithium](https://discourse.libcinder.org/u/lithium)\
**Post date:** [August 26, 2018, 3:46am UTC](https://discourse.libcinder.org/t/basic-vbo-mesh-question/1279/2 "2018-08-26T03:46:39Z")

</div>

You actually need more vertices and normals than you think for a cube. Each corner needs to have 3 separate normals to work correctly; i.e one that runs along each of the 3 axes.

![](https://canada1.discourse-cdn.com/flex030/uploads/libcinder/original/1X/68dbce5f2ebdc450f4bd78f94b24303a9ba28021.jpeg)

Have a look at the vertices and normals that the `geom::Cube()` primitive generates to see what I mean.

Looking at your code, it seems to me that this is more an educational exercise to work with `VboMesh`es directly, but just in case all you’re after is a cube, I should tell you you can do the following and save yourself a lot of trouble.

```cpp
    mShader = gl::getStockShader( gl::ShaderDef().lambert() ); // using a Cinder generic shader, since we always need a shader to draw
    mBatch = gl::Batch::create( geom::Cube() >> geom::Scale(3,3,3) , mShader );

```

---

<div class="post-metadata">

**Author:** ![paul.houx](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/paul.houx/32/31_2.png) [@paul.houx](https://discourse.libcinder.org/u/paul.houx)\
**Post date:** [August 26, 2018, 5:42am UTC](https://discourse.libcinder.org/t/basic-vbo-mesh-question/1279/3 "2018-08-26T05:42:06Z")

</div>

Correct, a proper cube requires 24 vertices and normals (6 faces \* 4 vertices). Apart from that, your code is flawless.

Want another interesting challenge? Try a cone. You’ll find out it’s much harder than you think.

---

<div class="post-metadata">

**Author:** ![marie](https://avatars.discourse-cdn.com/v4/letter/m/bc8723/32.png) [@marie](https://discourse.libcinder.org/u/marie)\
**Post date:** [August 26, 2018, 5:18pm UTC](https://discourse.libcinder.org/t/basic-vbo-mesh-question/1279/4 "2018-08-26T17:18:07Z")

</div>

Ah, okay. Yes, I’m doing this as a vbo mesh to learn things like this about working in OpenGL and Cinder. Thank you both so much.
