From 3d4341cee4f516da745a5e63e3c761c8b4cc4c6a Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 23 Sep 2013 15:18:45 -0700 Subject: [PATCH] hacking on voxel geometry shader --- .../resources/shaders/duplicate_geometry.geom | 46 ++++++ interface/resources/shaders/passthrough.geom | 150 ++++++++++++++++++ interface/resources/shaders/passthrough.vert | 6 + interface/src/Application.cpp | 4 + interface/src/Application.h | 2 + interface/src/renderer/TestGeometry.cpp | 65 ++++++++ interface/src/renderer/TestGeometry.h | 44 +++++ libraries/voxels/src/VoxelConstants.h | 6 +- 8 files changed, 320 insertions(+), 3 deletions(-) create mode 100644 interface/resources/shaders/duplicate_geometry.geom create mode 100644 interface/resources/shaders/passthrough.geom create mode 100644 interface/resources/shaders/passthrough.vert create mode 100644 interface/src/renderer/TestGeometry.cpp create mode 100644 interface/src/renderer/TestGeometry.h diff --git a/interface/resources/shaders/duplicate_geometry.geom b/interface/resources/shaders/duplicate_geometry.geom new file mode 100644 index 0000000000..2ab92fc4e3 --- /dev/null +++ b/interface/resources/shaders/duplicate_geometry.geom @@ -0,0 +1,46 @@ +#version 420 + +layout(triangles) in; +layout (triangle_strip, max_vertices=6) out; + +layout (std140) uniform Matrices { + mat4 projModelViewMatrix; + mat3 normalMatrix; +}; + +in VertexData { + vec2 texCoord; + vec3 normal; +} VertexIn[]; + +out VertexData { + vec2 texCoord; + vec3 normal; +} VertexOut; + +void main() +{ + for(int i = 0; i < gl_in.length(); i++) + { + // copy attributes + gl_Position = projModelViewMatrix * gl_in[i].gl_Position; + VertexOut.normal = normalize(normalMatrix * VertexIn[i].normal); + VertexOut.texCoord = VertexIn[i].texCoord; + + // done with the vertex + EmitVertex(); + } + EndPrimitive(); + + for(int i = 0; i < gl_in.length(); i++) + { + // copy attributes and displace copy + gl_Position = projModelViewMatrix * (gl_in[i].gl_Position + vec4(20.0, 0.0, 0.0, 0.0)); + VertexOut.normal = normalize(normalMatrix * VertexIn[i].normal); + VertexOut.texCoord = VertexIn[i].texCoord; + + // done with the vertex + EmitVertex(); + } + EndPrimitive(); +} diff --git a/interface/resources/shaders/passthrough.geom b/interface/resources/shaders/passthrough.geom new file mode 100644 index 0000000000..1ce193cda9 --- /dev/null +++ b/interface/resources/shaders/passthrough.geom @@ -0,0 +1,150 @@ +#version 120 +#extension GL_ARB_geometry_shader4 : enable + +// use GL_POINTS +// have point be the corner of voxel +// have a second dataset (? similar to how voxel avatars pass in bones??) +// which is the voxel size? +// +// In vertex shader DON'T transform.. therefor passing the world coordinate xyz to geometric shader +// In geometric shader calculate xyz for triangles the same way we currently do triangles outside of OpenGL +// do transform on these triangles +// gl_Position = gl_ModelViewProjectionMatrix * cube_coord; +// +// output GL_TRIANGLE_STRIP +// +// NOTE: updateNodeInArrays() does the covert from voxel corner to 12 triangles or 36 points or 36*3 floats +// but since it operates on the array of floats, it is kinda weird and hard to follow. The %3 is for the +// xyz.. and identityVertices[j] term in the math is actually a string of floats but they should be thought +// of as triplets of x,y,z +// +// do we need to add the light to these colors?? +// + +//GEOMETRY SHADER +/////////////////////// +void main() +{ + //increment variable + int i; + vec4 vertex; + vec4 color,red,green,blue; + + green = vec4(0,1.0,0,1.0); + red = vec4(1.0,0,0,1.0); + blue = vec4(0,0,1.0,1.0); + + ///////////////////////////////////////////////////////////// + //This example has two parts + // step a) draw the primitive pushed down the pipeline + // there are gl_VerticesIn # of vertices + // put the vertex value into gl_Position + // use EmitVertex => 'create' a new vertex + // use EndPrimitive to signal that you are done creating a primitive! + // step b) create a new piece of geometry + // I just do the same loop, but I negate the vertex.z + // result => the primitive is now mirrored. + //Pass-thru! + for(i = 0; i < gl_VerticesIn; i++) { + color = gl_FrontColorIn[i]; + gl_FrontColor = color; // + gl_Position = gl_ModelViewProjectionMatrix * gl_PositionIn[i]; + EmitVertex(); + } + EndPrimitive(); + + for(i = 0; i < gl_VerticesIn; i++) { + gl_FrontColor = red; // + vertex = gl_PositionIn[i]; + vertex.y += 0.05f; + gl_Position = gl_ModelViewProjectionMatrix * vertex; + EmitVertex(); + } + EndPrimitive(); + + for(i = 0; i < gl_VerticesIn; i++) { + gl_FrontColor = green; // + vertex = gl_PositionIn[i]; + vertex.x += 0.05f; + gl_Position = gl_ModelViewProjectionMatrix * vertex; + EmitVertex(); + } + EndPrimitive(); + + for(i = 0; i < gl_VerticesIn; i++) { + gl_FrontColor = blue; // + vertex = gl_PositionIn[i]; + vertex.z += 0.05f; + gl_Position = gl_ModelViewProjectionMatrix * vertex; + EmitVertex(); + } + EndPrimitive(); + +/** + + for(i = 0; i < gl_VerticesIn; i++) { + green = vec4(0,1.0,0,1.0); + red = vec4(1.0,0,0,1.0); + //gl_FrontColor = gl_FrontColorIn[i]; + gl_FrontColor = red; + + // v -> v+x -> v+x+y + vertex = gl_PositionIn[i]; + gl_Position = vertex; + EmitVertex(); + vertex.x += 0.1f; + gl_Position = vertex; + EmitVertex(); + vertex.y += 0.1f; + gl_Position = vertex; + EmitVertex(); + EndPrimitive(); + + // v+x+y -> v+y -> v + vertex = gl_PositionIn[i]; + vertex.x += 0.1f; + vertex.y += 0.1f; + gl_Position = vertex; + EmitVertex(); + vertex.x -= 0.1f; + gl_Position = vertex; + EmitVertex(); + vertex.y -= 0.1f; + gl_Position = vertex; + EmitVertex(); + EndPrimitive(); + // v+z -> v+z+x -> v+z+x+y + gl_FrontColor = green; + vertex = gl_PositionIn[i]; + vertex.z -= 0.1f; + gl_Position = vertex; + EmitVertex(); + + vertex.x += 0.1f; + gl_Position = vertex; + EmitVertex(); + vertex.y += 0.1f; + gl_Position = vertex; + EmitVertex(); + EndPrimitive(); + + // v+z+x+y -> v+z+y -> v+z + vertex = gl_PositionIn[i]; + vertex.z -= 0.1f; + vertex.x += 0.1f; + vertex.y += 0.1f; + gl_Position = vertex; + EmitVertex(); + vertex.x -= 0.1f; + gl_Position = vertex; + EmitVertex(); + vertex.y -= 0.1f; + gl_Position = vertex; + EmitVertex(); + EndPrimitive(); + + } +**/ + + +} \ No newline at end of file diff --git a/interface/resources/shaders/passthrough.vert b/interface/resources/shaders/passthrough.vert new file mode 100644 index 0000000000..a3a6219b6a --- /dev/null +++ b/interface/resources/shaders/passthrough.vert @@ -0,0 +1,6 @@ +#version 120 + +void main(void) { + gl_FrontColor = gl_Color; //vec4(1.0, 0.0, 0.0, 1.0); + gl_Position = gl_Vertex;// ftransform(); +} \ No newline at end of file diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index d90c82e6ca..503f8f434e 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -389,6 +389,7 @@ void Application::paintGL() { } else { _glowEffect.prepare(); + glMatrixMode(GL_MODELVIEW); glPushMatrix(); @@ -1539,6 +1540,7 @@ void Application::init() { _glowEffect.init(); _ambientOcclusionEffect.init(); + _testGeometry.init(); _handControl.setScreenDimensions(_glWidget->width(), _glWidget->height()); @@ -2579,7 +2581,9 @@ void Application::displaySide(Camera& whichCamera) { // brad's frustum for debugging if (Menu::getInstance()->isOptionChecked(MenuOption::DisplayFrustum)) { + _testGeometry.begin(); renderViewFrustum(_viewFrustum); + _testGeometry.end(); } // render voxel fades if they exist diff --git a/interface/src/Application.h b/interface/src/Application.h index e951afc735..690dbc154c 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -51,6 +51,7 @@ #include "renderer/AmbientOcclusionEffect.h" #include "renderer/GeometryCache.h" #include "renderer/GlowEffect.h" +#include "renderer/TestGeometry.h" #include "renderer/TextureCache.h" #include "ui/BandwidthDialog.h" #include "ui/ChatEntry.h" @@ -342,6 +343,7 @@ private: GlowEffect _glowEffect; AmbientOcclusionEffect _ambientOcclusionEffect; + TestGeometry _testGeometry; #ifndef _WIN32 Audio _audio; diff --git a/interface/src/renderer/TestGeometry.cpp b/interface/src/renderer/TestGeometry.cpp new file mode 100644 index 0000000000..e30a210dde --- /dev/null +++ b/interface/src/renderer/TestGeometry.cpp @@ -0,0 +1,65 @@ +// +// TestGeometry.cpp +// interface +// +// Created by Brad Hefta-Gaub on 9/22/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. + +// include this before QOpenGLFramebufferObject, which includes an earlier version of OpenGL +#include "InterfaceConfig.h" + +#include + +#include "Application.h" +#include "TestGeometry.h" +#include "ProgramObject.h" +#include "RenderUtil.h" + +TestGeometry::TestGeometry() + : _initialized(false) +{ +} + +TestGeometry::~TestGeometry() { + if (_initialized) { + delete _testProgram; + } +} + +static ProgramObject* createGeometryShaderProgram(const QString& name) { + ProgramObject* program = new ProgramObject(); + program->addShaderFromSourceFile(QGLShader::Vertex, "resources/shaders/passthrough.vert" ); + + + program->addShaderFromSourceFile(QGLShader::Geometry, "resources/shaders/" + name + ".geom" ); + + program->setGeometryInputType(GL_LINES); + program->setGeometryOutputType(GL_LINE_STRIP); + program->setGeometryOutputVertexCount(100); // hack? + + program->link(); + //program->log(); + + return program; +} + +void TestGeometry::init() { + if (_initialized) { + qDebug("[ERROR] TestProgram is already initialized.\n"); + return; + } + + switchToResourcesParentIfRequired(); + + _testProgram = createGeometryShaderProgram("passthrough"); + _initialized = true; +} + +void TestGeometry::begin() { + _testProgram->bind(); +} + +void TestGeometry::end() { + _testProgram->release(); +} + diff --git a/interface/src/renderer/TestGeometry.h b/interface/src/renderer/TestGeometry.h new file mode 100644 index 0000000000..cec13196b0 --- /dev/null +++ b/interface/src/renderer/TestGeometry.h @@ -0,0 +1,44 @@ +// +// TestGeometry.h +// interface +// +// Created by Andrzej Kapolka on 8/7/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// + +#ifndef __interface__TestGeometry__ +#define __interface__TestGeometry__ + +#include +#include + +class QOpenGLFramebufferObject; + +class ProgramObject; + +/// A generic full screen glow effect. +class TestGeometry : public QObject { + Q_OBJECT + +public: + TestGeometry(); + ~TestGeometry(); + + void init(); + + /// Starts using the geometry shader effect. + void begin(); + + /// Stops using the geometry shader effect. + void end(); + +public slots: + +private: + + bool _initialized; + + ProgramObject* _testProgram; +}; + +#endif /* defined(__interface__TestGeometry__) */ diff --git a/libraries/voxels/src/VoxelConstants.h b/libraries/voxels/src/VoxelConstants.h index 08fdbefee3..1c6e6cd0e6 100644 --- a/libraries/voxels/src/VoxelConstants.h +++ b/libraries/voxels/src/VoxelConstants.h @@ -33,9 +33,9 @@ const int NUMBER_OF_CHILDREN = 8; const int MAX_VOXEL_PACKET_SIZE = 1492; const int MAX_TREE_SLICE_BYTES = 26; const int MAX_VOXELS_PER_SYSTEM = 200000; -const int VERTICES_PER_VOXEL = 24; -const int VERTEX_POINTS_PER_VOXEL = 3 * VERTICES_PER_VOXEL; -const int INDICES_PER_VOXEL = 3 * 12; +const int VERTICES_PER_VOXEL = 24; // 6 sides * 4 corners per side +const int VERTEX_POINTS_PER_VOXEL = 3 * VERTICES_PER_VOXEL; // ???? xyz for each VERTICE_PER_VOXEL?? +const int INDICES_PER_VOXEL = 3 * 12; // 6 sides * 2 triangles per size * 3 vertices per triangle const int COLOR_VALUES_PER_VOXEL = NUMBER_OF_COLORS * VERTICES_PER_VOXEL; typedef unsigned long int glBufferIndex;