So, I took the wonderful Xcode-provided template for: OpenGL Game.
The basis is that it loads a cube into a vertex array, and then copies itself twice on the screen does some really basic animation.
So I switched that cube out for a simple 2d plane, and thus far have been happy with how my learning has been progressing. My next step naturally, was to put back in the cube… on top of my 2dplane( my terrain ), and then Ill have the camera follow this ‘cube’ around.
So heres the problem though:
I created another GLFLoat [216] with all the vertices and normals, and I have the camera setup so I CAN view both the 2dplane and the 3dcube without any changes…
But, how do I add this (2nd object= the cube) into the data stream so that it behaves like a completely seperate object… NOT binded to the original 2d plane.
Those who are using XCODE and OpenGL know that know we have the wonderful GLKIT to perform alot of these difficult operations below. And Im afraid that if I simple ‘stack’ this 2nd object(3dcube) to the end of the array right before it goes into the buffer. Then I assume that the 2dplane and the 3dobjects are all thought of as the same “object”… and so If I do any transformations on the data, it will probably affect ALL the data. The code is below:
GLfloat gCubeVertexData[216] =
{
// Data layout for each line below is:
// positionX, positionY, positionZ, normalX, normalY, normalZ,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f
};
GLfloat g2dPlaneData[36] =
{
// Data layout for each line below is:
// positionX, positionY, positionZ, normalX, normalY, normalZ,
-0.5f, -0.0f, -0.5f, 0.0f, 0.1f, 0.0f,
0.5f, -0.0f, -0.5f, 0.0f, 0.1f, 0.0f,
-0.5f, -0.0f, 0.5f, 0.0f, 0.1f, 0.0f,
-0.5f, -0.0f, 0.5f, 0.0f, 0.1f, 0.0f,
0.5f, -0.0f, -0.5f, 0.0f, 0.1f, 0.0f,
0.5f, -0.0f, 0.5f, 0.0f, 0.1f, 0.0f,
};
and then the SETUP
- (void)setupGL
{
[EAGLContext setCurrentContext:self.context];
//int totalNumberOfObjects;
[self loadShaders];
self.effect = [[GLKBaseEffect alloc] init];
self.effect.light0.enabled = GL_TRUE;
self.effect.light0.diffuseColor = GLKVector4Make(1.0f, 0.4f, 0.4f, 1.0f);
glEnable(GL_DEPTH_TEST);
//for (totalNumberOfObjects = 1; totalNumberOfObjects == 2; totalNumberOfObjects ++);{
//}
// Damnit, I dont think this is the way.....
glGenVertexArraysOES(1, &_vertexArray);
glBindVertexArrayOES(_vertexArray);
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g2dPlaneData), g2dPlaneData, GL_STATIC_DRAW);
// glBufferData(GL_ARRAY_BUFFER, sizeof(gCubeVertexData), gCubeVertexData, GL_STATIC_DRAW);
// I can SWITCH out EITHER of the above lines of code and it will still work...
// But, how do I add both of them, and also have each object be independent in the UPDATE methods
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(0));
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(12));
glBindVertexArrayOES(0);
}
And then the UPDATE methods:
- (void)update
{
float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 100.0f);
self.effect.transform.projectionMatrix = projectionMatrix;
GLKMatrix4 baseModelViewMatrix = GLKMatrix4MakeTranslation(0.0f, -0.0, -2.0f);
//NSLog (@"%f", _rotation);
//both objects will rotate as a group
baseModelViewMatrix = GLKMatrix4Scale (baseModelViewMatrix, 1.0f, 1.0f, 1.0f);
// baseModelViewMatrix = GLKMatrix4Rotate(baseModelViewMatrix, (_rotation/3), 1.0f, 0.0f, 0.0f);
// Compute the model view matrix for the object rendered with GLKit
GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, 0.0f);
//both objects rotated on its axis as individuals below
//modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, _rotation, 0.0f, 1.0f, 0.0f);
modelViewMatrix = GLKMatrix4Multiply(baseModelViewMatrix, modelViewMatrix);
// Look @ Variables<#float eyeX#>,<#float eyeY#>,<#float eyeZ#>,<#float centerX#>,<#float centerY#>,<#float centerZ#>,<#float upX#>,<#float upY#>,<#float upZ#>
// eye x,y,z = eye coordinates
// center x,y,z = target coordinates
// up x,y,z = cameras up vector
//
//
//
GLKMatrix4 viewMatrix = GLKMatrix4MakeLookAt(0.0f, 2.0f, 2.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f);
self.effect.transform.modelviewMatrix = GLKMatrix4Multiply (viewMatrix, modelViewMatrix);
// Compute the model view matrix for the object rendered with ES2
//modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, 1.5f);
//modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, _rotation, 1.0f, 1.0f, 1.0f);
//modelViewMatrix = GLKMatrix4Multiply(baseModelViewMatrix, modelViewMatrix);
_normalMatrix = GLKMatrix3InvertAndTranspose(GLKMatrix4GetMatrix3(modelViewMatrix), NULL);
_modelViewProjectionMatrix = GLKMatrix4Multiply(projectionMatrix, modelViewMatrix);
_rotation += self.timeSinceLastUpdate * 0.5f;
}
As you can see from my comments in the SETUP code, I can attach either object to the buffer… But Im not understanding the process for adding both.
So understandably, I have a few questions:
-
You can’t just add the cube data onto the stack for the buffer right? Otherwise, wouldnt the Update methods apply to the entire data sets within? Example: Meaning, you couldnt manipulate specific objects in their own-right way… like rotating a cube, but maybe you have another cube in the stack that needs to move in a completely separate way.
-
If you do still have to add onto the stack of the vertex buffer… You would probably do like an NSARRAY: add with another?
3.Im getting confused with the baseModelView and how it goes up the heiarchy to the view… I understand how it needs to transform from 3d coordinates and multiplied into the eye’s view… etc and into 2d coordinates for the screen. I think I understand that the baseModelView is created based on what was previously assigned to the buffer, as my objects?? meaning both objects, the plane and the cube, are really understood in my program to be the same object? Is this a correct assumption?
How do I manipulate each object independently in my UPDATE method?