Guest User

Untitled

a guest
Jul 16th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. float* getVertices( int width, int height ) {
  2. ...
  3. }
  4.  
  5. void render() {
  6. glEnableClientState(GL_VERTEX_ARRAY);
  7. glVertexPointer(3, GL_FLOAT, 0, getVertices(width,heigth));
  8. glDrawArrays(GL_TRIANGLE_STRIP, 0, width*height);
  9. glDisableClientState(GL_VERTEX_ARRAY);
  10. }
  11.  
  12. void make_plane(int rows, int columns, float *vertices, int *indices) {
  13. // Set up vertices
  14. for (int r = 0; r < rows; ++r) {
  15. for (int c = 0; c < columns; ++c) {
  16. int index = r*columns + c;
  17. vertices[3*index + 0] = (float) c;
  18. vertices[3*index + 1] = (float) r;
  19. vertices[3*index + 2] = 0.0f;
  20. }
  21. }
  22.  
  23. // Set up indices
  24. int i = 0;
  25. for (int r = 0; r < rows - 1; ++r) {
  26. indices[i++] = r * columns;
  27. for (int c = 0; c < columns; ++c) {
  28. indices[i++] = r * columns + c;
  29. indices[i++] = (r + 1) * columns + c;
  30. }
  31. indices[i++] = (r + 1) * columns + (columns - 1);
  32. }
  33. }
  34.  
  35. int width;
  36. int height;
  37. float* vertices = 0;
  38. int* indices = 0;
  39.  
  40. int getVerticesCount( int width, int height ) {
  41. return width * height * 3;
  42. }
  43.  
  44. int getIndicesCount( int width, int height ) {
  45. return (width*height) + (width-1)*(height-2);
  46. }
  47.  
  48. float* getVertices( int width, int height ) {
  49. if ( vertices ) return vertices;
  50.  
  51. vertices = new float[ getVerticesCount( width, height ) ];
  52. int i = 0;
  53.  
  54. for ( int row=0; row<height; row++ ) {
  55. for ( int col=0; col<width; col++ ) {
  56. vertices[i++] = (float) col;
  57. vertices[i++] = 0.0f;
  58. vertices[i++] = (float) row;
  59. }
  60. }
  61.  
  62. return vertices;
  63. }
  64.  
  65. int* getIndices( int width, int height ) {
  66. if ( indices ) return indices;
  67.  
  68. indices = new int[ iSize ];
  69. int i = 0;
  70.  
  71. for ( int row=0; row<height-1; row++ ) {
  72. if ( (row&1)==0 ) { // even rows
  73. for ( int col=0; col<width; col++ ) {
  74. indices[i++] = col + row * width;
  75. indices[i++] = col + (row+1) * width;
  76. }
  77. } else { // odd rows
  78. for ( int col=width-1; col>0; col-- ) {
  79. indices[i++] = col + (row+1) * width;
  80. indices[i++] = col - 1 + + row * width;
  81. }
  82. }
  83. }
  84. if ( (mHeight&1) && mHeight>2 ) {
  85. mpIndices[i++] = (mHeight-1) * mWidth;
  86. }
  87.  
  88. return indices;
  89. }
  90.  
  91. void render() {
  92. glEnableClientState( GL_VERTEX_ARRAY );
  93. glVertexPointer( 3, GL_FLOAT, 0, getVertices(width,height) );
  94. glDrawElements( GL_TRIANGLE_STRIP, getIndicesCount(width,height), GL_UNSIGNED_INT, getIndices(width,height) );
  95. glDisableClientState( GL_VERTEX_ARRAY );
  96. }
  97.  
  98. // center x,z on origin
  99. float offset = worldSize / 2.0f, scale = worldSize / (float)vSize;
  100.  
  101. // create local vertices
  102. VertexPositionColor[] vertices = new VertexPositionColor[vSize * vSize];
  103.  
  104. for (uint z = 0; z < vSize; z++) {
  105. for (uint x = 0; x < vSize; x++) {
  106. uint index = x + (z * vSize);
  107. vertices[index].Position = new Vector3((scale*(float)x) - offset,
  108. heightValue,
  109. (scale*(float)z) - offset);
  110. vertices[index].Color = Color.White;
  111. }
  112. }
  113.  
  114. // create local indices
  115. var indices = new System.Collections.Generic.List<IndexType>();
  116.  
  117. for (int z = 0; z < vSize - 1; z++) {
  118. // degenerate index on non-first row
  119. if (z != 0) indices.Add((IndexType)(z * vSize));
  120.  
  121. // main strip
  122. for (int x = 0; x < vSize; x++) {
  123. indices.Add((IndexType)(z * vSize + x));
  124. indices.Add((IndexType)((z + 1) * vSize + x));
  125. }
  126.  
  127. // degenerate index on non-last row
  128. if (z != (vSize-2)) indices.Add((IndexType)((z + 1) * vSize + (vSize - 1)));
  129. }
Add Comment
Please, Sign In to add comment