Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. class Triangle {
  2. unsigned int vao; // vertex array object id
  3. float sx, sy; // scaling
  4. float wTx, wTy; // translation
  5. BezierCurve bc1;
  6. BezierCurve bc2;
  7. BezierCurve bc3;
  8. BezierCurve bc4;
  9. BezierCurve bc5;
  10. BezierCurve bc6;
  11. BezierSurface bs;
  12. int vertexCoordsSize = 0;
  13. int colorSize = 0;
  14.  
  15.  
  16. public:
  17. Triangle() {
  18. Animate(0);
  19. }
  20.  
  21. void Create() {
  22. for (int i = 0;i < 5;i++) {
  23. bc1.AddControlPoint(vec4(-1 + i*0.5, 1 , 0, 1));
  24. bc2.AddControlPoint(vec4(-1 + i*0.5, 1 - 0.5, (-(pow(i-2,2))/4)+0.9, 1));
  25. bc3.AddControlPoint(vec4(-1 + i*0.5, 1 - 1 , (-(pow(i - 2, 2)) / 4) + 1.9, 1));
  26. bc4.AddControlPoint(vec4(-1 + i*0.5, 1 - 1.5, (-(pow(i - 2, 2)) / 4) + 0.9, 1));
  27. bc5.AddControlPoint(vec4(-1 + i*0.5, 1 - 2 , 0, 1));
  28. }
  29.  
  30. bs.AddCurve(bc1);
  31. bs.AddCurve(bc2);
  32. bs.AddCurve(bc3);
  33. bs.AddCurve(bc4);
  34. bs.AddCurve(bc5);
  35.  
  36. /*vec4 a = bs.r(0, 0) * camera.Pinv() * camera.Vinv();
  37. vec4 b = bs.r(0, 0.25)* camera.Pinv() * camera.Vinv();
  38. vec4 c = bs.r(0.1, 0)* camera.Pinv() * camera.Vinv();
  39. vec4 d = bs.r(0.1, 0.25)* camera.Pinv() * camera.Vinv();
  40. vec4 e = bs.r(0.2, 0)* camera.Pinv() * camera.Vinv();
  41. vec4 f = bs.r(1, 1)* camera.Pinv() * camera.Vinv();*/
  42.  
  43. glGenVertexArrays(1, &vao); // create 1 vertex array object
  44. glBindVertexArray(vao); // make it active
  45.  
  46. unsigned int vbo[2]; // vertex buffer objects
  47. glGenBuffers(2, &vbo[0]); // Generate 2 vertex buffer objects
  48.  
  49. // vertex coordinates: vbo[0] -> Attrib Array 0 -> vertexPosition of the vertex shader
  50. glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); // make it active, it is an array
  51. static float vertexCoords[90000]; // vertex data on the CPU
  52. static float height[90000];
  53.  
  54. for (int i = 0; i < 21; i++) {
  55. for (int j = 0; j < 21; j++)
  56. {
  57. float u = j * 0.05f;
  58. float v = i * 0.05f;
  59. vec4 vect = bs.r(u, v) * camera.Pinv() * camera.Vinv();
  60. vertexCoords[vertexCoordsSize] = vect.v[0];
  61. vertexCoords[vertexCoordsSize + 1] = vect.v[1];
  62. height[colorSize] = vect.v[2];
  63.  
  64. float u2 = j * 0.05f;
  65. float v2 = (i+1)*0.05f;
  66. vec4 vect2 = bs.r(u2, v2) * camera.Pinv() * camera.Vinv();
  67. vertexCoords[vertexCoordsSize + 2] = vect2.v[0];
  68. vertexCoords[vertexCoordsSize + 3] = vect2.v[1];
  69. height[colorSize + 1] = vect2.v[2];
  70.  
  71. vertexCoordsSize += 4;
  72. colorSize += 2;
  73. }
  74. vertexCoords[vertexCoordsSize] = vertexCoords[vertexCoordsSize - 2];
  75. vertexCoords[vertexCoordsSize + 1] = vertexCoords[vertexCoordsSize - 1];
  76. height[colorSize] = height[colorSize - 1];
  77.  
  78. vertexCoords[vertexCoordsSize + 2] = (bs.r(0, (i+1)*0.05f) * camera.Pinv() * camera.Vinv()).v[0];
  79. vertexCoords[vertexCoordsSize + 3] = (bs.r(0, (i+1)*0.05f) * camera.Pinv() * camera.Vinv()).v[1];
  80. height[colorSize + 1] = (bs.r(0, (i+1)*0.05f) * camera.Pinv() * camera.Vinv()).v[2];
  81.  
  82. vertexCoordsSize += 4;
  83. colorSize += 2;
  84. }
  85.  
  86. /*vertexCoords[vertexCoordsSize++] = a.v[0];
  87. vertexCoords[vertexCoordsSize++] = a.v[1];
  88. vertexCoords[vertexCoordsSize++] = b.v[0];
  89. vertexCoords[vertexCoordsSize++] = b.v[1];
  90. vertexCoords[vertexCoordsSize++] = c.v[0];
  91. vertexCoords[vertexCoordsSize++] = c.v[1];
  92. vertexCoords[vertexCoordsSize++] = d.v[0];
  93. vertexCoords[vertexCoordsSize++] = d.v[1];
  94. vertexCoords[vertexCoordsSize++] = e.v[0];
  95. vertexCoords[vertexCoordsSize++] = e.v[1];
  96. vertexCoords[vertexCoordsSize++] = f.v[0];
  97. vertexCoords[vertexCoordsSize++] = f.v[1];*/
  98.  
  99.  
  100. glBufferData(GL_ARRAY_BUFFER, // copy to the GPU
  101. sizeof(vertexCoords), // number of the vbo in bytes
  102. vertexCoords, // address of the data array on the CPU
  103. GL_STATIC_DRAW); // copy to that part of the memory which is not modified
  104. // Map Attribute Array 0 to the current bound vertex buffer (vbo[0])
  105. glEnableVertexAttribArray(0);
  106. // Data organization of Attribute Array 0
  107. glVertexAttribPointer(0, // Attribute Array 0
  108. 2, GL_FLOAT, // components/attribute, component type
  109. GL_FALSE, // not in fixed point format, do not normalized
  110. 0, NULL); // stride and offset: it is tightly packed
  111.  
  112. // vertex colors: vbo[1] -> Attrib Array 1 -> vertexColor of the vertex shader
  113. glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); // make it active, it is an array
  114. static float vertexColors[90000];
  115. int a = 0;
  116. for (int i = 0;i < colorSize;i++) { // vertex data on the CPU
  117.  
  118. vertexColors[a] = height[i];
  119. vertexColors[a + 1] = 1- height[i];
  120. vertexColors[a + 2] = 0;
  121. a += 3;
  122. }
  123. glBufferData(GL_ARRAY_BUFFER, sizeof(vertexColors), vertexColors, GL_STATIC_DRAW); // copy to the GPU
  124.  
  125. // Map Attribute Array 1 to the current bound vertex buffer (vbo[1])
  126. glEnableVertexAttribArray(1); // Vertex position
  127. // Data organization of Attribute Array 1
  128. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL); // Attribute Array 1, components/attribute, component type, normalize?, tightly packed
  129. }
  130.  
  131. void Animate(float t) {
  132. sx = 1; // sinf(t);
  133. sy = 1; //cosf(t);
  134. wTx = 0; //4 * cosf(t / 2);
  135. wTy = 0; //4 * sinf(t / 2);
  136. }
  137.  
  138. void Draw() {
  139. mat4 Mscale(sx, 0, 0, 0,
  140. 0, sy, 0, 0,
  141. 0, 0, 0, 0,
  142. 0, 0, 0, 1); // model matrix
  143.  
  144. mat4 Mtranslate(1, 0, 0, 0,
  145. 0, 1, 0, 0,
  146. 0, 0, 0, 0,
  147. wTx, wTy, 0, 1); // model matrix
  148.  
  149. mat4 MVPTransform = Mscale * Mtranslate * camera.V() * camera.P();
  150.  
  151. // set GPU uniform matrix variable MVP with the content of CPU variable MVPTransform
  152. int location = glGetUniformLocation(shaderProgram, "MVP");
  153. if (location >= 0) glUniformMatrix4fv(location, 1, GL_TRUE, MVPTransform); // set uniform variable MVP to the MVPTransform
  154. else printf("uniform MVP cannot be set\n");
  155.  
  156. glBindVertexArray(vao); // make the vao and its vbos active playing the role of the data source
  157. glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexCoordsSize/2); // draw a single triangle with vertices defined in vao
  158. }
  159. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement