mirror of
https://github.com/lubosz/overte.git
synced 2025-04-23 23:33:48 +02:00
changed gl calls to gpu::
This commit is contained in:
parent
fe74ec3f89
commit
883aa7af8d
1 changed files with 128 additions and 35 deletions
|
@ -21,20 +21,27 @@
|
|||
#include <QLoggingCategory>
|
||||
|
||||
#include <gpu/Context.h>
|
||||
#include <gpu/Batch.h>
|
||||
#include <gpu/Stream.h>
|
||||
#include <gpu/StandardShaderLib.h>
|
||||
//#include <shared/ViewFrustrum.h>
|
||||
|
||||
#include <gpu/GLBackend.h>
|
||||
|
||||
#include <QOpenGLBuffer>
|
||||
//#include <QOpenGLBuffer>
|
||||
#include <QOpenGLContext>
|
||||
#include <QOpenGLDebugLogger>
|
||||
#include <QOpenGLShaderProgram>
|
||||
#include <QOpenGLTexture>
|
||||
#include <QOpenGLVertexArrayObject>
|
||||
//#include <QOpenGLDebugLogger>
|
||||
//#include <QOpenGLShaderProgram>
|
||||
//#include <QOpenGLTexture>
|
||||
//#include <QOpenGLVertexArrayObject>
|
||||
#include <QResizeEvent>
|
||||
#include <QTime>
|
||||
#include <QTimer>
|
||||
#include <QWindow>
|
||||
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
|
||||
#include <PathUtils.h>
|
||||
|
||||
|
@ -74,6 +81,15 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
const char * basicVS =
|
||||
" varying vec3 pos; "
|
||||
" void main(void) { "
|
||||
" gl_Position.xyz = pos; "
|
||||
" } ";
|
||||
const char * basicFS =
|
||||
" void main(void) { "
|
||||
" gl_FragColor.xyz = vec3(0.7, 0.2, 0.5); "
|
||||
" } ";
|
||||
|
||||
const QString& getQmlDir() {
|
||||
static QString dir;
|
||||
|
@ -90,8 +106,14 @@ const QString& getQmlDir() {
|
|||
class QTestWindow : public QWindow {
|
||||
Q_OBJECT
|
||||
|
||||
QOpenGLContext* _context{ nullptr };
|
||||
QOpenGLContext* _qGlContext{ nullptr };
|
||||
QSize _size;
|
||||
|
||||
gpu::ContextPointer _context;
|
||||
gpu::PipelinePointer _pipeline;
|
||||
gpu::BufferPointer _buffer;
|
||||
gpu::Stream::FormatPointer _format;
|
||||
|
||||
//TextRenderer* _textRenderer[4];
|
||||
RateCounter fps;
|
||||
|
||||
|
@ -117,27 +139,62 @@ public:
|
|||
|
||||
setFormat(format);
|
||||
|
||||
_context = new QOpenGLContext;
|
||||
_context->setFormat(format);
|
||||
_context->create();
|
||||
_qGlContext = new QOpenGLContext;
|
||||
_qGlContext->setFormat(format);
|
||||
_qGlContext->create();
|
||||
|
||||
show();
|
||||
makeCurrent();
|
||||
|
||||
gpu::Context::init<gpu::GLBackend>();
|
||||
|
||||
|
||||
|
||||
{
|
||||
QOpenGLDebugLogger* logger = new QOpenGLDebugLogger(this);
|
||||
logger->initialize(); // initializes in the current context, i.e. ctx
|
||||
logger->enableMessages();
|
||||
connect(logger, &QOpenGLDebugLogger::messageLogged, this, [&](const QOpenGLDebugMessage & debugMessage) {
|
||||
qDebug() << debugMessage;
|
||||
});
|
||||
// logger->startLogging(QOpenGLDebugLogger::SynchronousLogging);
|
||||
_context = std::make_shared<gpu::Context>();
|
||||
|
||||
// Clear screen
|
||||
gpu::Batch batch;
|
||||
batch.clearColorFramebuffer(gpu::Framebuffer::BUFFER_COLORS, { 1.0, 0.0, 0.5, 1.0 });
|
||||
_context->render(batch);
|
||||
|
||||
// Create default shaders
|
||||
|
||||
std::string vsSource (basicVS);
|
||||
std::string fsSource (basicFS);
|
||||
|
||||
auto vs = gpu::ShaderPointer(gpu::Shader::createVertex(vsSource));
|
||||
auto fs = gpu::ShaderPointer(gpu::Shader::createPixel(fsSource));
|
||||
auto shader = gpu::ShaderPointer(gpu::Shader::createProgram(vs, fs));
|
||||
|
||||
gpu::Shader::BindingSet bindings;
|
||||
if (!gpu::Shader::makeProgram(*shader, bindings)) {
|
||||
printf("Could not compile shader");
|
||||
exit(-1);
|
||||
}
|
||||
qDebug() << (const char*)glGetString(GL_VERSION);
|
||||
|
||||
// auto shader = gpu::StandardShaderLib::getDrawViewportQuadTransformTexcoordVS();
|
||||
|
||||
auto state = std::make_shared<gpu::State>();
|
||||
_pipeline = gpu::PipelinePointer(gpu::Pipeline::create(shader, state));
|
||||
|
||||
float z = 10.0f;
|
||||
const glm::vec3 vertices[] = {
|
||||
{ -1.0f, 1.0f, z },
|
||||
{ 1.0f, -1.0f, z },
|
||||
{ -1.0f, -1.0f, z }
|
||||
};
|
||||
_buffer = std::make_shared<gpu::Buffer>(sizeof(vertices), (const gpu::Byte*)vertices);
|
||||
_format = std::make_shared<gpu::Stream::Format>();
|
||||
_format->setAttribute(gpu::Stream::POSITION, gpu::Stream::POSITION, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ));
|
||||
|
||||
|
||||
// {
|
||||
// QOpenGLDebugLogger* logger = new QOpenGLDebugLogger(this);
|
||||
// logger->initialize(); // initializes in the current context, i.e. ctx
|
||||
// logger->enableMessages();
|
||||
// connect(logger, &QOpenGLDebugLogger::messageLogged, this, [&](const QOpenGLDebugMessage & debugMessage) {
|
||||
// qDebug() << debugMessage;
|
||||
// });
|
||||
// // logger->startLogging(QOpenGLDebugLogger::SynchronousLogging);
|
||||
// }
|
||||
// qDebug() << (const char*)glGetString(GL_VERSION);
|
||||
|
||||
//_textRenderer[0] = TextRenderer::getInstance(SANS_FONT_FAMILY, 12, false);
|
||||
//_textRenderer[1] = TextRenderer::getInstance(SERIF_FONT_FAMILY, 12, false,
|
||||
|
@ -146,12 +203,14 @@ public:
|
|||
// false, TextRenderer::OUTLINE_EFFECT);
|
||||
//_textRenderer[3] = TextRenderer::getInstance(INCONSOLATA_FONT_FAMILY, 24);
|
||||
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glClearColor(0.2f, 0.2f, 0.2f, 1);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
// glEnable(GL_BLEND);
|
||||
// glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
// glClearColor(0.2f, 0.2f, 0.2f, 1);
|
||||
// glDisable(GL_DEPTH_TEST);
|
||||
|
||||
makeCurrent();
|
||||
|
||||
_context->syncCache();
|
||||
|
||||
setFramePosition(QPoint(-1000, 0));
|
||||
resize(QSize(800, 600));
|
||||
|
@ -162,7 +221,7 @@ public:
|
|||
|
||||
void draw();
|
||||
void makeCurrent() {
|
||||
_context->makeCurrent(this);
|
||||
_qGlContext->makeCurrent(this);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
@ -180,8 +239,12 @@ protected:
|
|||
//static const wchar_t* EXAMPLE_TEXT = L"\xC1y Hello 1.0\ny\xC1 line 2\n\xC1y";
|
||||
static const glm::uvec2 QUAD_OFFSET(10, 10);
|
||||
|
||||
static const glm::vec3 COLORS[4] = { { 1.0, 1.0, 1.0 }, { 0.5, 1.0, 0.5 }, {
|
||||
1.0, 0.5, 0.5 }, { 0.5, 0.5, 1.0 } };
|
||||
static const glm::vec3 COLORS[4] = {
|
||||
{ 1.0, 1.0, 1.0 },
|
||||
{ 0.5, 1.0, 0.5 },
|
||||
{ 1.0, 0.5, 0.5 },
|
||||
{ 0.5, 0.5, 1.0 }
|
||||
};
|
||||
|
||||
|
||||
void QTestWindow::draw() {
|
||||
|
@ -190,15 +253,45 @@ void QTestWindow::draw() {
|
|||
}
|
||||
|
||||
makeCurrent();
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glViewport(0, 0, _size.width() * devicePixelRatio(), _size.height() * devicePixelRatio());
|
||||
|
||||
_context->swapBuffers(this);
|
||||
glFinish();
|
||||
|
||||
gpu::Batch batch;
|
||||
static int frameNum = 0;
|
||||
frameNum++;
|
||||
batch.clearColorFramebuffer(gpu::Framebuffer::BUFFER_COLORS, { 1.0, float(frameNum % 60)/60.0f, 0.5, 1.0 });
|
||||
// _context->render(batch);
|
||||
|
||||
|
||||
//// batch.clear();
|
||||
// batch.setViewportTransform({ 0, 0, _size.width() * devicePixelRatio(), _size.height() * devicePixelRatio() });
|
||||
//
|
||||
glm::quat cubeOrientation;
|
||||
//
|
||||
batch.setViewTransform(Transform());
|
||||
batch.setProjectionTransform(glm::mat4());
|
||||
// batch.setProjectionTransform(_viewFrustrum->getProjection());
|
||||
batch.setModelTransform(Transform().setRotation(cubeOrientation));
|
||||
batch.setPipeline(_pipeline);
|
||||
batch.setInputBuffer(gpu::Stream::POSITION, _buffer, 0, 3);
|
||||
batch.setInputFormat(_format);
|
||||
batch.draw(gpu::TRIANGLES, 3);
|
||||
_context->render(batch);
|
||||
//
|
||||
//// gpu::Stream::Format format;
|
||||
//// format.setAttribute(gpu::Stream::POSITION, gpu::Stream::POSITION, gpu::Element(gpu::Vec3, gpu::FLOAT, gpu::XYZ));
|
||||
//// batch.setInputBuffer(gpu::Stream::POSITION, _trianglePosBuffer, )
|
||||
//
|
||||
//
|
||||
//// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
//// glViewport(0, 0, _size.width() * devicePixelRatio(), _size.height() * devicePixelRatio());
|
||||
//
|
||||
//// _context->swapBuffers(this);
|
||||
//// glFinish();
|
||||
//
|
||||
_qGlContext->swapBuffers(this);
|
||||
|
||||
fps.increment();
|
||||
if (fps.elapsed() >= 2.0f) {
|
||||
qDebug() << "FPS: " << fps.rate();
|
||||
qDebug() << "FPS: " << fps.rate(); // This prints out the frames per 2 secs (ie. half of the actual fps) bug...?
|
||||
fps.reset();
|
||||
}
|
||||
}
|
||||
|
@ -207,7 +300,7 @@ int main(int argc, char** argv) {
|
|||
QGuiApplication app(argc, argv);
|
||||
QTestWindow window;
|
||||
QTimer timer;
|
||||
timer.setInterval(1);
|
||||
timer.setInterval(1000 / 120.0f);
|
||||
app.connect(&timer, &QTimer::timeout, &app, [&] {
|
||||
window.draw();
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue