Skip to content

Commit

Permalink
Merge pull request #19 from JerboaBurrow/addMSAA
Browse files Browse the repository at this point in the history
adds msaa setting into instanced
  • Loading branch information
Jerboa-app authored Nov 20, 2023
2 parents 17df367 + d6fe0b7 commit 17c21db
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 90 deletions.
101 changes: 54 additions & 47 deletions examples/Particles/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ int main(int argv, char ** argc)
double rdt, rut, pt;

jGLInstance->setTextProjection(glm::ortho(0.0,double(resX),0.0,double(resY)));
jGLInstance->setMSAA(1);

std::shared_ptr<jGL::Particles> jGLParticles = jGLInstance->createParticles(nParticles);
std::vector<jGL::TexturedParticle> & jGLParticleStates = jGLParticles->getParticles();
Expand Down Expand Up @@ -75,65 +76,69 @@ int main(int argv, char ** argc)
{
tic = high_resolution_clock::now();

jGLInstance->clear();
jGLInstance->beginFrame();

pt0 = high_resolution_clock::now();
jGLInstance->clear();

particles.step(jGLParticleStates);
pt0 = high_resolution_clock::now();

pt = duration_cast<duration<double>>(high_resolution_clock::now()-pt0).count();
particles.step(jGLParticleStates);

rut0 = high_resolution_clock::now();
pt = duration_cast<duration<double>>(high_resolution_clock::now()-pt0).count();

jGLParticles->update
(
jGL::Particles::UpdateInfo {true, false, false}
);
rut0 = high_resolution_clock::now();

rut = duration_cast<duration<double>>(high_resolution_clock::now()-rut0).count();
jGLParticles->update
(
jGL::Particles::UpdateInfo {true, false, false}
);

rdt0 = high_resolution_clock::now();
rut = duration_cast<duration<double>>(high_resolution_clock::now()-rut0).count();

jGLParticles->draw(camera.getVP());
rdt0 = high_resolution_clock::now();

rdt = duration_cast<duration<double>>(high_resolution_clock::now()-rdt0).count();
jGLParticles->draw(camera.getVP());

delta = 0.0;
for (int n = 0; n < 60; n++)
{
delta += deltas[n];
}
delta /= 60.0;

std::stringstream debugText;

double mouseX, mouseY;
display.mousePosition(mouseX,mouseY);

debugText << "Delta: " << fixedLengthNumber(delta,6)
<< " ( FPS: " << fixedLengthNumber(1.0/delta,4)
<< ")\n"
<< "Physics/Render update/ Render draw time: \n"
<< " " << fixedLengthNumber(pt, 6) << ", " << fixedLengthNumber(rut, 6) << ", " << fixedLengthNumber(rdt, 6) << "\n"
<< "Mouse (" << fixedLengthNumber(mouseX,4)
<< ","
<< fixedLengthNumber(mouseY,4)
<< ")\n";

jGLInstance->text(
debugText.str(),
glm::vec2(64.0f, resY-64.0f),
0.5f,
glm::vec4(0.0f,0.0f,0.0f,1.0f)
);

if (frameId == 30)
{
if (log.size() > 0)
rdt = duration_cast<duration<double>>(high_resolution_clock::now()-rdt0).count();

delta = 0.0;
for (int n = 0; n < 60; n++)
{
std::cout << log << "\n";
delta += deltas[n];
}
delta /= 60.0;

std::stringstream debugText;

double mouseX, mouseY;
display.mousePosition(mouseX,mouseY);

debugText << "Delta: " << fixedLengthNumber(delta,6)
<< " ( FPS: " << fixedLengthNumber(1.0/delta,4)
<< ")\n"
<< "Physics/Render update/ Render draw time: \n"
<< " " << fixedLengthNumber(pt, 6) << ", " << fixedLengthNumber(rut, 6) << ", " << fixedLengthNumber(rdt, 6) << "\n"
<< "Mouse (" << fixedLengthNumber(mouseX,4)
<< ","
<< fixedLengthNumber(mouseY,4)
<< ")\n";

jGLInstance->text(
debugText.str(),
glm::vec2(64.0f, resY-64.0f),
0.5f,
glm::vec4(0.0f,0.0f,0.0f,1.0f)
);

if (frameId == 30)
{
if (log.size() > 0)
{
std::cout << log << "\n";
}
}
}

jGLInstance->endFrame();

display.loop();

Expand All @@ -143,5 +148,7 @@ int main(int argv, char ** argc)
frameId = (frameId+1) % 60;
}

jGLInstance->finish();

return 0;
}
88 changes: 48 additions & 40 deletions examples/Sprite/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ int main(int argv, char ** argc)
double rdt = 0.0;

jGLInstance->setTextProjection(glm::ortho(0.0,double(resX),0.0,double(resY)));
jGLInstance->setMSAA(1);

