Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float* getVertices( int width, int height ) {
- ...
- }
- void render() {
- glEnableClientState(GL_VERTEX_ARRAY);
- glVertexPointer(3, GL_FLOAT, 0, getVertices(width,heigth));
- glDrawArrays(GL_TRIANGLE_STRIP, 0, width*height);
- glDisableClientState(GL_VERTEX_ARRAY);
- }
- void make_plane(int rows, int columns, float *vertices, int *indices) {
- // Set up vertices
- for (int r = 0; r < rows; ++r) {
- for (int c = 0; c < columns; ++c) {
- int index = r*columns + c;
- vertices[3*index + 0] = (float) c;
- vertices[3*index + 1] = (float) r;
- vertices[3*index + 2] = 0.0f;
- }
- }
- // Set up indices
- int i = 0;
- for (int r = 0; r < rows - 1; ++r) {
- indices[i++] = r * columns;
- for (int c = 0; c < columns; ++c) {
- indices[i++] = r * columns + c;
- indices[i++] = (r + 1) * columns + c;
- }
- indices[i++] = (r + 1) * columns + (columns - 1);
- }
- }
- int width;
- int height;
- float* vertices = 0;
- int* indices = 0;
- int getVerticesCount( int width, int height ) {
- return width * height * 3;
- }
- int getIndicesCount( int width, int height ) {
- return (width*height) + (width-1)*(height-2);
- }
- float* getVertices( int width, int height ) {
- if ( vertices ) return vertices;
- vertices = new float[ getVerticesCount( width, height ) ];
- int i = 0;
- for ( int row=0; row<height; row++ ) {
- for ( int col=0; col<width; col++ ) {
- vertices[i++] = (float) col;
- vertices[i++] = 0.0f;
- vertices[i++] = (float) row;
- }
- }
- return vertices;
- }
- int* getIndices( int width, int height ) {
- if ( indices ) return indices;
- indices = new int[ iSize ];
- int i = 0;
- for ( int row=0; row<height-1; row++ ) {
- if ( (row&1)==0 ) { // even rows
- for ( int col=0; col<width; col++ ) {
- indices[i++] = col + row * width;
- indices[i++] = col + (row+1) * width;
- }
- } else { // odd rows
- for ( int col=width-1; col>0; col-- ) {
- indices[i++] = col + (row+1) * width;
- indices[i++] = col - 1 + + row * width;
- }
- }
- }
- if ( (mHeight&1) && mHeight>2 ) {
- mpIndices[i++] = (mHeight-1) * mWidth;
- }
- return indices;
- }
- void render() {
- glEnableClientState( GL_VERTEX_ARRAY );
- glVertexPointer( 3, GL_FLOAT, 0, getVertices(width,height) );
- glDrawElements( GL_TRIANGLE_STRIP, getIndicesCount(width,height), GL_UNSIGNED_INT, getIndices(width,height) );
- glDisableClientState( GL_VERTEX_ARRAY );
- }
- // center x,z on origin
- float offset = worldSize / 2.0f, scale = worldSize / (float)vSize;
- // create local vertices
- VertexPositionColor[] vertices = new VertexPositionColor[vSize * vSize];
- for (uint z = 0; z < vSize; z++) {
- for (uint x = 0; x < vSize; x++) {
- uint index = x + (z * vSize);
- vertices[index].Position = new Vector3((scale*(float)x) - offset,
- heightValue,
- (scale*(float)z) - offset);
- vertices[index].Color = Color.White;
- }
- }
- // create local indices
- var indices = new System.Collections.Generic.List<IndexType>();
- for (int z = 0; z < vSize - 1; z++) {
- // degenerate index on non-first row
- if (z != 0) indices.Add((IndexType)(z * vSize));
- // main strip
- for (int x = 0; x < vSize; x++) {
- indices.Add((IndexType)(z * vSize + x));
- indices.Add((IndexType)((z + 1) * vSize + x));
- }
- // degenerate index on non-last row
- if (z != (vSize-2)) indices.Add((IndexType)((z + 1) * vSize + (vSize - 1)));
- }
Add Comment
Please, Sign In to add comment