0

OpenGL でステンシル マスクを作成しようとしています。私はこのソース ( http://open.gl/depthstencils、より具体的にはhttp://open.gl/content/code/c5_reflection.txt ) のモデルに従っていますが、私が知る限り、適切に例に従いました。私のコードは、1 つの正方形のステンシルを描画し、その上に別の正方形を描画しています。最初の正方形と同じスペースを覆っている 2 番目の回転する緑色の正方形の部分だけが見えると思っていました。私が実際に見ているのは、重なり合った 2 つの正方形で、1 つは透明度なしで回転しています。この例との顕著な違いの 1 つは、テクスチャを使用していないことです。問題ありますか?これはもっと簡単な例だと思いました。

私は ES2.0 にかなり慣れていないので、明らかにばかげたことをしている場合はお知らせください。

初期化:

GLuint attributes[] = { GLKVertexAttribPosition, GLKVertexAttribColor, GLKVertexAttribTexCoord0 };
const char *attributeNames[] = { "position", "color", "texcoord0" };

// These are all global GLuint variables
// vshSrc and fshSrc are const char* filenames (the files are found properly)
_myProgram = loadProgram(vshSrc, fshSrc, 3, attributes, attributeNames);
_myProgramUniformMVP = glGetUniformLocation(_myProgram, "modelViewProjectionMatrix");
_myProgramUniformTex = glGetUniformLocation(_myProgram, "tex");
_myProgramUniformOverrideColor = glGetUniformLocation(_myProgram, "overrideColor");

描画ループ:

glEnable(GL_DEPTH_TEST);
glUseProgram(_myProgram);
glDisable(GL_BLEND);

  glClearColor(1.0, 1.0, 1.0, 1.0);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  GLfloat gSquare[20] = { // not using the textures currently
    // posX, posY, posZ,     texX, texY,
    -0.5f, -0.5f, 0,        0.0f, 0.0f,
    0.5f, -0.5f, 0,         1.0f, 0.0f,
    -0.5f, 0.5f, 0,         0.0f, 1.0f,
    0.5f, 0.5f, 0,         1.0f, 1.0f
  };

  // Projection matrix
  float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
  GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 100.0f);
  // Put the squares where they can be seen
  GLKMatrix4 baseModelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -4.0f);

  glEnable(GL_STENCIL_TEST);

  // Build the stencil
  glStencilFunc(GL_ALWAYS, 1, 0xFF); // Set any stencil to 1
  glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  glStencilMask(0xFF); // Write to stencil buffer
  glDepthMask(GL_FALSE); // Don't write to depth buffer
  glClear(GL_STENCIL_BUFFER_BIT); // Clear stencil buffer (0 by default)

  GLKMatrix4 mvp = GLKMatrix4Multiply(projectionMatrix, baseModelViewMatrix);

  // Draw a stationary red square for the stencil (though the color shouldn't matter)
  glUniformMatrix4fv(_chetProgramUniformMVP, 1, 0, mvp.m);
  glUniform1i(_chetProgramUniformTex, 0);
  glUniform4f(_chetProgramUniformOverrideColor, 1.0f, 1.0f, 1.0f,0.0f);

  glVertexAttrib4f(GLKVertexAttribColor, 1, 0, 0, 1);
  glEnableVertexAttribArray(GLKVertexAttribPosition);
  glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 20, gSquare);
  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);



  // Prepare the mask
  glStencilFunc(GL_EQUAL, 1, 0xFF); // Pass test if stencil value is 1
  glStencilMask(0x00); // Don't write anything to stencil buffer
  glDepthMask(GL_TRUE); // Write to depth buffer

  glUniform4f(_myProgramUniformOverrideColor, 0.3f, 0.3f, 0.3f,1.0f);

  // A slow rotating green square to be masked by the stencil
  static float rotation = 0;
  rotation += 0.01;
  baseModelViewMatrix = GLKMatrix4Rotate(baseModelViewMatrix, rotation, 0, 0, 1);
  mvp = GLKMatrix4Multiply(projectionMatrix, baseModelViewMatrix);

  glUniformMatrix4fv(_myProgramUniformMVP, 1, 0, mvp.m);//The transformation matrix
  glUniform1i(_myProgramUniformTex, 0); // The texture
  glVertexAttrib4f(GLKVertexAttribColor, 0, 1, 0, 1);
  glEnableVertexAttribArray(GLKVertexAttribPosition);
  glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 20, gSquare);
  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

  glDisable(GL_STENCIL_TEST);

編集:次のシェーダーのものは、私が抱えていた問題とは無関係です。ステンシルはシェーダーでは行われません。

頂点シェーダー:

attribute vec4 position;
attribute vec4 color;
attribute vec2 texcoord0;

varying lowp vec4 colorVarying;
varying lowp vec2 texcoord;

uniform mat4 modelViewProjectionMatrix;
uniform vec4 overrideColor;
void main()
{
  colorVarying = overrideColor * color;
  texcoord = texcoord0;
  gl_Position = modelViewProjectionMatrix * position;
}

フラグメント シェーダー:

varying lowp vec4 colorVarying;
varying lowp vec2 texcoord;

uniform sampler2D tex;

void main()
{
  gl_FragColor = colorVarying * texture2D(tex, texcoord);
}
4

1 に答える 1