0

I am having problem managing my code and imagining a good design. What i wanna do is simply draw a line first, then after line has been rendered, draw the square so it overlaps the line partially and then keep on doing this.

I created 2 classes, Lines and Squares. I have set up and bounded a VAO and a VBO in the main class. In my lines and squares classes i only pass the data to this buffer and make appropriate calls to VertexAttribPointer and GlDrawArrays in their draw() method.

In the main class there is a main game loop that runs infinitely till the user closes the window. Now what i want to do is call the draw() method of both Lines and Squares in an Update() method. After this Render() is called which only swaps buffers and polls events. Basically a rough sketch is...

while(window_close != true)
{   
    update();
    render();
}

public void update()
{
   GL11.glClearColor(0.2f,0.4f,0.3f, 1);
   GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);  
   lines.draw();  // object of Lines class
   squares.draw();   // object of Squares class
}

public void render()
{
   swapbuffers(); 
}

But there is a problem since update is in a loop it'll feed lines data into the buffer then it'll overwrite that data with squares one. I want it to alternate i.e render lines, then square. Hence i thought to have a flag inside each class. Before the draw() methods feed data they would check which one has been rendered and then draw accordingly or else return and do nothing.

Furthermore since at the start of Update() The background is cleared, i need to draw lines, render it, then draw squares and render it before clearing the background. I know i could do

while(window_close != true)
{  
   GL11.glClearColor(0.2f,0.4f,0.3f, 1);
   GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);   
   lines.draw();
   render();
   squares.draw();
}

which is ok, but for bigger projects it'd become ugly. i'd have to write render() alternatively.

gallickgunner
  • 472
  • 5
  • 20
  • 1
    What is the problem exactly? What you are doing right now is what most people do. – fordcars Mar 06 '16 at 05:08
  • I didn't know that.. I thought there must be some other way such that i don't have to do anything after the call to `render()` and everything is managed by `update()` – gallickgunner Mar 06 '16 at 08:25
  • Ahhh waitwaitwaitwait, I am having trouble understanding what is the problem. What you want is to swap buffers alternatively? – fordcars Mar 06 '16 at 19:03
  • @fordcars - Check my original question now. The last snippet of code is what i want. but i want to do it in a different way such that all the calls to `draw()` are in the update method. – gallickgunner Mar 07 '16 at 19:56
  • Ok, uh you shouldn't have this problem! I'll try to explain it breifly; `swapBuffers()` does nothing you would normally care about in this way. When you call anything like `glDrawArrays()`, OpenGL draws stuff into a framebuffer. So, you would draw the line, then the square just fine, since it will only modify the framebuffer where the shape is actually located (so it would overlap like you would like). Then, all `swapBuffers()` does is that it copies, pixel-by-pixel, everything from that framebuffer onto the screen. – fordcars Mar 08 '16 at 04:38
  • You simply should not have the problem you are having right now because of this. `swapBuffers()` should only be called **once** per frame! It would be great if you posted the actual drawing code in your question. Maybe you are rendering the scene as a texture or something? It would be great to see everything. – fordcars Mar 08 '16 at 04:40
  • Also, if you are using depth test, you need to clear the depth buffer using `glClear` too! – fordcars Mar 08 '16 at 04:41
  • damn, i didn't know how `swapbuffers()` worked. Thanks for telling me that, everything is fine now. I thought we could write to buffer only one time after that we must swap the buffers before writing again. Thanks again @fordcars – gallickgunner Mar 08 '16 at 12:03

0 Answers0