Results 1 to 3 of 3

Thread: A first example in OpenGL

  1. #1
    Junior Member
    Join Date
    Sep 2017
    Posts
    8

    A first example in OpenGL

    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
    Junior Member
    Join Date
    Jun 2010
    Posts
    19
    Quote Originally Posted by tomfrank View Post
    I went for learning OpenGL through a famous book "OpenGL.Programming.Guide.9th.Edition". ... Example1.1
    More discussion on this cross-posted thread here:
    * The first example I see in a book (OpenGL Beginners Forum)

    Are you targeting OpenGL or OpenGL ES?

  3. #3
    Junior Member
    Join Date
    Sep 2017
    Posts
    8
    I'm using OpenGL. There is no end reply there either!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean