In my OpenGL problem set, I have drawn a particular polygon and with the mouse selection, I want to draw a square from the place I have selected. The square drawn gets updated rapidly with my change in mouse motion and finally should be affixed to the end point where I release the mouse button.
In my mouse motion routine, I tried doing the glClear(GL_COLOR_BUFFER_BIT); and called a drawSquare function that I have written. The glClear ensures that I have only a unique square drawn and not a blotch of previous images appearing. However, this clearing also clears my initial polygon that I had drawn.
Please advice on how to make sure I have this new square (from mouse) on top of my existing polygon.
P.S : I tried drawing the underlying polygon on the backbuffer and the square on the front, but the glutSwapBuffers() created havoc.
Like many others you seem to mistake OpenGL for a scene managment API, which is plain wrong. OpenGL does nothing else than draw something to the screen. After it's drawn OpenGL completely forgets about the polygon. So what this means is, that you have to draw everything you want to be shown every frame (often after clearing the framebuffer).
So clear the framebuffer, draw your polygon and draw your square. Once the square position (or anything else) changes, clear the framebuffer again, draw the polygon and draw your square. That's the way it works.
And also you should of course not draw anything in the mouse routine, let aside call glClear, which results in exactly the kind of rubbish you're experiencing. Instead update your scene and request the window to redraw itself, which in turn draws the whole scene in the display method:
mouseFunc()
{
updateSquare();
glutPostRedisplay();
}
displayFunc()
{
glClear(...);
drawPolygon();
drawSquare();
glutSwapBuffers();
}
And of course by all means don't draw to the front buffer when using double buffering.
Related
I am wondering what kind of methods are commonly used when we do zoom in/out.
In my current project, I have to display millions of 2D rectangles on the screen, and I am using a fixed viewport and changing glortho2D variables when I have to zoom in/out.
I am wondering if this is a good way of doing it and what other solution can I use.
I also have another question which I think it is related to
how should I do zoom in/out.
As I said, I am currenly using a fixed viewport and changing glortho2D variables in my code, and I assumed that opengl will be able to figure out which rectangles are out of the screen and not render them. However, it seems like opengl is redrawing all the rectangles again and again. The rendering time of viewing millions of rectangles (zoom out) is equal to vewing hundreds of rectangles (zoom into a particular area), which is opposite of what I expected. I am wondering if it is related to the zooming methods I used or am I missing something important.
ie . I am using VBO while rendering the rectangles.
and I assumed that opengl will be able to figure out which rectangles are out of the screen
You assumed wrong
and not render them.
OpenGL is a rather dumb drawing API. There's no such thing like a scene in OpenGL. All it does is coloring pixels on the framebuffer one point, line or triangle at a time. When geometry lies outside the viewport it still has to be processed up to the point it gets clipped (and then discarded).
I'm making a 2D game that uses directx. Currently, I have a background texture (with more to come) that I draw to the screen. However, I only want a portion of the texture drawn to the screen. I know that I could use a rectangle with the draw function, but I need a greater degree of control. Is there a way to draw several triangles (using custom vertices) to the screen from my drawing? I've looked around the internet and this site, but I just can't seem to find what I want. I can give more information/code if needed. Thank you!
Suppose i have drawn a triangle,a cube,a square using draw_triangle,draw_cube and draw_square function respectively.How can I delete a cube using keypress func.?..If i use glClear(GL_COLOR_BUFFER_BIT) entire screen will be erased..How to delete a specific polygon.?
How to delete a specific polygon.?
Don't render it in next frame.
Once you draw a polygon, there is no polygon, just a bunch of pixels in color buffer + values in depth buffer. So you can't "delete" it, and you can't restore previous state of color/depth buffer. So clear screen, redraw the scene without polygon you didn't want.
If you want to clear only a specific part of the screen or any specific image, then redisplay it by using a keyboard interrupt wihout clearing the whole screen.
I think the best solution is to have a boolean variable which indicates whether your object must be displayed or not. Thus, only when a key is pressed, you clear the screen and redraw the entire scene.
maybe something like that:
Render()
{
clear_screen();
setup_camera_and_other_scene_states();
if (cube) drawCube();
if (sphere) drawSphere();
swap_buffers():
}
keyPress()
{
if (presses_some_key) cube = !cube;
}
I was trying to use CCRenderTexture for pixel perfect collision detection, as outlined in this forum posting:
http://www.cocos2d-iphone.org/forum/topic/18522/page/2
The code "as is" works, and I have integrated it with my project
But I am having trouble doing some of the other things discussed:
If I create the renderTexture to be any size less than the screen size, the collision detection doesn't work properly - It seems to show collisions when the sprites are close (<15px) to each other but not actually colliding.
Also I have trouble changing the location of the render texture. Regardless of the position I specify, it seems to go from bottom left (0,0) till the width & height specified. I followed this post:
http://www.cocos2d-iphone.org/forum/topic/18796
But it doesn't solve my problem. I still get bad collisions like specified above. Also, the first post I mentioned on the list contains comments by many users who have resized their textures to 10x10, and repositioned them off screen.
Does anyone have any sample code, so I can see what I am doing wrong? I just use the boilerplate code:
CCRenderTexture* _rt = [CCRenderTexture renderTextureWithWidth:winSize.width height:winSize.height];
_rt.position = CGPointMake(winSize.width*0.5f, winSize.height*0.5f);
[[RIGameScene sharedGameScene]addChild:_rt];
_rt.visible = YES;
I use cocos2d-iphone 1.0.1
You need to move the sprites you intend to draw into the region of the renderTexture before calling draw or visit. Moving the renderTexture does not change the position of _rt.sprite.
The intersection rectangle must be in the region of the renderTexture, otherwise you get inaccurate collisions.
It seems that you cannot change the position of _rt.sprite.
The solution that I use is to determine the origin (x,y) of the intersection box, and offset both the colliding sprites by that much. This will ensure that the intersection rectangle will have its origin at 0,0. Then I calculate the intersection rectangle again (after ensuring the origin of the intersection rect is 0,0) . Then I follow the instructions in the forum posting.
When determining the dimensions of the render texture, I ensure that they are at least as large as the intersection rectangle, and I ensure that the intersection rectangle is fully inside the render texture. This way there are accurate collisions. If even part of the intersection box is outside the render texture i get inaccurate collisions, so before drawing into the render texture, make sure you move the sprites you intend to visit so that the intersection box is entirely within the render texture.
Remember to move the sprites back after you're done. :)
I am developing a paint-like application using C++ and Open GL. But every time i draw objects like circle, lines etc they don't ** stay ** on the page. By this I mean that every new object I draw is getting placed on a blank page. How do I get my drawn objects to persist?
OpenGL has no geometry persistency. Basically it's pencils, brushes and paint, with which you draw on a canvas called the "framebuffer". So after you drawn something and clear the framebuffer, it will not reappear in some magic way.
There are two solutions:
you keep a list of all drawing operations and at each redraw you repaint everything from that list.
After drawing something copy the image in the framebuffer to a texture and instead of glClear you fill the background with that texture.
Both techniques can be combined.
Just don't clear the framebuffer and anything you draw will stay on the screen. This is the same method I use to allow users to draw on my OpenGL models. This is only good for marking up an image, since by using this method you can't erase what you've drawn, unless your method of erasing is to draw using your background color.