std::shared_ptr<jGL::Texture> heart = jGLInstance->createTexture
(
Expand Down Expand Up @@ -89,60 +90,67 @@ int main(int argv, char ** argc)
{
tic = high_resolution_clock::now();

jGLInstance->clear();
jGLInstance->beginFrame();

theta += 1.0/60.0 * 0.1;
scale = 0.1*std::abs(std::sin(theta))+0.05;
jGLInstance->clear();

sprites->getSprite("sHeart").update(jGL::Transform(0.5f, 0.5f, theta, 0.1f));
sprites->getSprite("sPi").update(jGL::Transform(0.2f, 0.2f, theta, scale));
theta += 1.0/60.0 * 0.1;
scale = 0.1*std::abs(std::sin(theta))+0.05;

sprites->draw({"sHeart", "sPi", "sDice"});
sprites->getSprite("sHeart").update(jGL::Transform(0.5f, 0.5f, theta, 0.1f));
sprites->getSprite("sPi").update(jGL::Transform(0.2f, 0.2f, theta, scale));

delta = 0.0;
for (int n = 0; n < 60; n++)
{
delta += deltas[n];
}
delta /= 60.0;

std::stringstream debugText;

double mouseX, mouseY;
display.mousePosition(mouseX,mouseY);

debugText << "Delta: " << fixedLengthNumber(delta,6)
<< " ( FPS: " << fixedLengthNumber(1.0/delta,4)
<< ")\n"
<< "Render draw time: \n"
<< " " << fixedLengthNumber(rdt, 6) << "\n"
<< "Mouse (" << fixedLengthNumber(mouseX,4)
<< ","
<< fixedLengthNumber(mouseY,4)
<< ")\n";

jGLInstance->text(
debugText.str(),
glm::vec2(64.0f, resY-64.0f),
0.5f,
glm::vec4(0.0f,0.0f,0.0f,1.0f)
);

if (frameId == 30)
{
if (log.size() > 0)
sprites->draw({"sHeart", "sPi", "sDice"});

delta = 0.0;
for (int n = 0; n < 60; n++)
{
std::cout << log << "\n";
delta += deltas[n];
}
}
delta /= 60.0;

std::stringstream debugText;

double mouseX, mouseY;
display.mousePosition(mouseX,mouseY);

debugText << "Delta: " << fixedLengthNumber(delta,6)
<< " ( FPS: " << fixedLengthNumber(1.0/delta,4)
<< ")\n"
<< "Render draw time: \n"
<< " " << fixedLengthNumber(rdt, 6) << "\n"
<< "Mouse (" << fixedLengthNumber(mouseX,4)
<< ","
<< fixedLengthNumber(mouseY,4)
<< ")\n";

jGLInstance->text(
debugText.str(),
glm::vec2(64.0f, resY-64.0f),
0.5f,
glm::vec4(0.0f,0.0f,0.0f,1.0f)
);

if (frameId == 30)
{
if (log.size() > 0)
{
std::cout << log << "\n";
}
}

jGLInstance->endFrame();

display.loop();

tock = high_resolution_clock::now();

deltas[frameId] = duration_cast<duration<double>>(tock-tic).count();
frameId = (frameId+1) % 60;

}

jGLInstance->finish();

return 0;
}
4 changes: 3 additions & 1 deletion include/jGL/Display/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ namespace jGL
GLFWwindow * getWindow() const { return glfwWindow; }

bool isOpen(){ if (glfwWindow != NULL) { return !glfwWindow ? false : true; } return false; }


bool closing(){ return glfwWindowShouldClose(glfwWindow); }

void open(){
if (glfwWindow == NULL)
{
Expand Down
104 changes: 104 additions & 0 deletions include/jGL/OpenGL/glDrawFramebuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#ifndef GLDRAWFRAMEBUFFER
#define GLDRAWFRAMEBUFFER

#include <cstdint>

#include <glm/glm.hpp>

#include <jGL/OpenGL/gl.h>

namespace jGL::GL
{
class glDrawFramebuffer
{

public:

glDrawFramebuffer()
{
if(!glIsFramebuffer(buffer))
{
glGenFramebuffers(1, &buffer);
}

if(!glIsTexture(texture))
{
glGenTextures(1, &texture);
}
}

void destroy()
{
if (glIsFramebuffer(buffer))
{
glDeleteFramebuffers(1, &buffer);
}

if (glIsTexture(texture))
{
glDeleteTextures(1, &texture);
}
}

void bind()
{
glBindFramebuffer(GL_FRAMEBUFFER, buffer);
glBufferStatus("bind framebuffer");
}

void blit()
{
glBindFramebuffer(GL_READ_FRAMEBUFFER, buffer);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glBlitFramebuffer(0, 0, resX, resY, 0, 0, resX, resY, GL_COLOR_BUFFER_BIT, GL_NEAREST);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glError("blit framebuffer");
}

void setClear(glm::vec4 c)
{
clearColour = c;
glClearColor(clearColour.r, clearColour.g, clearColour.b, clearColour.a);
}

void clear() { glClear(GL_COLOR_BUFFER_BIT); }

void setResolution(glm::vec2 res)
{
resX = res.x;
resY = res.y;
}

void setMSAA(uint8_t samples)
{
msaaSamples = samples;

glBindFramebuffer(GL_FRAMEBUFFER, buffer);
glActiveTexture(GL_TEXTURE0);

glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, texture);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, msaaSamples, GL_RGB, resX, resY, GL_TRUE);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, texture, 0);
setClear(clearColour);
}

static const bool isMSAA() {return msaaSamples > 0;}

private:

static bool initialised;

static uint8_t msaaSamples;

static GLuint buffer, texture;

static uint16_t resX, resY;

static glm::vec4 clearColour;

};
}

#endif /* GLDrawFRAMEBUFFER */
Loading

0 comments on commit 17c21db

Please sign in to comment.