List of usage examples for org.lwjgl.opengl GL20 glVertexAttribPointer
public static void glVertexAttribPointer(@NativeType("GLuint") int index, @NativeType("GLint") int size, @NativeType("GLenum") int type, @NativeType("GLboolean") boolean normalized, @NativeType("GLsizei") int stride, @NativeType("void const *") FloatBuffer pointer)
From source file:tk.ivybits.engine.gl.GL.java
License:Open Source License
public static void glVertexAttribPointer(int a, int b, int c, boolean d, int e, ByteBuffer f) { GL20.glVertexAttribPointer(a, b, c, d, e, f); }
From source file:uk.co.ifs_studios.engine.geometry.BasicDrawElement.java
License:Open Source License
/** * Set verticies to DrawElement.//from w w w . j a v a 2s . c o m * * @param verticies * - Verticies. */ public void setVerticies(Vertex[] verticies) { float[] vertexValues = new float[verticies.length * 4]; int vertexIndex = 0; for (int i = 0; i < vertexValues.length; i += 4) { vertexValues[i + 0] = verticies[vertexIndex].getX(); vertexValues[i + 1] = verticies[vertexIndex].getY(); vertexValues[i + 2] = verticies[vertexIndex].getZ(); vertexValues[i + 3] = verticies[vertexIndex].getW(); vertexIndex++; } FloatBuffer verticiesBuffer = BufferUtils.createFloatBuffer(vertexValues.length); verticiesBuffer.put(vertexValues); verticiesBuffer.flip(); GL30.glBindVertexArray(this.vao); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, this.vbo); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticiesBuffer, GL15.GL_STATIC_DRAW); GL20.glVertexAttribPointer(0, 4, GL11.GL_FLOAT, false, 0, 0); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); GL30.glBindVertexArray(0); }
From source file:uk.co.ifs_studios.engine.geometry.BasicDrawElement.java
License:Open Source License
/** * Set colors to DrawElement./*from ww w .ja v a 2 s . c o m*/ * * @param colors * - Colors. */ public void setColors(Color[] colors) { float[] colorValues = new float[colors.length * 4]; int colorIndex = 0; for (int i = 0; i < colorValues.length; i += 4) { colorValues[i + 0] = colors[colorIndex].getRed(); colorValues[i + 1] = colors[colorIndex].getGreen(); colorValues[i + 2] = colors[colorIndex].getBlue(); colorValues[i + 3] = colors[colorIndex].getAlpha(); colorIndex++; } FloatBuffer colorsBuffer = BufferUtils.createFloatBuffer(colorValues.length); colorsBuffer.put(colorValues); colorsBuffer.flip(); GL30.glBindVertexArray(this.vao); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, this.cbo); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, colorsBuffer, GL15.GL_STREAM_DRAW); GL20.glVertexAttribPointer(1, 4, GL11.GL_FLOAT, false, 0, 0); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); GL30.glBindVertexArray(0); }
From source file:vertigo.graphics.lwjgl.LWJGL_Renderer.java
License:Open Source License
private void drawShape(Shape obj) { // Geometry: VBO and IBO if (obj.isDirty(Node.VBO)) { processBO(obj);/*from w ww.j av a2 s .c om*/ obj.setDirty(Node.VBO, false); } ShaderProg glshader = obj.getMaterial().getShaderMaterial(); GL20.glUseProgram(glshader.getHandle()); updateUniform(); VBO vbo = null; for (Attribute attrib : attribute) { if (vbo.getType().equals(attrib.getType())) { int alocation = GL20.glGetAttribLocation(glshader.getHandle(), attrib.getName()); GL20.glEnableVertexAttribArray(alocation); GL20.glVertexAttribPointer(alocation, attrib.getSize(), GL11.GL_FLOAT, false, vbo.getStride() << 2, 0L); } } if (isIndexed) { // draw with index GL11.glDrawElements(getOpenGLStyle(obj.getDrawingStyle()), /* elements */ ibo.getSize(), GL_UNSIGNED_INT, 0L); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY); GL20.glDisableVertexAttribArray(0); } else { // draw without index GL11.glDrawArrays(getOpenGLStyle(obj.getDrawingStyle()), 0, capacity / vbo3f.getStride()); GL20.glDisableVertexAttribArray(0); GL30.glBindVertexArray(0); } GL20.glUseProgram(0); }
From source file:voxels.Mesh.java
License:Open Source License
private void createMesh() { // We'll define our quad using 4 vertices of the custom 'TexturedVertex' class VertexData v0 = new VertexData(); v0.setXYZ(-0.5f, 0.5f, 0.5f);//from www. ja v a 2 s .c o m v0.setRGB(1, 0, 0); v0.setST(0, 0); VertexData v1 = new VertexData(); v1.setXYZ(-0.5f, -0.5f, 0.5f); v1.setRGB(0, 1, 0); v1.setST(0, 1); VertexData v2 = new VertexData(); v2.setXYZ(0.5f, -0.5f, 0.5f); v2.setRGB(0, 0, 1); v2.setST(1, 1); VertexData v3 = new VertexData(); v3.setXYZ(0.5f, 0.5f, 0.5f); v3.setRGB(1, 1, 1); v3.setST(1, 0); VertexData v4 = new VertexData(); v4.setXYZ(-0.5f, 0.5f, -0.5f); v4.setRGB(1, 0, 0); v4.setST(0, 0); VertexData v5 = new VertexData(); v5.setXYZ(-0.5f, -0.5f, -0.5f); v5.setRGB(0, 1, 0); v5.setST(0, 1); VertexData v6 = new VertexData(); v6.setXYZ(-0.5f, -0.5f, 0.5f); v6.setRGB(0, 0, 1); v6.setST(1, 1); VertexData v7 = new VertexData(); v7.setXYZ(-0.5f, 0.5f, 0.5f); v7.setRGB(1, 1, 1); v7.setST(1, 0); VertexData v8 = new VertexData(); v8.setXYZ(-0.5f, 0.5f, -0.5f); v8.setRGB(1, 0, 0); v8.setST(0, 0); VertexData v9 = new VertexData(); v9.setXYZ(-0.5f, 0.5f, 0.5f); v9.setRGB(0, 1, 0); v9.setST(0, 1); VertexData v10 = new VertexData(); v10.setXYZ(0.5f, 0.5f, 0.5f); v10.setRGB(0, 0, 1); v10.setST(1, 1); VertexData v11 = new VertexData(); v11.setXYZ(0.5f, 0.5f, -0.5f); v11.setRGB(1, 1, 1); v11.setST(1, 0); VertexData v12 = new VertexData(); v12.setXYZ(0.5f, -0.5f, 0.5f); v12.setRGB(1, 0, 0); v12.setST(0, 0); VertexData v13 = new VertexData(); v13.setXYZ(-0.5f, -0.5f, 0.5f); v13.setRGB(0, 1, 0); v13.setST(0, 1); VertexData v14 = new VertexData(); v14.setXYZ(-0.5f, -0.5f, -0.5f); v14.setRGB(0, 0, 1); v14.setST(1, 1); VertexData v15 = new VertexData(); v15.setXYZ(0.5f, -0.5f, -0.5f); v15.setRGB(1, 1, 1); v15.setST(1, 0); VertexData v16 = new VertexData(); v16.setXYZ(0.5f, -0.5f, -0.5f); v16.setRGB(1, 0, 0); v16.setST(0, 0); VertexData v17 = new VertexData(); v17.setXYZ(-0.5f, -0.5f, -0.5f); v17.setRGB(0, 1, 0); v17.setST(0, 1); VertexData v18 = new VertexData(); v18.setXYZ(-0.5f, 0.5f, -0.5f); v18.setRGB(0, 0, 1); v18.setST(1, 1); VertexData v19 = new VertexData(); v19.setXYZ(0.5f, 0.5f, -0.5f); v19.setRGB(1, 1, 1); v19.setST(1, 0); VertexData v20 = new VertexData(); v20.setXYZ(0.5f, -0.5f, 0.5f); v20.setRGB(1, 0, 0); v20.setST(0, 0); VertexData v21 = new VertexData(); v21.setXYZ(0.5f, -0.5f, -0.5f); v21.setRGB(0, 1, 0); v21.setST(0, 1); VertexData v22 = new VertexData(); v22.setXYZ(0.5f, 0.5f, -0.5f); v22.setRGB(0, 0, 1); v22.setST(1, 1); VertexData v23 = new VertexData(); v23.setXYZ(0.5f, 0.5f, 0.5f); v23.setRGB(1, 1, 1); v23.setST(1, 0); vertices = new VertexData[] { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23 }; // Put each 'Vertex' in one FloatBuffer verticesByteBuffer = BufferUtils.createByteBuffer(vertices.length * VertexData.stride); FloatBuffer verticesFloatBuffer = verticesByteBuffer.asFloatBuffer(); for (VertexData vertice : vertices) { // Add position, color and texture floats to the buffer verticesFloatBuffer.put(vertice.getElements()); } verticesFloatBuffer.flip(); // OpenGL expects to draw vertices in counter clockwise order by default short[] indices = new short[6 * vertices.length / 4]; for (short i = 0; i < indices.length / 6; i++) { // 0, 1, 2, 2, 3, 0,4, 5 , 0, 5, 1, 0, 1, 5, 2, 5, 6, 2, 3, 0, 7, 0, 2, 7, 8, 6, 4, 6, 5, 4, 2, 3, 8, 3, 6, 8 indices[i * 6 + 0] = (short) (0 + 4 * i); indices[i * 6 + 1] = (short) (1 + 4 * i); indices[i * 6 + 2] = (short) (2 + 4 * i); indices[i * 6 + 3] = (short) (2 + 4 * i); indices[i * 6 + 4] = (short) (3 + 4 * i); indices[i * 6 + 5] = (short) (0 + 4 * i); } //indices = new byte[]{0, 1, 2, 2, 3, 0,4, 5 , 0, 5, 1, 0, 1, 5, 2, 5, 6, 2, 3, 0, 7, 0, 2, 7, 8, 6, 4, 6, 5, 4, 2, 3, 8, 3, 6, 8}; vertices = null; indicesCount = indices.length; ShortBuffer indicesBuffer = BufferUtils.createShortBuffer(indicesCount); indicesBuffer.put(indices); indicesBuffer.flip(); // Create a new Vertex Array Object in memory and select it (bind) vaoId = GL30.glGenVertexArrays(); GL30.glBindVertexArray(vaoId); // Create a new Vertex Buffer Object in memory and select it (bind) vboId = GL15.glGenBuffers(); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticesFloatBuffer, GL15.GL_STREAM_DRAW); // Put the position coordinates in attribute list 0 GL20.glVertexAttribPointer(0, VertexData.positionElementCount, GL11.GL_FLOAT, false, VertexData.stride, VertexData.positionByteOffset); // Put the color components in attribute list 1 GL20.glVertexAttribPointer(1, VertexData.colorElementCount, GL11.GL_FLOAT, false, VertexData.stride, VertexData.colorByteOffset); // Put the texture coordinates in attribute list 2 GL20.glVertexAttribPointer(2, VertexData.textureElementCount, GL11.GL_FLOAT, false, VertexData.stride, VertexData.textureByteOffset); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); // Deselect (bind to 0) the VAO GL30.glBindVertexArray(0); // Create a new VBO for the indices and select it (bind) - INDICES vboiId = GL15.glGenBuffers(); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboiId); GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL15.GL_STATIC_DRAW); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); }
From source file:wrapper.vbo.Vbo.java
License:Open Source License
public void render() { if (vertid != -1) { shader.bind();//from w w w. j a va2s. c o m GL11.glEnable(GL11.GL_BLEND); GL11.glBindTexture(GL11.GL_TEXTURE_2D, texid); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertid); GL20.glEnableVertexAttribArray(vertexattrib); GL20.glEnableVertexAttribArray(textureattrib); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertid); GL20.glVertexAttribPointer(vertexattrib, 3, GL11.GL_FLOAT, false, 0, 0); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, texcoordid); GL20.glVertexAttribPointer(textureattrib, 2, GL11.GL_FLOAT, false, 0, 0); GL20.glUniform3f(rotationuniform, rotation_x, rotation_y, rotation_z); GL20.glUniform3f(transformuniform, transformation.x, transformation.y, transformation.z); GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, vertcount); GL20.glDisableVertexAttribArray(vertexattrib); GL20.glDisableVertexAttribArray(textureattrib); GL11.glEnable(GL11.GL_BLEND); shader.unbind(); } else { System.out.println("Error no bound vertid"); return; } }
From source file:wrath.client.graphics.Model.java
License:Open Source License
/** * Creates a 2D or 3D model from a list of verticies. * Models are always assumed to be made with triangles, and will be rendered as such. * @param name The {@link java.lang.String} name of the Model. * @param verticies The list of verticies in the model. One point is represented by (x, y, z), and there must be at least 3 points. * @param indicies The list of points to connect for OpenGL. Look up indicies in OpenGL for reference. * @param normals The list of 3 float vectors describing the normal vector of the model's surface. * @param useDefaultShaders If true, shaders will be set up automatically. * @return Returns the {@link wrath.client.graphics.Model} object of your model. *//* w ww .j a va 2 s. c om*/ public static Model createModel(String name, float[] verticies, int[] indicies, float[] normals, boolean useDefaultShaders) { // Generating VAO int vaoid = GL30.glGenVertexArrays(); GL30.glBindVertexArray(vaoid); // Generating Verticies VBO int vtvboid = GL15.glGenBuffers(); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vtvboid); FloatBuffer vbuffer = BufferUtils.createFloatBuffer(verticies.length); vbuffer.put(verticies); vbuffer.flip(); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vbuffer, GL15.GL_STATIC_DRAW); GL20.glVertexAttribPointer(VERTICIES_ATTRIB_INDEX, 3, GL11.GL_FLOAT, false, 0, 0); // Generating Normals VBO int nmvboid = GL15.glGenBuffers(); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, nmvboid); FloatBuffer nbuffer = BufferUtils.createFloatBuffer(normals.length); nbuffer.put(normals); nbuffer.flip(); GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, nbuffer, GL15.GL_STATIC_DRAW); GL20.glVertexAttribPointer(NORMALS_ATTRIB_INDEX, 3, GL11.GL_FLOAT, false, 0, 0); // Generating Indicies VBO int invboid = GL15.glGenBuffers(); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, invboid); IntBuffer ibuffer = BufferUtils.createIntBuffer(indicies.length); ibuffer.put(indicies); ibuffer.flip(); GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, ibuffer, GL15.GL_STATIC_DRAW); // Creating Model Object Model model; File mfile = new File("assets/models/" + name); if (!mfile.exists()) model = new Model(name, vaoid, new Integer[] { vtvboid, invboid, nmvboid }, verticies, indicies, normals, useDefaultShaders); else model = new Model(name, vaoid, new Integer[] { vtvboid, invboid, nmvboid }, null, null, null, useDefaultShaders); Game.getCurrentInstance().getLogger().println("Loaded model '" + name + "' with " + verticies.length + " verticies, " + indicies.length + " indicies, and " + normals.length + " normals."); if (useDefaultShaders) model.attachShader(ShaderProgram.DEFAULT_SHADER); // Unbinding OpenGL Objects GL30.glBindVertexArray(0); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); Game.getCurrentInstance().addToTrashCleanup(model); Game.getCurrentInstance().addToRefreshList(model); return model; }
From source file:wrath.client.graphics.Model.java
License:Open Source License
/** * Applies a {@link wrath.client.graphics.Texture} to the model to be rendered on top of the Model. * Only one can be attached at a time./*from ww w . j a v a 2 s . com*/ * @param texture The {@link wrath.client.graphics.Texture} to associate with this model. * @param textureCoords The (u, v) coordinates of the texture to the model. */ public void attachTexture(Texture texture, float[] textureCoords) { this.texture = texture; if (this.textureCoords != null) textureCoords = this.textureCoords; if (shader == null) Game.getCurrentInstance().getLogger().println( "Warning: If no shader is present to pass texture co-ordinates, then the texture will not render!"); GL30.glBindVertexArray(vao); int vboid = GL15.glGenBuffers(); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboid); FloatBuffer vbuffer = BufferUtils.createFloatBuffer(textureCoords.length); vbuffer.put(textureCoords); vbuffer.flip(); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vbuffer, GL15.GL_STATIC_DRAW); GL20.glVertexAttribPointer(TEXTURE_ATTRIB_INDEX, 2, GL11.GL_FLOAT, false, 0, 0); if (shader != null) shader.bindAttribute(TEXTURE_ATTRIB_INDEX, "in_TextureCoord"); GL30.glBindVertexArray(0); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); vbos.add(vboid); EntityRenderer.preLoadedModels.put(name + "," + texture.getTextureFile().getName(), this); }
From source file:wrath.client.graphics.Model.java
License:Open Source License
@Override public void reload() { float[] varray = null; float[] narray = null; int[] iarray; File modelFile = new File("assets/models/" + name); if (modelFile.exists()) { ArrayList<String> src = readObjFile(modelFile); ArrayList<Vector3f> verticies = new ArrayList<>(); ArrayList<Vector2f> texCoords = new ArrayList<>(); ArrayList<Vector3f> normals = new ArrayList<>(); ArrayList<Integer> indicies = new ArrayList<>(); float[] tarray = null; boolean tmp = true; for (String inp : src) { String[] buf = inp.split(" "); if (inp.startsWith("v ")) verticies.add(new Vector3f(Float.parseFloat(buf[1]), Float.parseFloat(buf[2]), Float.parseFloat(buf[3]))); else if (inp.startsWith("vt ")) texCoords.add(new Vector2f(Float.parseFloat(buf[1]), Float.parseFloat(buf[2]))); else if (inp.startsWith("vn ")) normals.add(new Vector3f(Float.parseFloat(buf[1]), Float.parseFloat(buf[2]), Float.parseFloat(buf[3]))); else if (inp.startsWith("f ")) { if (tmp) { varray = new float[verticies.size() * 3]; narray = new float[verticies.size() * 3]; tarray = new float[verticies.size() * 2]; tmp = false;/*from ww w . j av a2s. co m*/ } for (int x = 1; x <= 3; x++) { String[] curDat; if (buf[x].contains("//")) { curDat = buf[x].split("//"); int ptr = Integer.parseInt(curDat[0]) - 1; indicies.add(ptr); Vector3f norm = normals.get(Integer.parseInt(curDat[1]) - 1); narray[ptr * 3] = norm.x; narray[ptr * 3 + 1] = norm.y; narray[ptr * 3 + 2] = norm.z; } else { curDat = buf[x].split("/"); int ptr = Integer.parseInt(curDat[0]) - 1; indicies.add(ptr); Vector2f tex = texCoords.get(Integer.parseInt(curDat[1]) - 1); tarray[ptr * 2] = tex.x; tarray[ptr * 2 + 1] = 1 - tex.y; Vector3f norm = normals.get(Integer.parseInt(curDat[2]) - 1); narray[ptr * 3] = norm.x; narray[ptr * 3 + 1] = norm.y; narray[ptr * 3 + 2] = norm.z; } } } } int i = 0; for (Vector3f ve : verticies) { varray[i] = ve.x; varray[i + 1] = ve.y; varray[i + 2] = ve.z; i += 3; } iarray = new int[indicies.size()]; for (int z = 0; z < indicies.size(); z++) iarray[z] = indicies.get(z); textureCoords = tarray; indiciesLen = iarray.length; } else { varray = verticies; narray = normals; iarray = indicies; indiciesLen = indicies.length; } // Generating VAO vao = GL30.glGenVertexArrays(); GL30.glBindVertexArray(vao); // Generating Verticies VBO int vtvboid = GL15.glGenBuffers(); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vtvboid); FloatBuffer vbuffer = BufferUtils.createFloatBuffer(varray.length); vbuffer.put(varray); vbuffer.flip(); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vbuffer, GL15.GL_STATIC_DRAW); GL20.glVertexAttribPointer(VERTICIES_ATTRIB_INDEX, 3, GL11.GL_FLOAT, false, 0, 0); // Generating Normals VBO int nmvboid = GL15.glGenBuffers(); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, nmvboid); FloatBuffer nbuffer = BufferUtils.createFloatBuffer(narray.length); nbuffer.put(narray); nbuffer.flip(); GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, nbuffer, GL15.GL_STATIC_DRAW); GL20.glVertexAttribPointer(NORMALS_ATTRIB_INDEX, 3, GL11.GL_FLOAT, false, 0, 0); // Generating Indicies VBO int invboid = GL15.glGenBuffers(); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, invboid); IntBuffer ibuffer = BufferUtils.createIntBuffer(iarray.length); ibuffer.put(iarray); ibuffer.flip(); GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, ibuffer, GL15.GL_STATIC_DRAW); // Generating Texture VBO int texvboid = GL15.glGenBuffers(); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, texvboid); FloatBuffer tbuffer = BufferUtils.createFloatBuffer(textureCoords.length); tbuffer.put(textureCoords); tbuffer.flip(); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, tbuffer, GL15.GL_STATIC_DRAW); GL20.glVertexAttribPointer(TEXTURE_ATTRIB_INDEX, 2, GL11.GL_FLOAT, false, 0, 0); // Creating Model Object vbos.add(vtvboid); vbos.add(invboid); vbos.add(nmvboid); vbos.add(texvboid); Game.getCurrentInstance().getLogger().println("Reloaded model '" + name + "'!"); if (defaultShaders) this.attachShader(ShaderProgram.DEFAULT_SHADER); // Unbinding OpenGL Objects GL30.glBindVertexArray(0); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); }