私のピラミッドは前が小さく、後ろが大きいようです。
-(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);
// cube position data
GLfloat vertices[] = {0.0, 100.0, 0.0, -50.0, 0.0, 50.0, 50.0, 0.0, 50.0, 50.0, 0.0, -50.0, -50.0, 0.0, -50.0};
// cube indices data
GLubyte indices[] = {0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 1, 1, 4, 3, 1, 3, 2};
// cube color data
GLfloat color[] = {0.0, 1.0, 0.0, 1.0,
0.0, 0.3, 0.8, 1.0,
1.0, 0.0, 0.0, 1.0,
0.5, 0.0, 0.8, 1.0,
0.5, 0.6, 0.3, 1.0};
// 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(vertices), vertices, 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(color), color, 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, vertices);
glVertexAttribPointer(VERTEX_COLOR_INDEX, VERTEX_COLOR_SIZE, GL_FLOAT, GL_FALSE, 0, color);
GLfloat offset[] = {0.0, 0.0, -200.0};
GLint offsetUnif = glGetUniformLocation(programID, "offset");
GLint zNearUnif = glGetUniformLocation(programID, "zNear");
GLint zFarUnif = glGetUniformLocation(programID, "zFar");
GLint frustumScaleUnif = glGetUniformLocation(programID, "frustumScale");
glUniform3fv(offsetUnif, 1, offset);
glUniform1f(frustumScaleUnif, 1.0f);
glUniform1f(zNearUnif, 0.1);
glUniform1f(zFarUnif, 1000.0);
// draw the elements
glDrawElements(GL_TRIANGLES, sizeof(indices)/sizeof(GLubyte), GL_UNSIGNED_BYTE, indices);
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 vec3 offset;
uniform float zNear;
uniform float zFar;
uniform float frustumScale;
varying vec4 outColor;
void main()
{
vec4 cameraPos = vec4(position.x, position.y, -position.z, 1.0) + vec4(offset.x, offset.y, offset.z, 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;
}
コードを実行すると、この画像が表示されます。表よりも後ろの方が大きいピラミッドのように見えることに注意してください。
「前」の角を丸で囲みました(反対側から見ているはずなので、実際には後ろの角です)。問題がどこにあるのかについての考えや提案はありますか?