Added simple geometry shader which draws a red triangle which changes it's height over time.

This commit is contained in:
Faerbit 2015-02-24 23:30:59 +01:00
parent a21aa613bd
commit b0b2c71139
6 changed files with 69 additions and 4 deletions

View File

@ -75,7 +75,7 @@
<gColour>1.0</gColour>
<bColour>1.0</bColour>
<intensity>4.0</intensity>
<flameOffset>0.5</flameOffset>
<flameOffset>-1.2</flameOffset>
</light>
</composition>

7
data/shader/flame.fsh Normal file
View File

@ -0,0 +1,7 @@
#version 150
out vec4 oColor;
void main() {
oColor = vec4(1.0, 0.0, 0.0, 0.5);
}

20
data/shader/flame.gsh Normal file
View File

@ -0,0 +1,20 @@
#version 150
uniform mat4 viewProjectionMatrix;
uniform float time;
layout(points) in;
layout(triangle_strip, max_vertices = 3) out;
void main() {
gl_Position = gl_in[0].gl_Position + viewProjectionMatrix * vec4(-0.5, 0.0, 0.0, 0.0);
EmitVertex();
gl_Position = gl_in[0].gl_Position + viewProjectionMatrix * vec4(0.5, 0.0, 0.0, 0.0);
EmitVertex();
gl_Position = gl_in[0].gl_Position + viewProjectionMatrix * vec4(0.0, 2.0 + sin(time), 0.0, 0.0);
EmitVertex();
EndPrimitive();
}

9
data/shader/flame.vsh Normal file
View File

@ -0,0 +1,9 @@
#version 150
uniform mat4 viewProjectionMatrix;
in vec3 aPosition;
void main () {
gl_Position = viewProjectionMatrix * vec4(aPosition, 1.0);
}

View File

@ -49,6 +49,15 @@ void Graphics::init(Level* level) {
depthCubeShader = ShaderProgramCreator("depth_cube")
.attributeLocations(vao->getAttributeLocations()).create();
flame_positions_ab = SharedArrayBuffer(new ArrayBuffer());
flame_positions_ab->defineAttribute("aPosition", GL_FLOAT, 3);
flame_positions = SharedVertexArrayObject(new VertexArrayObject());
flame_positions->setMode(GL_POINTS);
flame_positions->attachAllAttributes(flame_positions_ab);
flameShader = ShaderProgramCreator("flame")
.attributeLocations(flame_positions->getAttributeLocations()).create();
depthTexture = SharedTexture2D( new Texture2D(windowSize, GL_DEPTH_COMPONENT24));
depthTexture->setMinFilter(GL_NEAREST);
depthTexture->setMagFilter(GL_NEAREST);
@ -157,7 +166,7 @@ void Graphics::render(double time)
double nextUpdate = lastUpdate + lightUpdateDelay;
if (time >= nextUpdate)
{
updateShaderLights();
updateLights();
lastUpdate = time;
}
@ -187,6 +196,12 @@ void Graphics::render(double time)
// render the level
level->render(lightingShader, true, &lightingViewProjectionMatrix, &shadowVPs);
// draw flames on top
flameShader->use();
flameShader->setUniform("viewProjectionMatrix", lightingViewProjectionMatrix);
flameShader->setUniform("time", (float) time);
flame_positions->render();
}
bool Graphics::compareLightDistances(Light a, Light b) {
@ -210,7 +225,7 @@ void Graphics::updateClosestLights() {
}
}
void Graphics::updateShaderLights() {
void Graphics::updateLights() {
updateClosestLights();
if (closestLights.size() > 0) {
lightingShader->setUniform("lightCount", (int) closestLights.size());
@ -247,6 +262,17 @@ void Graphics::updateShaderLights() {
lightingShader->setUniform("directionalIntensity",
level->getDirectionalLight()->getIntensity());
}
float flamePositionsData[closestLights.size() * 3] = {};
int flameIndex = 0;
for (unsigned int i = 0; i<closestLights.size(); i++) {
if (closestLights.at(i).getFlameYOffset() != 0.0f) {
flamePositionsData[flameIndex + 0] = closestLights.at(i).getPosition().x;
flamePositionsData[flameIndex + 1] = closestLights.at(i).getPosition().y + closestLights.at(i).getFlameYOffset();
flamePositionsData[flameIndex + 2] = closestLights.at(i).getPosition().z;
flameIndex+=3;
}
}
flame_positions_ab->setDataElements(flameIndex, flamePositionsData);
}
void Graphics::resize(glm::uvec2 windowSize) {

View File

@ -19,7 +19,7 @@ class Graphics {
void resize(glm::uvec2 windowSize);
float getFarPlane();
private:
void updateShaderLights();
void updateLights();
void updateClosestLights();
bool compareLightDistances(Light a, Light b);
void saveDepthBufferToDisk(int face, std::string);
@ -31,10 +31,13 @@ class Graphics {
ACGL::OpenGL::SharedShaderProgram lightingShader;
ACGL::OpenGL::SharedShaderProgram depthCubeShader;
ACGL::OpenGL::SharedShaderProgram depthShader;
ACGL::OpenGL::SharedShaderProgram flameShader;
ACGL::OpenGL::SharedTexture2D depthTexture;
ACGL::OpenGL::SharedFrameBufferObject framebuffer;
std::vector<ACGL::OpenGL::SharedTextureCubeMap> depth_cubeMaps;
ACGL::OpenGL::SharedFrameBufferObject framebuffer_cube;
ACGL::OpenGL::SharedVertexArrayObject flame_positions;
ACGL::OpenGL::SharedArrayBuffer flame_positions_ab;
int cube_size;
unsigned int maxShadowRenderCount;
Level* level;