You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.6 KiB

  1. #include "vertexbuffer.h"
  2. GLuint VertexBuffer::CreateBufferObject(GLenum bufferType, GLsizeiptr size, GLenum usage, void * data)
  3. {
  4. GLuint object;
  5. glGenBuffers(1, &object);
  6. glBindBuffer(bufferType, object);
  7. glBufferData(bufferType, size, data, usage);
  8. glBindBuffer(bufferType, 0);
  9. return object;
  10. }
  11. void VertexBuffer::SetSize(int vertexCount)
  12. {
  13. mVertexCount = vertexCount;
  14. mVertexes = new Vertex[mVertexCount];
  15. memset(mVertexes, 0, sizeof(Vertex)*mVertexCount);
  16. mVBO = CreateBufferObject(GL_ARRAY_BUFFER, sizeof(Vertex)*mVertexCount, GL_STATIC_DRAW, nullptr);
  17. }
  18. void VertexBuffer::SetPosition(int index, float x, float y, float z, float w) {
  19. mVertexes[index].Position[0] = x;
  20. mVertexes[index].Position[1] = y;
  21. mVertexes[index].Position[2] = z;
  22. mVertexes[index].Position[3] = w;
  23. }
  24. void VertexBuffer::SetColor(int index, float r, float g, float b, float a) {
  25. mVertexes[index].Color[0] = r;
  26. mVertexes[index].Color[1] = g;
  27. mVertexes[index].Color[2] = b;
  28. mVertexes[index].Color[3] = a;
  29. }
  30. void VertexBuffer::SetTexcoord(int index, float x, float y) {
  31. mVertexes[index].Texcoord[0] = x;
  32. mVertexes[index].Texcoord[1] = y;
  33. }
  34. void VertexBuffer::SetNormal(int index, float x, float y, float z) {
  35. mVertexes[index].Normal[0] = x;
  36. mVertexes[index].Normal[1] = y;
  37. mVertexes[index].Normal[2] = z;
  38. mVertexes[index].Normal[3] = 1.0;
  39. }
  40. void VertexBuffer::Bind() {
  41. glBindBuffer(GL_ARRAY_BUFFER, mVBO);
  42. glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(Vertex)*mVertexCount, mVertexes);
  43. }
  44. void VertexBuffer::Unbind() {
  45. glBindBuffer(GL_ARRAY_BUFFER, 0);
  46. }
  47. Vertex& VertexBuffer::Get(int index) {
  48. return mVertexes[index];
  49. }