I am trying to figure out how to use OpenGL with QtQuickPaintedItem. According to the documentation, this seems to be possible, and there are several "helper" classes for that.
I tried this code below, and I also tried to create QOpenGLPaintDevice, QOpenGLContext, and QOpenGLFramebufferObject and pass their instances to their functions in many combinations, but it did not work either.
/// Simple3d.h
#ifndef SIMPLE3D_H
#define SIMPLE3D_H
#include<QtQuick/QQuickPaintedItem>
#include<QOpenGLFunctions>
// Not sure if I need to use any of these classes.
//#include<QOpenGLPaintDevice>
//#include<QOpenGLContext>
//#include<QOpenGLFramebufferObject>
//#include<QOpenGLShaderProgram>
//#include <GL/gl.h>
/** #brief inherets from QtQuickItem and it draws
* triangle.
*
*/
class Simple3d : public QQuickPaintedItem
{
Q_OBJECT
public:
/** Call this function to be able to instantiate this
* item in a QML file.
*/
static void register_qml_item();
Simple3d();
void paint(QPainter * painter) override;
virtual ~Simple3d() override;
private:
bool mDebug;
/** OpenGL stuff */
bool mOpenGlInitialized;
GLuint mProgramId;
static const GLuint mVertexArrayID = 0;
QOpenGLFunctions *mGlFuncs;
// Not sure when is a good time to update these:
GLsizei mWidth, mHeight;
GLuint load_shader(GLenum type, const char *shaderSrc);
void verify_initialized();
void draw_open_gl();
};
#endif
/// Simple3d.cpp
#include "simple3d.h"
#include <QSGGeometryNode>
#include <QSGVertexColorMaterial>
#include <QtMath>
#include<QOpenGLFunctions>
#include<QPainter>
#include<QGuiApplication>
using namespace std;
Simple3d::Simple3d()
{
mDebug = true;
mOpenGlInitialized = false;
mGlFuncs = nullptr;
setFlag(ItemHasContents, true);
}
void Simple3d::paint(QPainter *painter)
{
if (mDebug) qDebug("Simple3d is running paint()");
if (!mGlFuncs){
mGlFuncs = new QOpenGLFunctions();
mGlFuncs->initializeOpenGLFunctions();
}
painter->beginNativePainting();
verify_initialized();
draw_open_gl();
painter->endNativePainting();
if (mDebug) qDebug("Simple3d finished rendering.");
}
GLuint Simple3d::load_shader(GLenum type, const char *shaderSrc)
{
GLuint shader;
GLint compiled;
// Create the shader object
shader = mGlFuncs->glCreateShader(type);
if(shader == 0)
return 0;
// Load the shader source
mGlFuncs->glShaderSource(shader, 1, &shaderSrc, nullptr);
// Compile the shader
mGlFuncs->glCompileShader(shader);
// Check the compile status
mGlFuncs->glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if(!compiled)
{
GLint infoLen = 0;
mGlFuncs->glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
if(infoLen > 1)
{
char* infoLog = new char[size_t(infoLen)+1];
mGlFuncs->glGetShaderInfoLog(shader, infoLen, nullptr, infoLog);
qDebug("In the shader source:\n%s", shaderSrc);
qDebug("Error compiling shader:\n%s\n", infoLog);
delete [] infoLog;
}
mGlFuncs->glDeleteShader(shader);
return 0;
}
return shader;
}
void Simple3d::verify_initialized()
{
if (mOpenGlInitialized) return;
// This piece is partially taken from:
// https://www.khronos.org/assets/uploads/books/openglr_es_20_programming_guide_sample.pdf
mWidth = 50;
mHeight = 50;
const char vShaderStr[] =
"attribute vec4 vPosition;\n"
"void main()\n"
"{\n"
" gl_Position = vPosition;\n"
"}\n";
const char fShaderStr[] =
"//precision mediump float;\n"
"void main()\n"
"{\n"
" gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); \n"
"}\n";
GLuint vertexShader;
GLuint fragmentShader;
GLint linked;
// Load the vertex/fragment shaders
vertexShader = load_shader(GL_VERTEX_SHADER, vShaderStr);
fragmentShader = load_shader(GL_FRAGMENT_SHADER, fShaderStr);
// Create the program object
mProgramId = mGlFuncs->glCreateProgram();
if(mProgramId == 0) {
qDebug("Failed to create the OpenGL program.");
return;
}
mGlFuncs->glAttachShader(mProgramId, vertexShader);
mGlFuncs->glAttachShader(mProgramId, fragmentShader);
// Bind vPosition to attribute 0
mGlFuncs->glBindAttribLocation(mProgramId, mVertexArrayID, "vPosition");
// Link the program
mGlFuncs->glLinkProgram(mProgramId);
// Check the link status
mGlFuncs->glGetProgramiv(mProgramId, GL_LINK_STATUS, &linked);
if(!linked)
{
qDebug("Failed to link OpenGL program.");
GLint infoLen = 0;
mGlFuncs->glGetProgramiv(mProgramId, GL_INFO_LOG_LENGTH, &infoLen);
if(infoLen > 1)
{
char* infoLog = new char[size_t(infoLen)];
mGlFuncs->glGetProgramInfoLog(mProgramId, infoLen, nullptr, infoLog);
qDebug("Error linking program:\n%s\n", infoLog);
delete [] infoLog;
}
mGlFuncs->glDeleteProgram(mProgramId);
return;
}
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
mOpenGlInitialized = true;
qDebug("OpenGL was initialized successfully.");
return;
}
void Simple3d::draw_open_gl()
{
if (!mOpenGlInitialized){
QGuiApplication::exit(1);
return;
}
mGlFuncs->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
mGlFuncs->glEnable(GL_DEPTH_TEST);
mGlFuncs->glUseProgram(mProgramId);
GLfloat vVertices[] =
{0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f};
// Set the viewport
mGlFuncs->glViewport(0, 0, mWidth, mHeight);
// Clear the color buffer
glClear(GL_COLOR_BUFFER_BIT);
// Load the vertex data
mGlFuncs->glVertexAttribPointer(mVertexArrayID, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
mGlFuncs->glEnableVertexAttribArray(0);
mGlFuncs->glDrawArrays(GL_TRIANGLES, 0, 3);
mGlFuncs->glFlush();
mGlFuncs->glFinish();
mGlFuncs->glUseProgram(0); // release the program.
}
Simple3d::~Simple3d()
{
}
void Simple3d::register_qml_item()
{
qmlRegisterType<Simple3d>("CustomGraphicsItems", 1, 0, "Simple3d");
}
/// main.cpp
#include "simple3d.h"
#include <stdexcept>
using namespace std;
int main(int argc, char *argv[]){
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
Simple3d::register_qml_item();
QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/example_Simple3d.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl)
{
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
int num_root_objects = engine.rootObjects().size();
qDebug("The number of root objects: %i", num_root_objects);
if (engine.rootObjects().size()!=1){
throw logic_error("Expected exactly one root object"
"in the qml file");
}
QObject * root = engine.rootObjects()[0];
Simple3d * draw_3d_item = root->findChild<Simple3d*>("draw_3d_item");
if (!draw_3d_item){
throw logic_error("Did not find item 'draw_3d_item'.");
}
int result = app.exec();
qDebug("The app is exiting.");
return result;
}
/// example_Simple3d.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import CustomGraphicsItems 1.0
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.12
Window {
visible: true
Simple3d{
id: draw_3d_item
width: 50
height: 50
anchors.fill: parent
objectName: "draw_3d_item"
}
}
The code should display a red triangle on black background, but nothing is being drawn.
This question is similar to this: Rendering custom opengl in qt5's qtquick 2.0
I liked the answer by Gunnar Sletta. The trick is to use QQuickFramebufferObject, not QtQuickPaintedItem.
The first lines of QQuickFramebufferObject reference page say that it is an auxiliary class, but in fact it is the key class if you want to create a custom OpenGL within QtQuick framework. They should have called it
QQuickOpenGLItem.
Just read this is the example of using QQuickFramebufferObject:
https://doc.qt.io/qt-5/qtquick-scenegraph-textureinsgnode-example.html (Click on link "Example project # code.qt.io".)
Related
I have created a simple wxGLCanvas for demonstrating OpenGl using wxWidgets. The demo is working fine except when resizing the window the memory usage increases from a few megabytes to almost 400 megabytes and it stays there and doesn't decrease, here are the code snippets.
// ctor
TriangleCanvas::TriangleCanvas(wxWindow* parent, wxGLAttributes& attribList)
: wxGLCanvas(parent, attribList, wxID_ANY, { 0,0 }, wxDefaultSize),
m_vbo(0), m_vao(0), ctx_attr(new wxGLContextAttrs)
{
ctx_attr->CoreProfile().OGLVersion(4, 3).EndList();
m_context = new wxGLContext(this, NULL, ctx_attr);
Bind(wxEVT_PAINT, &TriangleCanvas::OnPaint, this);
Bind(wxEVT_SIZE, &TriangleCanvas::Resize, this);
}
// Paint method
void TriangleCanvas::OnPaint(wxPaintEvent& event)
{
wxPaintDC(this);
SetCurrent(*m_context);
shader->use();
// set background to black
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// draw the graphics
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glFlush();
SwapBuffers();
}
void TriangleCanvas::Resize(wxSizeEvent& event) {
event.Skip();
glViewport(0, 0, event.GetSize().x, event.GetSize().y);
if (!setup) {
InitializeGLEW();
SetupGraphics();
}
}
I think the best way to use wxGLCanvas with an extension loader is to use a helper class and keep all OpenGL drawing in the cpp portion of that helper class.
For example, here is a small helper class for drawing a triangle:
glhelper.h
#ifndef GLHELPER_H_INCLUDED
#define GLHELPER_H_INCLUDED
class GLHelper
{
public:
bool InitGlew();
void Render();
void SetSize(int w, int h);
bool InitData();
void Cleanup();
private:
unsigned int m_VBO, m_VAO, m_shaderProgram;
};
#endif // GLHELPER_H_INCLUDED
glhelper.cpp
#include <GL/glew.h>
#ifdef __WXMSW__
#include <GL/wglew.h>
#elif defined(__WXGTK__)
#include <GL/glxew.h>
#endif // defined
#include "glhelper.h"
static const char *vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
static const char *fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n\0";
bool GLHelper::InitGlew()
{
GLenum initStatus = glewInit();
return initStatus == GLEW_OK;
}
bool GLHelper::InitData()
{
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
// check for shader compile errors
int success;
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
return false;
}
// fragment shader
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
// check for shader compile errors
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
return false;
}
// link shaders
m_shaderProgram = glCreateProgram();
glAttachShader(m_shaderProgram, vertexShader);
glAttachShader(m_shaderProgram, fragmentShader);
glLinkProgram(m_shaderProgram);
// check for linking errors
glGetProgramiv(m_shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
return false;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
float vertices[] = {
-0.5f, -0.5f, 0.0f, // left
0.5f, -0.5f, 0.0f, // right
0.0f, 0.5f, 0.0f // top
};
//unsigned int VBO, VAO;
glGenVertexArrays(1, &m_VAO);
glGenBuffers(1, &m_VBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s),
// and then configure vertex attributes(s).
glBindVertexArray(m_VAO);
glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// note that this is allowed, the call to glVertexAttribPointer registered
//VBO as the vertex attribute's bound vertex buffer object so afterwards we
//can safely unbind
glBindBuffer(GL_ARRAY_BUFFER, 0);
// You can unbind the VAO afterwards so other VAO calls won't accidentally
//modify this VAO, but this rarely happens. Modifying other VAOs requires a
//call to glBindVertexArray anyways so we generally don't unbind VAOs (nor
// VBOs) when it's not directly necessary.
glBindVertexArray(0);
return true;
}
void GLHelper::Cleanup()
{
glDeleteVertexArrays(1, &m_VAO);
glDeleteBuffers(1, &m_VBO);
glDeleteProgram(m_shaderProgram);
}
void GLHelper::Render()
{
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// draw our first triangle
glUseProgram(m_shaderProgram);
glBindVertexArray(m_VAO);
// seeing as we only have a single VAO there's no need to bind it every
//time, but we'll do so to keep things a bit more organized
glDrawArrays(GL_TRIANGLES, 0, 3);
}
void GLHelper::SetSize(int width, int height)
{
glViewport(0, 0, width, height);
}
And here is a short demo that uses this helper class to draw the trinagle.
demo.cpp
#include "wx/wx.h"
#include <wx/glcanvas.h>
#include "glhelper.h"
class wxGlewFrame: public wxFrame
{
public:
wxGlewFrame(wxWindow*);
~wxGlewFrame();
private:
void OnCanvasSize(wxSizeEvent&);
void OnCanvasPaint(wxPaintEvent&);
void InitGL();
wxGLCanvas* m_canvas;
wxGLContext* m_context;
GLHelper m_helper;
};
wxGlewFrame::wxGlewFrame(wxWindow* parent)
: wxFrame(parent, wxID_ANY, wxString())
{
// Create the canvas and context.
#if wxCHECK_VERSION(3,1,0)
// These settings should work with any GPU from the last 10 years.
wxGLAttributes dispAttrs;
dispAttrs.PlatformDefaults().RGBA().DoubleBuffer().EndList();
wxGLContextAttrs cxtAttrs;
cxtAttrs.PlatformDefaults().CoreProfile().OGLVersion(3, 3).EndList();
m_canvas = new wxGLCanvas(this, dispAttrs);
m_context = new wxGLContext(m_canvas, NULL, &cxtAttrs);
if ( !m_context->IsOK() )
{
SetTitle("Failed to create context.");
return;
}
#else
int dispAttrs[] = { WX_GL_RGBA, WX_GL_DOUBLEBUFFER, WX_GL_CORE_PROFILE,
WX_GL_MAJOR_VERSION ,3, WX_GL_MINOR_VERSION, 3, 0 };
m_canvas = new wxGLCanvas(this, wxID_ANY, dispAttrs);
m_context = new wxGLContext(m_canvas, NULL);
// Unfortunately, there doesn't seem to be any way to check if the
// context is ok prior to wxWidgets 3.1.0.
#endif // wxCHECK_VERSION
// On Linux, we must delay delay initialization until the canvas has
// been full created. On windows, we can finish now.
#ifdef __WXMSW__
InitGL();
#elif defined(__WXGTK__)
m_canvas->Bind(wxEVT_CREATE, [this](wxWindowCreateEvent&){InitGL();});
#endif // defined
}
wxGlewFrame::~wxGlewFrame()
{
m_helper.Cleanup();
delete m_context;
}
void wxGlewFrame::OnCanvasSize(wxSizeEvent& event)
{
wxSize sz = event.GetSize();
m_helper.SetSize(sz.GetWidth(), sz.GetHeight());
event.Skip();
}
void wxGlewFrame::OnCanvasPaint(wxPaintEvent&)
{
wxPaintDC dc(m_canvas);
m_helper.Render();
m_canvas->SwapBuffers();
}
void wxGlewFrame::InitGL()
{
// First call SetCurrent or GL initialization will fail.
m_context->SetCurrent(*m_canvas);
// Initialize GLEW.
bool glewInialized = m_helper.InitGlew();
if ( !glewInialized )
{
SetTitle("Failed it initialize GLEW.");
return;
}
SetTitle("Context and GLEW initialized.");
// Initialize the triangle data.
m_helper.InitData();
// Bind event handlers for the canvas. Binding was delayed until OpenGL was
// initialized because these handlers will need to call OpenGL functions.
m_canvas->Bind(wxEVT_SIZE, &wxGlewFrame::OnCanvasSize, this);
m_canvas->Bind(wxEVT_PAINT, &wxGlewFrame::OnCanvasPaint, this);
}
class MyApp : public wxApp
{
public:
virtual bool OnInit()
{
wxGlewFrame* frame = new wxGlewFrame(NULL);
frame->Show();
return true;
}
};
wxIMPLEMENT_APP(MyApp);
(This is basically the Hello Triangle sample from learnopengl.com except rewritten to use wxGLCanvas and GLEW instead of GLFW and GLAD.
This takes up about 19MB of memory on my system and only increases up to about 24 or 25MB when resizing. That might sound like a lot for such a simple program, but the running the official "Hello Triangle" sample uses 26MB. So I think the memory usage is about what should be expected.
I've just started following OpenGL SuperBible 7th ed, and translating the examples into LWJGL, but have become stuck on the tessellation shader. In the following program there is the line " //IF THESE TWO LINES..." if the following two lines are commented out then the vertex and fragment shaders work but when the control.tess.glsl and eval.tess.glsl are included then the triangle no longer renders.
I've uploaded my program onto github but will reproduce the code here as well:
package com.ch3vertpipeline;
public class App {
public static void main(String [] args){
LwjglSetup setup = new LwjglSetup();
setup.run();
}
}
package com.ch3vertpipeline;
import java.nio.IntBuffer;
import java.util.Scanner;
import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import org.lwjgl.system.*;
import static org.lwjgl.glfw.Callbacks.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;
import static org.lwjgl.system.MemoryStack.stackPush;
import static org.lwjgl.system.MemoryUtil.NULL;
public class LwjglSetup {
private long window;
private int vertex_shader;
private int fragment_shader;
private int tess_control_shader;
private int tess_evaluation_shader;
private int program;
private int vertex_array_object;
public LwjglSetup() {
}
private void init() {
GLFWErrorCallback.createPrint(System.err).set();
if (!glfwInit()) {
throw new IllegalStateException("Unable to initialize GLFW");
}
// Configure GLFW
glfwDefaultWindowHints(); // optional, the current window hints are already the default
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // the window will stay hidden after creation
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable
// Create the window
window = glfwCreateWindow(300, 300, "Hello World!", NULL, NULL);
if (window == NULL) {
throw new RuntimeException("Failed to create the GLFW window");
}
// Setup a key callback. It will be called every time a key is pressed, repeated or released.
glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> {
if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) {
glfwSetWindowShouldClose(window, true); // We will detect this in the rendering loop
}
});
// Get the thread stack and push a new frame
try (MemoryStack stack = stackPush()) {
IntBuffer pWidth = stack.mallocInt(1); // int*
IntBuffer pHeight = stack.mallocInt(1); // int*
// Get the window size passed to glfwCreateWindow
glfwGetWindowSize(window, pWidth, pHeight);
// Get the resolution of the primary monitor
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
// Center the window
glfwSetWindowPos(
window,
(vidmode.width() - pWidth.get(0)) / 2,
(vidmode.height() - pHeight.get(0)) / 2
);
} // the stack frame is popped automatically
// Make the OpenGL context current
glfwMakeContextCurrent(window);
// Enable v-sync
glfwSwapInterval(1);
// Make the window visible
glfwShowWindow(window);
}
public void run() {
System.out.println("Hello LWJGL " + Version.getVersion() + "!");
init();
loop();
// Free the window callbacks and destroy the window
glfwFreeCallbacks(window);
glfwDestroyWindow(window);
// Terminate GLFW and free the error callback
glfwTerminate();
glfwSetErrorCallback(null).free();
}
private void loop() {
GL.createCapabilities();//Critical
System.out.println("OpenGL Verion: " + glGetString(GL_VERSION));
this.compileShader();
vertex_array_object = glGenVertexArrays();
glBindVertexArray(vertex_array_object);
while (!glfwWindowShouldClose(window)) {
double curTime = System.currentTimeMillis() / 1000.0;
double slowerTime = curTime;//assigned direcly but I was applying a factor here
final float colour[] = {
(float) Math.sin(slowerTime) * 0.5f + 0.5f,
(float) Math.cos(slowerTime) * 0.5f + 0.5f,
0.0f, 1.0f};
glClearBufferfv(GL_COLOR, 0, colour);
glUseProgram(program);
final float attrib[] = {
(float) Math.sin(slowerTime) * 0.5f,
(float) Math.cos(slowerTime) * 0.6f,
0.0f, 0.0f};
//glPatchParameteri(GL_PATCH_VERTICES, 3);//this is the default so is unneeded
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glVertexAttrib4fv(0, attrib);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window); // swap the color buffers
glfwPollEvents();
}
glDeleteVertexArrays(vertex_array_object);
glDeleteProgram(program);
}
private String readFileAsString(String filename) {
String next = new Scanner(LwjglSetup.class.getResourceAsStream(filename), "UTF-8").useDelimiter("\\A").next();
System.out.println("readFileAsString: " + next);
return next;
}
private void compileShader() {
//int program;
//NEW CODE
//create and compile vertex shader
String vertShaderSource = readFileAsString("/vert.glsl");
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, vertShaderSource);
glCompileShader(vertex_shader);
//check compilation
if (glGetShaderi(vertex_shader, GL_COMPILE_STATUS) != 1) {
System.err.println(glGetShaderInfoLog(vertex_shader));
System.exit(1);
}
//create and compile fragment shader
String fragShaderSource = readFileAsString("/frag.glsl");
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, fragShaderSource);
glCompileShader(fragment_shader);
//check compilation
if (glGetShaderi(fragment_shader, GL_COMPILE_STATUS) != 1) {
System.err.println(glGetShaderInfoLog(fragment_shader));
System.exit(1);
}
//create and compile tessellation shader
String tessControlShaderSource = readFileAsString("/control.tess.glsl");
tess_control_shader = glCreateShader(GL40.GL_TESS_CONTROL_SHADER);
glShaderSource(tess_control_shader, tessControlShaderSource);
glCompileShader(tess_control_shader);
//check compilation
if (glGetShaderi(tess_control_shader, GL_COMPILE_STATUS) != 1) {
System.err.println(glGetShaderInfoLog(tess_control_shader));
System.exit(1);
}
//create and compile tessellation shader
String tessEvaluationShaderSource = readFileAsString("/eval.tess.glsl");
tess_evaluation_shader = glCreateShader(GL40.GL_TESS_EVALUATION_SHADER);
glShaderSource(tess_evaluation_shader, tessEvaluationShaderSource);
glCompileShader(tess_evaluation_shader);
//check compilation
if (glGetShaderi(tess_evaluation_shader, GL_COMPILE_STATUS) != 1) {
System.err.println(glGetShaderInfoLog(tess_evaluation_shader));
System.exit(1);
}
//create program and attach it
program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
//IF THESE TWO LINES ARE COMMENTED PROGRAM WORKS...although there
//is no tessallation...
glAttachShader(program, tess_control_shader);
glAttachShader(program, tess_evaluation_shader);
glLinkProgram(program);
//check link
if (glGetProgrami(program, GL_LINK_STATUS) != 1) {
System.err.println(glGetProgramInfoLog(program));
System.exit(1);
}
glValidateProgram(program);
if (glGetProgrami(program, GL_VALIDATE_STATUS) != 1) {
System.err.println(glGetProgramInfoLog(program));
System.exit(1);
}
//delete shaders as the program has them now
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
glDeleteShader(tess_control_shader);
glDeleteShader(tess_evaluation_shader);
//return program;
}
}
vert.glsl
#version 440 core
//'offset' is an input vertex attribute
layout (location=0) in vec4 offset;
layout (location=1) in vec4 color;
out vec4 vs_color;
void main(void)
{
const vec4 vertices[3] = vec4[3]( vec4( 0.25, -0.25, 0.5, 1.0),
vec4(-0.25, -0.25, 0.5, 1.0),
vec4( 0.25, 0.25, 0.5, 1.0));
//Add 'offset' to hour hard-coded vertex position
gl_Position = vertices[gl_VertexID] + offset;
//Output a fixed value for vs_color
vs_color = color;
}
frag.glsl
#version 440 core
in vec4 vs_color;
out vec4 color;
void main(void)
{
color = vs_color;
}
control.tess.glsl
#version 440 core
layout (vertices=3) out;
void main(void)
{
//Only if I am invocation 0
if (gl_InvocationID == 0){
gl_TessLevelInner[0] = 5.0;
gl_TessLevelOuter[0] = 5.0;
gl_TessLevelOuter[1] = 5.0;
gl_TessLevelOuter[2] = 5.0;
}
//Everybody copies their input to their output?
gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;
}
eval.tess.glsl
#version 440 core
layout (triangles, equal_spacing, cw) in;
void main(void){
gl_Position = (gl_TessCoord.x * gl_in[0].gl_Position) +
(gl_TessCoord.y * gl_in[1].gl_Position) +
(gl_TessCoord.z * gl_in[2].gl_Position);
}
Finally, if it helps here is some version information, which is printed at the start of the application:
Hello LWJGL 3.1.5 build 1!
OpenGL Verion: 4.4.0 NVIDIA 340.107
glDrawArrays(GL_TRIANGLES, 0, 3);
When you draw something with tessellation, you are drawing patches, not triangles. Hence, you have to specify GL_PATCHES:
glDrawArrays(GL_PATCHES, 0, 3);
//Everybody copies their input to their output?
The reason is that the input vertices and output vertices of the tessellation control shader are not related to each other. The input vertices are taken from the input stream, i.e. your vertex buffers (after being processed by the vertex shader). Their number is specified by the GL_PATCH_VERTICES parameter. Each invocation takes this number of vertices from the buffer. The output vertices are kept internally in the pipeline. Their number is specified by the layout directive. This number can be different from the number of input vertices. They can also have different attributes. I find it more intuitive to think of these vertices as pieces of data instead of actual vertices with a geometric meaning. In some cases, this interpretation might make sense, but definitely not in all.
I am trying to render a triangle in a QGLWidget (Qt5.7) but by some reason, I am unable to show the triangle on screen, only the blue background is showed.
myapp.pro file:
QT += core gui opengl
CONFIG += c++11
TARGET = myapp
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
INCLUDEPATH += ../../libs/glew/include
SOURCES += main.cpp \
../../libs/glew/src/glew.c \
glwindow.cpp
HEADERS += \
../../libs/glew/include/GL/glew.h \
glwindow.h
This is the main function:
#include <QApplication>
#include <glwindow.h>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
glwindow v;
v.show();
return app.exec();
}
The window header:
#include <QtOpenGL/QGLWidget>
class glwindow : public QGLWidget
{
public:
glwindow();
protected:
void initializeGL() override;
void paintGL() override;
};
The cpp file:
#include <GL/glew.h>
#include <glwindow.h>
GLfloat vertices[] = {
+0.0f, +1.0f,
-1.0f, -1.0f,
+1.0f, -1.0f
};
glwindow::glwindow()
{}
void setupGeometry()
{
GLuint buffer_id;
glGenBuffers(1, &buffer_id);
glBindBuffer(GL_ARRAY_BUFFER, buffer_id);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
}
void glwindow::initializeGL()
{
glewInit();
setupGeometry();
}
void glwindow::paintGL()
{
glClearColor(0.0, 0.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
Update 1:
Added the shader code:
const GLchar *vs = "#version 150 // Specify which version of GLSL we are using."
"in vec2 in_Position;"
"void main() "
"{"
" gl_Position = vec4(in_Position.x, in_Position.y, 0.5, 1.0);"
"}";
const GLchar *fs = "#version 150 // Specify which version of GLSL we are using."
"precision highp float; // Video card drivers require this line to function properly"
"out vec4 fragColor;"
"void main()"
"{"
" fragColor = vec4(1.0,1.0,1.0,1.0); //Set colour of each fragment to WHITE"
"}";
The function that setup shader is:
void checkShader(GLuint ID)
{
GLint compile_status = 0;
glGetShaderiv(ID, GL_COMPILE_STATUS, &compile_status);
if(compile_status != GL_TRUE)
{
GLint info_length;
GLsizei buffer_size;
glGetShaderiv(ID, GL_INFO_LOG_LENGTH, &info_length);
GLchar *message = new GLchar[info_length];
glGetShaderInfoLog(ID, info_length, &buffer_size, message);
delete[] message;
}
}
void initShader()
{
GLuint program_id;
GLuint vs_id, fs_id;
vs_id = glCreateShader(GL_VERTEX_SHADER);
fs_id = glCreateShader(GL_FRAGMENT_SHADER);
const char *adapter[1];
adapter[0] = vs;
glShaderSource(vs_id, 1, adapter, 0);
adapter[1] = fs;
glShaderSource(fs_id, 1, adapter, 0);
glCompileShader(vs_id);
checkShader(vs_id);
glCompileShader(fs_id);
checkShader(fs_id);
program_id = glCreateProgram();
glAttachShader(program_id, vs_id);
glAttachShader(program_id, fs_id);
glLinkProgram(program_id);
glUseProgram(program_id);
}
So, the init function is changed to
void glview::initializeGL()
{
glewInit();
initGeometry();
initShader();
}
The shader initialization is failed with error message :
(GLchar *) 0x7efe21 \":1(10): error: GLSL 1.50 is not supported. Supported versions are: 1.10, 1.20, 1.30, 1.00 ES, and 3.00 ES\n\"
Qt 5.7 only supports to glsl 1.3.0. So if ppl want to use glsl 4.3, try glfw or SDL.
It appears if I drag-resize fast enough, the window itself is created by subclassing qwindow and making an openGLcontext on it
Code:
#include <QGuiApplication>
#include <QOpenGLContext>
#include <QWindow>
#include <QOpenGLFunctions_3_3_Core>
#include <stdio.h>
class OpenGLWindow : public QWindow, protected QOpenGLFunctions_3_3_Core{
public:
explicit OpenGLWindow();
~OpenGLWindow();
virtual void render();
bool isWindowInitialized;
void exposeEvent(QExposeEvent *event);
bool event(QEvent *event);
private:
QOpenGLContext* ctx;
QSurfaceFormat* fmt;
bool isGLInitialized;
GLuint VertexArrayID;
GLuint buffer1;
GLuint ProgramID;
};
bool OpenGLWindow::event(QEvent *event)
{
switch (event->type()) {
case QEvent::UpdateRequest:
render();
return true;
case QEvent::Close:
glDisableVertexAttribArray(0);
glDeleteBuffers(1, &buffer1);
glDeleteVertexArrays(1, &VertexArrayID);
glDeleteProgram(ProgramID);
return QWindow::event(event);
case QEvent::Resize:
if(isWindowInitialized && isGLInitialized)glViewport(0,0,width(),height());
return QWindow::event(event);
default:
return QWindow::event(event);
}
}
void OpenGLWindow::exposeEvent(QExposeEvent *event)
{
Q_UNUSED(event);
if (isExposed())render();
}
OpenGLWindow::OpenGLWindow()
:ctx(new QOpenGLContext)
,fmt(new QSurfaceFormat)
,isGLInitialized(0)
{
setSurfaceType(OpenGLSurface);
fmt->setRenderableType(QSurfaceFormat::OpenGL);
fmt->setVersion(3,3);
resize(640,480);
fmt->setProfile(QSurfaceFormat::CoreProfile);
setFormat(*fmt);
ctx->setFormat(*fmt);
ctx->create();
}
OpenGLWindow::~OpenGLWindow()
{}
void OpenGLWindow::render(){
//if(Image::isWindowInitialized || isExposed())return;
if(!isGLInitialized){
ctx->makeCurrent(this);
initializeOpenGLFunctions();
GLuint vsID = glCreateShader(GL_VERTEX_SHADER);
GLuint fsID = glCreateShader(GL_FRAGMENT_SHADER);
const char* vs="#version 330 core\n layout(location = 0) in vec3 vertexPosition_modelspace; \n void main(){ gl_Position.xyz = vertexPosition_modelspace; gl_Position.w = 1.0;}";
const char* fs="#version 330 core\n out vec4 color;\n void main(){ color = vec4(1,0,0,1);}";
glShaderSource(vsID, 1, &vs , NULL);
glCompileShader(vsID);
GLint isCompiled = 0;
glGetShaderiv(vsID, GL_COMPILE_STATUS, &isCompiled);
if(isCompiled == GL_FALSE){
GLint maxLength = 0;
glGetShaderiv(vsID, GL_INFO_LOG_LENGTH, &maxLength);
char info[512];
glGetShaderInfoLog(vsID, maxLength, &maxLength, info);
printf("%s",info);}
glShaderSource(fsID, 1, &fs , NULL);
glCompileShader(fsID);
glGetShaderiv(fsID, GL_COMPILE_STATUS, &isCompiled);
if(isCompiled == GL_FALSE){
GLint maxLength = 0;
glGetShaderiv(fsID, GL_INFO_LOG_LENGTH, &maxLength);
char info[512];
glGetShaderInfoLog(fsID, maxLength, &maxLength, info);
printf("%s",info);
}
GLuint ProgramID = glCreateProgram();
glAttachShader(ProgramID, vsID);
glAttachShader(ProgramID, fsID);
glLinkProgram(ProgramID);
glDeleteShader(vsID);
glDeleteShader(fsID);
static const GLfloat data1[] = { -1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f };
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
glGenBuffers(1, &buffer1);
glBindBuffer(GL_ARRAY_BUFFER,buffer1);
glBufferData(GL_ARRAY_BUFFER, sizeof(data1),data1, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0 ,3 ,GL_FLOAT ,GL_FALSE,0 ,(void*)0 );
glClearColor(1.0f, 0.0f, 0.4f,1.0f);
glUseProgram(ProgramID);
glViewport(0,0,width(),height());
isGLInitialized=true;
}
if(isExposed()){
glClear( GL_COLOR_BUFFER_BIT );
glDrawArrays(GL_TRIANGLES, 0, 3);
ctx->swapBuffers(this);
}
}
int main(int argc, char **argv)
{
QGuiApplication app(argc, argv);
OpenGLWindow* win = new OpenGLWindow();
win->show();
win->isWindowInitialized=true;
return app.exec();
}
I'm using Qt 5.4.2 on Qt Creator 3.4.1
Kit : Desktop Qt 5.4.2 MSVC2013 64bit
I'm kida lost here since i've never used openGL in qt and it's hard for me to picture where the problem might originate
Try setting a combination of any of the following attributes:
this->setAttribute(Qt::WA_OpaquePaintEvent, true);
this->setAttribute(Qt::WA_PaintOnScreen, true);
this->setAttribute(Qt::WA_DontCreateNativeAncestors, true);
this->setAttribute(Qt::WA_NativeWindow, true);
this->setAttribute(Qt::WA_NoSystemBackground, true);
this->setAttribute(Qt::WA_MSWindowsUseDirect3D, true);
this->setAutoFillBackground(false);
OS : mac osx 10.8.3
compiler : clang3.2
I am a beginner of opengl, trying to play opengl with Qt5
There are two problems about this simple program(plot a triangle)
I can't see the triangle
The program could not exit even I close the window
hpp
#include <QGLWidget>
#include <QtGui/QOpenGLFunctions>
#include <QtGui/QOpenGLShaderProgram>
class QWidget;
class ch1HelloTriangle : public QGLWidget, protected QOpenGLFunctions
{
Q_OBJECT
public:
explicit ch1HelloTriangle(QWidget *parent = 0);
protected:
virtual void initializeGL();
void initShaders();
void InitializeVertexBuffer();
virtual void resizeGL(int w, int h);
virtual void paintGL();
private:
QOpenGLShaderProgram program;
GLuint positionBufferObject;
};
.cpp
#include <locale.h>
#include <QWidget>
#include "ch1HelloTriangle.hpp"
namespace
{
float const vertexPositions[] = {
0.75f, 0.75f, 0.0f, 1.0f,
0.75f, -0.75f, 0.0f, 1.0f,
-0.75f, -0.75f, 0.0f, 1.0f,
};
}
ch1HelloTriangle::ch1HelloTriangle(QWidget *parent) :
QGLWidget(parent)
{
}
void ch1HelloTriangle::initializeGL()
{
initializeOpenGLFunctions();
InitializeVertexBuffer();
initShaders();
}
void ch1HelloTriangle::initShaders()
{
// Override system locale until shaders are compiled
setlocale(LC_NUMERIC, "C");
// Compile vertex shader
if (!program.addShaderFromSourceCode(QOpenGLShader::Vertex,
"attribute vec4 position;\n"
"void main()\n"
"{\n"
" gl_Position = position;\n"
"}\n"))
{
close();
}
// Compile fragment shader
if (!program.addShaderFromSourceCode(QOpenGLShader::Fragment,
"out vec4 outputColor;\n"
"void main()\n"
"{\n"
" outputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);\n"
"}\n"))
{
close();
}
// Link shader pipeline
if (!program.link())
close();
// Bind shader pipeline for use
if (!program.bind())
close();
// Restore system locale
setlocale(LC_ALL, "");
}
void ch1HelloTriangle::InitializeVertexBuffer()
{
glGenBuffers(1, &positionBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPositions), vertexPositions, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void ch1HelloTriangle::resizeGL(int w, int h)
{
// Set OpenGL viewport to cover whole widget
glViewport(0, 0, w, h);
}
void ch1HelloTriangle::paintGL()
{
/*
//codes propose by http://stackoverflow.com/questions/13111291/displaying-a-triangle-with-qt-and-opengl?rq=1, can't see the triangle either
QSize viewport_size = size();
glViewport(0, 0, viewport_size.width(), viewport_size.height());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1, 1, -1, 1, 5, 7); // near and far match your triangle Z distance
glMatrixMode(GL_MODELVIEW);*/
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
int vertexLocation = program.attributeLocation("position");
program.enableAttributeArray(vertexLocation);
glVertexAttribPointer(vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
main.cpp
#include <QApplication>
#include "ch1HelloTriangle.hpp"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ch1HelloTriangle ch1;
ch1.show();
return a.exec();
}
After a lot of trial and error, I solved the problem.
Change two things :
1 : read the shader by files
// Compile vertex shader
if (!program.addShaderFromSourceFile(QOpenGLShader::Vertex, "modernOpenGLShader/ch1/vertex")){
QMessageBox::warning(this, "QOpenGLShader::Vertex", "QOpenGLShader::Vertex" + program.log());
close();
}
// Compile fragment shader
if (!program.addShaderFromSourceFile(QOpenGLShader::Fragment, "modernOpenGLShader/ch1/frag")){
QMessageBox::warning(this, "QOpenGLShader::Fragment", "QOpenGLShader::Fragment" + program.log());
close();
}
2 : change the fragment shader, remove the output variable
void main() {
//set every drawn pixel to white
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}
From the websites, "modern opengl":http://www.arcsynthesis.org/gltut/
and "another site":tomdalling.com/blog/modern-opengl/01-getting-started-in-xcode-and-visual-cpp/
The output qualify should exist(atleast the codes of the second website will work on my pc)
But Qt will throw error message
QOpenGLShader::FragmentERROR: 0:4: Invalid qualifiers 'out' in global variable context
Do anyone know why?Thanks