The first example of OpenGL
-
Hi all,
I'm sorry, I don't know whether it's an appropriate question for this forum or not.
I'm reading the book OpenGL.Programming.Guide.9th.Edition. It declares its first example this way:#include <iostream> using namespace std; #include "vgl.h" #include "LoadShaders.h" enum VAO_IDs { Triangles, NumVAOs }; enum Buffer_IDs { ArrayBuffer, NumBuffers }; enum Attrib_IDs { vPosition = 0 }; GLuint VAOs[NumVAOs]; GLuint Buffers[NumBuffers]; const GLuint NumVertices = 6; //-------------------------------------------------------------------- // // init // void init(void) { static const GLfloat vertices[NumVertices][2] = { { -0.90, -0.90 }, // Triangle 1 { 0.85, -0.90 }, { -0.90, 0.85 }, { 0.90, -0.85 }, // Triangle 2 { 0.90, 0.90 }, { -0.85, 0.90 } }; glCreateBuffers(NumBuffers, Buffers); glNamedBufferStorage(Buffers[ArrayBuffer], sizeof(vertices), vertices, 0); ShaderInfo shaders[] = { { GL_VERTEX_SHADER, "triangles.vert" }, { GL_FRAGMENT_SHADER, "triangles.frag" }, { GL_NONE, NULL } }; GLuint program = LoadShaders(shaders); glUseProgram(program); glGenVertexArrays(NumVAOs, VAOs); glBindVertexArray(VAOs[Triangles]); glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]); glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); glEnableVertexAttribArray(vPosition); } //-------------------------------------------------------------------- // // display // void display(void) { static const float black[] = { 0.0f, 0.0f, 0.0f, 0.0f }; glClearBufferfv(GL_COLOR, 0, black); glBindVertexArray(VAOs[Triangles]); glDrawArrays(GL_TRIANGLES, 0, NumVertices); } //-------------------------------------------------------------------- // // main // int main(int argc, char** argv) { glfwInit(); GLFWwindow* window = glfwCreateWindow(640, 480, "Triangles", NULL, NULL); glfwMakeContextCurrent(window); gl3wInit(); init(); while (!glfwWindowShouldClose(window)) { display(); glfwSwapBuffers(window); glfwPollEvents(); } glfwDestroyWindow(window); glfwTerminate(); }
I downloaded a zip file from here and also glfw-3.2.1 from here.
I've been directed to this github but I can't use the info there to make the code above run.
It seems that there is an IDE named cmake that is used for OpenGL programming.
I would be glad if I could use VS 2017 for that as I use it for C++. -
@tomy You can use any IDE you want for opengl. Cmake is a make system maker, if that makes sense, lol.
Think of cmake like qmake. It is used to parse a project file and create a build system out of it. Cmake fully supports Qt and it is what I use in all my qt applications. It is super powerful ... and awesome!
It supports many generators on many platforms. So with cmake a single project file can then be used to make a build for Visual Studio (many different versions), nmake (also vs), mingw, ninja, gnu make, etc. There are tons of generators, you can use
cmake --help
to see a list.So if you wanted to use that project you referenced in visual studio you would do this (on the command line is my preferred but cmake has a windows gui too):
$ cd /path/to/project $ mkdir build $ cd build $ cmake -G "Visual Studio 15 2017" ..
Now you should have a sln file that you can open with visual studio. Or you can build on the command line.
To build on the command line you can do:
$ cmake --build --config Release
Just to show some cmake generators, here is the list on one of my windows development boxes:
Generators The following generators are available on this platform: Visual Studio 15 2017 [arch] = Generates Visual Studio 2017 project files. Optional [arch] can be "Win64" or "ARM". Visual Studio 14 2015 [arch] = Generates Visual Studio 2015 project files. Optional [arch] can be "Win64" or "ARM". Visual Studio 12 2013 [arch] = Generates Visual Studio 2013 project files. Optional [arch] can be "Win64" or "ARM". Visual Studio 11 2012 [arch] = Generates Visual Studio 2012 project files. Optional [arch] can be "Win64" or "ARM". Visual Studio 10 2010 [arch] = Generates Visual Studio 2010 project files. Optional [arch] can be "Win64" or "IA64". Visual Studio 9 2008 [arch] = Generates Visual Studio 2008 project files. Optional [arch] can be "Win64" or "IA64". Visual Studio 8 2005 [arch] = Generates Visual Studio 2005 project files. Optional [arch] can be "Win64". Visual Studio 7 .NET 2003 = Deprecated. Generates Visual Studio .NET 2003 project files. Borland Makefiles = Generates Borland makefiles. NMake Makefiles = Generates NMake makefiles. NMake Makefiles JOM = Generates JOM makefiles. Green Hills MULTI = Generates Green Hills MULTI files (experimental, work-in-progress). MSYS Makefiles = Generates MSYS makefiles. MinGW Makefiles = Generates a make file for use with mingw32-make. Unix Makefiles = Generates standard UNIX makefiles. Ninja = Generates build.ninja files. Watcom WMake = Generates Watcom WMake makefiles. CodeBlocks - MinGW Makefiles = Generates CodeBlocks project files. CodeBlocks - NMake Makefiles = Generates CodeBlocks project files. CodeBlocks - NMake Makefiles JOM = Generates CodeBlocks project files. CodeBlocks - Ninja = Generates CodeBlocks project files. CodeBlocks - Unix Makefiles = Generates CodeBlocks project files. CodeLite - MinGW Makefiles = Generates CodeLite project files. CodeLite - NMake Makefiles = Generates CodeLite project files. CodeLite - Ninja = Generates CodeLite project files. CodeLite - Unix Makefiles = Generates CodeLite project files. Sublime Text 2 - MinGW Makefiles = Generates Sublime Text 2 project files. Sublime Text 2 - NMake Makefiles = Generates Sublime Text 2 project files. Sublime Text 2 - Ninja = Generates Sublime Text 2 project files. Sublime Text 2 - Unix Makefiles = Generates Sublime Text 2 project files. Kate - MinGW Makefiles = Generates Kate project files. Kate - NMake Makefiles = Generates Kate project files. Kate - Ninja = Generates Kate project files. Kate - Unix Makefiles = Generates Kate project files. Eclipse CDT4 - NMake Makefiles = Generates Eclipse CDT 4.0 project files. Eclipse CDT4 - MinGW Makefiles = Generates Eclipse CDT 4.0 project files. Eclipse CDT4 - Ninja = Generates Eclipse CDT 4.0 project files. Eclipse CDT4 - Unix Makefiles= Generates Eclipse CDT 4.0 project files.
-
Thanks. Great info.
If Cmake is that awesome so I too would like to use it.
What to do with that?
My OS is a Windows x64.
I think I should download that cmake and install it on my OS and create a project, paste that code there and hit the "run" button. :)Incidentally, I've started learning Ubuntu and will ask for whatever needed there for C++/QML/Qt/OpenGL programming.
-
@tomy You just grab cmake and install it, then it works just like qmake does for Qt.
It's not an ide or anything.
So for example, here is a qmake pro file:
TEMPLATE = app TARGET = tbutton INCLUDEPATH += . QT += widgets gui RESOURCES = tbutton.qrc # Input SOURCES += main.cpp
And here is the equivalent CMakeLists.txt file that would be used to build that project with cmake:
cmake_minimum_required(VERSION 3.4) project(tbutton) find_package(Qt5Gui required) set(SRCS main.cpp ) qt5_add_resources(RES_SRC tbutton.qrc) add_executable(${PROJECT_NAME} ${SRCS} ${RES_SRC}) qt5_use_modules(${PROJECT_NAME} Core Gui Widgets)
Hope that helps understand cmake a bit better. It's basically just qmake on steroids. Can be used truly cross platform with many different build systems. Qmake for instance only uses gnu make files, or visual studio. Cmake can use ninja, gnu make, vs, mingw, msys, etc. It has many more.
-
@ambershark
Well, thank you very much. I got some info by your post but it's still too complex for a new user of Cmake to make the code above run for the first time.Now I've installed Cmake (cmake-3.9.3-win64-x64.msi) on my Windows 7 x64 machine and also have the source code above. If I want for the time being use Windows to run that code on, what steps should I go through, please?
-
So if you wanted to use that project you referenced in visual studio you would do this (on the command line is my preferred but cmake has a windows gui too):
$ cd /path/to/project
$ mkdir build
$ cd build
$ cmake -G "Visual Studio 15 2017" ..Now you should have a sln file that you can open with visual studio.
I used Windows command prompt (cmd on start menu) and typed:
cd C:\Users\Abbasi\Desktop\OGLPG-9th-Edition\OGLPG-9th-Edition\buildcmake -G "Visual Studio 15 2017"
The output: 'cmake' is not recognized as an internal or external
Presumably some other command prompt was meant.Therefore, I went for the gui option:
It made the files onto the build folder and now have two .sln files, one in the *OGLPG-9th-Edition\OGLPG-9th-Edition\build folder named vermilion9.sln and the other in the OGLPG-9th-Edition\OGLPG-9th-Edition\build\lib\glfw folder named GLFW.sln.Which one should I open?
-
@tomy I'd open the vermillion one. That seems to be the overall project whereas the other one was just for a library.
As for the command not recognized, that's just because cmake isn't in your path for the cmd. You need to add your cmake binary directory to your PATH environment variable, or you need to call it via the whole path like:
> "c:\program files\cmake\bin\cmake.exe" -G "Visual Studio 15 2017" /path/to/your/source
Made assumption on folder, get the exact one for your install. :)
-
/path/to/your/source
Does it mean that I should save that source code in a C++ file, or any other file, and give its path to the cmd as above?
By the way, the method used here is much longer than the one using these cmd commands.
-
@tomy No.. so here's a quick little project I wrote for someone on these forums today, and I use cmake as the build system. Here is the directory structure:
rowcolor/ CMakeLists.txt src/ main.cpp window.cpp
Now given the above structure I could do this to build:
$ cd rowcolor $ mkdir build $ cd build $ cmake -G "Visual Studio 15 2017" .. $ cmake --build
That
..
in there is the/path/to/source
I mentioned. In this case it's one directory up from build which is indicated by 2 periods.Hope that helps makes sense.
As for that link you sent, the cmake they are using is with the cmake GUI. Which is fine, some people prefer that. It is still very simple you see the specify the generator and the path to the source. The variables on the right hand side of the screen shot are things that can be customized. You could do that on the command line as well. It's still very simple though.
Edit: here's a link to that thread if you want to see the cmakelists.txt file https://forum.qt.io/topic/83800/set-background-of-specific-row-in-qlistview/8
-
@ambershark
I looked at that post of you on that thread. The .cpp and .h files seem to be of Qt. My code, in the first post of this thread, is of OpenGL.Do you mean that I should save that code in a Win32 Console Application (by visual studio for example) and put it and that CMakeLists.txt in the directories above and then use the commands?
By the way, even using that Cmake GUI link I haven't been able to run my code yet! :( Take a look at here please.
-
@tomy No you don't need cmake for what you're doing. I mean you can use it, but it's not required or anything. The opengl stuff should work in Visual Studio.
You're in an area I'm not good with though. I'm not really a windows programmer so Visual Studio isn't something I use very often. And I've never done OpenGL stuff outside of using Qt's implementations for it.
I wish I could help you more but I just don't know that stuff.
From the sounds of that other thread, you do not have your include directories set properly though. If it can't find <glad/glad.h> you may have the directory set to the include that contains glad.h rather than the one above it. Just a guess. :)