The first example I see in a book

Hello to all,

As a C++ programmer and learner of Qt/QML, I went for learning OpenGL through a famous book “OpenGL.Programming.Guide.9th.Edition”.
In page 40, it offers a first code named Example1.1 with this code:

#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 sought the book in normal way by which to make it run and enjoy the first example. But I haven’t found how to do this yet.
That is the book doesn’t say “in what IDE”, using “what kind of project” and “by doing what process” to write that code and run it!!

I will be glad if I hear your opinions.
Thanks.

[QUOTE=Tomyfr;1288708]“OpenGL.Programming.Guide.9th.Edition”. … Example1.1

make it run and enjoy the first example. But I haven’t found how to do this yet.
That is the book doesn’t say “in what IDE”, using “what kind of project” and “by doing what process” to write that code and run it!![/QUOTE]

Inside the book, on page “xlii” in the section How to Obtain the Sample Code, it lists a URL to the examples:

[ul]
[li]The OpenGL® Programming Guide 9th Edition, which has a URL to: [/li][li]OpenGL Programming Guide Examples (GitHub) and [/li][li]OpenGL Programming Guide Examples (ZIP File) [/li][/ul]
It also contains a pointer to where you can get GLFW, which is a dependency for the examples.

If you go to the GitHub examples URL (see above), it gives you details on how to Build and Run the example code.

The README.md file at the top of the project (in the GitHub project or in the ZIP) also provides info.

Thanks.
I downloaded both glfw-3.2.1 and OGLPG-9th-Edition folders, and also looked at github. I’ve never used github.
It’s apparently using Cmake (if it’s an IDE).

I read here. It says:
To build the samples, enter the “build” subdirectory and enter “cmake -G “{your generator here}” …”

In the OGLPG-9th-Edition folder there is a subdirectory named build but no “cmake -G” there (!), also in the github: examples/build/templates at master · openglredbook/examples · GitHub, no such a file!

I use VS 2017 for C++ programming. I wish I could use it for OpenGL too (if it’s well for that too).

CMake is a cross-platform tool that builds what you need to build source trees. On MS Windows, it produces MSVS solution/project files, and on Linux it produces Makefiles. You can download a binary installer for it here:

[ul]
[li]CMake [/li][/ul]
Just click on the Download link.

I read here. It says:
To build the samples, enter the “build” subdirectory and enter “cmake -G “{your generator here}” …”

Right. Do this at the top-level of the example directory tree:


mkdir build
cd build
cmake ..

Now you should have MSVS solution/project files for your version of MSVS. Load them up into VS and build as usual.

cmake should automatically sense which version(s) of MSVS you have installed and just build the right solution/project files for it. However, if you want to override which MSVS version it targets by default, that’s when you’d specify the -G option.

I use VS 2017 for C++ programming. I wish I could use it for OpenGL too (if it’s well for that too).

That’ll probably be fine. The only possible rub I see is that the examples don’t say they’ve been tested with MSVS 2017. They say they have been tested with MSVS 2013 with support for 2015 coming.

I’m currently making my way slowly through that very same book, and had the same startup problem as you had.

I devoted a youtube channel to helping individuals start with opengl, and I’m making new videos all the time, simply because I enjoy it.

https://www.youtube.com/channel/UCzx8alrxVELz5h1dfCdkdfg?view_as=subscriber

… I switched over midway from VS2015 to VS2017 because it became available, and I haven’t noticed any problems other than the compiler re-doing the solution before compiling the first time.

Hope this helps,

Jeff

@Dark Photon

Just click on the Download link.

I downloaded both cmake-3.9.3-win64-x64.msi and cmake-3.9.3-win64-x64.zip for my win 7 x64. I think the first is installable and the latter is a standalone Cmake. So I installed the former using its default wizard options.
Now I have the CMake (cmake-gui) app installed on my machine.

Do this at the top-level of the example directory tree:

mkdir build
cd build
cmake …

Where is there please?

If I do these steps I will follow the rest to finally have the issue solved.
Thank you for your guidance.

I devoted a youtube channel to helping individuals start with opengl, and I’m making new videos all the time, simply because I enjoy it.

https://www.youtube.com/channel/UCzx…_as=subscriber

Thank you. Where is your first video spotting the issue I have now. If the videos are helpful, and I hope so, I’ll subscribe.

What do you mean?

You run these commands in a DOS shell command window (or some other shell command window, such as Cygwin bash shell window).

You have two options: you can do this with cmake and the command-line, or with cmake-gui:

  1. [b]cmake

[/b]Do this:

  • Download the “OGLPG-9th-Edition.zip” examples ZIP file, and unzip it somewhere.
    Open a shell command window and cd into the directory where you unzipped it.
    Run these commands:

cd OGLPG-9th-Edition\OGLPG-9th-Edition\build
cmake ..

