Guest User

VBO trouble

a guest
Oct 6th, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. package RenderingModes;
  2. import org.lwjgl.BufferUtils;
  3. import org.lwjgl.LWJGLException;
  4. import org.lwjgl.input.Keyboard;
  5. import org.lwjgl.opengl.Display;
  6. import org.lwjgl.opengl.DisplayMode;
  7. import org.lwjgl.opengl.GL11;
  8. import org.lwjgl.opengl.GL12;
  9. import org.newdawn.slick.opengl.Texture;
  10. import org.newdawn.slick.opengl.TextureLoader;
  11. import org.newdawn.slick.util.ResourceLoader;
  12.  
  13. import java.awt.image.BufferedImage;
  14. import java.io.File;
  15. import java.io.FileInputStream;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.io.ObjectInputStream.GetField;
  19. import java.nio.ByteBuffer;
  20. import java.nio.FloatBuffer;
  21.  
  22. import javax.imageio.ImageIO;
  23.  
  24. import static org.lwjgl.opengl.GL11.*;
  25. import static org.lwjgl.opengl.GL15.*;
  26.  
  27. /**
  28. * Renders a colored triangle using Vertex Buffer Objects
  29. *
  30. * @author Oskar
  31. */
  32. public class VertexBufferObjectDemo {
  33.  
  34.  
  35. private static ByteBuffer dataFromImage(BufferedImage source){
  36. int[] pixels = new int[source.getWidth() * source.getHeight()];
  37. source.getRGB(0, 0, source.getWidth(), source.getHeight(), pixels, 0, source.getWidth());
  38. ByteBuffer buffer = BufferUtils.createByteBuffer(source.getWidth() * source.getHeight() * 4);
  39.  
  40. for (int y = 0; y < source.getHeight(); y++) {
  41. for (int x = 0; x < source.getWidth(); x++) {
  42. int pixel = pixels[y * source.getWidth() + x];
  43. buffer.put((byte) ((pixel >> 16) & 0xFF)); // Red component
  44. buffer.put((byte) ((pixel >> 8) & 0xFF)); // Green component
  45. buffer.put((byte) (pixel & 0xFF)); // Blue component
  46. buffer.put((byte) ((pixel >> 24) & 0xFF)); // Alpha component.
  47. }
  48. }buffer.flip();
  49.  
  50. return buffer;
  51. }
  52.  
  53. public static void main(String[] args) {
  54. try {
  55. Display.setDisplayMode(new DisplayMode(640, 480));
  56. Display.setTitle("Vertex Buffer Object Demo");
  57. Display.create();
  58. } catch (LWJGLException e) {
  59. e.printStackTrace();
  60. Display.destroy();
  61. System.exit(1);
  62. }
  63.  
  64. glMatrixMode(GL_PROJECTION);
  65. glLoadIdentity();
  66. glOrtho(1, -1, 1, -1, 1, -1);
  67. glMatrixMode(GL_MODELVIEW);
  68. glLoadIdentity();
  69.  
  70. final int amountOfVertices = 6;
  71. final int vertexSize = 3;
  72. final int colorSize = 3;
  73.  
  74. FloatBuffer vertexData = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize);
  75. vertexData.put(new float[]{ -0.5f, 0.5f, 0,//LEFT BOTTOM
  76. -0.5f, -0.5f, 0,
  77. 0.5f, -0.5f, 0,
  78.  
  79. 0.5f, -0.5f, 0,//RIGHT TOP
  80. 0.5f, 0.5f, 0,
  81. -0.5f, 0.5f, 0,
  82. });
  83. vertexData.flip();
  84.  
  85. FloatBuffer colorData = BufferUtils.createFloatBuffer(amountOfVertices * colorSize);
  86. colorData.put(new float[]{1, 1, 1, 1, 1, 1, 1, 1, 1, /**/1, 1, 1, 1, 1, 1, 1, 1, 1});
  87. colorData.flip();
  88.  
  89. FloatBuffer textureUVData = BufferUtils.createFloatBuffer(amountOfVertices * 2); // amount of vertices * two texture coordinates per-vertex
  90. textureUVData.put(new float[]{0,1 , 0,0 , 1,0 , 1,1 /**/, 0,1 , 0,0 });
  91. textureUVData.flip();
  92.  
  93.  
  94.  
  95. int vboVertexHandle = glGenBuffers();
  96. glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
  97. glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
  98. glBindBuffer(GL_ARRAY_BUFFER, 0);
  99.  
  100. int vboColorHandle = glGenBuffers();
  101. glBindBuffer(GL_ARRAY_BUFFER, vboColorHandle);
  102. glBufferData(GL_ARRAY_BUFFER, colorData, GL_STATIC_DRAW);
  103. glBindBuffer(GL_ARRAY_BUFFER, 0);
  104.  
  105.  
  106. String path = "/ufo.png";
  107. InputStream in1 = VertexBufferObjectDemo.class.getResourceAsStream(path);
  108.  
  109.  
  110. BufferedImage image = null;
  111. try {
  112. image = ImageIO.read(in1);
  113. } catch (IOException e) {
  114. // TODO Auto-generated catch block
  115. e.printStackTrace();
  116. }
  117.  
  118. //During setup ONLY!!!
  119. int myTextureID = glGenTextures();
  120. glBindTexture(GL_TEXTURE_2D, myTextureID);
  121. GL11.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, dataFromImage(image));
  122.  
  123. int vboTextureUVHandle = glGenBuffers();
  124. glBindBuffer(GL_ARRAY_BUFFER, vboTextureUVHandle);
  125. glBufferData(GL_ARRAY_BUFFER, textureUVData, GL_DYNAMIC_DRAW);
  126. glBindBuffer(GL_ARRAY_BUFFER, 0);
  127.  
  128.  
  129. while (!Display.isCloseRequested()) {
  130. glClear(GL_COLOR_BUFFER_BIT);
  131.  
  132. vertexData.clear();
  133.  
  134. //Specifies how the texture will be handled when out - of - range
  135. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
  136. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
  137.  
  138. //Specifies how the texture should interpolate scaling (GL_NEAREST for nearest-neighbour, and GL11.GL_LINEAR for blurry stuffs)
  139. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
  140. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
  141.  
  142.  
  143. GL11.glEnable(GL11.GL_TEXTURE_2D);
  144.  
  145. glBindTexture(GL_TEXTURE_2D, myTextureID);
  146. GL11.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, dataFromImage(image));
  147.  
  148. glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
  149. glVertexPointer(vertexSize, GL_FLOAT, 0, 0L);
  150.  
  151. glBindBuffer(GL_ARRAY_BUFFER, vboColorHandle);
  152. glColorPointer(colorSize, GL_FLOAT, 0, 0L);
  153.  
  154. glBindBuffer(GL_ARRAY_BUFFER, vboTextureUVHandle);
  155. // 2 Because we have 2 texture coordinates per-vertex
  156. glTexCoordPointer(2, GL_FLOAT, 0, 0L);
  157.  
  158. glEnableClientState(GL_VERTEX_ARRAY);
  159. glEnableClientState(GL_COLOR_ARRAY);
  160. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  161. glBindTexture(GL_TEXTURE_2D, myTextureID);
  162. glDrawArrays(GL_TRIANGLES, 0, amountOfVertices);
  163. glDisableClientState(GL_COLOR_ARRAY);
  164. glDisableClientState(GL_VERTEX_ARRAY);
  165. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  166.  
  167. Display.update();
  168. Display.sync(60);
  169. }
  170.  
  171. glDeleteBuffers(vboVertexHandle);
  172. glDeleteBuffers(vboColorHandle);
  173. glDeleteBuffers(vboTextureUVHandle);
  174.  
  175. Display.destroy();
  176. System.exit(0);
  177. }
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment