Hi I needed to draw a round corner rectangle.
I followed the procedure of the above image. I first drew the green rectangle. Then I drew the two black rectangles.And then I drew circles on the edges to make the corner round. Now what I get after doing this is in the image below.
As it can be seen that the corner circles have less transparency on the portions where they overlap with the rectangles. But more transparency when not overlapped with the rectangles. The rectangles have alpha set to 0.5f. and the circle also have 0.5f alpha. So thats why its white on the overlapped portions and transparent on non overlapped portions. I want the overlapped portions to have same transparency as the rectangle so that the overlapped circle portion can not be seen.My blend function is glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); I tried to understand the blend functions in more details in here. But I could not understand anything.
My code is below,
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, (int) screenWidth, (int) screenHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(0.0f, (double)screenWidth / screenHeight, 0.0f, 1.0f, -1.0f, 1.0f);
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_TEXTURE_2D);
glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, bubbleMiddleRectStartCoord);
glColorPointer(4, GL_FLOAT, 0, rectColor);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glVertexPointer(3, GL_FLOAT, 0, bubbleTopRectStartCoord);
glColorPointer(4, GL_FLOAT, 0, rectColor);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glVertexPointer(3, GL_FLOAT, 0, bubbleBottomRectStartCoord);
glColorPointer(4, GL_FLOAT, 0, rectColor);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
//smooth edge of the bubble rectangle
drawCircle(triangleAmount,bubbleEdgeRadius,bubbleMiddleRectStartCoord->upperLeft.x+bubbleEdgeRadius,bubbleMiddleRectStartCoord->upperLeft.y,255,255,255,128);
drawCircle(triangleAmount,bubbleEdgeRadius,bubbleMiddleRectStartCoord->lowerLeft.x+bubbleEdgeRadius,bubbleMiddleRectStartCoord->lowerLeft.y,255,255,255,128);
drawCircle(triangleAmount,bubbleEdgeRadius,bubbleMiddleRectStartCoord->upperRight.x-bubbleEdgeRadius,bubbleMiddleRectStartCoord->upperRight.y,255,255,255,128);
drawCircle(triangleAmount,bubbleEdgeRadius,bubbleMiddleRectStartCoord->lowerRight.x-bubbleEdgeRadius,bubbleMiddleRectStartCoord->lowerRight.y,255,255,255,128);
glDisableClientState(GL_COLOR_ARRAY);
glEnable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_MATERIAL);
glDisable(GL_TEXTURE_2D);
swapBuffers();
rectColor has value
GLfloat rectColor[]=
{
1.0f,1.0f,1.0f,0.5,
1.0f,1.0f,1.0f,0.5,
1.0f,1.0f,1.0f,0.5,
1.0f,1.0f,1.0f,0.5
};
drawCircle function generates the points for the circle and draws it. The drawing portion of that function is
glVertexPointer(2, GL_FLOAT, 0, vertices);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);
glDrawArrays(GL_TRIANGLE_FAN, 0, triangleAmount+2);
Can anyone help me to solve the problem? Thanks.
EDIT: this is how it looks after using those two blend functions.
I see where you are going with this and seeing your result you probably only need to disable blend while you are drawing the mask (the 3 rectangles and 4 circles), then using glBlendFunc(GL_DST_ALPHA, GL_ZERO). Though this will only work if nothing has already been drawn on the scene.
To explain what you did there is you are drawing a white color with .5 alpha and blending it.
Consider on the beginning the pixel color "destination" is (0,0,0,0) and incoming "source" is always in your case (1,1,1,.5). Lets say source color is "S" and destination is "D" while the components are (r,g,b,a) so that source alpha is "S.a" what you wrote in your blend function is:
output = S*S.a + D*(1.0-S.a) =
(1,1,1,.5)*.5 + (0,0,0,0)*(1.0-.5) =
(.5, .5, .5, .25) + (0,0,0,0) =
(.5, .5, .5, .25)
so when you draw your circle over the already drawn rectangle:
output = S*S.a + D*(1.0-S.a) =
(1,1,1,.5)*.5 + (.5, .5, .5, .25)*(1.0-.5) =
(.5, .5, .5, .25) + (.25, .25, .25, .125) =
(.75, .75, .75, .375)
resulting in alpha difference. So from this I hope you can understand what the 2 parameters mean in the blend function: First one tells what factor to use to multiply the source (incoming) color and the second one how to multiply the destination color. In the end they are summed together.
So for your case you would like to force the alpha channel to some value everywhere you draw those primitives. To achieve that you would need S*1.0 + D*.0 and parameters for that are glBlendFunc(GL_ONE, GL_ZERO), though this is the same as just disabling the blend. Only writing this primitives would produce a white(gray) rounded rect with transparency of .5 while all the rest is fully transparent. Now after this you need to set blend function to multiply your incoming color with the destination alpha glBlendFunc(GL_DST_ALPHA, GL_ZERO).
EDIT:
I did not totally understand what you want to achieve till now. As I mentioned above, this will not work if you already have some scene drawn.
To overlay an existing scene with some complex object (in this case the object is overlapping itself on some parts) it would be most bulletproof to use a stencil buffer. Creating it is much like depth buffer but you may consider it as another color channel, it is easy to draw to it and later use it so you might want to look at it at some point.
In your case it is probably safe to say this is your main buffer and is meant for displaying. In that case you can just use the alpha channel:
To draw only to alpha channel you have to set glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE) and when you are done, put all the parameters to true.
To clear the alpha channel you have to draw a fullscreen rect with some color with desired alpha (I suggest you use (1,1,1,1)) and draw only to alpha channel
To draw that mask (the 3 rects and 4 circles) use glBlendFunc(GL_ONE, GL_ZERO) and color (1,1,1, 1-desiredAlpha)
To draw your rounded label use glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA)
So the procedure would be:
//your background is drawn, time to overly labels
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE);
glColor(1.0f, 1.0f, 1.0f, 1.0f);
//draw fullscreen rect
glBlendFunc(GL_ONE, GL_ZERO);
glColor(1.0f, 1.0f, 1.0f, 1.0f-.5f);
//draw 3 rects and 4 circles
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA);
//draw the label as a normal rect (the rounded parts will be trimmed because of alpha channel)
and you can just repeat that in a for loop for all the labels.
I know things got a bit complicated but what you are trying to do is not as easy as it would seem. I presented this solution to you because this way you have least code to change, in general I would suggest to use stencil buffer (already mentioned) or a FBO (frame buffer object). The FBO system would be to create another frame buffer and attach a texture to it, draw the whole label object to it and then use the bound texture to draw it to main screen.
I am creating a 3D game. I have objects in my game. When an enemy hits my position I want my screen to go red for a short time. I have chosen to do this by trying to render a full screen red square at my camera position. This is my attempt which is in my render method.
RenderQuadTerrain();
//Draw the skybox
CreateSkyBox(vNewPos.x, vNewPos.y, vNewPos.z,3500,3000,3500);
DrawCoins();
CollisionTest(g_Camera.Position().x, g_Camera.Position().y, g_Camera.Position().z);
DrawEnemy();
DrawEnemy1();
//Draw SecondaryObjects models
DrawSecondaryObjects();
//Apply lighting effects
LightingEffects();
escapeAttempt();
if(hitbyenemy==true){
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE); // additive blending
float blendFactor = 1.0;
glColor3f(blendFactor ,0,0); // when blendFactor = 0, the quad won't be visible. When blendFactor=1, the scene will be bathed in redness
glBegin(GL_QUADS); // Draw A Quad
glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glEnd();
}
All this does, however, is turn all of the objects in my game a transparent colour, and I can't see the square anywhere. I don't even know how to position the quad. I'm very new to openGL.
How my game looks without an attempt to render a quad:
How my game looks after my attempt:
With Kevin's code and glDisable(GL_DEPTH_TEST);
EDIT: I have changed the code to the below paste..still looks like image 1.
http://pastebin.com/eiVFcQqM
There are several possible contributions to the problem:
You probably want regular blending, not additive blending; additive blending will not turn white, yellow, or purple objects red. Change the blend func to glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); and use a color of glColor4f(1, 0, 0, blendFactor);
You should glDisable(GL_DEPTH_TEST); while drawing the overlay, to prevent it from being hidden by other geometry, and reenable it afterward (or use glPush/PopAttrib(GL_ENABLE_BIT)).
The projection and modelview matrixes should be the identity, to ensure a quad with those coordinates covers the entire screen. (However, you may have that implicitly already, since you say it is affecting the full screen, just not in the right way.)
If these suggestions do not fix it, please edit your question showing screenshots of your game with and without the red flash so we can understand the problem better.
I am trying to import .obj files but cant even get a triangle rendered. Here is what I'm trying to do just to test if it can show something.
glBegin(GL_TRIANGLES);
glColor3f(0.5, 0.5, 0.5);
glVertex3f(0, 0, 0);
glVertex3f(1, 1, 1);
glVertex3f(2, 2, 2);
glEnd();
This doesn't display anything. However when I change GL_TRIANGLES for GL_LINES it displays the line.
How can I get the triangle to display?
Language: C++
OS: Mac 10.7.3
A triangle where all three points are colinear (in the same line) has zero area. And a triangle renderer only renders the area of a triangle, not the edges of it. So it renders zero pixels.
I'm wondering if it is possible to simulate the effect of looking through the keyhole in OpenGL.
I have my 3D scene drawn but I want to make everythig black everything except a central circle.
I tried this solution but its doing the completely opposite of what I want:
// here i draw my 3D scene
// Begin 2D orthographic mode
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
GLint viewport [4];
glGetIntegerv(GL_VIEWPORT, viewport);
gluOrtho2D(0, viewport[2], viewport[3], 0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
// Here I draw a circle in the center of the screen
float radius=50;
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x, y);
for( int n = 0; n <= 100; ++n )
{
float const t = 2*M_PI*(float)n/(float)100;
glVertex2f(x + sin(t)*r, y + cos(t)*r);
}
glEnd();
// end orthographic 2D mode
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
What I get is a circle drawn in the center, but I would like to obtain its complementary...
Like everything else in OpenGL, there are a few ways to do this. Here are two off the top of my head.
Use a circle texture: (recommended)
Draw the scene.
Switch to an orthographic projection, and draw a quad over the entire screen using a texture which has a white circle at the center. Use the appropriate blending function:
glEnable(GL_BLEND);
glBlendFunc(GL_ZERO, GL_SRC_COLOR);
/* Draw a full-screen quad with a white circle at the center */
Alternatively, you can use a pixel shader to generate the circular shape.
Use a stencil test: (not recommended, but it may be easier if you don't have textures or shaders)
Clear the stencil buffer, and draw the circle into it.
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, 1);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
/* draw circle */
Enable the stencil test for the remainder of the scene.
glEnable(GL_STENCIL_TEST)
glStencilFunc(GL_EQUAL, 1, 1);
glStencileOp(GL_KEEP, GL_KEEP, GL_KEEP);
/* Draw the scene */
Footnote: I recommend avoiding use of immediate mode at any point in your code, and using arrays instead. This will improve the compatibility, maintainability, readibility, and performance of your code --- a win in all areas.
I'm new to Direct3D and I was on a project taking pictures from a webcam and draw some 3D objects in front of it.
I was able to render webcam images as background using Orthogonal Projection.
//init matrix
D3DXMatrixOrthoLH(&Ortho, frameWidth, frameHeight, 0.0f, 100.0f);
//some code
D3DXVECTOR3 position = D3DXVECTOR3(0.0f, 0.0f, 100.0f);
g_pSprite->Begin(D3DXSPRITE_OBJECTSPACE);
g_pSprite->Draw(g_pTexture,NULL,¢er,&position,0xFFFFFFFF);
g_pSprite->End();
Then I tried to insert a simple triangle in front of it. The Matrices are setup as follow
D3DXMATRIXA16 matWorld;
D3DXMatrixTranslation( &matWorld, 0.0f,0.0f,5.0f );
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI / 4, 1.0f, 1.0f, 100.0f );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
5.0 should be < 100.0 and the triangle is supposed to be appear in front of the images. However it does not appear unless set the z position to 0. At position 0, i can see the triangle but background is blank.
Do you guys have any suggestions?
I would not draw the webcam image in the object space (D3DXSPRITE_OBJECTSPACE) if you intend to use your image solely for background purpose; something like
D3DXVECTOR3 backPos (0.f, 0.f, 0.f);
pBackgroundSprite->Begin(D3DXSPRITE_ALPHABLEND);
pBackgroundSprite->Draw (pBackgroundTexture,
0,
0,
&backPos,
0xFFFFFFFF);
pBackgroundSprite->End();
should hopefully do what you're looking for.
As a quick fix you could disable depth testing as follows;
g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
This way the z-index of the primitives being drawn should reflect the order in which they are drawn.
Also, try using the PIX debugging tool (this is bundled with the DirectX SDK). This is always my first port of call for drawing discrepancies as it allows you to debug each Draw call separately with access to the depth buffer and transformed vertices.