C
C
ctx262015-01-05 21:00:56
OpenGL
ctx26, 2015-01-05 21:00:56

How to change coordinates of OpenGL objects?

There is code, the following code
#include
#include
#include
using namespace std;
class Circle
{
public:
float x, y, r, alpha;
Circle(float _x=0.0, float _y=0.0, float _r=0.0):x(_x), y(_y), r(_r), alpha(1.0){};
void draw();
};
void Circle::draw()
{
glColor4f(1.0, 1.0, 0.0, this->alpha);
glBegin(GL_LINE_LOOP);
for(float i=0.0; i<2*3.14; i+=3.14/18)
{
glVertex2f(this->x+this->r*sin(i), this->y+this->r*cos(i ));
}
glEnd();
}
vector circ;
float WinWid=400.0;
float WinHei=400.0;
float X=0.0, Y=0.0;
void Draw()
{
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_ALPHA);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
vector::iterator i=circ.begin();
while(i!=circ.end())
{
i->draw();
if(i->alpha<=0.05f)
i=circ.erase(i);
else
++i;
}
glDisable(GL_BLEND);
glDisable(GL_ALPHA);
glutSwapBuffers();
}
void Timer(int value)
{
for(vector::iterator i=circ.begin(); i!=circ.end(); i++)
{
i->r++;
i->alpha/=1.05;
}
glutPostRedisplay();
glutTimerFunc(50, Timer, 0);
}
void Timer2(int)
{
Circle c(rand() % int(WinWid) - WinWid/2, 0 ,0);
circ.push_back(c);
glutPostRedisplay();
glutTimerFunc(200, Timer2, 0);
}
void Initialize()
{
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-WinWid/2, WinWid/2, WinHei/2, -WinHei/2, -200.0, 200.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(WinWid, WinHei);
glutInitWindowPosition(100, 200);
glutCreateWindow("Lesson 08");
glutDisplayFunc(Draw);
glutTimerFunc(0, Timer, 0);
glutTimerFunc(0, Timer2, 0);
Initialize();
glutMainLoop();
return 0;
}
Now we need to make the appearing circles move strictly up in y from zero and their radius depends on this coordinate. Those. for example, at coordinates y=200, the radius of the circle was the largest.
Upon reaching this radius, the circle disappeared.
Tell me how to do it?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Taratin, 2015-01-05
@Taraflex


glColor4f(1.0, 1.0, 0.0, this->alpha);
glBegin(GL_LINE_LOOP);
for(float i=0.0; i<2*3.14; i+=3.14/18)
{
glVertex2f(this->x+this->r*sin(i), this->y+this->r*cos(i ));
}
glEnd();
it was 2015... I
recommend to get acquainted with https://code.google.com/p/gl33lessons/ without water and that's it

S
SHVV, 2015-01-06
@SHVV

In your case, the circle drawing function can render them in arbitrary coordinates, so it’s enough to move them in the first timer and delete them there:

for(vector::iterator i=circ.begin(); i!=circ.end(); i++) {
  i->r++; // Увеличивается радиус
  i->alpha/=1.05; // Уменьшается прозрачность
  i->y += 10; // Двигаем вверх по y (может придётся поменять на - , если будут двигаться не в ту сторону)
  if (i->y >= 200) {
    i = circ.erase(i);
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question