segunda-feira, 29 de fevereiro de 2016

Transformações Geométricas

Hoje iremos falar sobre transformações do OpenGL.

Abordaremos as seguintes transformações geométricas:


  1. TRANSLAÇÃO: reposiciona um objeto;
  2. ESCALA: modifica a escala (aumentando ou diminuindo) de um objeto;
  3. ROTAÇÃO: rotaciona determinado(s) eixo(s) em relação a angulação desejada;

OBS: As transformações geométricas são cumulativas.

Para as demonstrações dos exemplos abaixo utilizaremos o trecho de código a seguir:

#include <iostream>
#include <gl/gl.h>
#include <gl/glut.h>
#include <stdlib.h>
#include <stdio.h>

void resize(int w, int h), display(void);

int main(int argc, char **argv){
glutInit(&argc, argv);
glutInitWindowSize(400, 400);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Scale");
glutDisplayFunc(display);
glutReshapeFunc(resize);
glutMainLoop();
return 0;
}

void display(void){
## Utilizar o código do "display(void)" de cada exemplo abaixo
}

void resize(int w, int h){
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-4.0, 8.0, -4.0, 8.0, -4.0, 8.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}



TRANSLAÇÃO



void display(void){
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLineWidth(4.0);
    
glBegin(GL_LINES);
glColor3f(1, 1, 1);
glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 0.0, 5.0);
glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 5.0, 0.0);
glVertex3f(0.0, 0.0, 0.0); glVertex3f(5.0, 0.0, 0.0);
glEnd();
glTranslatef(2.0f, 2.5f, 0.0f);
glBegin(GL_QUADS);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(1.0, 1.0, 0.0);
glVertex3f(1.0, 3.0, 0.0);
glVertex3f(3.0, 3.0, 0.0);
glVertex3f(3.0, 1.0, 0.0);
glEnd();
glFlush();
glutSwapBuffers();
}

ESCALA



void display(void){
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLineWidth(4.0);
    
glBegin(GL_LINES);
glColor3f(1, 1, 1);
glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 0.0, 5.0);
glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 5.0, 0.0);
glVertex3f(0.0, 0.0, 0.0); glVertex3f(5.0, 0.0, 0.0);
glEnd();

glScalef(2.0f,1.3f,1.0f);

glBegin(GL_QUADS);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(1.0, 1.0, 0.0);  glVertex3f(1.0, 3.0, 0.0);
glVertex3f(3.0, 3.0, 0.0);  glVertex3f(3.0, 1.0, 0.0);
glEnd();
glFlush();
glutSwapBuffers();
}




ROTAÇÃO




void display(void){
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLineWidth(4.0);

glRotatef(40, 0.1, 0.2, 0.0);
##Rotação de 4º em X (40*0.1) e 8º em Y (40*0.2)

glBegin(GL_LINES);
  glColor3f(1, 1, 1);
  glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 0.0, 5.0);
  glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 5.0, 0.0);
  glVertex3f(0.0, 0.0, 0.0); glVertex3f(5.0, 0.0, 0.0);
  glEnd();
glBegin(GL_QUADS);
glColor3f(0.0, 0.0, 1.0);
  glVertex3f(1.0, 1.0, 0.0);  glVertex3f(1.0, 3.0, 0.0);
  glVertex3f(3.0, 3.0, 0.0); glVertex3f(3.0, 1.0, 0.0);
glEnd();
  glFlush();
  glutSwapBuffers();
}