Related
Any ideas why this is not producing a smooth circle?
public void draw(ShapeRenderer sRenderer) {
sRenderer.begin(ShapeType.Filled);
sRenderer.setColor(1.0f, 0.0f, 0.0f, 0.0f);
sRenderer.identity();
sRenderer.translate(1.0f, 1.0f, 0);
sRenderer.rotate(0.0f, 0.0f, 1.0f, (float) Math.toDegrees(getBody().getAngle()));
sRenderer.circle(0.0f, 0.0f, 1.0f);
sRenderer.end();
}
circle() takes another argument for setting the number of segments manually. You have it set to estimate and since it's thinking in pixels and not world units it assumed a 1 pixel radius circle.
public void draw(ShapeRenderer sRenderer) {
sRenderer.begin(ShapeType.Filled);
sRenderer.setColor(1.0f, 0.0f, 0.0f, 0.0f);
sRenderer.identity();
sRenderer.translate(1.0f, 1.0f, 0);
sRenderer.rotate(0.0f, 0.0f, 1.0f, (float) Math.toDegrees(getBody().getAngle()));
sRenderer.circle(0.0f, 0.0f, 1.0f, 100);
sRenderer.end();
}
That should get you somewhere, 100 is just a number I threw in, tune it to your needs.
I have a problem regarding glBufferData and glVertexAttribPointer. For some reason, they just don't work together. Here is the code:
float[] triangleArray = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f
};
IntBuffer vacantNameBuffer = IntBuffer.allocate(2);
gl.glGenBuffers(1, vacantNameBuffer);
int bufferIndex = vacantNameBuffer.get();
FloatBuffer triangleVertexBuffer = Buffers.newDirectFloatBuffer(triangleArray);
gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, bufferIndex);
IntBuffer paramBuffer = IntBuffer.allocate(2);
gl.glGetBufferParameteriv(GL2.GL_ARRAY_BUFFER, GL2.GL_BUFFER_SIZE, paramBuffer);
System.out.println(paramBuffer.get());
int triangleBufferSize = triangleVertexBuffer.capacity() * Buffers.SIZEOF_FLOAT;
gl.glBufferData(
GL2.GL_ARRAY_BUFFER,
triangleBufferSize,
triangleVertexBuffer,
GL2.GL_STATIC_DRAW);
gl.glGetBufferParameteriv(GL2.GL_ARRAY_BUFFER, GL2.GL_BUFFER_SIZE, paramBuffer);
System.out.println(paramBuffer.get());
gl.glEnableVertexAttribArray(0);
gl.glVertexAttribPointer(
0,
3,
GL2.GL_FLOAT,
false,
0,
0);
gl.glDrawArrays(GL2.GL_TRIANGLES, 0, 3);
gl.glDisableVertexAttribArray(0);
However, this works perfectly:
float[] triangleArray = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f
};
FloatBuffer triangleVertexBuffer = Buffers.newDirectFloatBuffer(triangleArray);
/*
IntBuffer vacantNameBuffer = IntBuffer.allocate(2);
gl.glGenBuffers(1, vacantNameBuffer);
int bufferIndex = vacantNameBuffer.get();
gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, bufferIndex);
IntBuffer paramBuffer = IntBuffer.allocate(2);
gl.glGetBufferParameteriv(GL2.GL_ARRAY_BUFFER, GL2.GL_BUFFER_SIZE, paramBuffer);
System.out.println(paramBuffer.get());
int triangleBufferSize = triangleVertexBuffer.capacity() * Buffers.SIZEOF_FLOAT;
gl.glBufferData(
GL2.GL_ARRAY_BUFFER,
triangleBufferSize,
triangleVertexBuffer,
GL2.GL_STATIC_DRAW);
gl.glGetBufferParameteriv(GL2.GL_ARRAY_BUFFER, GL2.GL_BUFFER_SIZE, paramBuffer);
System.out.println(paramBuffer.get());
*/
gl.glEnableVertexAttribArray(0);
gl.glVertexAttribPointer(
0,
3,
GL2.GL_FLOAT,
false,
0,
triangleVertexBuffer);
gl.glDrawArrays(GL2.GL_TRIANGLES, 0, 3);
gl.glDisableVertexAttribArray(0);
If someone would be so kind as to point out why my code doesn't work. I would be very grateful.
I found the problem, it was not with my code, but it's with the limitation on jogl that requires a gl shader program to be used.
Found answer on this post: glDrawArrays() behaving weirdly on Mac OS X;
I have a short program which draw a 3D GL.GL_QUADS , here its display() method -
public void display(GLAutoDrawable drawable) {
....
gl.glBegin(GL.GL_QUADS); // of the color cube
// Top-face
gl.glColor3f(0.0f, 1.0f, 0.0f); // green
gl.glVertex3f(1.0f, 1.0f, -1.0f);
gl.glVertex3f(-1.0f, 1.0f, -1.0f);
gl.glVertex3f(-1.0f, 1.0f, 1.0f);
gl.glVertex3f(1.0f, 1.0f, 1.0f);
// Bottom-face
gl.glColor3f(1.0f, 0.5f, 0.0f); // orange
gl.glVertex3f(1.0f, -1.0f, 1.0f);
gl.glVertex3f(-1.0f, -1.0f, 1.0f);
gl.glVertex3f(-1.0f, -1.0f, -1.0f);
gl.glVertex3f(1.0f, -1.0f, -1.0f);
// Front-face
gl.glColor3f(1.0f, 0.0f, 0.0f); // red
gl.glVertex3f(1.0f, 1.0f, 1.0f);
gl.glVertex3f(-1.0f, 1.0f, 1.0f);
gl.glVertex3f(-1.0f, -1.0f, 1.0f);
gl.glVertex3f(1.0f, -1.0f, 1.0f);
// Back-face
gl.glColor3f(1.0f, 1.0f, 0.0f); // yellow
gl.glVertex3f(1.0f, -1.0f, -1.0f);
gl.glVertex3f(-1.0f, -1.0f, -1.0f);
gl.glVertex3f(-1.0f, 1.0f, -1.0f);
gl.glVertex3f(1.0f, 1.0f, -1.0f);
// Left-face
gl.glColor3f(0.0f, 0.0f, 1.0f); // blue
gl.glVertex3f(-1.0f, 1.0f, 1.0f);
gl.glVertex3f(-1.0f, 1.0f, -1.0f);
gl.glVertex3f(-1.0f, -1.0f, -1.0f);
gl.glVertex3f(-1.0f, -1.0f, 1.0f);
// Right-face
gl.glColor3f(1.0f, 0.0f, 1.0f); // violet
gl.glVertex3f(1.0f, 1.0f, -1.0f);
gl.glVertex3f(1.0f, 1.0f, 1.0f);
gl.glVertex3f(1.0f, -1.0f, 1.0f);
gl.glVertex3f(1.0f, -1.0f, -1.0f);
....
}
Now I want to change the above drawing to "Vertex Arrays" mode and to get same results , so I did the following :
Created a float array of all the vertices coodinates of the GL.GL_QUADS .
put this array to a Buffer .
Tell OpenGL where the vertices are (8 vertices) .
Tell OpenGL which indices to draw .
Here is the main code I wrote (edited) -
public class DisplayWithArray extends GLCanvas implements GLEventListener,
KeyListener {
private float[] cubeVertices = { 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f,
1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f,
-1.0f, -1.0f,
1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f,
-1.0f, 1.0f,
1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f,
1.0f, -1.0f,
-1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f,
-1.0f, -1.0f };
private float[] colorVertices ={ 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.5f, 0.0f, 1.0f, 0.5f, 0.0f,
1.0f, 0.5f, 0.0f, 1.0f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f };
// 1st edit - 24 positions of the GL.GL_QUADS
private int[] indices = new int[24] ;
private IntBuffer indicesBuf ;
}
public DisplayWithArray(GLCapabilities capabilities, int width, int height) {
for (int i=0 ; i<24 ; i++) {
this.indices[i] = i ;
}
public void init(GLAutoDrawable drawable) {
...
final GL gl = drawable.getGL();
...
setupPointer(gl);
}
public void display(GLAutoDrawable drawable) {
final GL gl = drawable.getGL();
// draw
gl.glDrawArrays(GL.GL_QUADS, 0, 24);
}
public void setupPointer(GL gl) {
FloatBuffer tmpVerticesBuf = BufferUtil
.newFloatBuffer(cubeVertices.length);
;
FloatBuffer tmpColorVerticesBuf = BufferUtil
.newFloatBuffer(colorVertices.length);
for (int i = 0; i < cubeVertices.length; i++) {
tmpVerticesBuf.put(cubeVertices[i]);
}
for (int i = 0; i < colorVertices.length; i++) {
tmpColorVerticesBuf.put(colorVertices[i]);
}
tmpVerticesBuf.rewind();
tmpColorVerticesBuf.rewind();
gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL.GL_FLOAT, 0, tmpVerticesBuf);
gl.glColorPointer(3, GL.GL_FLOAT, 0, tmpColorVerticesBuf);
// Indices of polygon
IntBuffer tmpIndicesBuf = BufferUtil.newIntBuffer(this.indices.length);
for (int i = 0; i < indices.length; i++) {
tmpIndicesBuf.put(indices[i]);
}
tmpIndicesBuf.rewind();
indicesBuf = tmpIndicesBuf ;
}
When I run it I get an exception Exception in thread "AWT-EventQueue-0" javax.media.opengl.GLException: glGetError() returned the following error codes after a call to glDrawElements(): GL_INVALID_ENUM at DisplayWithArray.display(DisplayWithArray.java:152)
point to the line - gl.glDrawElements(GL.GL_QUADS, indices.length, GL.GL_INT, indicesBuf);
What is wrong here ?
Edit:
I changed the indices array to range [0,5] - according to 6 face of the GL.GL_QUADS.
And extend tmpColorVerticesBuf array to 72 indices (4 times each color) .
Still I have same exception as mentioned above .
Edit 2: (now it works well)
Solve by comparing between cubeVertices and colorVertices sizes (72 each array) , and in display() use gl.glDrawArrays(GL.GL_QUADS, 0, 24) (24 elements of size 3)
What's wrong is that your arrays are not the same size. Your vertex position array has 24 vec3's in it, while your color array only has six. So when your index array says "6", it's going to try to access the 7'th entry in an array with 6 elements. Hence the crash.
Every array uses the same index. Therefore, if you want to have colors and positions, then each color must have a single corresponding position. And vice-versa. You can't have 6 colors working with 32 positions. Nor can each attribute use different indices. If you're doing indexed rendering, they all must use the same index.
I'm currently trying to make a basic game with cubey terrain, similar to Minecraft classic. My first question:
How do I store a list of cubes in a VBO and then rebuild the list when I change that data? I currently render cubes like this:
vertices = BufferUtils.createFloatBuffer(3 * 4 * 6);
vertices.put(new float[] {
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -1.0f});
vertices.rewind();
vertexBufferID = createVBOID();
bufferData(vertexBufferID, vertices);
public static int createVBOID() {
if (GLContext.getCapabilities().GL_ARB_vertex_buffer_object) {
IntBuffer buffer = BufferUtils.createIntBuffer(1);
ARBVertexBufferObject.glGenBuffersARB(buffer);
return buffer.get(0);
}
return 0;
}
public static void bufferData(int id, FloatBuffer buffer) {
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, id);
ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, buffer, ARBVertexBufferObject.GL_DYNAMIC_DRAW_ARB);
}
public void renderGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glViewport(0, 0, Display.getWidth(), Display.getHeight());
glLoadIdentity();
glRotatef(xrot, 1.0f, 0.0f, 0.0f);
glRotatef(yrot, 0.f, 1.0f, 0.0f);
glTranslated(-xpos, -ypos - 19, -zpos - 5);
glEnableClientState(GL_VERTEX_ARRAY);
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, vertexBufferID);
glVertexPointer(3, GL_FLOAT, 0, 0);
for (int y = 0; y < 16; y++) {
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
glPushMatrix();
glTranslatef(x, y, z);
glDrawArrays(GL_QUADS, 0, 24);
glPopMatrix();
}
}
}
glDisableClientState(GL_VERTEX_ARRAY);
}
I know I need to use glMapBuffer and glUnMapBuffer to modify the VBO, but how do I store the cubes and remove them when needed? Should I use an ArrayList? Also, I've been told my way of rendering the cubes is wrong because everyone says I'm rendering one cube only with new coordinates. They say my vertex data stays the same, what do they mean by that?
So basically my questions are,
1. How do I use a Array to hold a list of cubes and load them into a VBO and then remove some of the cubes?
2. How can I change my vertex data so I'm rendering different cubes, not just one cube over and over?
as antonijn wrote you can have 1 cube as VBO and translating its modelview matrix
the translation is sometimes slower (when there are too much cubes).
the translation can be done even inside shader by some uniform offset which is little bit faster than matrix translations
faster way is to have all of the cubes in VBO
it wastes memory but cubes are not that much big (6 faces only)
I would have the cubes stored permanently in CPU accessible memory
when scene changes copy it to VBO for rendering.
do not transfering data between CPU<->GPU back and forward
only copy data from CPU to GPU.
also is better to use non indexed VBOs (drawarrays not drawelements)
indexing is not very good supported
often slows thing down even crashes on bad drivers
for the code look here: https://stackoverflow.com/a/18672821/2521214
but that post use VAO+VBO for GLSL purposes so if you not using GLSL then some changes are needed.
Mostly you have a CPU representation of your scene (which is in your case a list of cubes) and a GPU representation (which is the vertex buffers etc.)
In your case a cube is defined by its position only. They all have the same size and so on. Here are some options how you can render several cubes:
Define a uniform position variable in your vertex shader and add this to the vertex positions. If you don't use shaders, specify the offset as a translation matrix in the model view matrix. Render the same vertex buffer several time with different matrices / offsets.
Let the GPU create the cube in its geometry shader. Therefore, you would just specify the cubes' positions in the vertex buffer. When you map a vertex buffer, you will get a pointer to the data where you can copy the new data to.
Use instancing. Provide an array of positions to the shader and render the cube in instanced mode. Use the instance id to determine the offset vector.
I cannot seem to render a Texture to my square. I have gotten my program to render the blank square with color. Any help is greatly appreciated.
I've redesigned my code to the following and believe that the problem exists with how I'm setting up my Vertex Coordinates and Texture Coordinates. I also get a libc Fatal signal 11 at my glDrawArrays function.
Here are the Vertex and Texture Coordinates:
private final FloatBuffer vertexBuffer;
private final FloatBuffer textureBuffer;
static final int COORDS_PER_VERTEX = 3;
static float positionCoords[] = { // in counterclockwise order:
-1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
};
static final int COORDS_PER_TEXTURE = 2;
static float textureCoords[] = {
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f,
};
Here's my draw function in my square class:
public void draw(float[] mvpMatrix)
{
int MVPMatrixHandle = GLES20.glGetUniformLocation(shader.getProgram(), "u_MVPMatrix");
int textureHandler = GLES20.glGetUniformLocation(shader.getProgram(), "u_s_texture");
int positionHandler = GLES20.glGetAttribLocation(shader.getProgram(), "a_position");
int texCoordHandler = GLES20.glGetAttribLocation(shader.getProgram(), "a_texCoord");
Log.d(TAG, "Setting up GLProgram Handlers");
GlRenderer.checkGlError("Setup GLProgram Handlers");
GLES20.glEnableVertexAttribArray(positionHandler);
GLES20.glEnableVertexAttribArray(texCoordHandler);
GlRenderer.checkGlError("EnableVertexAttribArrays");
GLES20.glVertexAttribPointer(positionHandler, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertexStride, vertexBuffer);
GLES20.glVertexAttribPointer(textureHandler, COORDS_PER_TEXTURE,
GLES20.GL_FLOAT, false,
textureStride, textureBuffer);
GlRenderer.checkGlError("VertexAttribPointers (Position, Texture)");
GLES20.glUniformMatrix4fv(MVPMatrixHandle, 1, false, mvpMatrix, 0);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureID);
GLES20.glUniform1i(textureHandler, 0);
GlRenderer.checkGlError("Binding Texture");
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
GlRenderer.checkGlError("Draw Arrays");
GLES20.glDisableVertexAttribArray(positionHandler);
GLES20.glDisableVertexAttribArray(texCoordHandler);
GlRenderer.checkGlError("DisableVertexAttribArrays");
}
Your SetupGLPositionHandle function looks wrong to me. Why disable the PositionHandle attribute at the end of the function?
The attribute must be enabled at the time glDrawArrays is called.
My main problem in the above code was the fact that I was using the textureHandleinstead of the texCoordHandler in the glVertexAttribPointer //Texture
The code should look like this:
GLES20.glVertexAttribPointer(texCoordHandler, COORDS_PER_TEXTURE,
GLES20.GL_FLOAT, false,
textureStride, textureBuffer);
Since this problem arose I've rewritten my code yet again. Adding indices (draworder), combining my Vertex and Texture arrays into a Vertices array, and just reference a vertexBuffer //Contains both position coordinates and texture coordinates instead of a textureBuffer and a vertexBuffer //Only contains position coordinates
I've also changed the GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount); to:
GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.length,
GLES20.GL_UNSIGNED_SHORT, indexBuffer);