PDA

View Full Version : Draw a circle in opengl but a line is going from the circle nice to the origin point



zee2200
05-29-2016, 10:46 PM
Hey guys, i have a sprite class that will be handling initializing and drawing shapes to the screen but i have a problem with it right now since when i try to draw a circle it scaled properly and it in the right position but it always has a line going to the 0,0 point I'm not sure what exactly is causing this and my only guess would be because i have glOrtho setup as such glOrtho(0, _width, _height, 0, 0, 1). also here's the init function for the circle.


void Sprite::init(int x, int y, int vertNum, float r, Shape shape) {
_vertNum = vertNum;
if (_vboID == 0) {
glGenBuffers(1, &_vboID);
}
std::vector<Vertex> vertexData(_vertNum);
if (shape == Shape::CIRCLE) {
for (int i = 0; i < _vertNum; i++) {
float angle = 2.0 * M_PI * i / _vertNum;
vertexData[i].setPosition(r*cos(i) + x, r*sin(i) + y);
}
}
glBindBuffer(GL_ARRAY_BUFFER, _vboID);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) *vertexData.size(), vertexData.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}

by the way i tried taking the sine and cosine of both i and angle but neither of them work i just ended up using i since it made a full circle with a line but taking the sine and cosine of angle gave me more of a baseball shape.

GClements
05-30-2016, 02:59 AM
for (int i = 0; i < _vertNum; i++) {
float angle = 2.0 * M_PI * i / _vertNum;
vertexData[i].setPosition(r*cos(i) + x, r*sin(i) + y);
}


That will generate something more like a star than a circle, because you're passing i to sin/cos instead of angle.

zee2200
05-30-2016, 03:59 PM
That will generate something more like a star than a circle, because you're passing i to sin/cos instead of angle.

Like i already said sine i tried taking the sine and cosine of both i and of angle but i get a line in both cases i just went with i because it looked closer to a circle.\
For example(sorry for using links by the way i just can't get the images inserted for some reason.) this is the result of the sine and cosine of i:
http://i.imgur.com/R0Ozg7W.png

and this is the sine and cosine of angle:
http://i.imgur.com/zPPwJ3i.png

GClements
05-31-2016, 02:20 AM
I missed this the first time:




std::vector<Vertex> vertexData(_vertNum);



glBufferData(GL_ARRAY_BUFFER, sizeof(float) *vertexData.size(), vertexData.data(), GL_STATIC_DRAW);


sizeof(float) should be sizeof(Vertex). As it is, you're only copying a subset of the array to the buffer.

zee2200
05-31-2016, 04:21 PM
Ah ok i see thank you so much!