Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.91 KB | None | 0 0
  1. package openglzadanie;
  2. import java.awt.*;
  3.  
  4. import javax.swing.*;
  5. import java.awt.event.*;
  6. import com.jogamp.opengl.*;
  7. import com.jogamp.opengl.awt.*;
  8. import com.jogamp.opengl.util.gl2.GLUT;
  9. import java.awt.*;
  10. import java.awt.event.*;
  11. import javax.swing.*;
  12. import javax.imageio.ImageIO;
  13. import java.awt.image.BufferedImage;
  14. import java.io.IOException;
  15.  
  16. import com.jogamp.opengl.GL2;
  17. import com.jogamp.opengl.GL4;
  18. import com.jogamp.opengl.GLAutoDrawable;
  19. import com.jogamp.opengl.GLCapabilities;
  20. import com.jogamp.opengl.GLEventListener;
  21. import com.jogamp.opengl.GLProfile;
  22. import com.jogamp.opengl.awt.GLCanvas;
  23. import com.jogamp.opengl.glu.GLU;
  24. import com.jogamp.opengl.util.FPSAnimator;
  25.  
  26. import javax.swing.JFrame;
  27.  
  28. import java.lang.Math;
  29.  
  30.  
  31. /**
  32. * CPSC 424, Fall 2015, Lab 4: Some objects in 3D. The arrow keys
  33. * can be used to rotate the object. The number keys 1 through 6
  34. * select the object. The space bar toggles the use of anaglyph
  35. * stereo.
  36. */
  37. public class Lab5 extends GLJPanel implements GLEventListener, KeyListener{
  38.  
  39. /**
  40. *
  41. */
  42. private static final long serialVersionUID = 6394428038266948206L;
  43.  
  44.  
  45. /**
  46. * A main routine to create and show a window that contains a
  47. * panel of type Lab5. The program ends when the user closes the
  48. * window.
  49. */
  50. public static void main(String[] args) {
  51. JFrame window = new JFrame("Some Objects in 3D");
  52. Lab5 panel = new Lab5();
  53. window.setContentPane(panel);
  54. window.pack();
  55. window.setResizable(false);
  56. window.setLocation(50,50);
  57. window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  58. window.setVisible(true);
  59. }
  60.  
  61. /**
  62. * Constructor for class Lab4.
  63. */
  64. public Lab5() {
  65. super( new GLCapabilities(null) ); // Makes a panel with default OpenGL "capabilities".
  66. setPreferredSize( new Dimension(700,700) );
  67. addGLEventListener(this); // This panel will respond to OpenGL events.
  68. addKeyListener(this); // The panel will respond to key events.
  69. }
  70.  
  71.  
  72. //------------------- TODO: Complete this section! ---------------------
  73.  
  74. private int objectNumber = 1; // Which object to draw (1 ,2, 3, 4, 5, or 6)?
  75. // (Controlled by number keys.)
  76.  
  77. private boolean useAnaglyph = false; // Should anaglyph stereo be used?
  78. // (Controlled by space bar.)
  79.  
  80. private int rotateX = 0; // Rotations of the cube about the axes.
  81. private int rotateY = 0; // (Controlled by arrow, PageUp, PageDown keys;
  82. private int rotateZ = 0; // Home key sets all rotations to 0.)
  83. double ang, x, y, z = -2;
  84.  
  85. private GLUT glut = new GLUT(); // An object for drawing GLUT shapes.
  86.  
  87.  
  88. /**
  89. * The method that draws the current object, with its modeling transformation.
  90. */
  91. private void draw(GL2 gl2) {
  92.  
  93. gl2.glRotatef(rotateZ,0,0,1); // Apply rotations to complete object.
  94. gl2.glRotatef(rotateY,0,1,0);
  95. gl2.glRotatef(rotateX,1,0,0);
  96.  
  97. if(objectNumber==2)
  98. {
  99. gl2.glBegin(GL.GL_TRIANGLES);
  100. gl2.glVertex2d(-0.7, -0.5);
  101. gl2.glVertex2d(0.7, -0.5);
  102. gl2.glVertex2d(0, -0.7);
  103. gl2.glEnd();
  104. }
  105. else if(objectNumber==3)
  106. {
  107. gl2.glBegin( GL2.GL_TRIANGLES );
  108.  
  109. // przod
  110. gl2.glColor3f( 1.0f, 0.0f, 0.0f );
  111. gl2.glVertex3f( 1.0f, 2.0f, 0.0f );
  112. gl2.glVertex3f( -1.0f, -1.0f, 1.0f );
  113. gl2.glVertex3f( 1.0f, -1.0f, 1.0f );
  114.  
  115. // prawy bok
  116. gl2.glColor3f( 0.0f, 1.0f, 0.0f );
  117. gl2.glVertex3f( 1.0f, 2.0f, 0.0f );
  118. gl2.glVertex3f( 1.0f, -1.0f, 1.0f );
  119. gl2.glVertex3f( 1.0f, -1.0f, -1.0f );
  120.  
  121. // tyl
  122. gl2.glColor3f( 0.0f, 0.0f, 1.0f );
  123. gl2.glVertex3f( 1.0f, 2.0f, 0.0f );
  124. gl2.glVertex3f( 1.0f, -1.0f, -1.0f );
  125. gl2.glVertex3f( -1.0f, -1.0f, -1.0f );
  126.  
  127. //lewy bok
  128. gl2.glColor3f( 1.0f, 1.0f, 0.0f );
  129. gl2.glVertex3f( 1.0f, 2.0f, 0.0f );
  130. gl2.glVertex3f( -1.0f, -1.0f, -1.0f );
  131. gl2.glVertex3f( -1.0f, -1.0f, 1.0f );
  132.  
  133. gl2.glEnd();
  134. }
  135. else if(objectNumber==4) //rysowanie spirali
  136. {
  137.  
  138. gl2.glLineWidth(3);
  139. gl2.glBegin(GL2.GL_LINE_STRIP);
  140.  
  141. float x,z;
  142. float y = -10; //poczatkowa wysokosc
  143. float angle;
  144. int numberOfSpirals = 6;
  145. float distance = 0.2f; //od nastepnego punktu
  146.  
  147.  
  148. for (angle = 0.0f; angle<= (java.lang.Math.PI * 2.16f * numberOfSpirals); angle += distance) {
  149. x = (float) Math.sin(angle) * 5;
  150. z = (float) Math.cos(angle) * 5;
  151. gl2.glColor3f(0.0f, 0.0f, 1.0f);
  152. gl2.glVertex3f(x, y, z);
  153. y += 0.05;
  154. }
  155. gl2.glEnd();
  156. }
  157. else if(objectNumber==5) //rysowanie n-bokowej piramidy
  158. {
  159.  
  160. gl2.glLineWidth(3);
  161. gl2.glBegin(GL2.GL_TRIANGLES);
  162.  
  163. float x,z;
  164. float y = -5; //wysokosc
  165. int angle;
  166. int boki = 7;
  167. int rozmiar =5;
  168.  
  169.  
  170.  
  171. for (angle = 0; angle< boki*2; angle += 1) { //boki
  172. x = (float) Math.sin(angle * 2 * Math.PI / boki)*rozmiar;
  173. z = (float) Math.cos(angle * 2 * Math.PI / boki)*rozmiar;
  174. gl2.glColor3f(1.0f, 0.0f, 0.0f);
  175. gl2.glVertex3f(x, y, z);
  176. if(angle%2==0) {
  177. gl2.glVertex3f(0, 0, 0);}
  178. }
  179. gl2.glEnd();
  180. gl2.glBegin(GL2.GL_TRIANGLES);
  181.  
  182. for (angle = 0; angle<= boki*2; angle += 1) {// podstawa
  183. x = (float) Math.sin(angle * 2 * Math.PI / boki)*rozmiar;
  184. z = (float) Math.cos(angle * 2 * Math.PI / boki)*rozmiar;
  185. gl2.glColor3f(1.0f, 1.0f, 0.0f);
  186. gl2.glVertex3f(x, y, z);
  187. if(angle%2==0) {
  188. gl2.glVertex3f(0, y, 0);}
  189.  
  190. }
  191.  
  192. gl2.glEnd();
  193.  
  194. }
  195.  
  196.  
  197. // TODO: Draw the currently selected object, number 1, 2, 3, 4, 5, or 6.
  198. // (Objects should lie in the cube with x, y, and z coordinates in the
  199. // range -5 to 5.)
  200.  
  201.  
  202. }
  203.  
  204. //-------------------- Draw the Scene -------------------------
  205.  
  206. /**
  207. * The display method is called when the panel needs to be drawn.
  208. * It's called when the window opens and it is called by the keyPressed
  209. * method when the user hits a key that modifies the scene.
  210. */
  211. public void display(GLAutoDrawable drawable) {
  212.  
  213. GL2 gl2 = drawable.getGL().getGL2(); // The object that contains all the OpenGL methods.
  214.  
  215. if (useAnaglyph) {
  216. gl2.glDisable(GL2.GL_COLOR_MATERIAL); // in anaglyph mode, everything is drawn in white
  217. gl2.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_AMBIENT_AND_DIFFUSE, new float[]{1,1,1,1}, 0);
  218. }
  219. else {
  220. gl2.glEnable(GL2.GL_COLOR_MATERIAL); // in non-anaglyph mode, glColor* is respected
  221. }
  222. gl2.glNormal3f(0,0,1); // (Make sure normal vector is correct for object 1.)
  223.  
  224. gl2.glClearColor( 0, 0, 0, 1 ); // Background color (black).
  225. gl2.glClear( GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT );
  226.  
  227.  
  228. if (useAnaglyph == false) {
  229. gl2.glLoadIdentity(); // Make sure we start with no transformation!
  230. gl2.glTranslated(0,0,-15); // Move object away from viewer (at (0,0,0)).
  231. draw(gl2);
  232. }
  233. else {
  234. gl2.glLoadIdentity(); // Make sure we start with no transformation!
  235. gl2.glColorMask(true, false, false, true);
  236. gl2.glRotatef(4,0,1,0);
  237. gl2.glTranslated(1,0,-15);
  238. draw(gl2); // draw the current object!
  239. gl2.glColorMask(true, false, false, true);
  240. gl2.glClear(GL2.GL_DEPTH_BUFFER_BIT);
  241. gl2.glLoadIdentity();
  242. gl2.glRotatef(-4,0,1,0);
  243. gl2.glTranslated(-1,0,-15);
  244. gl2.glColorMask(false, true, true, true);
  245. draw(gl2);
  246. gl2.glColorMask(true, true, true, true);
  247. }
  248.  
  249. } // end display()
  250.  
  251. /** The init method is called once, before the window is opened, to initialize
  252. * OpenGL. Here, it sets up a projection, turns on some lighting, and enables
  253. * the depth test.
  254. */
  255. public void init(GLAutoDrawable drawable) {
  256. GL2 gl2 = drawable.getGL().getGL2();
  257. gl2.glMatrixMode(GL2.GL_PROJECTION);
  258. gl2.glFrustum(-3.5, 3.5, -3.5, 3.5, 5, 25);
  259. gl2.glMatrixMode(GL2.GL_MODELVIEW);
  260. gl2.glEnable(GL2.GL_LIGHTING);
  261. gl2.glEnable(GL2.GL_LIGHT0);
  262. gl2.glLightfv(GL2.GL_LIGHT0,GL2.GL_DIFFUSE,new float[] {0.7f,0.7f,0.7f},0);
  263. gl2.glLightModeli(GL2.GL_LIGHT_MODEL_TWO_SIDE, 1);
  264. gl2.glEnable(GL2.GL_DEPTH_TEST);
  265. gl2.glLineWidth(3); // make wide lines for the stellated dodecahedron.
  266. }
  267.  
  268. public void dispose(GLAutoDrawable drawable) {
  269. // called when the panel is being disposed
  270. }
  271.  
  272. public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
  273. // called when user resizes the window
  274. }
  275.  
  276. // ---------------- Methods from the KeyListener interface --------------
  277.  
  278. /**
  279. * Responds to keypressed events. The four arrow keys control the rotations
  280. * about the x- and y-axes. The PageUp and PageDown keys control the rotation
  281. * about the z-axis. The Home key resets all rotations to zero. The number
  282. * keys 1, 2, 3, 4, 5, and 6 select the current object number. Pressing the space
  283. * bar toggles anaglyph stereo on and off. The panel is redrawn to reflect the
  284. * change.
  285. */
  286. public void keyPressed(KeyEvent evt) {
  287. int key = evt.getKeyCode();
  288. boolean repaint = true;
  289. if ( key == KeyEvent.VK_LEFT )
  290. rotateY -= 6;
  291. else if ( key == KeyEvent.VK_RIGHT )
  292. rotateY += 6;
  293. else if ( key == KeyEvent.VK_DOWN)
  294. rotateX += 6;
  295. else if ( key == KeyEvent.VK_UP )
  296. rotateX -= 6;
  297. else if ( key == KeyEvent.VK_PAGE_UP )
  298. rotateZ += 6;
  299. else if ( key == KeyEvent.VK_PAGE_DOWN )
  300. rotateZ -= 6;
  301. else if ( key == KeyEvent.VK_HOME )
  302. rotateX = rotateY = rotateZ = 0;
  303. else if (key == KeyEvent.VK_1)
  304. objectNumber = 1;
  305. else if (key == KeyEvent.VK_2)
  306. objectNumber = 2;
  307. else if (key == KeyEvent.VK_3)
  308. objectNumber = 3;
  309. else if (key == KeyEvent.VK_4)
  310. objectNumber = 4;
  311. else if (key == KeyEvent.VK_5)
  312. objectNumber = 5;
  313. else if (key == KeyEvent.VK_6)
  314. objectNumber = 6;
  315. else if (key == KeyEvent.VK_SPACE)
  316. useAnaglyph = ! useAnaglyph;
  317. else
  318. repaint = false;
  319. if (repaint)
  320. repaint();
  321. }
  322.  
  323. public void keyReleased(KeyEvent evt) {
  324. }
  325.  
  326. public void keyTyped(KeyEvent evt) {
  327. }
  328.  
  329. } // end class Lab4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement