Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Move a Triangle through glTranslated

  1. #1
    Newbie Newbie
    Join Date
    Nov 2018
    Posts
    1

    Red face Move a Triangle through glTranslated

    Good morning dear friends, I apologize for my ignorance, but what is happening in my code, because I can only move once in my triangle, by pressing the key, I need to always move to the left side .....
    I'm a rookie myself, please help me ....



    Code cpp:
    #include<windows.h>
    #include<gl/GL.h>
    #include<gl/glu.h>
    #include<gl/glut.h>
    #include <stdio.h>
     
    typedef struct {
        GLint x,y;
    }ponto;
     
    void desenha();
    void inicializacao();
    void GerenciaTeclado(unsigned char key, int x, int y);
    void desenha_triangulo();
    int main(int argc, char **argv){
     
        glutInit(&argc,argv);
        glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
        glutInitWindowSize(800,600);
        glutInitWindowPosition(0,0);
        glutCreateWindow("Trabalho 1");
        glutKeyboardFunc(GerenciaTeclado);
        inicializacao();
        glutDisplayFunc(desenha);
        glutMainLoop();
        return 0;
    }
     
    void inicializacao(void){
     
        glClearColor(1.0f,1.0f,1.0f,1.0f); // cor de fundo branca
        glPointSize(8.0); // tamanho dos pontos de cada pixels
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluOrtho2D(0,800,0,600);
        glLineWidth(4.0);
    }
     
    void desenha(){
        glClear(GL_COLOR_BUFFER_BIT);
        glColor3f(1.0,0.1,0.1);
        desenha_triangulo();
    }
     
    void desenha_triangulo(){
     
        //desenha um retangulo vermelho
        glBegin(GL_TRIANGLES);
            glVertex2i(100,100);
            glVertex2i(200,200);
            glVertex2i(300,100);
        glEnd();
        glFlush();   //envia toda a saída para a exibição
    }
     
     
    void GerenciaTeclado(unsigned char key, int x, int y){
        printf("Tecla Pressionada %d\n",key);
        if ((key == 97) || (key ==65)){ // tecla A mover para a esquerda
     
            glPushMatrix();
            glTranslated(-20,0,1.0); // translação de -10 para mover para a esquerda
            desenha();
            glPopMatrix();
           // glutPostRedisplay();
        }
    }
    Last edited by Dark Photon; 11-07-2018 at 06:09 AM.

  2. #2
    Newbie Newbie
    Join Date
    Nov 2018
    Posts
    2
    It won't be storing the value of the matrix, you'll need to store the translation in a float and increment it and then use that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •