/OJGL/GLPrimitive.j

http://github.com/tartiflop/ojgl · Unknown · 131 lines · 98 code · 33 blank · 0 comment · 0 complexity · e21775471c79022ed366ce240a006593 MD5 · raw file

  1. @import <Foundation/CPObject.j>
  2. @import "GLContext.j"
  3. @import "GLRenderer.j"
  4. @import "GLMaterial.j"
  5. @implementation GLPrimitive : CPObject {
  6. Array _vertices;
  7. Array _normals;
  8. Array _uvs;
  9. Array _indices;
  10. int _vertexBufferId;
  11. int _normalBufferId;
  12. int _uvBufferId;
  13. int _indicesBufferId;
  14. GLMaterial _material;
  15. Matrix4D _transformation;
  16. }
  17. - (id)init:(GLMaterial)material {
  18. self = [super init];
  19. if (self) {
  20. _material = material;
  21. [_material setPrimitive:self];
  22. _transformation = new Matrix4D();
  23. }
  24. return self;
  25. }
  26. - (void)buildPrimitive {
  27. _vertices = [];
  28. _normals = [];
  29. _uvs = [];
  30. _indices = [];
  31. }
  32. - (void)prepareGL:(GLContext)glContext {
  33. // Initialise the material
  34. [_material prepareGL:glContext];
  35. // Create and initialise buffer data
  36. _vertexBufferId = [glContext createBufferFromArray:_vertices];
  37. _indicesBufferId = [glContext createBufferFromElementArray:_indices];
  38. }
  39. - (void)prepareUVs:(GLContext)glContext {
  40. // Create and initialise UV buffer data
  41. _uvBufferId = [glContext createBufferFromArray:_uvs];
  42. }
  43. - (void)prepareNormals:(GLContext)glContext {
  44. // Create and initialise normal buffer data
  45. _normalBufferId = [glContext createBufferFromArray:_normals];
  46. }
  47. - (void)rotate:(float)angle x:(float)x y:(float)y z:(float)z {
  48. _transformation.rotate(angle, x, y, z);
  49. }
  50. - (void)setRotation:(float)angle x:(float)x y:(float)y z:(float)z {
  51. _transformation.setRotation(angle, x, y, z);
  52. }
  53. - (void)translate:(float)x y:(float)y z:(float)z {
  54. _transformation.translate(x, y, z);
  55. }
  56. - (void)setTranslation:(float)x y:(float)y z:(float)z {
  57. _transformation.setTranslation(x, y, z);
  58. }
  59. - (void)resetTransformation {
  60. _transformation.makeIdentity();
  61. }
  62. - (Array)vertices {
  63. return _vertices;
  64. }
  65. - (Array)normals {
  66. return _normals;
  67. }
  68. - (Array)uvs {
  69. return _uvs;
  70. }
  71. - (Array)indices {
  72. return _indices;
  73. }
  74. - (int)numberOfElements {
  75. return _indices.length;
  76. }
  77. - (int)numberOfVertices {
  78. return _vertices.length;
  79. }
  80. - (int)getUVBufferId {
  81. return _uvBufferId;
  82. }
  83. - (int)getVertexBufferId {
  84. return _vertexBufferId;
  85. }
  86. - (int)getNormalBufferId {
  87. return _normalBufferId;
  88. }
  89. - (int)getIndicesBufferId {
  90. return _indicesBufferId;
  91. }
  92. - (GLMaterial)getMaterial {
  93. return _material;
  94. }
  95. - (Matrix4D)getTransformation {
  96. return _transformation;
  97. }
  98. @end