Boas pessoal, estou a começar a trabalhar com openGL. Estas funcoes sao dadas, com pesquisas na internet, mas no entanto foi me solicitado para reformular as funções dadas de modo a que os cálculos sejam mais eficazes e redesenhar as funções do modo a que tenham as parametrizações adequadas para servirem como primitivas para a computação gráfica… Andei as voltas, mas não sei como simplificar isto ainda mais, porque influencia nas próprias funções. Obrigado desde já.
Aqui segue-se o código, apenas direccionado aos poligonos.
- contorno_cilindro(gl);
- contorno_cone(gl);
- contorno_praboloid(gl);
- contorno_sphere(gl);
- contorno_oval(gl);
public class Hilfe implements GLEventListener {
public static void main(String[] args) {
Frame frame = new Frame("Simple JOGL Application");
GLCanvas canvas = new GLCanvas();
canvas.addGLEventListener(new Hilfe());
frame.add(canvas);
frame.setSize(640, 480);
final Animator animator = new Animator(canvas);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// Run this on another thread than the AWT event queue to
// make sure the call to Animator.stop() completes before
// exiting
new Thread(new Runnable() {
public void run() {
animator.stop();
System.exit(0);
}
}).start();
}
});
// Center frame
frame.setLocationRelativeTo(null);
frame.setVisible(true);
animator.start();
}
public void init(GLAutoDrawable drawable) {
// Use debug pipeline
// drawable.setGL(new DebugGL(drawable.getGL()));
GL gl = drawable.getGL();
System.err.println("INIT GL IS: " + gl.getClass().getName());
// Enable VSync
gl.setSwapInterval(1);
// Setup the drawing area and shading mode
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glShadeModel(GL.GL_SMOOTH); // try setting this to GL_FLAT and see what happens.
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL gl = drawable.getGL();
GLU glu = new GLU();
if (height <= 0) { // avoid a divide by zero error!
height = 1;
}
final float h = (float) width / (float) height;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, h, 1.0, 20.0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void display(GLAutoDrawable drawable)
{GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(-1.5f, 0.0f, -6.0f);
//contorno_cilindro(gl);
//contorno_cone(gl);
contorno_praboloid(gl);
//contorno_sphere(gl);
//contorno_oval(gl);
// int max=100;
//for (int n=0;n<max;++n)
// pontos_circulo(gl,10000,n*(float)(1)/max);
gl.glFlush();
}
public void triangulo(GL gl) {
gl.glBegin(GL.GL_TRIANGLES);
gl.glColor3f(1.0f, 0.0f, 0.0f); // Set the current drawing color to red
gl.glVertex3f(0.0f, 1.0f, 0.0f); // Top
gl.glColor3f(0.0f, 1.0f, 0.0f); // Set the current drawing color to green
gl.glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
gl.glColor3f(0.0f, 0.0f, 1.0f); // Set the current drawing color to blue
gl.glVertex3f(1.0f, -1.0f, 0.0f); // Bottom Right
// Finished Drawing The Triangle
gl.glEnd();
}
public void quadrado(GL gl) {
gl.glBegin(GL.GL_QUADS);
gl.glColor3f(0.5f, 0.5f, 1.0f); // Set the current drawing color to light blue
gl.glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
gl.glVertex3f(1.0f, 1.0f, 0.0f); // Top Right
gl.glVertex3f(1.0f, -1.0f, 0.0f); // Bottom Right
gl.glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
// Done Drawing The Quad
gl.glEnd();
}
private void polygon_circulo(GL gl,int n)
{
gl.glBegin(GL.GL_POLYGON);
for (int i=0;i<n;++i)
{gl.glColor3f(1.0f-(float)(i)/n,(float)(i)/n, 1.0f);
gl.glVertex3f((float)(Math.sin((Double)(i*6.283/n))),(float)(Math.cos((Double)(i*6.283/n))), 0.0f);
}
gl.glEnd();
}
private void contorno_circulo(GL gl,int n)
{
gl.glBegin(GL.GL_POINTS);
for (int i=0;i<n;++i)
{gl.glColor3f(1.0f-(float)(i)/n,(float)(i)/n, 1.0f);
gl.glVertex3f((float)(Math.sin((Double)(i*6.283/n))),(float)(Math.cos((Double)(i*6.283/n))), 0.0f);
gl.glVertex3f((float)(Math.sin((Double)((i+1)*6.283/n))),(float)(Math.cos((Double)((i+1)*6.283/n))), 0.0f);
}
gl.glEnd();
}
private void pontos_circulo(GL gl,int n,float r)
{
gl.glBegin(GL.GL_POINTS);
for (int i=0;i<n;++i)
{gl.glColor3f(1.0f-(float)(i)/n,(float)(i)/n, 1.0f);
gl.glVertex3f(r*(float)(Math.sin((Double)(i*6.283/n))),r*(float)(Math.cos((Double)(i*6.283/n))), 0.0f);
gl.glVertex3f(r*(float)(Math.sin((Double)((i+1)*6.283/n))),r*(float)(Math.cos((Double)((i+1)*6.283/n))), 0.0f);
}
gl.glEnd();
}
private void contorno_sphere(GL gl)
{
gl.glBegin(GL.GL_POINTS);
int n=1000; int m=1000;
for (int j=0;j<m;++j)
for (int i=0;i<n;++i)
{gl.glColor3f(1.0f,(float)(j)/m, 1.0f);
gl.glVertex3f((float)(Math.sin((Double)((float)(i)/n*3.1415))
*Math.cos((Double)((float)(j)/m*6.283))),
(float)(Math.cos((Double)((float)(i)/n*3.1415))),
(float)(Math.sin((Double)((float)(i)/n*3.1415))
*Math.sin((Double)((float)(j)/m*6.283))));
// /(float)(Math.cos((Double)(j*6.283/m)))
}
gl.glEnd();
}
private void contorno_oval(GL gl)
{ int n=700; int m=700;
gl.glBegin(GL.GL_POINTS);
for (int j=0;j<m;++j)
for (int i=0;i<n;++i)
{gl.glColor3f(1.0f, 1.0f,(float)(j)/m); //
gl.glVertex3f(1.5f*(float)(Math.sin((Double)((float)(i)/n*3.1415))
*Math.cos((Double)((float)(j)/m*6.283))),
2*(float)(Math.cos((Double)((float)(i)/n*3.1415))),
1.5f*(float)(Math.sin((Double)((float)(i)/n*3.1415))
*Math.sin((Double)((float)(j)/m*6.283))));
}
gl.glEnd();
}
private void contorno_praboloid(GL gl)
{
gl.glBegin(GL.GL_POINTS);
int n=1000; int m=1000;
for (int j=0;j<m;++j)
for (int i=0;i<n;++i)
{gl.glColor3f(1.0f,(float)(j)/m, 1.0f);
gl.glVertex3f((float)(i)/n*(float)(Math.cos((Double)((float)(j)/m*6.283))),1.5f*(float)(i*i)/(n*n),(float)(i)/n*(float)(Math.sin(6.283*j/m)));
// /(float)(Math.cos((Double)(j*6.283/m)))
}
gl.glEnd();
}
private void contorno_cone(GL gl)
{
gl.glBegin(GL.GL_POINTS);
int n=1000; int m=1000;
for (int j=0;j<m;++j)
for (int i=0;i<n;++i)
{gl.glColor3f(1.0f,(float)(j)/m, 1.0f);
gl.glVertex3f((float)(i)/n*(float)(Math.cos((Double)((float)(j)/m*6.283))),
1.5f*(float)(i)/n,
(float)(i)/n*(float)(Math.sin(6.283*j/m)));
// /(float)(Math.cos((Double)(j*6.283/m)))
}
gl.glEnd();
}
private void contorno_cilindro(GL gl)
{
gl.glBegin(GL.GL_POINTS);
int n=1000; int m=1000;
for (int j=0;j<m;++j)
for (int i=0;i<n;++i)
{gl.glColor3f(1.0f,(float)(j)/m, 1.0f);
gl.glVertex3f(0.2f*(float)(Math.cos((Double)((float)(j)/m*6.283))),(float)(i)/n,0.2f*(float)(Math.sin(6.283*j/m)));
// /(float)(Math.cos((Double)(j*6.283/m)))
}
gl.glEnd();
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
}
}