Error in transforming parametric equation. Opengl and glut. h

Asked

Viewed 199 times

3

My question is this::

Let the parametric equations given below define a curve as a function of t R:

x(t) = sin(t) + 1/2(half a) sin(5t) + 1/4(a quarter) cos(2,3t)

y(t) = cos(t) + 1/2(half a) cos(5t) + 1/4(a quarter) sin(2,3t)

Use GL_LINE_STRIP to draw the curve line in white, and GL_POINTS to draw the last calculated point, in red color. Make sure the drawing is done in real time, that is, each instant, a new point is calculated, and in this way we will see the construction of the running curve, as illustrated in the figure below. Use the callback timer using 10 milliseconds as a standby time. Tip: use ortho ranging from -2.0 to 2.0 in x and y.

imagem

My code is practically ready, only with only two problems.

The first is: He’s not bending! He’s simply creating straight lines. I don’t know if I converted the wrong formula.

The second is: The new red dot is created, but the old one is still there. I still have to see a way to erase or overwrite the old one, but this by far is not the most important problem.

Follows my code

#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
void display(void);
void init(void);
void reshape (int w, int h); 
void desenhaEixos();
void projecao(void);
void desenhaCurvaLinha(float centerX, float centerY);
void timer (int i);
void desenhaPonto(float centerX, float centerY);

float minX = -2; //Parâmetros do glOrtho
float maxX = 2;
float minY = -2;
float maxY = 2;
float minZ = -1;
float maxZ = 1;

int k = 0;

int main(int argc, char *argv[]){
    //Escopo de criação de janela
    glutInit(&argc, argv);//Avisa que será criada uma janela
    glutInitWindowSize(500,500); //Diz o tamanho da janela
    glutInitWindowPosition(10,10); //Diz onde a janela vai abrir na tela, em pixel
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); //Prepara o sistema para a janela a ser criada, é extremamente importante

    glutCreateWindow("FreeGLUT Shapes");//Cria a janela

    //Escopo de registro de callbacks
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);   

    //glutIdleFunc(idle);
    glutTimerFunc(1000/*valor em milesegundos*/, timer, 1);

    //Demais
    init();
    glutMainLoop();//Fica esperando a ação do usuário



    return EXIT_SUCCESS;
}



void display(void){
     glClear(GL_COLOR_BUFFER_BIT);//Limpando o buffer de cor

     desenhaCurvaLinha(1, 1);
     desenhaPonto(1, 1);

     glutSwapBuffers();
     }



void init(void){

     glClearColor(0,0,0,0);//Escolhe a cor do fundo da janela, nesse caso, preto
}



void desenhaEixos(){

     //glLineWidth(3); //Caso queira mudar a espessura da linha

     glBegin(GL_LINES); //Indica que vou desenhar linhas

          glColor3f(1,0,0); // Vermelho
          glVertex3f(minX, 0, 0); //Estamos escrevendo o eixo X na tela
          glVertex3f(maxX, 0, 0);// Fui do mínimo até o máximo, da esquerda da tela até a direita

          glColor3f(0,1,0); //Verde
          glVertex3f( 0, minY, 0);
          glVertex3f( 0, maxY, 0);

          glColor3f(0,0,1); //Azul
          glVertex3f( 0, 0, minZ);
          glVertex3f( 0, 0, maxZ);

     glEnd();   
}



void desenhaCurvaLinha(float centerX, float centerY){
     float x, y;
     int t;
     glColor3f(1,1,1);
     glBegin(GL_LINE_STRIP);
          for(t=0;t<=k;t++){ 
               x = sin(t) + 0.5 * sin (5 * t) + 0.25 * cos (2.3*t);
               y = cos(t) + 0.5 * cos (5 * t) + 0.25 * sin (2.3*t);
               glVertex2f(x, y);      
          }  
     glEnd();
}

void desenhaPonto(float centerX, float centerY){
     float x, y;
     int t;
     glPointSize(5);
     glColor3f(1,0,0);
     glBegin(GL_POINTS);
          for(t=0;t<=k;t++){ 
               x = sin(t) + 0.5 * sin (5 * t) + 0.25 * cos (2.3*t);
               y = cos(t) + 0.5 * cos (5 * t) + 0.25 * sin (2.3*t);
               glVertex2f(x, y);      
          }  
     glEnd();
}



void reshape (int w, int h){
     glViewport(0,0,w,h); //linha protocolo
     projecao();
}



void projecao(void){

     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
     glOrtho(minX, maxX, minY, maxY, minZ, maxZ); // Diz agora, que o X da minha janela começa no -10 e termina no 10, o y também.
     glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();

}



void timer (int i){


      k++;    
      glutPostRedisplay();
      glutTimerFunc(1000, timer, 1);

}

1 answer

2

Apparently the problem is in the function "drawLine", you just need to change the type of the variable t to float and in the loop "for" you need to change the increment from t++ to t += 0.05 or other precision, so that your code looks like this:

void desenhaCurvaLinha(float centerX, float centerY){
     float x, y;
     float t;
     glColor3f(1,1,1);
     glBegin(GL_LINE_STRIP);
          for(t=0.00f;t<=k;t+=0.05){
               x = sin(t) + 0.5 * sin (5 * t) + 0.25 * cos (2.3*t);
               y = cos(t) + 0.5 * cos (5 * t) + 0.25 * sin (2.3*t);
               glVertex2f(x, y);
          }
     glEnd();
}

This was the result of running the program on my computer:

inserir a descrição da imagem aqui

What happened was that with the increment of 1 in the variable t caused several angles to be ignored, so the smaller the increment of the variable t, the more rounded this curve will be, for example, when using 0.1 instead of 0.05, it is possible to see the points where each loop 'for' to:

inserir a descrição da imagem aqui

I had forgotten to talk about the red dots, it is not necessary to overwrite this point, because at each rendering cycle the whole screen is erased, what you need is basically to draw only the last point in the rendering cycle, in this way:

void desenhaPonto(float centerX, float centerY){
     float x, y;
     float t;
     glPointSize(5);
     glColor3f(1,0,0);
     glBegin(GL_POINTS);
        x = sin(k) + 0.5 * sin (5 * k) + 0.25 * cos (2.3*k);
        y = cos(k) + 0.5 * cos (5 * k) + 0.25 * sin (2.3*k);
        glVertex2f(x, y);
     glEnd();
}

Basically, the drawing of the curves stopped at the angle k, so basically the red dot will be rendered only at the angle k at each cycle, see the image of the program working:

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.