Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: The first example I see in a book

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2017
    Posts
    29

    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:

    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.

  2. #2
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,169
    Quote Originally Posted by Tomyfr View Post
    "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!!
    Inside the book, on page "xlii" in the section How to Obtain the Sample Code, it lists a URL to the examples:


    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.
    Last edited by Dark Photon; 09-30-2017 at 06:54 PM.

  3. #3
    Junior Member Newbie
    Join Date
    Sep 2017
    Posts
    29
    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: https://github.com/openglredbook/exa...uild/templates, 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).
    Last edited by Tomyfr; 10-01-2017 at 01:07 PM.

  4. #4
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,169
    Quote Originally Posted by Tomyfr View Post
    It's apparently using Cmake (if it's an IDE).
    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:


    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:

    Code :
    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.

  5. #5
    Intern Contributor
    Join Date
    Dec 2016
    Location
    Ocean Shores, WA USA
    Posts
    72

    Cool

    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/UCzx..._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

  6. #6
    Junior Member Newbie
    Join Date
    Sep 2017
    Posts
    29
    @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:

    Code :
    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.

  7. #7
    Junior Member Newbie
    Join Date
    Sep 2017
    Posts
    29
    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.

  8. #8
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,169
    Quote Originally Posted by Tomyfr View Post
    Where is there please?
    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) cmake

    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:

    Code :
    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) cmake-gui

    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:

    Code :
    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.
    Last edited by Dark Photon; 10-02-2017 at 07:04 AM.

  9. #9
    Junior Member Newbie
    Join Date
    Sep 2017
    Posts
    29
    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.

  10. #10
    Intern Contributor
    Join Date
    Dec 2016
    Location
    Ocean Shores, WA USA
    Posts
    72
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •