好的,我一遍又一遍地检查,到目前为止,我“认为”我了解 opengl 是如何绘制的以及如何指定我想要绘制的内容,但是我似乎无法让它绘制超过 1 条线。
使用 Xcode 4.2 中的模板,我可以获得以下代码。它确实使用 GLKIT 绘制了我的方形骨架,因为我只是忽略了 opengl es 2.0 方法不使用着色器 atm,但是当我触摸屏幕时它应该绘制我自己的数组,它没有......
所以首先是方形格式(只是为了显示他们在模板中使用的变量类型)
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, -.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
};
这是我在“View did Load”函数中调用一次的 SetupGL。
- (void)setupGL
{
[EAGLContext setCurrentContext:self.context];
[self loadShaders];
self.effect = [[GLKBaseEffect alloc] init];
self.effect.light0.enabled = GL_TRUE; //No Idea why it wont work without this
glGenVertexArraysOES(1, &_vertexArray);
glBindVertexArrayOES(_vertexArray);
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(gCubeVertexData), gCubeVertexData, GL_STATIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(0));
glBindVertexArrayOES(0);
}
这是更新方法(只是让东西旋转)
- (void)update
{
float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 10000.0f);
self.effect.transform.projectionMatrix = projectionMatrix;
GLKMatrix4 baseModelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -10.0f);
baseModelViewMatrix = GLKMatrix4Rotate(baseModelViewMatrix, _rotation, 0.0f, 1.0f, 0.0f);
// Compute the model view matrix for the object rendered with GLKit
GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -1.5f);
modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, _rotation, 1.0f, 1.0f, 1.0f);
modelViewMatrix = GLKMatrix4Multiply(baseModelViewMatrix, modelViewMatrix);
self.effect.transform.modelviewMatrix = modelViewMatrix;
_rotation += self.timeSinceLastUpdate * 0.5f;
}
Draw In Rect 方法(我认为的渲染)
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArrayOES(_vertexArray);
// Render the object with GLKit
[self.effect prepareToDraw];
glDrawArrays(GL_LINE_STRIP, 0, 36); //Probably have to change the 36
}
这是触摸部分:
在开始接触时,我只是在创建 NSMutableData
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
startPoint = [touch locationInView:self.view];
vertexData3 = [NSMutableData dataWithLength: sizeof(GLKVector2)]; //HERE
if (!referenceAttitude)
referenceAttitude = motionManager.deviceMotion.attitude;
writtingON = YES;
}
在 Touches Ended 时,我正在尝试使用我得到的新顶点进行绘制。顶点是使用触发运动管理器读数的计时器获得的,在用户触摸屏幕的位置进行一些数学运算,因此值应该在屏幕坐标的范围内。
但是为了测试绘图,我只是创建了一个临时 NSMUtableData 变量,其中包含一些我手动添加的值(添加太可怕了)。我正在使用 NSMutableData,因为这就是他们在另一个教程中的建议。我 90% 确定它正确保存了数据,因为当我检索它时它就在那里。(http://games.ianterrell.com/how-to-draw-2d-shapes-with-glkit/)
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
////////Test array
tempVert = GLKVector2Make(10, -8);
NSRange r = {0*sizeof(GLKVector2), sizeof(GLKVector2)};
[vertexData3 replaceBytesInRange: r withBytes:&tempVert];
[vertexData3 increaseLengthBy:sizeof(GLKVector2)];
tempVert = GLKVector2Make(2, -8);
NSRange q = {1*sizeof(GLKVector2), sizeof(GLKVector2)};
[vertexData3 replaceBytesInRange: q withBytes:&tempVert];
[vertexData3 increaseLengthBy:sizeof(GLKVector2)];
tempVert = GLKVector2Make(4, 9);
NSRange w = {2*sizeof(GLKVector2), sizeof(GLKVector2)};
[vertexData3 replaceBytesInRange: w withBytes:&tempVert];
[vertexData3 increaseLengthBy:sizeof(GLKVector2)];
tempVert = GLKVector2Make(1, -9);
NSRange e = {3*sizeof(GLKVector2), sizeof(GLKVector2)};
[vertexData3 replaceBytesInRange: e withBytes:&tempVert];
[vertexData3 increaseLengthBy:sizeof(GLKVector2)];
tempVert = GLKVector2Make(2, -2);
NSRange t = {4*sizeof(GLKVector2), sizeof(GLKVector2)};
[vertexData3 replaceBytesInRange: t withBytes:&tempVert];
[vertexData3 increaseLengthBy:sizeof(GLKVector2)];
//////////////////////
glGenVertexArraysOES(1, &_vertexArray);
glBindVertexArrayOES(_vertexArray);
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData3), [vertexData3 mutableBytes], GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glBindVertexArrayOES(0);
}
最后一部分是让我发疯的部分!我认为它应该做的是,用我自己的数组修改缓冲区,以便自动更新绘制它。
任何建议将不胜感激。
谢谢
哦,如果有人知道如何设置正交矩阵,因为我真的只对二维组件 atm 感兴趣,所以我不能用透视矩阵替换它。
编辑:
好的,我只是尝试发送一个随机的 GLfloat 数组,它确实改变了形状,但为什么不使用我的 NSMutableData 数组呢?我以为我正在做与教程中完全相同的程序....