0
votes

I have downloaded a sample project that uses OpenGL ES with ios using Objective-C. The app creates a simple cube. I want to make the cube a rectangular prism by decreasing the distance between the front face and back face of the cube (make it slimmer). In order to do that I need to decrease the size of the top, bottom, left, and right faces. I am new to openGL and dont know which code to change in order to decrease the four faces of the cube. Here is the code

typedef struct {
  float Position[3];
  float Color[4];
  float TexCoord[2];
} Vertex;

const Vertex Vertices[] = {
  // Front
  {{1, -1, 1}, {1, 1, 1, 1}, {1, 0}},
  {{1, 1, 1}, {1, 1, 1, 1}, {1, 1}},
  {{-1, 1, 1}, {1, 1, 1, 1}, {0, 1}},
  {{-1, -1, 1}, {1, 1, 1, 1}, {0, 0}},
  // Back
  {{1, 1, -1}, {1, 1, 1, 1}, {0, 1}},
  {{-1, -1, -1}, {1, 1, 1, 1}, {1, 0}},
  {{1, -1, -1}, {1, 1, 1, 1}, {0, 0}},
  {{-1, 1, -1}, {1, 1, 1, 1}, {1, 1}},
  // Left
  {{-1, -1, 1}, {1, 1, 1, 1}, {1, 0}},
  {{-1, 1, 1}, {1, 1, 1, 1}, {1, 1}},
  {{-1, 1, -1}, {1, 1, 1, 1}, {0, 1}},
  {{-1, -1, -1}, {1, 1, 1, 1}, {0, 0}},
  // Right
  {{1, -1, -1}, {1, 1, 1, 1}, {1, 0}},
  {{1, 1, -1}, {1, 1, 1, 1}, {1, 1}},
  {{1, 1, 1}, {1, 1, 1, 1}, {0, 1}},
  {{1, -1, 1}, {1, 1, 1, 1}, {0, 0}},
  // Top
  {{1, 1, 1}, {1, 1, 1, 1}, {1, 0}},
  {{1, 1, -1}, {1, 1, 1, 1}, {1, 1}},
  {{-1, 1, -1}, {1, 1, 1, 1}, {0, 1}},
  {{-1, 1, 1}, {1, 1, 1, 1}, {0, 0}},
  // Bottom
  {{1, -1, -1}, {1, 1, 1, 1}, {1, 0}},
  {{1, -1, 1}, {1, 1, 1, 1}, {1, 1}},
  {{-1, -1, 1}, {1, 1, 1, 1}, {0, 1}},
  {{-1, -1, -1}, {1, 1, 1, 1}, {0, 0}}
};

const GLubyte Indices[] = {
  // Front
  0, 1, 2,
  2, 3, 0,
  // Back
  4, 6, 5,
  4, 5, 7,
  // Left
  8, 9, 10,
  10, 11, 8,
  // Right
  12, 13, 14,
  14, 15, 12,
  // Top
  16, 17, 18,
  18, 19, 16,
  // Bottom
  20, 21, 22,
  22, 23, 20
};

If you guys think that this isnt the code to determine the size of the faces, please tell me what method was probably used so I can find it in the project and post it here.

The problem was fixed thanks to Tommy. But now I have new issue. The size of the four faces has decreased but the front and back face now have a gap between the rest of the faces, here is a screenshot.

enter image description here

How can I move the front face inwards towards the other faces so its attached to them?

1

1 Answers

1
votes

Each entry in the Vertices array defines an instance of the Vertex struct. So the first three things are the Position — the first vertex listed has position {1, -1, 1}, the second has {1, 1, 1}, etc. They're all floating-point numbers in this code so anything will do.

Indices groups the vertices, into triangles it is strongly implied. So the 'front' is the triangle between the 0th, 1st and 2nd vertex plus the triangle between the 2nd, 3rd and 0th vertex.

Therefore the size of the top face is determined by the position of vertices 0, 1, 2 and 3. They all have z = 1. If you changed that to e.g. z = 0.5 then you'd move the top face towards the centre of the cube.