2d Triangle with OpenGL ES 2.0 - android

EDIT 2: Take a look on the Triangle2d sample of this GitHub project for a complete, working sample.
EDIT: See the accepted answer for a link with a good explanation on how the ortographic matrix works. In the end, I tweaked the provided code a little:
float[] mvp = {
2f/width, 0f, 0f, 0f,
0f, -2f/height, 0f, 0f,
0f, 0f, 0f, 0f,
-1f, 1f, 0f, 1f
};
Please note that my z is fixed in 0 and w fixed in 1. This matrix make the origin (0,0) at the bottom-left of the screen; if you want the origin at top-left, try:
float[] mvp = {
2f/width, 0f, 0f, 0f,
0f, 2f/height, 0f, 0f,
0f, 0f, 0f, 0f,
-1f, -1f, 0f, 1f
};
Another problem was the call to GLES20.glUniformMatrix4fv which I changed to:
FloatBuffer b = ByteBuffer.allocateDirect(mvp.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
b.put(mvp).position(0);
GLES20.glUniformMatrix4fv(uMvpPos, b.limit() / mvp.length, false, b);
If you want to mess with this a bit, try this online calculator. Just remember that the rows in your sorce file will be columns in the calculator.
Original Problem:
I'm trying to draw a 2d triangle using OpenGLES 2.0 on android, but so far, not much success. These are my shaders:
(Vertex shader)
uniform mat4 uMvp;
attribute vec3 aPosition;
attribute vec3 aColor;
varying vec4 vColor;
void main() {
vColor = vec4(aColor.xyz, 1.0);
vec4 position = vec4(aPosition.xyz, 1.0);
gl_Position = uMvp * position;
};
(Fragment shader)
precision mediump float;
varying vec4 vColor;
void main(void)
{
gl_FragColor = vColor;
};
Then, in the onSurfaceChanged method of GLSurfaceView.Renderer, I put the following:
public void onSurfaceChanged(GL10 gl, int width, int height)
{
// here I load and compile the shaders and link the program.
// in the end, I use GLES20.glUseProgram(programHandle);
// (code omitted)
// create the matrix for the uniform
int uMvpPos = GLES20.glGetUniformLocation(programHandle, "uMvp");
float[] mvp = {width, 0f, 0f, 0f,
0f, -height, 0f, 0f,
0f, 0f, -2f, 0f,
-1f, 1, -1f, 1};
GLES20.glUniformMatrix4fv(uMvpPos, mvp.length * 4, false, mvp, 0);
// set viewport and clear color to white
GLES20.glViewport(0, 0, width, height);
GLES20.glClearColor(1f, 1f, 1f,1.0f);
}
I used the values of the matrix showed on this question. My intent here is to work with coordinates in the same way as a canvas works: (0,0) on top-left of screen and (width, height) on bottom-right.
And last but not least, this is the code of onDrawFrame:
public void onDrawFrame(GL10 gl)
{
int aPos = GLES20.glGetAttribLocation(programHandle,"aPosition");
int aColor = GLES20.glGetAttribLocation(programHandle,"aColor");
// assuming I correctly set up my coordinate system,
// these are the triangle coordinates and color
float[] data =
{
// XYZ, RGB
100f, 100f, 0f,
1f, 0f, 0f,
50f, 50f, 0f,
1f, 0f, 0f,
150f, 50f, 0f,
1f, 0f, 0f,
};
// put all my data into a float buffer
// the '* 4' is because a float has 4 bytes
FloatBuffer dataVertex = ByteBuffer.allocateDirect(data.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
dataVertex.put(data).position(0);
// set the POSITION values
// attribute, dataSize(number of elements), data type, normalized?, size(bytes), data
GLES20.glEnableVertexAttribArray(aPos);
GLES20.glVertexAttribPointer(aPos, 3, GLES20.GL_FLOAT, false, 6 * 4, dataVertex);
// set the COLOR values
dataVertex.position(3); // offset
GLES20.glEnableVertexAttribArray(aColor);
GLES20.glVertexAttribPointer(aColor, 3, GLES20.GL_FLOAT, false, 6 * 4, dataVertex);
// put a white background
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
// and finally draw the triangle!
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
}
The end result is... a boring white screen, without the triangle. I guess I'm committing a very simple mistake, but I just can't spot it. Any thoughts?

Your orthographic projection matrix is wrong.
Orthographic projection is defined as (in column major order):
2/(r-l), 0, 0, 0,
0, 2/(t-b), 0, 0,
0, 0, 2/(f-n), 0,
(r+l)/(l-r), (t+b)/(b-t), (f+n)/(n-f), 1
In your case r=0, l=width, t=height, b=0, f=1, n=0 and you get:
-2/width, 0, 0, 0,
0, 2/height, 0, 0,
0, 0, 2, 0,
1, -1, -1, 1

Related

How do I apply projection and camera views in OpenGL ES?

I tried applying projection and camera views according to the official documentation (https://developer.android.com/training/graphics/opengl/projection#kotlin). Unfortunately I got an empty screen instead of regular triangle. Below are my onSurfaceCreated and onDrawFrame functions. I populate a projection transformation matrix not in the onSurfaceChanged function (as it is shown in the docs) because I have it never called in my app (in that case the result was the same).
override fun onSurfaceCreated(unused: GL10, config: EGLConfig) {
mTriangle = Triangle()
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f)
Matrix.frustumM(projectionMatrix, 0, -ratio, ratio, -1f, 1f, 3f, 7f)
}
override fun onDrawFrame(unused: GL10) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT)
// Set the camera position (View matrix)
Matrix.setLookAtM(viewMatrix, 0, 0f, 0f, -3f, 0f, 0f, 0f, 0f, 1.0f, 0.0f)
// Calculate the projection and view transformation
Matrix.multiplyMM(vPMatrix, 0, projectionMatrix, 0, viewMatrix, 0)
// Draw shape
mTriangle.draw(vPMatrix)
}
Then I came up with rewriting my draw function. Firstly I set vertexShaderCode as it was said in the docs.
private val vertexShaderCode =
// This matrix member variable provides a hook to manipulate
// the coordinates of the objects that use this vertex shader
"uniform mat4 uMVPMatrix;" +
"attribute vec4 vPosition;" +
"void main() {" +
// the matrix must be included as a modifier of gl_Position
// Note that the uMVPMatrix factor *must be first* in order
// for the matrix multiplication product to be correct.
" gl_Position = uMVPMatrix * vPosition;" +
"}"
Secondly I changed the function itself:
fun draw(mvpMatrix: FloatArray) { // pass in the calculated transformation matrix
// get handle to shape's transformation matrix
vPMatrixHandle = GLES20.glGetUniformLocation(program, "uMVPMatrix")
// Pass the projection and view transformation to the shader
GLES20.glUniformMatrix4fv(vPMatrixHandle, 1, false, mvpMatrix, 0)
// Draw the triangle
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount)
// Disable vertex array
GLES20.glDisableVertexAttribArray(positionHandle)
}
I got an empty screen... Then I tried to combine code above and working version of it. I got:
fun draw(mvpMatrix: FloatArray) {
// Add program to OpenGL ES environment
GLES20.glUseProgram(mProgram)
// get handle to vertex shader's vPosition member
positionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition").also {
// Enable a handle to the triangle vertices
GLES20.glEnableVertexAttribArray(it)
// Prepare the triangle coordinate data
GLES20.glVertexAttribPointer(
it,
COORDS_PER_VERTEX,
GLES20.GL_FLOAT,
false,
vertexStride,
vertexBuffer
)
// get handle to fragment shader's vColor member
mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor").also { colorHandle ->
// Set color for drawing the triangle
GLES20.glUniform4fv(colorHandle, 1, color, 0)
}
// get handle to shape's transformation matrix
vPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix")
// Pass the projection and view transformation to the shader
GLES20.glUniformMatrix4fv(vPMatrixHandle, 1, false, mvpMatrix, 0)
// Draw the triangle
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount)
// Disable vertex array
GLES20.glDisableVertexAttribArray(it)
}
}
This didn't work too. How can I get my triangle drawn?
P.S.
Changing 3f->2f->1f for "near" value at Matrix.frustumM(projectionMatrix, 0, -ratio, ratio, -1f, 1f, 3f, 7f) does nothing
Using gl_Position = vPosition; instead of gl_Position = uMVPMatrix * vPosition; in the vertexShaderCode with second (big) version of draw function gives the triangle. Problem seems to be with matrices.
Okay, finally it works!
Used Matrix.frustumM(projectionMatrix, 0, -1.5f, 1.5f, -1f, 1f, 1f, 7f) and maybe everything the same as above is.

