Guest User

3dTestWithOBJ

a guest
Jan 3rd, 2011
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. import processing.opengl.*;
  2. import stereo.*;
  3. import saito.objloader.*;
  4. import javax.media.opengl.*;
  5.  
  6. // declare that we need a OBJModel and we'll be calling it "model"
  7. OBJModel model;
  8. float rotX;
  9. float rotY;
  10. Stereo stereo = null;
  11.  
  12. // these booleans will be used to turn on and off bits of the OBJModel
  13. boolean bTexture = true;
  14. boolean bStroke = false;
  15. boolean bMaterial = true;
  16.  
  17. float cx = 0f; float cy = 0f; float cz = 10f;
  18.  
  19.  
  20. void setup()
  21. {
  22. size(600, 600, "stereo.PassiveStereoView");
  23. float eyeSep = (float) (999.999 / 30f); //?????
  24. eyeSep = .1f;
  25. frameRate(60);
  26.  
  27. stereo = new Stereo(this, eyeSep, 45f, .1f, 1000f, Stereo.StereoType.ANAGLYPH_REDLEFT_CYANRIGHT);
  28.  
  29. // making an object called "model" that is a new instance of OBJModel
  30. model = new OBJModel(this, "cassini.obj", "relative", TRIANGLES);
  31.  
  32. // turning on the debug output (it's all the stuff that spews out in the black box down the bottom)
  33. model.enableDebug();
  34.  
  35. model.scale(8);
  36.  
  37. noStroke();
  38. }
  39.  
  40. void draw()
  41. {
  42.  
  43. background(32);
  44. lights();
  45.  
  46. //this will do nothing until the model material is turned off
  47. fill(255,0,255);
  48. PassiveStereoView pgl = (PassiveStereoView) g;
  49.  
  50. GL gl = pgl.beginGL();
  51. {
  52. // only needs to be called repeatedly if you are
  53. // changing camera position
  54. stereo.start(gl,
  55. cx, cy, cz,
  56. 0f, 0f, -1f,
  57. 0f, 1f, 0f);
  58.  
  59. stereo.right(gl); // right eye rendering
  60. pushMatrix();
  61. translate(width/2, height/2, 0);
  62. rotateX(rotY);
  63. rotateY(rotX);
  64.  
  65. render(gl);
  66.  
  67. popMatrix();
  68.  
  69. stereo.left(gl); // left eye rendering
  70. pushMatrix();
  71. translate(width/2, height/2, 0);
  72. rotateX(rotY);
  73. rotateY(rotX);
  74.  
  75. render(gl);
  76.  
  77. popMatrix();
  78.  
  79. // only needed for anaglyph
  80. stereo.end(gl);
  81. }
  82. pgl.endGL();
  83. }
  84.  
  85. void render(GL gl)
  86. {
  87. gl.glColor4f(1f,0f,1f,1f);
  88.  
  89. gl.glTranslatef(0f,0f,-10f);
  90. model.draw();
  91. }
  92.  
  93.  
  94. void keyPressed() {
  95. // turns on and off the texture listed in .mtl file
  96. if(key == 't') {
  97. if(!bTexture) {
  98. model.enableTexture();
  99. bTexture = true;
  100. }
  101. else {
  102. model.disableTexture();
  103. bTexture = false;
  104. }
  105. }
  106.  
  107. else if(key == 'm') {
  108. // turns on and off the material listed in .mtl file
  109. if(!bMaterial) {
  110. model.enableMaterial();
  111. bMaterial = true;
  112. }
  113. else {
  114. model.disableMaterial();
  115. bMaterial = false;
  116. }
  117. }
  118.  
  119. else if(key == 's') {
  120. if(!bStroke) {
  121. stroke(10, 10, 10);
  122. bStroke = true;
  123. }
  124. else {
  125. noStroke();
  126. bStroke = false;
  127. }
  128. }
  129.  
  130. // the follwing changes the render modes
  131. // POINTS mode is a little flakey in OPENGL (known processing bug)
  132. // the best one to use is the one you exported the obj as
  133. // when in doubt try TRIANGLES or POLYGON
  134. else if(key=='1') {
  135. stroke(10, 10, 10);
  136. bStroke = true;
  137. model.shapeMode(POINTS);
  138. }
  139.  
  140. else if(key=='2') {
  141. stroke(10, 10, 10);
  142. bStroke = true;
  143. model.shapeMode(LINES);
  144. }
  145.  
  146. else if(key=='3') {
  147. model.shapeMode(TRIANGLES);
  148. }
  149.  
  150. else if(key=='4') {
  151. model.shapeMode(POLYGON);
  152. }
  153.  
  154. else if(key=='5') {
  155. model.shapeMode(TRIANGLE_STRIP);
  156. }
  157.  
  158. else if(key=='6') {
  159. model.shapeMode(QUADS);
  160. }
  161.  
  162. else if(key=='7') {
  163. model.shapeMode(QUAD_STRIP);
  164. }
  165.  
  166. if (key == 'a')
  167. {
  168. cz += .1f;
  169. }
  170.  
  171. if (key == 'z')
  172. {
  173. cz -= .1f;
  174. }
  175.  
  176. if(keyCode == LEFT) stereo.eyeSeperation -= .01;
  177. if(keyCode == RIGHT) stereo.eyeSeperation += .01;
  178. }
  179.  
  180. void mouseDragged()
  181. {
  182. rotX += (mouseX - pmouseX) * 0.01;
  183. rotY -= (mouseY - pmouseY) * 0.01;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment