I need to draw indented(concave) solid polygon with some vertices.
I use
void HelloWorld::draw(void)
{
CCPoint vertices[5] = {ccp(200, 200), ccp(400, 400), ccp(200, 600), ccp(500, 600), ccp(500, 200)};
ccDrawSolidPoly(vertices, 5, ccc4f(0.7f, 0.7f, 0.7f, 0.5f));
}
And get Rectangle with triangle inside.
But I expect indented(concave) solid polygon as in the picture
Try using drawPolygon function in CCDrawNode
void drawPolygon(CCPoint* verts, unsigned int count, const ccColor4F &fillColor,
float borderWidth, const ccColor4F& borderColor)
here is an example
CCPoint vertices[5] = {ccp(200, 200), ccp(400, 400), ccp(200, 600), ccp(500, 600), ccp(500, 200)};
CCDrawNode* polygon = CCDrawNode::create();
//creating red polygon with thin black border
polygon->drawPolygon(vertices, 5, ccc4f(1, 0, 0, 1), 1, ccc4f(0, 0, 0, 1));
addChild(polygon);
I hope it works
Without modifications to Cocos2d engine you can achive the polygon of the desired form either by starting your array from ccp(400,400) or by adding one more point ccp(500,400)! And then your array of points should start from this point like in the picture I attach.
The reason for this is that cocos2d uses by default GL_TRIANGLE_FAN flag while drawing complex polygons. This means that all the points in the triangles in your polygon will be built relative to the first point in the array of points.
You can go to CCDrawNode.cpp file and replace this flag by GL_TRIANGLE_SPLIT. To know more just google these two flags.
Related
It may be a duplicate with this question,
but I don't know how to apply this approach in my app, and which method I should use in cocos2d-x to draw a Bezier curve. My app should allow users to draw lines and curves when they touch the canvas. How can I achieve that?
From Cocos2dx v3.3 you can use DrawNode to draw Bezier curves. Check the DrawPrimitivesTest.cpp, it is very easy to use.
This is only a sample script taken from the above mentioned file. You can use it anywhere in your Scene:
auto draw = DrawNode::create();
addChild(draw, 10);
auto s = Director::getInstance()->getWinSize();
draw->drawQuadBezier(Vec2(0, s.height), Vec2(s.width/2, s.height/2), Vec2(s.width, s.height), 50, Color4F(CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), 0.5));
draw->drawCubicBezier(VisibleRect::center(), Vec2(VisibleRect::center().x+30,VisibleRect::center().y+50), Vec2(VisibleRect::center().x+60,VisibleRect::center().y-50),VisibleRect::right(),100, Color4F(CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), 0.5));
in CCDrawPrimitives.cpp file.
You can use this method.
ccDrawCubicBezier
ccDrawQuadBezier
-MyClass::draw() {
glLineWidth(4.0f);
ccPointSize(4);
//Draw a blue quadratic bezier curve
ccDrawColor4B(0, 0, 255, 255);
ccDrawQuadBezier(ccp(90,0), ccp(200, 70), ccp(350,0), 12);
//Draw cubic red bezier curve
ccDrawColor4B(255, 0, 0, 255);
ccDrawCubicBezier(ccp(100,100), ccp(300,150), ccp(250,50), ccp(350,100), 12);
//Restore original values
glLineWidth(1);
ccDrawColor4B(255,255,255,255);
ccPointSize(1);
}
Every time you move your touch positions, ccTouchesMoved method is called as you may know.
You can control the curve shape using the method and member variables.
I am a beginner in openGL. I am currently working on a program which take in inputs the width and the length of a board. Given those inputs i want to dynamically position my camera so that i can have a view on the whole board. Let' s say that my window size is 1024x768.
Are there any mathematical formula to compute the different parameters of the opengl function glookat to make it possible ?
the view i want to have on the board should look like this.
It doesn't matter if a board too big will make things look tiny. What matters the most here is to position the camera in a way that the view on the whole board is made possible
So far i am hopelessly randomly changing the parameters of my glookat function till i ran into something decent for a X size width and and Y size Height.
my gluperpective function :
gluPerspective(70 ,1024 / 768,1,1000)
my glooatfunction for a 40 * 40 board
gluLookAt(20, 20, 60, 20, -4, -20, 0, 1, 0);
how i draw my board (plane):
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
gluLookAt(20, 20, 60, 20, -4, -20, 0, 1, 0);
glBindTexture(GL_TEXTURE_2D, texture_sol);
glBegin(GL_QUADS);
glTexCoord2i(0, 0); glVertex3i(width, 0, height);
glTexCoord2i(10, 0); glVertex3i(0, 0, height)
glTexCoord2i(10, 10); glVertex3i(0, 0, 0);
glTexCoord2i(0, 10); glVertex3i(width, 0, 0);
glEnd();
the output looks as follow :
gluLookAt takes 2 points and a vector; the eye and centre positions and the up vector. There's no issue with the last parameter. The first two are relevant to your question.
I see that your board in the world space is extending on the positive X and Y axes with some arbitrary width and height values. Lets take width = height = 1.0 for instance. So the board spans from (0, 0), (1, 0), (1, 1), (0, 1); the Y value is ignored here since the board lies on the Y = 0 plane and have the same value for all vertices; these are just (X, Z) values.
Now coming to gluLookAt, eye is where the camera is in world space and centre is the point where you want the camera to be looking at (in world space)
Say you want the camera to look at centre of the board I presume, so
eye = (width / 2.0f, 0, height/2.0f);
Now you've to position the camera at its vantage point. Say somewhere above the board but towards the positive Z direction since there's where the user is (assuming your world space is right handed and positive Z direction is towards the viewer), so
centre = (width / 2.0f, 5.0f, 1.0f);
Since the farthest point on Z is 0, I just added one more to be slightly father than that. Y is how much above you want to see the board from, I just chose 5.0 as an example. These are just arbitrary values I can come up with, you'll still have to experiment with these values. But I hope you got the essence of how gluLookAt works.
Though this is written as an XNA tutorial, the basic technique and math behind it should carry over to OpenGL and your project:
Positioning the Camera to View All Scene Objects
Also see
OpenGL FAQ
8.070 How can I automatically calculate a view that displays my entire model? (I know the bounding sphere and up vector.)
Edit in response to the comment question
A bounding sphere is simply a sphere that completely encloses your model. It can be described as:
A bounding sphere, S, of a point set P with n points is described by
a center point, c, and a radius, r.
So,
P = the vertices of your model (the board in this case)
c = origin of your model
r = distance from origin of the vertex, in P, farthest from the origin
So the Bounding Sphere for your board would be composed of the origin location (c) and the distance from one corner to the origin (r) assuming the board is a square and all points are equidistant.
For more complicated models, you may employ pre-created solutions [1] or implement your own calculations [2] [3]
I want to draw a rectangle shape using cocos2d-android . i googled all my way to draw the rectangle in my scene and trying
CGRect rect = CGRect.make(numbers[0][0],numbers[0][1],70,70);
but it was not displaying any rectangle. could any one help me out to draw a rectangle in scene?
Check out the example file for drawing primitives. You can see an example of drawing a arbitrary polygon there:
// closed purple poly
gl.glColor4f(1.0f, 0.0f, 1.0f, 1.0f);
gl.glLineWidth(2);
CCPoint vertices2[] = {CCPoint.ccp(30, 130), CCPoint.ccp(30, 230), CCPoint.ccp(50, 200)};
Primitives.drawPoly(gl, vertices2, 3, true);
Just define your rectangle points there.
There are a lot more test examples to be found there that can answer the basic drawing questions like this.
I am using the SFML library, which in its latest RC allows you to manipulate vertices and draw them like so :
// define a 100x100 square, red, with a 40x40 texture mapped on it
sf::Vertex vertices[] =
{
sf::Vertex(sf::Vector2f( 0, 0), sf::Color::White, sf::Vector2f( 0, 0)),
sf::Vertex(sf::Vector2f( 0, 100), sf::Color::White, sf::Vector2f( 0, 40)),
sf::Vertex(sf::Vector2f(100, 100), sf::Color::White, sf::Vector2f(40, 40)),
sf::Vertex(sf::Vector2f(100, 0), sf::Color::White, sf::Vector2f(40, 0))
};
Now my question is: what would be the best way to draw a curve / an angular sector?
The rendering process is calling OpenGL so you may be more familliar with it than SFML.
Basically is it possible to draw curves by not defining 1 point per pixel?
thanks
Without using OpenGL directly, no. But usually curves are made of a sequence of lines. Just calc points of your curve (no need to be one per pixel), and draw a line strip.
I am wondering if gluLookAt together with glFrustum is distorting the rendered picture.
This is how a scene is rendered:
And here's the code that rendered it.
InitCamera is called once and should, as I understand it now, set up a matrix so as if I looked from a position 2 units above and 3 units in front of the origin towards the origin. Also glFrustum is used in order to create a perspective`.
void InitCamera() {
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt (
0, 2 , 3,
0, 0 , 0,
0, 1 , - 0
);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum (- 1, 1,
- 1, 1,
1,1000.0);
glMatrixMode(GL_MODELVIEW);
}
Then TheScene is what actually draws the picture:
void TheScene() {
glClear(
GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT
);
glMatrixMode(GL_MODELVIEW);
// Draw red circle around origin and radius 2 units:
glColor3d(1,0,0);
glBegin(GL_LINE_LOOP);
for (double i = 0; i<=2 * M_PI; i+=M_PI / 20.0) {
glVertex3d(std::sin(i) * 2.0, 0, std::cos(i) * 2.0);
}
glEnd();
// draw green sphere at origin:
glColor3d(0,1,0);
glutSolidSphere(0.2,128, 128);
// draw pink sphere a bit away
glPushMatrix();
glColor3d(1,0,1);
glTranslated(8, 3, -10);
glutSolidSphere(0.8, 128, 128);
glPopMatrix();
SwapBuffers(hDC_opengl);
}
The red ball should be drawn in the origin and at the center of the red circle around it. But looking at it just feels wierd, and gives me the imprssion that the green ball is not in the center at all.
Also, the pink ball should, imho, be drawn as a perfect circle, not as an ellipse.
So, am I wrong, and the picture is drawn correctly, or am I setting up something wrong?
Your expectations are simply wrong
The perspective projection of a 3d circle (if the circle is fully visible) is an ellipse, however the projection of the center of the circle is NOT in general the center of the ellipse.
The outline of the perspective projection of a sphere is in general a conic section i.e. can be a circle, an ellipse, a parabola or an hyperbola depending on the position of viewpoint, projection plane and sphere in 3D. The reason is that the outline of the sphere can be imagined as a cone starting from the viewpoint and touching the sphere being intersected with the projection plane.
Of course if you're looking at a circle with a perfectly perpendicular camera the center of the circle will be projected to the center of the circle projection. In the same manner if your camera is pointing exactly to a sphere the sphere outline will be a circle, but those are special cases, not the general case.
These differences between the centers are more evident with strong perspective (wide angle) cameras. With a parallel projection instead this apparent distortion is absent (i.e. the projection of the center of a circle is exactly the center of the projection of the circle).
To see the green sphere in the centre of the screen with a perfect circle around it you need to change the camera location like so:
gluLookAt (
0, 3, 0,
0, 0, 0,
0, 0, 1
);
Not sure what's causing the distortion of the purple sphere though.
The perspective is correct, it just looks distorted because that's how things fell together here.
try this for gluLookAt, and play around a bit more.:
gluLookAt (
0, 2 , 10,
0, 0 , 0,
0, 1 , 0
);
The way I tried it out was with a setup that allows me to adjust the position and view direction with the mouse, so you get real time motion. Your scene looks fine when I move around. If you want I can get you the complete code so you can do that too, but it's a bit more than I want to shove into an answer here.