Unable to draw vertices with Z axis on screen

I need to plot few coordinates on screen, the coordinates without Z axis get displayed but the coordinates with Z axis values aren't being displayed. I tried normalizing the coordinates before plotting them which worked. When normalized all the coordinates get plotted. But in the case of unnormalized coordinates the vertices with Z axis are hidden.
OpenGL Version : ES 2.0
Coordinates:
float squareCoords[] = {
202.00002f, 244.00002f, 0.0f,
440.00003f, 625.00006f, 0.0f,
440.00003f, 625.00006f, 0.0f,
690.00006f, 186.0f,0.0f,
202.00002f, 244.00002f, 50.0f,
440.00003f, 625.00006f, 50.0f,
440.00003f, 625.00006f, 50.0f,
690.00006f, 186.0f, 50.0f
};
indices:
short[] drawOrder = {
0,1,2,3,
0,4,
1,5,
2,6,
4,5,6,7
};
Draw Code:
GLES20.glDrawElements(
GLES20.GL_LINES, drawOrder.length,
GLES20.GL_UNSIGNED_SHORT, drawListBuffer);
On Surface Changed code:
public void onSurfaceChanged(GL10 unused, int width, int height) {
mWidth = width;
mHeight = height;
GLES20.glViewport(0, 0, mWidth, mHeight);
float ratio = (float) mWidth / mHeight;
// this projection matrix is applied to object coordinates
// in the onDrawFrame() method
Matrix.orthoM(mProjMatrix, 0, 0f, width, 0.0f, height, 0, 50);
}
OnDraw:
public void onDrawFrame(GL10 unused) {
Square square = new Square();
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT|GLES20.GL_DEPTH_BUFFER_BIT);
if (mFirstDraw)
mFirstDraw = false;
long time = SystemClock.uptimeMillis() % 4000L;
float angle = 0.090f * ((int) time);
// float angle = 90;
// Matrix.setRotateM(mRotationMatrix, 0, angle, 0, 0, -1.0f);
// angle += 0.7f;
if (angle > 360f)
angle = 0f;
Matrix.setLookAtM(mVMatrix, 0, 0f, 0f, 4f, 0f, 0f, 0f, 0f, 1f, 0f);
// projection x view = modelView
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
// Creating rotation matrix
Matrix.setRotateM(rotationMatrix, 0, angle, -1f, 0f, 0f);
// rotation x camera = modelView
float[] duplicateMatrix = Arrays.copyOf(mMVPMatrix, 16);
Matrix.multiplyMM(mMVPMatrix, 0, duplicateMatrix, 0, rotationMatrix, 0);
square.draw(mMVPMatrix);
}
I'm rotating the diagram to figure out whether the vertices on Z axis are drawn.
I personally think this line is the culprit, here I've given far value 50 and near value 0. I wonder what these values should be
Matrix.orthoM(mProjMatrix, 0, 0f, width, 0.0f, height, 0, 50);
The problem here was the value of far wasn't higher enough. I put far as 500
Matrix.orthoM(mProjectionMatrix, 0, 0f, width, 0.0f, height,0, 500);
and changed the coordinates to:
float squareCoords[] = {
202.00002f, 244.00002f, 0.0f,
440.00003f, 625.00006f, 0.0f,
440.00003f, 625.00006f, 0.0f,
690.00006f, 186.0f,0.0f,
202.00002f, 244.00002f, 200.0f,
440.00003f, 625.00006f, 200.0f,
440.00003f, 625.00006f, 200.0f,
690.00006f, 186.0f, 200.0f
};
Its working now.

OpenGL 2D colors gone wrong

I am having problems with my simple 2D OpenGL game.
Its really weird, I get textures to display correctly etc. but when I wanted to create particle effect with simple changing colors, for some reason it displays wrong colors from the buffer. I am using Android's OpenGL ES 1.1 but its the same with any version of OpenGL that uses VBO's.
I initialize the screen etc. and red triangle is displayed, but according to my color buffer it should be white, whats wrong?
GL11 gl11 = (GL11) gl;
gl11.glLoadIdentity();
gl11.glClear(GL_COLOR_BUFFER_BIT);
GLU.gluLookAt(gl, 0f, 0f, -container.getCamera().getScale(), 0f, 0f, 0f, 0f, -1f, 0f);
gl11.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl11.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl11.glTranslatef(container.getCamera().getX(), container.getCamera().getY(), 0.0f);
container.addParticle(new ColouredParticle(-container.getCamera().getX(), -container.getCamera().getY(), (float)Math.random(), (float)Math.random(), 0f, 5000));
particleColorBufferPointer = createFloatBuffer(gl11, GL11.GL_ARRAY_BUFFER, new float[] {
1f, 1f, 1f, 1f,
1f, 1f, 1f, 1f,
1f, 1f, 1f, 1f,
});
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, particleColorBufferPointer);
gl11.glColorPointer(4, GL10.GL_FLOAT, 0, 0);
particleVertexBufferPointer = createFloatBuffer(gl11, GL11.GL_ARRAY_BUFFER, new float[]{
-0.1f, -0.05f,
0.1f, -0.05f,
0.0f, 0.05f
});
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, particleVertexBufferPointer);
gl11.glVertexPointer(2, GL10.GL_FLOAT, 0, 0);
gl11.glDrawArrays(GL11.GL_TRIANGLES, 0, 3);
gl11.glDeleteBuffers(2, new int[]{particleVertexBufferPointer, particleColorBufferPointer}, 0);
gl11.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl11.glDisableClientState(GL10.GL_COLOR_ARRAY);
int error = gl11.glGetError();
if(error != GL11.GL_NO_ERROR) {
Log.v(TAG, "error " + Integer.toHexString(error));
}
Its just simple hardcoded float array containing the triangle vertices and colors, but for some reason the colors are wrong as I said before, what can possibly go wrong with such small amount of code? the color doesn't change to anything but black, like it only reads the red value from the buffer. Also it gives absolutely no error at all!
createFloatBuffer method:
private int createFloatBuffer(GL11 gl, int type, float[] data) {
int[] bufferPointerBuffer = new int[1];
gl.glGenBuffers(1, bufferPointerBuffer, 0);
int bufferPointer = bufferPointerBuffer[0];
gl.glBindBuffer(type, bufferPointer);
FloatBuffer dataBuffer = createFloatBuffer(data);
gl.glBufferData(type, data.length * FLOAT_SIZE, dataBuffer, GL_STATIC_DRAW);
gl.glBindBuffer(type, -1);
return bufferPointer;
}
Wow.. It all was because I had texture bound and it for some reason tried to draw it even if I had texture coords disabled, I wish i could have just listened when I got told to disable any state thats not needed anymore.. Thanks guys! Problem solved!

Android OpenGL ES doesn't display my triangulated shape using TRIANGLES

I want to display shapes as different as one likes using OpenGL ES on an Android device. Problem is that my code doesn't even work for easy shapes like a rectangle (which I am going to use below).
I think somthing is wrong with the glTranslatef. I've adjusted all the values but I can't figure out what it is.
The Rectangle is defined by the points P(0,0,0), P(0,1,0), P(1,1,0), P(1,0,0). In the Activity I implemented the GLSurfaceView.Renderer like this:
private static FloatBuffer getVertexCoords() {
float coords[] = {
0f, 0f, 0f, // first triangle first point
0f, 1f, 0f, // first triangle second point
1f, 1f, 0f, // first triangle third point
1f, 1f, 0f, // second triangle first point
1f, 0f, 0f, // second triangle second point
0f, 0f, 0f, // second triangle third point
}
ByteBuffer vbb = ByteBuffer.allocateDirect(coords.length * 4); // n coords * 4 bytes per float
vbb.order(ByteOrder.nativeOrder());
FloatBuffer trianglesVB = vbb.asFloatBuffer();
trianglesVB.put(coords);
trianglesVB.position(0);
return trianglesVB;
}
#Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0f, 0f, -4f);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); // glBegin
gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, getVertexCoords());
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 2 * 3 * 3); // triangles * points * coords
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); // glEnd
int error = gl.glGetError();
if (error != GL10.GL_NO_ERROR) {
Log.e(TAG, "OpenGL ES Error: " + error);
}
}
#Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
// think this one doesn't matter
}
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // white background
gl.glFrontFace(GL10.GL_CW); // front face is clockwise
}
I think you need a projection matrix in there somewhere. If you don't set one, then you are drawing directly in normalized device coordinates, of which the only valid z-values are from (-1 to 1).
Simply your triangle is outside of the depth range displayed.
Try adding a simple projection matrix to onSurfaceCreated:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10, 10, -10, 10, 0, 10);

Problem rendering a mesh from an OBJ file

I am having a problem adapting and or I guess understanding the vertices/indices/faces in an OBJ file. I want to eventually parse a OBJ file programmatically but first I need to understand how to do it manually. I am using an adaptation of the example from GLWallpaperService(provided by a dev on the web) to do this.
The problem I am having is it seems to work fine with the code he provided to produce a generic cube with different colored faces which he had to implement and draw individually. My adjusted code below trying to draw just a basic cube from the coords I exported from blender and plugged in values.
public class GLCube {
private final int _nrOfVertices = 8;
private FloatBuffer _vertexBuffer;
private FloatBuffer _normalBuffer;
private ShortBuffer _indiceBuffer;
public GLCube() {
this.init();
}
private void init() {
// 3 is the number of coordinates to each vertex.
_vertexBuffer = BufferFactory.createFloatBuffer(_nrOfVertices * 3);
_normalBuffer = BufferFactory.createFloatBuffer(18);
_indiceBuffer = BufferFactory.createShortBuffer(72);
// Coordinates for the vertexes of the cube.
float[] vertexCoords = {
/*
1f, 1f, 0f,
0f, 1f, 0f,
0f, 0f, 0f,
1f, 0f, 0f,
1f, 0f, -1f,
1f, 1f, -1f,
0f, 1f, -1f,
0f, 0f, -1f,
*/
2.195671f, -0.176713f, -1.292541f,
2.195671f, -0.176713f, 0.707459f,
0.195671f, -0.176713f, 0.707459f,
0.195672f, -0.176713f, -1.292542f,
2.195672f, 1.823287f, -1.292541f,
2.195671f, 1.823287f, 0.707460f,
0.195671f, 1.823287f, 0.707459f,
0.195671f, 1.823287f, -1.292541f
};
short[] indicesArray = {/*
0, 1, 2, 0, 2, 3,
3, 4, 0, 4, 5, 0,
7, 2, 6, 6, 2, 1,
4, 7, 5, 7, 6, 5,
1, 0, 6, 6, 0, 5,
2, 7, 3, 7, 4, 3
*/
1,1,2, 1,3,1,
1,1,3, 1,4,1,
5,2,8, 2,7,2,
5,2,7, 2,6,2,
1,3,5, 3,6,3,
1,3,6, 3,2,3,
2,4,6, 4,7,4,
2,4,7, 4,3,4,
3,5,7, 5,8,5,
3,5,8, 5,4,5,
5,6,1, 6,4,6,
5,6,4, 6,8,6
};
//Coordinates for Normal Vector. Used for Lighting calculations
float[] normalCoords = {/*
0f, 0f, 1f, 0f, 0f, 1f,
0f, 0f, 1f, 0f, 0f, 1f,
0f, 0f, 1f, 0f, 0f, 1f,
1f, 0f, 0f, 1f, 0f, 0f,
1f, 0f, 0f, 1f, 0f, 0f,
1f, 0f, 0f, 1f, 0f, 0f,
-1f, 0f, 0f, -1f, 0f, 0f,
-1f, 0f, 0f, -1f, 0f, 0f,
-1f, 0f, 0f, -1f, 0f, 0f,
0f, 0f,-1f, 0f, 0f,-1f,
0f, 0f,-1f, 0f, 0f,-1f,
0f, 0f,-1f, 0f, 0f,-1f,
0f, 1f, 0f, 0f, 1f, 0f,
0f, 1f, 0f, 0f, 1f, 0f,
0f, 1f, 0f, 0f, 1f, 0f,
0f,-1f, 0f, 0f,-1f, 0f,
0f,-1f, 0f, 0f,-1f, 0f,
0f,-1f, 0f, 0f,-1f, 0f
*/
0f, -1f, 0f,
0f, 1f, 0f,
1f, -0f, 0f,
-0f, -0f, 1f,
-1f, -0f, -0f,
0f, 0f, -1f
};
_vertexBuffer.put(vertexCoords);
_normalBuffer.put(normalCoords);
_indiceBuffer.put(indicesArray);
_indiceBuffer.position(0);
_vertexBuffer.position(0);
_normalBuffer.position(0);
}
public void draw(GL10 gl) {
// 3 coordinates in each vertex
// 0 is the space between each vertex. They are densely packed in the array, so the value is 0
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer);
// 0 is the space between each vertex. They are densely packed in the array, so the value is 0
gl.glNormalPointer(GL10.GL_FLOAT, 0, _normalBuffer);
gl.glColor4f(1.0f, 0f, 0f, 1f); //Red
gl.glDrawElements(GL10.GL_TRIANGLES, 72, GL10.GL_UNSIGNED_SHORT, _indiceBuffer);
}
}
This is the Obj file and maybe I'm just not putting the values in the correct arrays but I have been trying to figure this out for awhile and honestly just don't have any background working with OpenGL so this is my first project trying to work with it.
# Blender v2.58 (sub 1) OBJ File: ''
# www.blender.org
v 2.195671 -0.176713 -1.292541
v 2.195671 -0.176713 0.707459
v 0.195671 -0.176713 0.707459
v 0.195672 -0.176713 -1.292542
v 2.195672 1.823287 -1.292541
v 2.195671 1.823287 0.707460
v 0.195671 1.823287 0.707459
v 0.195671 1.823287 -1.292541
vn 0.000000 -1.000000 0.000000
vn 0.000000 1.000000 0.000000
vn 1.000000 -0.000000 0.000000
vn -0.000000 -0.000000 1.000000
vn -1.000000 -0.000000 -0.000000
vn 0.000000 0.000000 -1.000000
usemtl (null)
s off
f 1//1 2//1 3//1
f 1//1 3//1 4//1
f 5//2 8//2 7//2
f 5//2 7//2 6//2
f 1//3 5//3 6//3
f 1//3 6//3 2//3
f 2//4 6//4 7//4
f 2//4 7//4 3//4
f 3//5 7//5 8//5
f 3//5 8//5 4//5
f 5//6 1//6 4//6
f 5//6 4//6 8//6
Any help with this would be so greatly appreciated. I left his values commented in so you could see what works. The image I see on my screen is a completely distorted set of triangles and it appears its trying to make my cube but is just not quite getting there.
An answer containing what to change would be awesome. -=)
I think you're interpreting the .obj format in the wrong way. Have a look at this document.
Basically the indices for your faces into the vertexCoords should be the first ints.
Each face is defined by indices in a format like this:
f v1/t1/n1 v2/t2/n2 v3/t3/n3
Where the v's are vertex indices, the t's are texture coordinate indices and the n's are normal indices.
So for this line (which defines a single face/triangle)
f 1//1 2//1 3//1
the vertex indices would be 1,2,3and the normal indices 1,1,1.
You have no texture coordinates, so no indices there and hence the double slash.
Keep in mind that the indices for the vertices that make up a face and the indices of the associated normals are different. So you will have to create two sets of indices. (See my edit below about glDrawElements though)
Another minor detail is to keep in mind that .obj files start counting with indices at 1, although that might be (an probably is) the 0th value in your array. So you'll have to account for that (i.e. subtract 1 from all index values).
Edit:
Looking at your code I see you're using glDrawElements. From what I know (but correct me if I'm wrong) this doesn't allow you to have multiple (different) sets of indices for vertices, texcoords and normals. So you will have to change your normal data to match the same indices as you have for your vertices.

Categories

Resources