/* * ground.c * * Copyright (c) 2003-2012, NVIDIA CORPORATION. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ // // Ground plane // #include "ground.h" #include "random.h" #include "vbo.h" #include "vector.h" #include "shaders.h" // Mesh resolution #define RESOLUTION 10 #define MAXSIZE (RESOLUTION + 1) #define MAXAREA (MAXSIZE * MAXSIZE) // Ground texture static GLuint texture; // Ground vertex info static float3 vertices [MAXSIZE*MAXSIZE]; static float3 normals [MAXSIZE*MAXSIZE]; static float2 texcoords[MAXSIZE*MAXSIZE]; static float3 colors [MAXSIZE*MAXSIZE]; static GLubyte indicies[RESOLUTION*2*MAXSIZE]; // Ground VBO indices static unsigned long VBOvertices, VBOnormals, VBOtexcoords, VBOcolors; // Utility function to handling index wrapping static int wrap( int i) { while (i > RESOLUTION) i -= RESOLUTION; while (i < 0) i += RESOLUTION; return i; } // Build the ground plane vertices static void build(void) { int i, j, step; GLubyte *indx; for (j = 0; j < MAXSIZE; ++j) { float y = ((float)j) / ((float)RESOLUTION) * 2.0f - 1.0f; float py = y * GROUND_SIZE / 2.0f; float t = (((float)j) / ((float)RESOLUTION)); for (i = 0; i< MAXSIZE; ++i) { float pz = ((float)GetRandom() - 0.5f) * 0.04f * GROUND_SIZE; float x = ((float)i)/((float)RESOLUTION) * 2.0f - 1.0f; float px = x * GROUND_SIZE / 2.0f; float s = (((float)i) / ((float)RESOLUTION)); float r2; set_3(vertices [j*MAXSIZE+i], px, py, pz); set_2(texcoords[j*MAXSIZE+i], s, t); r2 = (x*x+y*y); if (r2>1.0) r2 = 0.0; else r2 = 1.0f-r2; set_3(colors[j*MAXSIZE+i], r2, r2, r2); } } // wrap edge for (j=0; j