XUPWUP.NL
Login
Or create an account

LWJGL glVertex etc. to vertex buffer

0 Posted 06 Jan 2014
User avatar
Rick Hendricksen
Administrator
Posts: 2251
  1. package nl.xupwup.Util;
  2. import java.nio.ByteBuffer;
  3. import java.util.ArrayList;
  4. import org.lwjgl.BufferUtils;
  5. import static org.lwjgl.opengl.GL11.*;
  6. import static org.lwjgl.opengl.GL15.*;
  7. /**
  8. *
  9. * @author Rick Hendricksen
  10. */
  11. public class GeometryObject {
  12. private Color currentColor = null;
  13. private boolean alpha = false;
  14. private Vec3d currentNormal = null;
  15. private Vec2d currentTexCoord = null;
  16. private static final int sizeofFloat = 4;
  17. private ArrayList<Vertex> vertices;
  18. private int nvertices = -1;
  19. private int vbo = -1;
  20. private int sizePerVertex = -1;
  21. public GeometryObject(){
  22. vertices = new ArrayList<>();
  23. }
  24. @Override
  25. protected void finalize() throws Throwable {
  26. super.finalize();
  27. if(vbo != -1){
  28. throw new Exception("Geometry object was not cleaned up properly.");
  29. }
  30. }
  31. public void destroy(){
  32. if(sizePerVertex == -1){
  33. throw new IllegalStateException("Nothing to destroy!");
  34. }
  35. glDeleteBuffers(vbo);
  36. vbo = -1;
  37. sizePerVertex = -1;
  38. vertices = null;
  39. }
  40. /**
  41. * Puts the collected vertices in a VBO.
  42. */
  43. public void finish(){
  44. sizePerVertex = 3;
  45. if(currentColor != null){
  46. sizePerVertex += alpha ? 4 : 3;
  47. }
  48. if(currentNormal != null){
  49. sizePerVertex += 3;
  50. }
  51. if(currentTexCoord != null){
  52. sizePerVertex += 2;
  53. }
  54. nvertices = vertices.size();
  55. ByteBuffer buffer = BufferUtils.createByteBuffer(nvertices * sizeofFloat * sizePerVertex);
  56. for (int i = 0; i < vertices.size(); i++) {
  57. Vertex v = vertices.get(i);
  58. buffer.putFloat((float) v.vtx.x).putFloat((float) v.vtx.y).putFloat((float) v.vtx.z);
  59. if(currentColor != null){
  60. if(alpha){
  61. buffer.putFloat((float) v.color.r).putFloat((float) v.color.g).putFloat((float) v.color.b).putFloat((float) v.color.a);
  62. }else{
  63. buffer.putFloat((float) v.color.r).putFloat((float) v.color.g).putFloat((float) v.color.b);
  64. }
  65. }
  66. if(currentNormal != null){
  67. buffer.putFloat((float) v.normal.x).putFloat((float) v.normal.y).putFloat((float) v.normal.z);
  68. }
  69. if(currentTexCoord != null){
  70. buffer.putFloat((float) v.texcoord.x).putFloat((float) v.texcoord.y);
  71. }
  72. }
  73. buffer.flip();
  74. vbo = glGenBuffers();
  75. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  76. glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
  77. glBindBuffer(GL_ARRAY_BUFFER, 0);
  78. }
  79. /**
  80. * Draw
  81. * @param mode GL_TRIANGLES, GL_QUAD_STRIP, etc.
  82. */
  83. public void draw(int mode){
  84. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  85. glEnableClientState(GL_VERTEX_ARRAY);
  86. glVertexPointer(3, GL_FLOAT, sizePerVertex * sizeofFloat, 0);
  87. int offset = 3;
  88. if(currentColor != null){
  89. glEnableClientState(GL_COLOR_ARRAY);
  90. int colorsize = alpha ? 4 : 3;
  91. glColorPointer(colorsize, GL_FLOAT, sizePerVertex * sizeofFloat, offset * sizeofFloat);
  92. offset += colorsize;
  93. }
  94. if(currentNormal != null){
  95. glEnableClientState(GL_NORMAL_ARRAY);
  96. glNormalPointer(GL_FLOAT, sizePerVertex * sizeofFloat, offset * sizeofFloat);
  97. offset += 3;
  98. }
  99. if(currentTexCoord != null){
  100. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  101. glTexCoordPointer(2, GL_FLOAT, sizePerVertex * sizeofFloat, offset * sizeofFloat);
  102. offset += 2;
  103. }
  104. glDrawArrays(mode, 0, nvertices);
  105. if(currentNormal != null){
  106. glDisableClientState(GL_COLOR_ARRAY);
  107. }
  108. if(currentNormal != null){
  109. glDisableClientState(GL_NORMAL_ARRAY);
  110. }
  111. if(currentTexCoord != null){
  112. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  113. }
  114. glBindBuffer(GL_ARRAY_BUFFER, 0);
  115. }
  116. public void vertex(float x, float y, float z){
  117. if(sizePerVertex != -1){
  118. throw new IllegalStateException("After calling finish, you are not allowed to make any modifications.");
  119. }
  120. vertices.add(new Vertex(new Vec3d(x,y,z), currentColor, currentNormal, currentTexCoord));
  121. }
  122. /**
  123. * This will affect the next vertex to be specified.
  124. * @param x
  125. * @param y
  126. * @param z
  127. */
  128. public void normal(float x, float y, float z){
  129. if(sizePerVertex != -1){
  130. throw new IllegalStateException("After calling finish, you are not allowed to make any modifications.");
  131. }
  132. if(!vertices.isEmpty() && currentNormal == null){
  133. throw new IllegalStateException("You are not allowed to supply a normal here. "
  134. + "Every vertex should have an associated normal, or no vertex should.");
  135. }
  136. currentNormal = new Vec3d(x, y, z);
  137. }
  138. /**
  139. * This will affect the next vertex to be specified.
  140. * @param x
  141. * @param y
  142. */
  143. public void texCoord(float x, float y){
  144. if(sizePerVertex != -1){
  145. throw new IllegalStateException("After calling finish, you are not allowed to make any modifications.");
  146. }
  147. if(!vertices.isEmpty() && currentTexCoord == null){
  148. throw new IllegalStateException("You are not allowed to supply a texture coordinate here. "
  149. + "Every vertex should have an associated texture coordinate, or no vertex should.");
  150. }
  151. currentTexCoord = new Vec2d(x, y);
  152. }
  153. /**
  154. * This will affect the next vertex to be specified.
  155. * @param r
  156. * @param g
  157. * @param b
  158. */
  159. public void color(float r, float g, float b){
  160. if(sizePerVertex != -1){
  161. throw new IllegalStateException("After calling finish, you are not allowed to make any modifications.");
  162. }
  163. if(!vertices.isEmpty() && currentColor == null){
  164. throw new IllegalStateException("You are not allowed to supply a color here. "
  165. + "Every vertex should have an associated color, or no vertex should.");
  166. }
  167. currentColor = new Color(r, g, b);
  168. }
  169. /**
  170. * This will affect the next vertex to be specified.
  171. * @param r
  172. * @param g
  173. * @param b
  174. * @param a
  175. */
  176. public void color(float r, float g, float b, float a){
  177. if(sizePerVertex != -1){
  178. throw new IllegalStateException("After calling finish, you are not allowed to make any modifications.");
  179. }
  180. if(!vertices.isEmpty() && currentColor == null){
  181. throw new IllegalStateException("You are not allowed to supply a color here. "
  182. + "Every vertex should have an associated color, or no vertex should.");
  183. }
  184. alpha = true;
  185. currentColor = new Color(r, g, b, a);
  186. }
  187. private class Vertex{
  188. Vec3d vtx;
  189. Color color;
  190. Vec3d normal;
  191. Vec2d texcoord;
  192. Vertex(Vec3d vtx, Color color, Vec3d normal, Vec2d texcoord) {
  193. this.vtx = vtx;
  194. this.color = color;
  195. this.normal = normal;
  196. this.texcoord = texcoord;
  197. }
  198. }
  199. }


