Simple QtOpenGL/QtWidget program compiled on Mac using make. Nothing rendering in window.
-
Hi all,
New to Qt framework here. I am following Jamie King's Game Engine Developement series (https://www.youtube.com/playlist?list=PLRwVmtr-pp04XomGtm-abzb-2M1xszjFx). However I am using a mac and trying to avoid IDEs etc for no other reason than I want to learn a command line workflow.
I am compiling using GNUMake and g++4.9. I am pulling the relevant dependencies as and when they're required.
I have gotten to the point where I have an OpenGL window and am trying to render a simple triangle in it. Everything compiles without error however when I run the program I am presented with a black screen.
If I put assert(0) in the paintGL function I get a run-time assertion failure so the function is being called. But the lack of errors otherwise is making this very difficult to debug.
Please can you take a look at my code and let me know what I may be missing?
Kind regards,
djr
Project folder:
@MyProject
- sandbox |- Makefile |- debug (output folder) |- src | |- sandbox.cpp | |- MyGLWindow.cpp |
- MyGLWindow.h
- middleware |- Frameworks (contains Qt frameworks and OpenGL.framework
- glew (bin/include/lib)@Makefile
@CFLAGS += -Wall -g
CXX=g++-4.9
LDFLAGS += -F../middleware/Frameworks -framework QtCore -framework QtGui -framework QtWidgets -framework QtOpenGL -framework OpenGL -framework Cocoa
LDFLAGS += -I../middleware/glew/include -L../middleware/glew/lib -lglewdebug/sandboxmain: debug/MyGLWindow.o debug/sandbox.o
$(CXX) $(CFLAGS) $(LDFLAGS) debug/MyGLWindow.o debug/sandbox.o -o debug/sandboxmaindebug/sandbox.o: ./src/sandbox.cpp
$(CXX) $(CFLAGS) $(LDFLAGS) -c src/sandbox.cpp -o debug/sandbox.odebug/MyGLWindow.o: ./src/MyGLWindow.h ./src/MyGLWindow.cpp
$(CXX) $(CFLAGS) $(LDFLAGS) -c src/MyGLWindow.cpp -o ./debug/MyGLWindow.oclean:
rm -f ./debug/sandbox.o
rm -f ./debug/MyGLWindow.o
rm -f ./debug/sandboxmain@src/sandbox.cpp
@#include <QtWidgets/qapplication.h>
#include <QtWidgets/qwidget.h>
#include "MyGLWindow.h"int main(int argc, char** argv)
{
QApplication application(argc, argv);
MyGLWindow myGLWindow;
myGLWindow.show();
myGLWindow.update();
return application.exec();
}@src/MyGLWindow.cpp
@#include <GL/glew.h>
#include "MyGLWindow.h"
#include <cassert>float verts[] =
{
+0.0f, +0.1f,
-0.1f, -0.1f,
+0.1f, -0.1f
};void MyGLWindow::initializeGL()
{
GLenum errorCode = glewInit();
assert(errorCode == 0);glGenBuffers(1, &vertexBufferID); glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID); glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
}
void MyGLWindow::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
}@src/MyGLWindow.h
@#ifndef SANDBOX_MY_GL_WINDOW_H
#define SANDBOX_MY_GL_WINDOW_H
#include <QtOpenGL/QGLWidget>class MyGLWindow : public QGLWidget
{
GLuint vertexBufferID;protected:
void initializeGL();
void paintGL();
};#endif@
-
I know there has been a lot of changes to OpenGL in Qt since 5.0. If using 5.4.0 or newer I believe there is a new QOpenGLWidget (not QGLWidget).
I would simplify your test a bit just to get a working window to start with. This is a copy-paste from another program. You should see a white screen with a black line in this example.
@
void MyGLWindow::initializeGL()
{
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_FALSE);glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CCW);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glShadeModel(GL_SMOOTH);glClearColor(1.0f,1.0f,1.0f,1.0f);
}void MyGLWindow::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLineWidth(1);
glEnable(GL_COLOR_MATERIAL);
glColor3d(0.0,0.0,0.0);glBegin(GL_LINES);
glVertex3d(1.0,1.0,1.0);
glVertex3d(-1.0,-1.0,-1.0);
glEnd();}
@ -
Hi Rondog,
Thank you for your response. The code you provided runs works and displays a diagonal line as expected. At least it's something on the screen!
However, I thought that the glBegin ... glEnd stuff was deprecated in modern OpenGL in favour of the implementation I presented?
Going back to the original code, I included the QOpenGLWidget header rather than QGLWidget and I was unfortunately presented with the same black screen. I also rolled back to Qt 4.8 which I believe is the same version used in the tutorial videos which also didn't work.
Do you have any other suggestions?
In the mean time, I may go back and update my old GLFW code to use modern OpenGL and see if that renders correctly.
Many thanks,
djr
-
You're absolutely right. glBegin()/glEnd() is old school.
I have been reading up on the new methods myself; using shaders and so on. I don't have a sample of anything using the newer methods but I know they are out there. I haven't sunk too much time into this yet.
From what I have read it is probably better to use the QOpenGLWidget over the older QGLWidget. There is also an QOpenGLWindow class. In your example you are probably missing a few important initial steps specific to how you are using it.
"QOpenGLWidget":http://doc-snapshot.qt-project.org/qt5-5.4/qopenglwidget.html