Advertisement
Guest User

D GL Sample

a guest
Aug 22nd, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 4.96 KB | None | 0 0
  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2005 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. *     IBM Corporation - initial API and implementation
  10. * Port to the D Programming Language:
  11. *     John Reimer <terminal.node@gmail.com>
  12. *******************************************************************************/
  13.  
  14. module org.eclipse.swt.snippets.Snippet195;
  15.  
  16. /*
  17. * SWT OpenGL snippet: based on snippet195.java
  18. *
  19. * For a list of all SWT example snippets see
  20. * http://www.eclipse.org/swt/snippets/
  21. *
  22. * @since 3.2
  23. */
  24.  
  25. import org.eclipse.swt.SWT;
  26.  
  27. import org.eclipse.swt.layout.FillLayout;
  28. import org.eclipse.swt.widgets.Shell;
  29. import org.eclipse.swt.widgets.Display;
  30. import org.eclipse.swt.widgets.Event;
  31. import org.eclipse.swt.widgets.Composite;
  32. import org.eclipse.swt.widgets.Listener;
  33. import org.eclipse.swt.graphics.Rectangle;
  34. import org.eclipse.swt.opengl.GLCanvas;
  35. import org.eclipse.swt.opengl.GLData;
  36.  
  37. import java.lang.all;
  38.  
  39. import derelict.opengl3.gl;
  40. import derelict.glfw3.glfw3;
  41.  
  42. import Math = std.math;
  43.  
  44.  
  45. void perspectiveGL( GLdouble fovY, GLdouble aspect, GLdouble zNear, GLdouble zFar )
  46. {
  47.     const GLdouble pi = 3.1415926535897932384626433832795;
  48.     GLdouble fW, fH;
  49.  
  50.     fH = Math.tan( fovY / 360 * pi ) * zNear;
  51.     fW = fH * aspect;
  52.  
  53.     glFrustum( -fW, fW, -fH, fH, zNear, zFar );
  54. }
  55.  
  56. void drawTorus(float r, float R, int nsides, int rings)
  57. {
  58.     float ringDelta = 2.0f * cast(float) Math.PI / rings;
  59.     float sideDelta = 2.0f * cast(float) Math.PI / nsides;
  60.     float theta = 0.0f, cosTheta = 1.0f, sinTheta = 0.0f;
  61.     for (int i = rings - 1; i >= 0; i--) {
  62.         float theta1 = theta + ringDelta;
  63.         float cosTheta1 = cast(float) Math.cos(theta1);
  64.         float sinTheta1 = cast(float) Math.sin(theta1);
  65.         glBegin(GL_QUAD_STRIP);
  66.         float phi = 0.0f;
  67.         for (int j = nsides; j >= 0; j--) {
  68.             phi += sideDelta;
  69.             float cosPhi = cast(float) Math.cos(phi);
  70.             float sinPhi = cast(float) Math.sin(phi);
  71.             float dist = R + r * cosPhi;
  72.             glNormal3f(cosTheta1 * cosPhi, -sinTheta1 * cosPhi, sinPhi);
  73.             glVertex3f(cosTheta1 * dist, -sinTheta1 * dist, r * sinPhi);
  74.             glNormal3f(cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi);
  75.             glVertex3f(cosTheta * dist, -sinTheta * dist, r * sinPhi);
  76.         }
  77.         glEnd();
  78.         theta = theta1;
  79.         cosTheta = cosTheta1;
  80.         sinTheta = sinTheta1;
  81.     }
  82. }
  83.  
  84. void main()
  85. {
  86.     DerelictGL.load();
  87.  
  88.     Display display = new Display();
  89.     Shell shell = new Shell(display);
  90.     shell.setLayout(new FillLayout());
  91.     Composite comp = new Composite(shell, SWT.NONE);
  92.     comp.setLayout(new FillLayout());
  93.     GLData data = new GLData ();
  94.     data.doubleBuffer = true;
  95.     GLCanvas canvas = new GLCanvas(comp, SWT.NONE, data);
  96.  
  97.     canvas.setCurrent();
  98.  
  99.     canvas.addListener(SWT.Resize, new class Listener {
  100.         public void handleEvent(Event event) {
  101.             Rectangle bounds = canvas.getBounds();
  102.             float fAspect = cast(float) bounds.width / cast(float) bounds.height;
  103.  
  104.             glViewport(0, 0, bounds.width, bounds.height);
  105.             glMatrixMode(GL_PROJECTION);
  106.             glLoadIdentity();
  107.             perspectiveGL(45.0f, fAspect, 0.5f, 400.0f);
  108.             glMatrixMode(GL_MODELVIEW);
  109.             glLoadIdentity();
  110.         }
  111.     });
  112.  
  113.     glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  114.     glColor3f(1.0f, 0.0f, 0.0f);
  115.     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  116.     glClearDepth(1.0);
  117.     glLineWidth(2);
  118.     glEnable(GL_DEPTH_TEST);
  119.  
  120.     shell.setText("SWT/DerelictGL Example");
  121.     shell.setSize(640, 480);
  122.     shell.open();
  123.  
  124.     display.asyncExec(new class Runnable {
  125.         int rot = 0;
  126.         public void run() {
  127.             if (!canvas.isDisposed()) {
  128.                 canvas.setCurrent();
  129.  
  130.                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  131.                 glClearColor(.3f, .5f, .8f, 1.0f);
  132.                 glLoadIdentity();
  133.                 glTranslatef(0.0f, 0.0f, -10.0f);
  134.                 float frot = rot;
  135.                 glRotatef(0.15f * rot, 2.0f * frot, 10.0f * frot, 1.0f);
  136.                 glRotatef(0.3f * rot, 3.0f * frot, 1.0f * frot, 1.0f);
  137.                 rot++;
  138.                 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  139.                 glColor3f(0.9f, 0.9f, 0.9f);
  140.                 drawTorus(1, 1.9f + (cast(float) Math.sin((0.004f * frot))), 15, 15);
  141.                 canvas.swapBuffers();
  142.                 display.asyncExec(this);
  143.             }
  144.         }
  145.     });
  146.  
  147.     while (!shell.isDisposed()) {
  148.         if (!display.readAndDispatch())
  149.             display.sleep();
  150.     }
  151.     display.dispose();
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement