From 03de2f3ea7db92e948035d2b87bb66ec8c86b897 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Mon, 5 Aug 2013 11:41:42 -0700 Subject: [PATCH] Render a green plane with fog, per Grayson's request. --- interface/src/Application.cpp | 17 ++++++ interface/src/renderer/GeometryCache.cpp | 70 +++++++++++++++++++++++- interface/src/renderer/GeometryCache.h | 6 +- 3 files changed, 90 insertions(+), 3 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 6087a4493e..5399c274e4 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2893,6 +2893,23 @@ void Application::displaySide(Camera& whichCamera) { //draw a grid ground plane.... if (_renderGroundPlaneOn->isChecked()) { + // draw grass plane with fog + glEnable(GL_FOG); + const float FOG_COLOR[] = { 1.0f, 1.0f, 1.0f, 1.0f }; + glFogfv(GL_FOG_COLOR, FOG_COLOR); + glFogi(GL_FOG_MODE, GL_EXP2); + glFogf(GL_FOG_DENSITY, 0.025f); + glPushMatrix(); + const float GRASS_PLANE_SIZE = 256.0f; + glTranslatef(-GRASS_PLANE_SIZE * 0.5f, -0.01f, GRASS_PLANE_SIZE * 0.5f); + glScalef(GRASS_PLANE_SIZE, 1.0f, GRASS_PLANE_SIZE); + glRotatef(-90.0f, 1.0f, 0.0f, 0.0f); + glColor3ub(70, 134, 74); + const int GRASS_DIVISIONS = 40; + _geometryCache.renderSquare(GRASS_DIVISIONS, GRASS_DIVISIONS); + glPopMatrix(); + glDisable(GL_FOG); + renderGroundPlaneGrid(EDGE_SIZE_GROUND_PLANE, _audio.getCollisionSoundMagnitude()); } // Draw voxels diff --git a/interface/src/renderer/GeometryCache.cpp b/interface/src/renderer/GeometryCache.cpp index 971f18706e..8331a5deb9 100644 --- a/interface/src/renderer/GeometryCache.cpp +++ b/interface/src/renderer/GeometryCache.cpp @@ -18,7 +18,7 @@ GeometryCache::~GeometryCache() { } void GeometryCache::renderHemisphere(int slices, int stacks) { - VerticesIndices& vbo = _hemisphereVBOs[SlicesStacks(slices, stacks)]; + VerticesIndices& vbo = _hemisphereVBOs[IntPair(slices, stacks)]; int vertices = slices * (stacks - 1) + 1; int indices = slices * 2 * 3 * (stacks - 2) + slices * 3; if (vbo.first == 0) { @@ -95,3 +95,71 @@ void GeometryCache::renderHemisphere(int slices, int stacks) { glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } + +void GeometryCache::renderSquare(int xDivisions, int yDivisions) { + VerticesIndices& vbo = _squareVBOs[IntPair(xDivisions, yDivisions)]; + int xVertices = xDivisions + 1; + int yVertices = yDivisions + 1; + int vertices = xVertices * yVertices; + int indices = 2 * 3 * xDivisions * yDivisions; + if (vbo.first == 0) { + GLfloat* vertexData = new GLfloat[vertices * 3]; + GLfloat* vertex = vertexData; + for (int i = 0; i <= yDivisions; i++) { + float y = (float)i / yDivisions; + + for (int j = 0; j <= xDivisions; j++) { + *(vertex++) = (float)j / xDivisions; + *(vertex++) = y; + *(vertex++) = 0.0f; + } + } + + glGenBuffers(1, &vbo.first); + glBindBuffer(GL_ARRAY_BUFFER, vbo.first); + const int BYTES_PER_VERTEX = 3 * sizeof(GLfloat); + glBufferData(GL_ARRAY_BUFFER, vertices * BYTES_PER_VERTEX, vertexData, GL_STATIC_DRAW); + delete[] vertexData; + + GLushort* indexData = new GLushort[indices]; + GLushort* index = indexData; + for (int i = 0; i < yDivisions; i++) { + GLushort bottom = i * xVertices; + GLushort top = bottom + xVertices; + for (int j = 0; j < xDivisions; j++) { + int next = j + 1; + + *(index++) = bottom + j; + *(index++) = top + next; + *(index++) = top + j; + + *(index++) = bottom + j; + *(index++) = bottom + next; + *(index++) = top + next; + } + } + + glGenBuffers(1, &vbo.second); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second); + const int BYTES_PER_INDEX = sizeof(GLushort); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices * BYTES_PER_INDEX, indexData, GL_STATIC_DRAW); + delete[] indexData; + + } else { + glBindBuffer(GL_ARRAY_BUFFER, vbo.first); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second); + } + glEnableClientState(GL_VERTEX_ARRAY); + + // all vertices have the same normal + glNormal3f(0.0f, 0.0f, 1.0f); + + glVertexPointer(3, GL_FLOAT, 0, 0); + + glDrawRangeElementsEXT(GL_TRIANGLES, 0, vertices - 1, indices, GL_UNSIGNED_SHORT, 0); + + glDisableClientState(GL_VERTEX_ARRAY); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); +} diff --git a/interface/src/renderer/GeometryCache.h b/interface/src/renderer/GeometryCache.h index 0c5ccea06f..bfce642554 100644 --- a/interface/src/renderer/GeometryCache.h +++ b/interface/src/renderer/GeometryCache.h @@ -19,13 +19,15 @@ public: ~GeometryCache(); void renderHemisphere(int slices, int stacks); + void renderSquare(int xDivisions, int yDivisions); private: - typedef QPair SlicesStacks; + typedef QPair IntPair; typedef QPair VerticesIndices; - QHash _hemisphereVBOs; + QHash _hemisphereVBOs; + QHash _squareVBOs; }; #endif /* defined(__interface__GeometryCache__) */