Commit 6d5e240d authored by David Gilbert's avatar David Gilbert

Transform feedback!

Next will be tweaking the LOD levels and recursive tessellation
parent c7f68f6a
......@@ -3,22 +3,19 @@
layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out;
precise in vec3 tPosition[3];
precise in vec3 tPatchDistance[3];
in vec3 tPosition[3];
in vec3 tPatchDistance[3];
precise in vec3 tNormal[3];
in vec3 tNormal[3];
in vec2 tTexCoord[3];
precise out vec3 gNormal;
precise out vec3 gPosition;
precise out vec2 gTexCoord;
out vec3 gNormal;
out vec3 gPosition;
out vec2 gTexCoord;
out vec3 gPatchDistance;
out vec3 gTriDistance;
uniform mat4 uViewProjectionMatrix;
uniform mat4 uModelMatrix;
void main()
{
vec3 A = tPosition[2] - tPosition[0];
......
......@@ -2,18 +2,14 @@
layout(vertices = 3) out;
precise in vec3 vNormal[];
precise in vec3 vPosition[];
precise in vec2 vTexCoord[];
in vec3 vNormal[];
in vec3 vPosition[];
in vec2 vTexCoord[];
precise out vec3 tcNormal[];
precise out vec2 tcTexCoord[];
precise out vec3 tcPosition[];
out vec3 tcNormal[];
out vec2 tcTexCoord[];
out vec3 tcPosition[];
in vec3 tcPatchDistance[];
in vec3 tcTriDistance[];
uniform mat4 uViewProjectionMatrix;
uniform mat4 uModelMatrix;
uniform vec3 cameraPosition;
......
......@@ -2,16 +2,13 @@
layout(triangles, equal_spacing, ccw) in;
precise in vec2 tcTexCoord[];
precise in vec3 tcNormal[];
precise in vec3 tcPosition[];
in vec2 tcTexCoord[];
in vec3 tcNormal[];
in vec3 tcPosition[];
in vec3 tcPatchDistance[];
in vec3 tcTriDistance[];
precise out vec3 tNormal;
precise out vec2 tTexCoord;
precise out vec3 tPosition;
out vec3 tNormal;
out vec2 tTexCoord;
out vec3 tPosition;
out vec3 tPatchDistance;
......
#version 410 core
precise in vec3 gNormal;
precise in vec3 gPosition;
precise in vec2 gTexCoord;
in vec3 gNormal;
in vec3 gPosition;
in vec2 gTexCoord;
in vec3 gPatchDistance;
in vec3 gTriDistance;
precise out vec3 vNormal;
precise out vec3 vPosition;
precise out vec2 vTexCoord;
out vec3 vPatchDistance;
out vec3 vTriDistance;
uniform mat4 uModelMatrix;
uniform mat4 uViewProjectionMatrix;
out vec3 vNormal;
out vec3 vPosition;
out vec2 vTexCoord;
void main(){
vNormal = gNormal;
vPosition = gPosition;
vTexCoord = gTexCoord;
vPatchDistance = gPatchDistance;
vTriDistance = gTriDistance;
//gl_Position = uViewProjectionMatrix * vec4(gPosition, 1);// vec4(0, 0, 0, 1);
}
\ No newline at end of file
......@@ -3,21 +3,15 @@
layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out;
precise in vec3 tPosition[3];
precise in vec3 tPatchDistance[3];
in vec3 tPosition[3];
precise in vec3 tNormal[3];
precise in vec2 tTexCoord[3];
in vec3 tNormal[3];
in vec2 tTexCoord[3];
precise out vec3 gNormal;
precise out vec3 gPosition;
precise out vec2 gTexCoord;
out vec3 gNormal;
out vec3 gPosition;
out vec2 gTexCoord;
precise out vec3 gPatchDistance;
precise out vec3 gTriDistance;
uniform mat4 uViewProjectionMatrix;
uniform mat4 uModelMatrix;
void main()
{
......@@ -26,28 +20,21 @@ void main()
gNormal = -normalize(cross(A, B));
//gNormal = vec3(1.11, 1.11, 1.11);
gPatchDistance = tPatchDistance[0];
//gNormal = tNormal[0];
gTexCoord = tTexCoord[0];
gPosition = tPosition[0];
gTriDistance = vec3(1, 0, 0);
gl_Position = gl_in[0].gl_Position; EmitVertex();
//gNormal = tNormal[1];
gTexCoord = tTexCoord[1];
gPosition = tPosition[1];
gPatchDistance = tPatchDistance[1];
gTriDistance = vec3(0, 1, 0);
gl_Position = gl_in[1].gl_Position; EmitVertex();
//gNormal = tNormal[2];
gTexCoord = tTexCoord[2];
gPosition = tPosition[2];
gPatchDistance = tPatchDistance[2];
gTriDistance = vec3(0, 0, 1);
gl_Position = gl_in[2].gl_Position; EmitVertex();
EndPrimitive();
......
......@@ -5,13 +5,11 @@ layout(vertices = 3) out;
in vec3 vPosition[];
in vec2 vTexCoord[];
in vec3 vNormal[];
in vec3 vLocal[];
precise out vec3 tcNormal[];
precise out vec2 tcTexCoord[];
precise out vec3 tcPosition[];
out vec3 tcNormal[];
out vec2 tcTexCoord[];
out vec3 tcPosition[];
uniform mat4 uViewProjectionMatrix;
uniform mat4 uModelMatrix;
uniform vec3 cameraPosition;
......
......@@ -2,16 +2,14 @@
layout(triangles, equal_spacing, ccw) in;
precise in vec2 tcTexCoord[];
precise in vec3 tcNormal[];
precise in vec3 tcPosition[];
in vec2 tcTexCoord[];
in vec3 tcNormal[];
in vec3 tcPosition[];
precise out vec3 tNormal;
precise out vec2 tTexCoord;
precise out vec3 tPosition;
out vec3 tPatchDistance;
out vec3 tNormal;
out vec2 tTexCoord;
out vec3 tPosition;
uniform mat4 uViewProjectionMatrix;
uniform mat4 uModelMatrix;
uniform vec3 cameraPostion;
......@@ -205,7 +203,7 @@ void main(){
tNormal = interpolate3D(tcNormal[0], tcNormal[1], tcNormal[2]);
tTexCoord = interpolate2D(tcTexCoord[0], tcTexCoord[1], tcTexCoord[2]);
tPatchDistance = gl_TessCoord;
//tPatchDistance = gl_TessCoord;
tPosition = interpolate3D(tcPosition[0], tcPosition[1], tcPosition[2]);
//vec4 camWorld = uModelMatrix * vec4(tPosition, 1.0);
......
......@@ -14,5 +14,5 @@ struct DrawCall {
Geometry geometry;
glm::mat4 lastRenderTransform;
glm::mat4 thisRenderTransform;
bool renderToBuffer;
int recursionDepth;
};
\ No newline at end of file
......@@ -5,10 +5,10 @@
#include <engine/graphics/Material.hpp>
struct Drawable : Component<Drawable> {
explicit Drawable(Geometry geom, Material mat, bool tf = false) : geometry(geom), material(mat), renderToBuffer(tf){}
explicit Drawable(Geometry geom, Material mat, int tf = 0) : geometry(geom), material(mat), recursionDepth(tf){}
Geometry geometry;
Material material;
bool visible = true;
bool renderToBuffer = false;
int recursionDepth = false;
};
......@@ -25,12 +25,9 @@ glm::mat4 interpolate(TransformData a, TransformData b, float t, glm::vec3 camPo
a.pos = a.pos - camPos;
b.pos = b.pos - camPos;
glm::vec3 pos = a.pos + (b.pos - a.pos) * t;
//pos = pos - camPos;
glm::quat rot = glm::slerp(a.rot, b.rot, t);
glm::vec3 s = a.scale + (b.scale - a.scale) * t;
printf("Pos: %f, %f, %f, Cam: %f, %f, %f \n", pos.x, pos.y, pos.z, camPos.x, camPos.y, camPos.z);
return glm::translate(pos) * glm::mat4_cast(rot) * glm::scale(s);
}
......@@ -75,32 +72,52 @@ bool RendererSystem::startup() {
m_txaaHistoryBuffer->attachColorTexture("oColor", createScreenspaceTexture(ScreenSpaceSize::FULL, GL_RGBA32F));
m_txaaHistoryBuffer->validate();
// Create VAB to store transform feedback vertices. Size is a bit tricky because nobody knows how much
// vertices the tessellation shader creates.
// Create two VABs to store transform feedback vertices. Size is a bit tricky because nobody knows how much
// vertices the tessellation shader creates.
// First Buffer
m_transformFeedbackVAO = SharedVertexArrayObject(new VertexArrayObject(GL_TRIANGLES));
m_transformFeedbackBuffer = SharedArrayBuffer(new ArrayBuffer());
m_transformFeedbackBuffer->defineAttribute("gNormal", GL_FLOAT, 3);
m_transformFeedbackBuffer->defineAttribute("gPosition", GL_FLOAT, 3);
m_transformFeedbackBuffer->defineAttribute("gTexCoord", GL_FLOAT, 2);
m_transformFeedbackBuffer->defineAttribute("gPatchDistance", GL_FLOAT, 3);
m_transformFeedbackBuffer->defineAttribute("gTriDistance", GL_FLOAT, 3);
m_transformFeedbackVAO->attachAllAttributes(m_transformFeedbackBuffer);
m_transformFeedbackBuffer->bind(GL_TRANSFORM_FEEDBACK_BUFFER);
m_transformFeedbackVAO->bind();
m_transformFeedbackBuffer->setData(GL_TRANSFORM_FEEDBACK_BUFFER, 20000*3*3*2*3*3, NULL, GL_DYNAMIC_COPY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
m_transformFeedbackBuffer->setData(GL_TRANSFORM_FEEDBACK_BUFFER, 200000*3*3*2, NULL, GL_DYNAMIC_COPY);
// Second buffer
m_transformFeedbackVAO2 = SharedVertexArrayObject(new VertexArrayObject(GL_TRIANGLES));
m_transformFeedbackBuffer2 = SharedArrayBuffer(new ArrayBuffer());
m_transformFeedbackBuffer2->defineAttribute("gNormal", GL_FLOAT, 3);
m_transformFeedbackBuffer2->defineAttribute("gPosition", GL_FLOAT, 3);
m_transformFeedbackBuffer2->defineAttribute("gTexCoord", GL_FLOAT, 2);
m_transformFeedbackVAO2->attachAllAttributes(m_transformFeedbackBuffer2);
m_transformFeedbackBuffer2->bind(GL_TRANSFORM_FEEDBACK_BUFFER);
m_transformFeedbackVAO2->bind();
m_transformFeedbackBuffer2->setData(GL_TRANSFORM_FEEDBACK_BUFFER, 200000 * 3 * 3 * 2, NULL, GL_DYNAMIC_COPY);
// Feedback objects
Feedback = 0;
glGenTransformFeedbacks(1, &Feedback);
glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, Feedback);
m_transformFeedbackBuffer->bindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0);
glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, 0);
Feedback2 = 0;
glGenTransformFeedbacks(1, &Feedback2);
glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, Feedback2);
m_transformFeedbackBuffer2->bindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0);
glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, 0);
m_deferredCombineProgram = ShaderProgramFileManager::the()->get(ShaderProgramCreator("DeferredCombine"));
m_blitProgram = ShaderProgramFileManager::the()->get(ShaderProgramCreator("Blit"));
......@@ -111,8 +128,8 @@ bool RendererSystem::startup() {
m_tfShaderProg = ShaderProgramFileManager::the()->get(ShaderProgramCreator("TerrainTransformFeedback"));
char * outs[] = { "gNormal", "gPosition", "gTexCoord", "gPatchDistance", "gTriDistance" };
glTransformFeedbackVaryings(m_tfShaderProg->getObjectName(), 5, outs, GL_INTERLEAVED_ATTRIBS);
char * outs[] = { "gNormal", "gPosition", "gTexCoord"};
glTransformFeedbackVaryings(m_tfShaderProg->getObjectName(), 3, outs, GL_INTERLEAVED_ATTRIBS);
m_tfShaderProg->link();
m_dispMappingProg = ShaderProgramFileManager::the()->get(ShaderProgramCreator("DispMapping")
......@@ -257,7 +274,7 @@ void RendererSystem::frame(double interp, double totalTime) {
// Render drawcall to buffer
if (drawCall.renderToBuffer) {
for (int i = 0; i < drawCall.recursionDepth; i++) {
glEnable(GL_RASTERIZER_DISCARD);
......@@ -267,31 +284,34 @@ void RendererSystem::frame(double interp, double totalTime) {
m_tfShaderProg->setUniform("uViewProjectionMatrix", viewProjectionMatrix);
m_tfShaderProg->setUniform("cameraPosition", glm::vec3{ camPos.x, camPos.y, camPos.z });
m_tfShaderProg->setTexture("uTexture", drawCall.material.mainTexture, 0);
m_tfShaderProg->setUniform("uTime", (float)totalTime);
m_tfShaderProg->setUniform("uTintColor", drawCall.material.tintColor);
m_tfShaderProg->setUniform("uEmissiveColor", drawCall.material.emissiveColor);
m_tfShaderProg->setUniform("uPrevModelMatrix", drawCall.lastRenderTransform);
m_tfShaderProg->setUniform("uPrevViewProjectionMatrix", prevViewProjectionMatrix);
//m_transformFeedbackVAO->bind();
//m_transformFeedbackBuffer->bindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0);
q->begin();
m_transformFeedbackVAO->bind();
m_transformFeedbackBuffer->bindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0);
glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, Feedback);
// First pass will render the geometry vao into the transform feedback buffer which is the first one initially
if (i == 0) {
glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, Feedback);
glBeginTransformFeedback(GL_TRIANGLES);
drawCall.geometry.vao->render();
glEndTransformFeedback();
}
q->begin();
glPatchParameteri(GL_PATCH_VERTICES, 3);
glBeginTransformFeedback(GL_TRIANGLES);
drawCall.geometry.vao->render();
glEndTransformFeedback();
// If we're doing 2 recursion, render the first buffer into the second one.
else {
glGetBufferSubData(GL_TRANSFORM_FEEDBACK_BUFFER, 0, 1000, &data);
m_transformFeedbackVAO->bind();
m_transformFeedbackBuffer->bindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0);
q->end();
glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, Feedback2);
glBeginTransformFeedback(GL_TRIANGLES);
glDrawTransformFeedback(GL_PATCHES, Feedback);
glEndTransformFeedback();
}
printf("Written %i \n", q->getResult());
printf("Buffer data %f, %f, %f \n", data[3], data[4], data[5]);
q->end();
glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, 0);
......@@ -318,11 +338,16 @@ void RendererSystem::frame(double interp, double totalTime) {
drawCall.material.prog->setUniform("uPrevViewProjectionMatrix", prevViewProjectionMatrix);
// Draw buffer content
if (drawCall.renderToBuffer ) {
if (drawCall.recursionDepth == 1) {
m_transformFeedbackVAO->bind();
m_transformFeedbackBuffer->bind(GL_ARRAY_BUFFER);
glDrawTransformFeedback(GL_PATCHES, Feedback);
}
else if (drawCall.recursionDepth){
m_transformFeedbackVAO2->bind();
m_transformFeedbackBuffer2->bind(GL_ARRAY_BUFFER);
glDrawTransformFeedback(GL_PATCHES, Feedback2);
}
// Draw other stuff
else {
......
......@@ -56,11 +56,11 @@ bool OrbitalSimulationSystem::startup() {
.fragmentDataLocations(m_renderer->getTransparentLocations()));
planetMat = { glm::vec4(1, 1, 1, 1), glm::vec4(0, 0, 0, 1), texture, dispMappingShader, false, RenderQueue::OPAQUE };
planetMat = { glm::vec4(1, 1, 1, 1), glm::vec4(0, 0, 0, 1), texture, dispMappingShader, 1, RenderQueue::OPAQUE };
trajectoryMat = { glm::vec4(1, 1, 1, 1), glm::vec4(1, 0, 0, 1), texture, testShader, false, RenderQueue::OPAQUE };
trajectoryMat = { glm::vec4(1, 1, 1, 1), glm::vec4(1, 0, 0, 1), texture, testShader, 0, RenderQueue::OPAQUE };
atmosphereMat = { glm::vec4(0, 0, 1, 0.9f), glm::vec4(0, 0, .2, 1), texture, atmosphereShader, false, RenderQueue::TRANSPARENT };
atmosphereMat = { glm::vec4(0, 0, 1, 0.9f), glm::vec4(0, 0, .2, 1), texture, atmosphereShader, 0, RenderQueue::TRANSPARENT };
return true;
}
......@@ -142,7 +142,7 @@ Entity OrbitalSimulationSystem::addPlanet(Transform::Handle sun, std::string n,
auto planetEntity = m_scene->create();
auto planetTransform = planetEntity.assign<Transform>();
planetTransform->parent = sun;
planetEntity.assign<Drawable>(defaultGeom, planetMat, true);
planetEntity.assign<Drawable>(defaultGeom, planetMat, 1);
//planetEntity.assign<Light>(glm::vec4(1, 1, 1, 1), glm::vec3(1, 0, 0), false);
// Mass, Radius, Eccentricity, Semimajor axis, Inclination, Ascending Node, Arg. of Periapsis, time at perihelion
......
......@@ -86,7 +86,7 @@ void SceneGraphSystem::prepareDraw(double interp) {
e.unpack<Drawable, Transform>(drawable, transform);
auto thisRenderTransform = interpolate(transform->lastGlobalTransform, transform->thisGlobalTransform, interp, cam);
if (drawable->visible) {
m_renderer->submit({ drawable->material, drawable->geometry, transform->lastRenderTransform, thisRenderTransform, drawable->renderToBuffer }, drawable->material.queue);
m_renderer->submit({ drawable->material, drawable->geometry, transform->lastRenderTransform, thisRenderTransform, drawable->recursionDepth }, drawable->material.queue);
}
transform->lastRenderTransform = thisRenderTransform;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment