Simple QtOpenGL/QtWidget program compiled on Mac using make. Nothing rendering in window.
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?
- sandbox |- Makefile |- debug (output folder) |- src | |- sandbox.cpp | |- MyGLWindow.cpp |- MyGLWindow.h
- middleware |- Frameworks (contains Qt frameworks and OpenGL.framework- glew (bin/include/lib)@
@CFLAGS += -Wall -g
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 -lglew
debug/sandboxmain: debug/MyGLWindow.o debug/sandbox.o
$(CXX) $(CFLAGS) $(LDFLAGS) debug/MyGLWindow.o debug/sandbox.o -o debug/sandboxmain
$(CXX) $(CFLAGS) $(LDFLAGS) -c src/sandbox.cpp -o debug/sandbox.o
debug/MyGLWindow.o: ./src/MyGLWindow.h ./src/MyGLWindow.cpp
$(CXX) $(CFLAGS) $(LDFLAGS) -c src/MyGLWindow.cpp -o ./debug/MyGLWindow.o
rm -f ./debug/sandbox.o
rm -f ./debug/MyGLWindow.o
rm -f ./debug/sandboxmain@
int main(int argc, char** argv)
QApplication application(argc, argv);
float verts =
GLenum errorCode = glewInit();
assert(errorCode == 0);
glGenBuffers(1, &vertexBufferID); glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID); glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
class MyGLWindow : public QGLWidget
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.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
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.
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.