As it turns out, there’s already a build subdirectory in the examples directory tree, so you don’t need to create it yourself.

2)[b] cmake-gui

[/b]You can probably do this same thing without a shell command window or running the above commands from “cmake-gui”. Just:

  • Download the “OGLPG-9th-Edition.zip” examples ZIP file, and unzip it somewhere.
    Run cmake-gui.
    Key in these paths:

Where is the source code: <<SOME_PATH>>\OGLPG-9th-Edition\OGLPG-9th-Edition
Where to build the binaries: <<SOME_PATH>>\OGLPG-9th-Edition\OGLPG-9th-Edition\build

Just replace <<SOME_PATH>> with the appropriate path on your system where you unzipped the examples ZIP file.

Open a shell command window and cd into the directory where you unzipped it.

I don’t know what the shell command window is or whether I have it by installing Cmake or not, but I showed Windows command prompt (cmd on start menu) up and typed:
cd C:\Users\Abbasi\Desktop\OGLPG-9th-Edition\OGLPG-9th-Edition\build
cmake

The output: ‘cmake’ is not recognized as an internal or external
Presumably some other command prompt was meant.

Therefore, I went for the latter option:
The first address: C:/Users/Abbasi/Desktop/OGLPG-9th-Edition/OGLPG-9th-Edition
the second address: C:/Users/Abbasi/Desktop/OGLPG-9th-Edition/OGLPG-9th-Edition/build
Should I have used backslashes instead of slashes there?

I chose MSVS 2017 x64 and then pressed Configure and Generate buttons.

The output there:

Configuring done.
Generating done.

Now many files are put to the build subdirectory.

Until now Okey?

So what’s next? I still have the source code and looking for the way to run it.

Actually, yes, what helped me get started was this page also:

https://learnopengl.com/

He shows how to use CMake to get the third party libraries going, right from the start.

Regards,

Jeff

Thank you.
I’m at the Compilation stage. I opened the GLFW.sln file by VS 2017 but no glfw3.lib file was created!
What could the problem be please?

I did these:

1- Added glfw3.lib to this address:
C:\Users\Abbasi\Documents\OpenGL_Projects\Libs

and glad.h and other header files of the include folder to:
C:\Users\Abbasi\Documents\OpenGL_Projects\Includes

2- Then, created a project on VS 2017:
File > New Project > Win32 console application (name testgl1) > Next > Empty project finished.
In the solution explorer: Add New Item > C++ source file (name testgl1) > Add

3- Went to testgl1’s properties:
VC++ Directories: Include directories => added: “C:\Users\Abbasi\Documents\OpenGL_Projects\Includes” > OK
Linker > Input > Additional Dependencies => added: “C:\Users\Abbasi\Documents\OpenGL_Projects\Libs” > OK

4- Now in the project when I write: #include <glad/glad.h>, it doesn’t recognized it!
And also when I type this code:

#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 get this result pointing to many unknown functions! :frowning:

What is/are the problem(s) please?
How did you run your OpenGL project using Cmake and Visual Studio for the first time?

It seemed that .h and .lib files hadn’t been put in the right folder. So I put the contents of both Include and Lib folders of:
C:\Users\Abbasi\Documents\OpenGL_Projects to the include and lib folders of this path:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.10.25017

Then in a Win32 Console Application on Visual Studio 2017 I typed:
<glad/glad.h>

Still not identified by the Visual Studio!!

I follow following strategy might help you.

  1. Where ever you .sln soultion file is in same directory create folders with name “include” and “lib”.

  2. All the .h file needed by your project put them in this include folder.

  3. All the .lib files needed by your project put them in .lib folder

  4. Open you solution file

  5. Go to projects -> settings -> c/c++ -> additional header
    In this set following path
    "$(SolutionDir)\include

  6. Click on apply

  7. In same Project->settings ->Linker -> General -> Additional Library Directories
    Put here “$(SolutionDir)\lib”
    8.Click apply

  8. Now go to Projects -> Settings->Linker -> input
    Here in right hand side first row you will see all .lib file names.
    Add all libs needed by your project here.
    example glut.lib;glew.lib

    Note : Please be careful while adding libs here. If you are in debuging mode please add debug versions of .libs.
    Also if you are developing applications on x64 i.e 64 bit platform please add 64 bit versions of libs.
    10.Again click apply -> then click OK

  9. After this your project should find includes and libs and it will compile and link and create exe in debug folder if you are in debug mode.

  10. Please before running make sure all dll files of the libs that you are using are present in this debug folder or folder where your final exe is generated.

Note : Please remember If what ever settings I have given above are done by you while you were working in debug mode and later on you change to release mode you have to do this setting again but only one time. Similar for changing platform from win32 to x64. Setting across this modes are not persistent.

Hope this will help.

Regards
PixelClear