简体   繁体   中英

Using Buffers in OpenGL to draw something

I want to draw lines by using my mouse. To do so, I am using a buffer object.

I am trying to store the location coordinates of the place where the mouse clicks, as the vertices of these lines. These vertices are being stored in the array variable that I have created but the lines are not being drawn.

The following is the important code:

typedef struct {
float x;
float y;
} vec2;

vec2 hotSpot[1000];

hotSpot is the variable where I store the vertex values. The buffer has been initialized as such:

 glBufferData( GL_ARRAY_BUFFER, sizeof(hotSpot), NULL, GL_STATIC_DRAW);

In the mouse() function, I store the value of the coordinates in hotSpot, whenever I click. As I understand, the main() function is always in a loop. So this is what I have written in the main() function:

glBufferData(GL_ARRAY_BUFFER, 0, sizeof(hotSpot), &hotSpot); 

Then in the display() function, this is what I do:

glClear(GL_COLOR_BUFFER_BIT); 
glDrawArrays(GL_LINES, 0, 2);
glFlush();  

Am I going wrong somewhere?

As I understand, the main() function is always in a loop.

No, the main function doesn't loop. Surely your program has a event loop, oftenly called the "main event loop", but this is not tied to the function "main".

A better method for you was not reuploading the whole buffer, but use glBufferSubData` to update just the one entry on the buffer that marks the next mouse position. This also allows you to do away with that large static buffer in your program. You just create a buffer object of the right size and do some bookkeeping (size of the buffer, current number of clicks stored).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM