VIZ++ Class: OpenGLBuffer

 VIZ++ Class Library  VIZ++ Samples  VIZ++ ClassTree 

Source code

/*
 * OpenGLBuffer.h 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


#pragma once

#include <viz++/opengl/OpenGLObject.h>
#include <viz++/opengl/OpenGLClientState.h>

namespace VIZ {

class OpenGLBuffer :public OpenGLObject {
private:
  GLuint id;
  GLenum target;
  GLsizei size;

public:
  OpenGLBuffer(GLenum target= GL_ARRAY_BUFFER) //_ARB)
//  OpenGLBuffer(GLenum target= GL_ARRAY_BUFFER_ARB)
  :OpenGLObject(),
  id(0),
  target(0),
  size(0)
  {
    glGenBuffers(1, &id);
    this -> target = target;
    size  = 0;
  } 


public:
  ~OpenGLBuffer()
  {
    glDeleteBuffers(1, &id);
  }

  void bind() 
  {
    glBindBuffer(this -> target, id);
  } 

  void unbind() 
  {
    glBindBuffer(this -> target, 0);
  } 

  void data(GLsizei sizei, const void* data, GLenum usage=GL_DYNAMIC_DRAW) //_ARB)
  {
    if (sizei > 0 && data != NULL) {
      printf("data set\n");
      this -> size = sizei;
      glBufferData(this -> target, size, data, usage);
    }
  }

  void subData(GLint offset, GLsizei sizei, void* data)
  {  
    if (offset > 0 && sizei > 0 && data != NULL) {
      glBufferSubData(this ->target, offset, sizei, data);
    }
  }
  
  
  void drawArray(GLenum style)
  {
    bind();
    OpenGLClientState state(GL_VERTEX_ARRAY);
    state.enable();
    GLuint vertexNum = 3;
    
    glVertexPointer(vertexNum, GL_FLOAT, 0, 0);
  
    int dataNum = this -> size / (vertexNum*sizeof(GLfloat));
    printf("dataNum %d\n", dataNum);
    glDrawArrays(style, 0 , dataNum);
    state.disable();
    
    unbind();
   }
};
  
}


Last modified: 10 Feb 2017

Copyright (c) 2009-2017 Antillia.com ALL RIGHTS RESERVED.