this is untested btw
1 Posted 26 May 2014
User avatar
Rick Hendricksen
Administrator
Posts: 2251
Ported to GLES2 (android). This version IS tested, and working.

  1. package nl.xupwup.Util;
  2. import java.nio.ByteBuffer;
  3. import java.nio.ByteOrder;
  4. import java.util.ArrayList;
  5. import static android.opengl.GLES20.*;
  6. /**
  7. *
  8. * @author Rick Hendricksen
  9. */
  10. public class GeometryObject {
  11. private Color currentColor = null;
  12. private boolean alpha = false;
  13. private Vec3d currentNormal = null;
  14. private Vec2d currentTexCoord = null;
  15. private static final int sizeofFloat = 4;
  16. private ArrayList<Vertex> vertices;
  17. private int nvertices = -1;
  18. private int vbo = -1;
  19. private int sizePerVertex = -1;
  20. private int positionAttribLocation = 0;
  21. private int colorAttribLocation = 1;
  22. private int normalAttribLocation = 2;
  23. private int texAttribLocation = 3;
  24. public void setPositionAttribLocation(int positionAttribLocation) {
  25. this.positionAttribLocation = positionAttribLocation;
  26. }
  27. public void setColorAttribLocation(int colorAttribLocation) {
  28. this.colorAttribLocation = colorAttribLocation;
  29. }
  30. public void setNormalAttribLocation(int normalAttribLocation) {
  31. this.normalAttribLocation = normalAttribLocation;
  32. }
  33. public void setTexAttribLocation(int texAttribLocation) {
  34. this.texAttribLocation = texAttribLocation;
  35. }
  36. public GeometryObject(){
  37. vertices = new ArrayList<Vertex>();
  38. }
  39. @Override
  40. protected void finalize() throws Throwable {
  41. super.finalize();
  42. if(vbo != -1){
  43. throw new Exception("Geometry object was not cleaned up properly.");
  44. }
  45. }
  46. public void destroy(){
  47. if(sizePerVertex == -1){
  48. throw new IllegalStateException("Nothing to destroy!");
  49. }
  50. glDeleteBuffers(1, new int[]{vbo}, 0);
  51. vbo = -1;
  52. sizePerVertex = -1;
  53. vertices = null;
  54. }
  55. /**
  56. * Puts the collected vertices in a VBO.
  57. */
  58. public void finish(){
  59. sizePerVertex = 3;
  60. if(currentColor != null){
  61. sizePerVertex += alpha ? 4 : 3;
  62. }
  63. if(currentNormal != null){
  64. sizePerVertex += 3;
  65. }
  66. if(currentTexCoord != null){
  67. sizePerVertex += 2;
  68. }
  69. nvertices = vertices.size();
  70. //BufferUtils.createByteBuffer(nvertices * sizeofFloat * sizePerVertex);
  71. ByteBuffer buffer = ByteBuffer.allocateDirect(nvertices * sizeofFloat * sizePerVertex);
  72. buffer.order(ByteOrder.nativeOrder());
  73. for (Vertex v : vertices) {
  74. buffer.putFloat((float) v.vtx.x).putFloat((float) v.vtx.y).putFloat((float) v.vtx.z);
  75. if(currentColor != null){
  76. if(alpha){
  77. buffer.putFloat((float) v.color.r).putFloat((float) v.color.g).putFloat((float) v.color.b).putFloat((float) v.color.a);
  78. }else{
  79. buffer.putFloat((float) v.color.r).putFloat((float) v.color.g).putFloat((float) v.color.b);
  80. }
  81. }
  82. if(currentNormal != null){
  83. buffer.putFloat((float) v.normal.x).putFloat((float) v.normal.y).putFloat((float) v.normal.z);
  84. }
  85. if(currentTexCoord != null){
  86. buffer.putFloat((float) v.texcoord.x).putFloat((float) v.texcoord.y);
  87. }
  88. }
  89. buffer.flip();
  90. int[] temp = new int[1];
  91. glGenBuffers(1, temp, 0);
  92. vbo = temp[0];
  93. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  94. glBufferData(GL_ARRAY_BUFFER, buffer.capacity(), buffer, GL_STATIC_DRAW);
  95. glBindBuffer(GL_ARRAY_BUFFER, 0);
  96. }
  97. /**
  98. * Draw
  99. * @param mode GL_TRIANGLES, GL_QUAD_STRIP, etc.
  100. */
  101. public void draw(int mode){
  102. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  103. glEnableVertexAttribArray(positionAttribLocation);
  104. glVertexAttribPointer(positionAttribLocation, 3, GL_FLOAT, true, sizePerVertex * sizeofFloat, 0);
  105. int offset = 3;
  106. if(currentColor != null){
  107. int colorsize = alpha ? 4 : 3;
  108. glEnableVertexAttribArray(colorAttribLocation);
  109. glVertexAttribPointer(colorAttribLocation, colorsize, GL_FLOAT, true, sizePerVertex * sizeofFloat, offset * sizeofFloat);
  110. offset += colorsize;
  111. }
  112. if(currentNormal != null){
  113. glEnableVertexAttribArray(normalAttribLocation);
  114. glVertexAttribPointer(normalAttribLocation, 3, GL_FLOAT, true, sizePerVertex * sizeofFloat, offset * sizeofFloat);
  115. offset += 3;
  116. }
  117. if(currentTexCoord != null){
  118. glEnableVertexAttribArray(texAttribLocation);
  119. glVertexAttribPointer(texAttribLocation, 2, GL_FLOAT, true, sizePerVertex * sizeofFloat, offset * sizeofFloat);
  120. offset += 2;
  121. }
  122. glDrawArrays(mode, 0, nvertices);
  123. if(currentTexCoord != null){
  124. glDisableVertexAttribArray(3);
  125. }
  126. if(currentNormal != null){
  127. glDisableVertexAttribArray(2);
  128. }
  129. if(currentColor != null){
  130. glDisableVertexAttribArray(1);
  131. }
  132. glDisableVertexAttribArray(0);
  133. glBindBuffer(GL_ARRAY_BUFFER, 0);
  134. }
  135. public void vertex(float x, float y, float z){
  136. if(sizePerVertex != -1){
  137. throw new IllegalStateException("After calling finish, you are not allowed to make any modifications.");
  138. }
  139. vertices.add(new Vertex(new Vec3d(x,y,z), currentColor, currentNormal, currentTexCoord));
  140. }
  141. /**
  142. * This will affect the next vertex to be specified.
  143. * @param x
  144. * @param y
  145. * @param z
  146. */
  147. public void normal(float x, float y, float z){
  148. if(sizePerVertex != -1){
  149. throw new IllegalStateException("After calling finish, you are not allowed to make any modifications.");
  150. }
  151. if(!vertices.isEmpty() && currentNormal == null){
  152. throw new IllegalStateException("You are not allowed to supply a normal here. "
  153. + "Every vertex should have an associated normal, or no vertex should.");
  154. }
  155. currentNormal = new Vec3d(x, y, z);
  156. }
  157. /**
  158. * This will affect the next vertex to be specified.
  159. * @param x
  160. * @param y
  161. */
  162. public void texCoord(float x, float y){
  163. if(sizePerVertex != -1){
  164. throw new IllegalStateException("After calling finish, you are not allowed to make any modifications.");
  165. }
  166. if(!vertices.isEmpty() && currentTexCoord == null){
  167. throw new IllegalStateException("You are not allowed to supply a texture coordinate here. "
  168. + "Every vertex should have an associated texture coordinate, or no vertex should.");
  169. }
  170. currentTexCoord = new Vec2d(x, y);
  171. }
  172. /**
  173. * This will affect the next vertex to be specified.
  174. * @param r
  175. * @param g
  176. * @param b
  177. */
  178. public void color(float r, float g, float b){
  179. if(sizePerVertex != -1){
  180. throw new IllegalStateException("After calling finish, you are not allowed to make any modifications.");
  181. }
  182. if(!vertices.isEmpty() && currentColor == null){
  183. throw new IllegalStateException("You are not allowed to supply a color here. "
  184. + "Every vertex should have an associated color, or no vertex should.");
  185. }
  186. currentColor = new Color(r, g, b);
  187. }
  188. /**
  189. * This will affect the next vertex to be specified.
  190. * @param r
  191. * @param g
  192. * @param b
  193. * @param a
  194. */
  195. public void color(float r, float g, float b, float a){
  196. if(sizePerVertex != -1){
  197. throw new IllegalStateException("After calling finish, you are not allowed to make any modifications.");
  198. }
  199. if(!vertices.isEmpty() && currentColor == null){
  200. throw new IllegalStateException("You are not allowed to supply a color here. "
  201. + "Every vertex should have an associated color, or no vertex should.");
  202. }
  203. alpha = true;
  204. currentColor = new Color(r, g, b, a);
  205. }
  206. private class Vertex{
  207. Vec3d vtx;
  208. Color color;
  209. Vec3d normal;
  210. Vec2d texcoord;
  211. @Override
  212. public String toString() {
  213. return vtx + "/" + color + "/" +normal + "/" + texcoord;
  214. }
  215. Vertex(Vec3d vtx, Color color, Vec3d normal, Vec2d texcoord) {
  216. this.vtx = vtx;
  217. this.color = color;
  218. this.normal = normal;
  219. this.texcoord = texcoord;
  220. }
  221. }
  222. private class Color{
  223. final float r, g, b, a;
  224. @Override
  225. public String toString() {
  226. return "(" + r + ", " + g + ", " + b + ", " + a + ")";
  227. }
  228. public Color(float r, float g, float b, float a) {
  229. this.r = r; this.g = g; this.b = b; this.a = a;
  230. }
  231. public Color(float r, float g, float b) {
  232. this(r, g, b, 1);
  233. }
  234. }
  235. }


You may want to use shaders similar to those below.

Fragment shader
  1. precision mediump float;
  2. varying vec4 oColor;
  3. void main() {
  4. gl_FragColor = oColor;
  5. }

Vertex Shader:
  1. attribute vec3 position;
  2. attribute vec4 color;
  3. attribute vec3 normal;
  4. attribute vec2 texcoord;
  5. varying vec4 oColor;
  6. void main() {
  7. gl_Position = vec4(position, 1);
  8. oColor = vec4(0,0.5,0.1,1) + color;
  9. }
© Rick Hendricksen