何度もチェックしてきましたが、これまでのところ、openglがどのように描画しているか、何を描画するかを指定する方法を理解していると思いますが、1本以上の線を描画させることはできません。
Xcode 4.2のテンプレートを使用すると、次のコードを取得できます。シェーダーatmを使用しないopengles2.0メソッドを無視しているため、GLKITを使用して正方形のスケルトンを描画しますが、画面にタッチすると、独自の配列が描画されますが、描画されません。
したがって、最初に正方形の形式(テンプレートで使用する変数のタイプを示すためだけに)
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
};
これが、「ViewdidLoad」関数で1回呼び出す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;
}
Touchs 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);
}
この最後の部分は私を夢中にさせる部分です!!! 自動更新でバッファが描画されるように、自分の配列でバッファを変更する必要があると思います。
どんな提案でも本当にありがたいです。
ありがとう
ああ、もし誰かが直交行列を設定する方法を知っているなら、私は本当に2Dコンポーネントatmにしか興味がないので、その遠近法行列に置き換えることはできません。
編集:
さて、GLfloatsのランダムな配列を送信しようとしましたが、形状が変わりますが、NSMutableDataの配列を使用しないのはなぜですか?チュートリアルとまったく同じ手順を実行していると思いました。