Created
February 18, 2011 23:51
-
-
Save MayogaX/834629 to your computer and use it in GitHub Desktop.
Esse é o primeiro exercicio com OPENGL na aula de programação 3D (começa com 2D) na universidade UMESP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.awt.Frame; | |
| import java.awt.event.WindowAdapter; | |
| import java.awt.event.WindowEvent; | |
| import javax.media.opengl.GL; | |
| import javax.media.opengl.GLAutoDrawable; | |
| import javax.media.opengl.GLCanvas; | |
| import javax.media.opengl.GLEventListener; | |
| import com.sun.opengl.util.Animator; | |
| /** | |
| * | |
| * @author MayogaX (Priscila Mayumi Sato) | |
| * | |
| * Esse é o primeiro exercicio com OpenGL | |
| * nas aulas da UMESP (aulas do prof Mauro Schineider) | |
| * | |
| * 2D Primitivas Gráficas com OpenGL | |
| * Cores em RGB http://www.colorschemer.com/online.html (normalizar em 255) | |
| * | |
| */ | |
| public class Aula01 implements GLEventListener { | |
| static int largura = 300; // width | |
| static int altura = 300; // heigth | |
| public void init(GLAutoDrawable gLDrawable) { | |
| GL gl = gLDrawable.getGL(); | |
| gl.glViewport(0, 0, largura, altura); | |
| gl.glOrtho(0, largura, 0, altura, -1, 1); | |
| } | |
| public void display(GLAutoDrawable gLDrawable) { | |
| GL gl = gLDrawable.getGL(); | |
| // Limpa a janela de visualização com a cor de fundo especificada | |
| gl.glClear(GL.GL_COLOR_BUFFER_BIT); | |
| //Quadro principal da casa | |
| gl.glBegin(gl.GL_QUADS); | |
| gl.glColor3f(1.0f, 1.0f, 0.0f); | |
| gl.glVertex2d(100, 20); | |
| gl.glVertex2d(200, 20); | |
| gl.glVertex2d(200, 115); | |
| gl.glVertex2d(100, 115); | |
| gl.glEnd(); | |
| //Quadrado puxado da casa | |
| gl.glBegin(gl.GL_QUADS); | |
| gl.glColor3f(1.0f, 1.0f, 0.0f); | |
| gl.glVertex2d(100, 20); | |
| gl.glVertex2d(100, 115); | |
| gl.glVertex2d(40, 135); | |
| gl.glVertex2d(40, 40); | |
| gl.glEnd(); | |
| //Quadrado puxado do telhado | |
| gl.glBegin(gl.GL_QUADS); | |
| gl.glColor3f(0.8f, 0.5f, 0.5f); | |
| gl.glVertex2d(100, 115); | |
| gl.glVertex2d(200, 115); | |
| gl.glVertex2d(180, 175); | |
| gl.glVertex2d(80, 175); | |
| gl.glEnd(); | |
| //Triangulo puxado do telhado | |
| gl.glBegin(gl.GL_TRIANGLES); | |
| gl.glColor3f(0.8f, 0.5f, 0.5f); | |
| gl.glVertex2d(100, 115); | |
| gl.glVertex2d(80, 175); | |
| gl.glVertex2d(40, 135); | |
| gl.glEnd(); | |
| //Quadro janela | |
| gl.glBegin(gl.GL_QUADS); | |
| gl.glColor3f(0.0f, 0.5f, 0.5f); | |
| gl.glVertex2d(140, 45); | |
| gl.glVertex2d(170, 45); | |
| gl.glVertex2d(170, 70); | |
| gl.glVertex2d(140, 70); | |
| gl.glEnd(); | |
| //Linha que cruza a janela (esquerda alta) | |
| gl.glBegin(GL.GL_LINES); | |
| gl.glColor3f(0.0f, 0.0f, 0.0f); | |
| gl.glVertex2i(170,45); | |
| gl.glVertex2i(140,70); | |
| gl.glEnd(); | |
| //Linha que cruza a janela (esquerda baixa) | |
| gl.glBegin(GL.GL_LINES); | |
| gl.glColor3f(0.0f, 0.0f, 0.0f); | |
| gl.glVertex2i(140,45); | |
| gl.glVertex2i(170,70); | |
| gl.glEnd(); | |
| //Quadrado puxado da casa | |
| gl.glBegin(gl.GL_QUADS); | |
| gl.glColor3f(0.0f, 0.0f, 1.0f); | |
| gl.glVertex2d(80, 27); | |
| gl.glVertex2d(80, 60); | |
| gl.glVertex2d(60, 65); | |
| gl.glVertex2d(60, 32); | |
| gl.glEnd(); | |
| gl.glFlush(); | |
| } | |
| // Vento de redimencionamento e reposicionamento | |
| public void displayChanged(GLAutoDrawable gLDrawable, boolean modeChanged, | |
| boolean deviceChanged) { | |
| } | |
| // Evento que indica modificação na configuracao de visualizacao | |
| public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height) { | |
| } | |
| public static void main(String[] args) { | |
| GLCanvas canvas = new GLCanvas(); | |
| Frame frame = new Frame("Primitivas Gráficas"); | |
| Animator animator = new Animator(canvas); | |
| canvas.addGLEventListener(new Aula02()); | |
| frame.add(canvas); | |
| frame.setSize(largura, altura); | |
| frame.addWindowListener(new WindowAdapter() { | |
| public void windowClosing(WindowEvent e) { | |
| System.exit(0); | |
| } | |
| }); | |
| frame.setVisible(true); | |
| animator.start(); | |
| canvas.requestFocus(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment