0

I do not understand how this main function works. I have a display function, which uses glDrawArrays, but I do not see it being called. I only see it being used as a parameter for glutDisplayFunction.

Here is my main:

int main(int argc, char** argv){

    // Set up the window
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
    glutInitWindowSize(800, 600);
    glutCreateWindow("Hello Triangle");
    // Tell glut where the display function is
    glutDisplayFunc(display);

     // A call to glewInit() must be done after glut is initialized!
    GLenum res = glewInit();
    // Check for any errors
    if (res != GLEW_OK) {
      fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
      return 1;
    }
    // Set up your objects and shaders
    init();
    // Begin infinite event loop
    glutMainLoop();
    return 0;
}

The problem is, I need to create two different triangles, on the same window, using seperate VAOs and VBOs. I've created the seperate VAO and VBO for my second triangle. However, I do not see how I am meant to generate and link my buffers, draw my arrays, switch to my second buffer, and draw those arrays, when I do not even know when my display function is being called.

My display function looks like this:

void display(){

glClear(GL_COLOR_BUFFER_BIT);
// NB: Make the call to draw the geometry in the currently activated vertex buffer. This is where the GPU starts to work!
glDrawArrays(GL_TRIANGLES, 0, 3);
glutSwapBuffers();
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
iaskdumbstuff
  • 409
  • 3
  • 16
  • 1
    Do you not see the comments in the code you posted? What do you find confusing about them? – Nicol Bolas Sep 30 '19 at 00:42
  • @NicolBolas I see the comments, but I don't know where it is that the display function is called. How am I meant to generate a buffer, draw, generate my second buffer, and draw again, if I don't know at what point my arrays are being drawn? Presumably, they're being drawn after init is called, but where? I've looked at the glutMainLoop() specification, and nothing on it suggested that it would call display. And even if it did, glutMainLoop() is not meant to be called more than once anyway, so how am I meant to create two objects using two VBOs in this case? – iaskdumbstuff Sep 30 '19 at 00:46
  • You put it where they tell you to put it. You're not *supposed* to sweat the details; you're learning OpenGL, not "how does GLUT work". – Nicol Bolas Sep 30 '19 at 00:49
  • @NicolBolas They don't tell me to put it anywhere. They've given me a code that, at one point, created a red triangle. In this exercise, they're asking me to change the code to create two triangles using 2 VBOs and VAOs for their data; they're not giving me any other information. – iaskdumbstuff Sep 30 '19 at 00:53

1 Answers1

1

All operations could be done in separate function named asyouwant called from main example:

#include <GL/glut.h>

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE);
    glutInitWindowSize(300, 300);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Hello world :D");
    glutDisplayFunc(displayMe);         // = > draw in displayme function
    glutMainLoop();
    return 0;
}


void displayMe(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_POLYGON);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(0.5, 0.0, 0.0);
        glVertex3f(0.5, 0.5, 0.0);
        glVertex3f(0.0, 0.5, 0.0);
    glEnd();
// a second geoform
    glBegin(GL_POLYGON);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(-0.5, 0.0, 0.0);
        glVertex3f(-0.5, -0.5, 0.0);
        glVertex3f(0.0, -0.5, 0.0);
    glEnd();

    glFlush();
}

As complement: for VAO and buffer 1- Init (declare VAO , declare buffer of vertices, ...)

GLuint VaoID;
glGenVertexArrays(1, &VaoID);
glBindVertexArray(VaoID);

// An array of 3 vectors which represents 3 vertices

static const GLfloat g_vertex_buffer_data[] = {
   -1.0f, -1.0f, 0.0f,
   1.0f, -1.0f, 0.0f,
   0.0f,  1.0f, 0.0f,
};

Once time only

// This will identify our vertex buffer
GLuint vertexbuffer;
// Generate 1 buffer, put the resulting identifier in vertexbuffer
glGenBuffers(1, &vertexbuffer);
// The following commands will talk about our 'vertexbuffer' buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
// Give our vertices to OpenGL.
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

2- Use it (bind and draw in display fucntion)

// 1st attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
   0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
   3,                  // size
   GL_FLOAT,           // type
   GL_FALSE,           // normalized?
   0,                  // stride
   (void*)0            // array buffer offset
);
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
glDisableVertexAttribArray(0);
Chronoslog
  • 107
  • 8
  • Thanks for the answer. Hm, this is interesting. However, this is a solution in which I would not be using buffers? Sorry if that's a silly question, I'm still new to OpenGL. – iaskdumbstuff Sep 30 '19 at 01:26
  • Well replace the code after glClear ... by a draw of your 2 triangles with buffers. – Chronoslog Sep 30 '19 at 01:39