caching mechanism in OpenGL????

So basically i want to know is there any caching mechanism?like i need to…

  1. Can i first draw a line and then later specify its color?
  2. Can i first draw a shape(using GL_LINE_LOOP) and in next step fill it with a color? (Not to use GL_POLYGON which specify the shape to be filled initially )
  3. Saving the current coordinate ( pen coordinate )
    Basically need to fill the previous shape i made.
    If OpenGL dont have such mechanism what can i do in c++?

The answer to all of your questions is “no”.

You are expected to make all of those decisions before you send data to OpenGL. OpenGL is a low-level API, so your code is expected to conform to its requirements, not have OpenGL to conform to yours.

If you want to be able to program that way, then you will need to build an abstraction on top of OpenGL that doesn’t make OpenGL calls until some point later, after you have provided all of the information needed for doing the rendering.


From somewhere i get this code , First drawing a rectangle and later changing its color(i dont know how its doing)
Can someone please explain it???


#define GL_GLEXT_PROTOTYPES
#include <GL/glut.h>
#include <string.h>

int nbVertices = 4;
void* colorOffset = (void*) (sizeof( float ) * 12);
float initialVBO[24] = {0,0,0,  0.5,0,0,  0,0.5,0,  0.5,0.5,0,    1,1,1,  1,1,1,  1,1,1,  1,1,1};
GLuint vboID;

void displayFunc(void) {
    glClear(GL_COLOR_BUFFER_BIT);

    glBindBuffer(GL_ARRAY_BUFFER, vboID);


    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);

    glVertexPointer(3, GL_FLOAT, 0, NULL);
    glColorPointer(3, GL_FLOAT, 0, colorOffset);

    glDrawArrays(GL_TRIANGLE_STRIP, 0, nbVertices);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glFlush();
}

void mouseFunc(int button, int state, int x, int y) {
    float color[12] = {1,0,0,  1,0,0,  1,0,0,  1,0,0};
    glBindBuffer(GL_ARRAY_BUFFER, vboID);

    void* vboAddress = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
    if(!vboAddress) {
        return;
    }
    memcpy((char*)vboAddress + sizeof( float ) * 12, color, sizeof( float ) * 12);

    glUnmapBuffer(GL_ARRAY_BUFFER);
    vboAddress = NULL;

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    displayFunc();
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE);
    glutInitWindowSize(300, 300);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("Rectangle fill");


    glGenBuffers(1, &vboID);
    glBindBuffer(GL_ARRAY_BUFFER, vboID);

    glBufferData(GL_ARRAY_BUFFER, sizeof(initialVBO), 0, GL_STATIC_DRAW);
    glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(initialVBO), initialVBO);

    glBindBuffer(GL_ARRAY_BUFFER, 0);


    glutDisplayFunc(displayFunc);
    glutMouseFunc(mouseFunc);
    glutMainLoop();

    glDeleteBuffers(1, &vboID);
    return 0;
}

[https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glBufferSubData.xhtml

glBufferSubData, glNamedBufferSubData — updates a subset of a buffer object’s data store

First drawing a rectangle and later changing its color

No, it first draws a rectangle. Then, when you press the mouse button, it draws a new rectangle using different values from the previous one. It is not changing the color of what it drew; it is merely drawing a new frame with a rectangle that has a different color.

What basically I need is… From a different program(some functions) use opengl main function and draw.
For example
/______

MakeRectangle(x1,y1,x2,y2);
Selectcolor(r,g,b);
Stroke(); // here it draws just a wire rectangle of selected colour boundary

/______

MakeRectangle(x1,y1,x2,y2);
Selectcolor(r,g,b);
Fill()
Stroke(); // here it draws a filled rectangle of selected colour

/______

Moveto(x,y)
Makeline(x1,y1);
Makeline(x2,y2);
Closeregion();
Selectcolor(r,g,b);
Stroke(); // here it draws just a triangle filled with selected colour

/_______

How to connect these func with opengl code (without changing the order of func calls here that is selectcolor or close region after make line or makereactangle)

(Note: need to render only in stroke)

Is there any solution using VBO, VAO???
Retained mode?? Not immediate mode…
Help!!!

Um, isn’t this kind of obvious? Stroke is the function that tells your system “draw the stuff I told you about”. So that’s where you put all of the OpenGL calls. Until then, you just hold on to the information they set in your own data structures.

Can i save whether a shape would be unfilled or filled? whether it will be closed or not?

You are a programmer; you can do whatever you want. My overall point is that your solution will, at the end of the day, have nothing to do with OpenGL itself.

You cannot turn each of those calls into direct OpenGL calls. You have to store their data and submit the appropriate OpenGL calls when Stroke gets called. How you store that data is up to you.