手動の遠近法変換を実行しようとした後、キューブを表示できません。私のコードは以下のとおりです。私はそれがおそらく私の近距離と遠距離の飛行機の数であるのではないかと疑っています。
-(void)drawRect:(NSRect)dirtyRect
{
// get the dimensions of the window
NSSize dim = [self frame].size;
// clear the background with color
glClearColor(0.0, 0.0, 0.0, 0.4);
glViewport(0, 0, dim.width, dim.height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// variables
GLfloat cubeHeight = 90.0;
GLfloat cubeWidth = 90.0;
GLfloat cubeLength = 200.0;
//GLfloat alpha = 0.0;
//GLfloat beta = 0.0;
//GLfloat gamma = 0.0;
// cube position data
GLfloat cubePositionData[] = {0.0, 0.0, 0.0,
0.0, 0.0, cubeLength,
cubeWidth, 0.0, cubeLength,
cubeWidth, 0.0, 0.0,
0.0, cubeHeight, 0.0,
0.0, cubeHeight, cubeLength,
cubeWidth, cubeHeight, cubeLength,
cubeWidth, cubeHeight, 0.0};
// cube indices data
GLubyte cubeIndices[] = {0, 1, 2, 0, 2, 3,
4, 5, 6, 4, 6, 7,
1, 2, 6, 1, 6, 5,
2, 3, 7, 2, 7, 6,
3, 0, 4, 3, 4, 7,
0, 1, 5, 0, 5, 4};
// cube color data
GLfloat cubeColorData[] = {0.0, 0.3, 0.8, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 0.3, 0.8, 1.0,
0.0, 0.3, 0.8, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 0.3, 0.8, 1.0};
// array to hold buffer IDs
GLuint vertexBuffers[2];
// bind each array of data to separate buffers
// bind cube position data to the first buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffers[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubePositionData), cubePositionData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// bind the cube color data to the second buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffers[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeColorData), cubeColorData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// enable the shader program
GLuint programID = [self loadShaders];
glUseProgram(programID);
// enable vertex attributes
// enable cube position attributes
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffers[0]);
glEnableVertexAttribArray(VERTEX_POS_INDEX);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// enable cube color attributes
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffers[1]);
glEnableVertexAttribArray(VERTEX_COLOR_INDEX);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// point to the enabled attribute data
glVertexAttribPointer(VERTEX_POS_INDEX, VERTEX_POS_SIZE, GL_FLOAT, GL_FALSE, 0, cubePositionData);
glVertexAttribPointer(VERTEX_COLOR_INDEX, VERTEX_COLOR_SIZE, GL_FLOAT, GL_FALSE, 0, cubeColorData);
GLfloat offset[] = {0.5, 0.5};
GLint offsetUnif = glGetUniformLocation(programID, "offset");
GLint zNearUnif = glGetUniformLocation(programID, "zNear");
GLint zFarUnif = glGetUniformLocation(programID, "zFar");
GLint frustumScaleUnif = glGetUniformLocation(programID, "frustumScale");
glUniform2fv(offsetUnif, 1, offset);
glUniform1f(frustumScaleUnif, 1.0f);
glUniform1f(zNearUnif, 1.0);
glUniform1f(zFarUnif, 3.0);
// draw the elements
glDrawElements(GL_TRIANGLES, sizeof(cubeIndices)/sizeof(GLubyte), GL_UNSIGNED_BYTE, cubeIndices);
glDisableVertexAttribArray(VERTEX_POS_INDEX);
glDisableVertexAttribArray(VERTEX_COLOR_INDEX);
glUseProgram(0);
// flush buffer
glFlush();
[[self openGLContext] flushBuffer];
}
問題は何でしょうか?ああ、そしてシェーダーも:
#version 120
attribute vec3 position;
attribute vec4 inColor;
uniform vec2 offset;
uniform float zNear;
uniform float zFar;
uniform float frustumScale;
varying vec4 outColor;
void main()
{
vec4 cameraPos = vec4(position, 1.0) + vec4(offset.x, offset.y, 0.0, 0.0);
vec4 clipPos;
clipPos.xy = cameraPos.xy * frustumScale;
clipPos.z = cameraPos.z * (zNear + zFar) / (zNear - zFar);
clipPos.z += 2 * zNear * zFar / (zNear - zFar);
clipPos.w = -cameraPos.z;
gl_Position = clipPos;
outColor = inColor;
}
#version 120
varying vec4 outColor;
void main()
{
gl_FragColor = outColor;
}
OK、コードのセクションを次のように変更しました。
GLfloat offset[] = {-2.0, -2.0};
GLint offsetUnif = glGetUniformLocation(programID, "offset");
GLint zNearUnif = glGetUniformLocation(programID, "zNear");
GLint zFarUnif = glGetUniformLocation(programID, "zFar");
GLint frustumScaleUnif = glGetUniformLocation(programID, "frustumScale");
glUniform2fv(offsetUnif, 1, offset);
glUniform1f(frustumScaleUnif, 1.0);
glUniform1f(zNearUnif, 1.0);
glUniform1f(zFarUnif, 25.0);
私はこれを手に入れます:
台形のベースのように、バックエンド(緑)は遠くでは小さく見え、大きくは見えませんか?