OpenGL ES 2.0 (speziell für das iPhone) Wiedergabe ist leicht ab. Beste Vermutung ist, es ist eine Projektionsmatrix Problem

StackOverflow https://stackoverflow.com/questions/3886329

Frage

So kaufte ich O'Reillys Iphone 3D-Programmierung und fand, was ich glaube, um ein Fehler in ihm Code. Allerdings kann ich nicht herausfinden, was das Problem ist, und wenn ich nicht tun kann ich nicht vorwärts mit meinem eigenen Code bewegen.

Ich werde fügen, was ich für den entsprechenden Code in diesem Beitrag sein, aber glücklicherweise der gesamte Code ist online verfügbar unter: http://examples.oreilly.com/9780596804831/HelloCone/

Das Problem Ich habe mit ihrem OpenGL ES 2.0-Renderer ist, zeigt es nicht in ihrem ES 1.1 Renderer auf.

Also, was ich habe bemerkt worden ist, dass der Kegel nicht genau in der richtigen Position nicht machen. Um dies zu testen änderte ich die ModelViewMatrix machen genau auf der FrustumNear Ebene. So ist der Kegel sollte in zwei Teile geschnitten vollständig erscheinen. Wenn ich dies tue mit dem ES 1.1 macht dies der Fall ist, wenn ich das gleiche tue 2.0 in OpenGL ES aber es nicht ist. Der Kegel ist zum größten Teil dort, aber leicht abgeschabt. Bedeutung es nicht landet genau auf der fustrum der in der Nähe von Gesicht.

Hier ist der Code für die Initialisierung, wo die Projektionsmatrix erstellt und eingerichtet ist:

void RenderingEngine2::Initialize(int width, int height)
{
const float coneRadius = 0.5f;
const float coneHeight = 1.0f;
const int coneSlices = 40;

{
    // Allocate space for the cone vertices.
    m_cone.resize((coneSlices + 1) * 2);

    // Initialize the vertices of the triangle strip.
    vector<Vertex>::iterator vertex = m_cone.begin();
    const float dtheta = TwoPi / coneSlices;
    for (float theta = 0; vertex != m_cone.end(); theta += dtheta) {

        // Grayscale gradient
        float brightness = abs(sin(theta));
        vec4 color(brightness, brightness, brightness, 1);

        // Apex vertex
        vertex->Position = vec3(0, 1, 0);
        vertex->Color = color;
        vertex++;

        // Rim vertex
        vertex->Position.x = coneRadius * cos(theta);
        vertex->Position.y = 1 - coneHeight;
        vertex->Position.z = coneRadius * sin(theta);
        vertex->Color = color;
        vertex++;
    }
}

{
    // Allocate space for the disk vertices.
    m_disk.resize(coneSlices + 2);

    // Initialize the center vertex of the triangle fan.
    vector<Vertex>::iterator vertex = m_disk.begin();
    vertex->Color = vec4(0.75, 0.75, 0.75, 1);
    vertex->Position.x = 0;
    vertex->Position.y = 1 - coneHeight;
    vertex->Position.z = 0;
    vertex++;

    // Initialize the rim vertices of the triangle fan.
    const float dtheta = TwoPi / coneSlices;
    for (float theta = 0; vertex != m_disk.end(); theta += dtheta) {
        vertex->Color = vec4(0.75, 0.75, 0.75, 1);
        vertex->Position.x = coneRadius * cos(theta);
        vertex->Position.y = 1 - coneHeight;
        vertex->Position.z = coneRadius * sin(theta);
        vertex++;
    }
}

// Create the depth buffer.
glGenRenderbuffers(1, &m_depthRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, m_depthRenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER,
                      GL_DEPTH_COMPONENT16,
                      width,
                      height);

// Create the framebuffer object; attach the depth and color buffers.
glGenFramebuffers(1, &m_framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER,
                          GL_COLOR_ATTACHMENT0,
                          GL_RENDERBUFFER,
                          m_colorRenderbuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER,
                          GL_DEPTH_ATTACHMENT,
                          GL_RENDERBUFFER,
                          m_depthRenderbuffer);

// Bind the color buffer for rendering.
glBindRenderbuffer(GL_RENDERBUFFER, m_colorRenderbuffer);

// Set up some GL state.
glViewport(0, 0, width, height);
glEnable(GL_DEPTH_TEST);

// Build the GLSL program.
m_simpleProgram = BuildProgram(SimpleVertexShader, SimpleFragmentShader);
glUseProgram(m_simpleProgram);

// Set the projection matrix.
GLint projectionUniform = glGetUniformLocation(m_simpleProgram, "Projection");
mat4 projectionMatrix = mat4::Frustum(-1.6f, 1.6, -2.4, 2.4, 5, 10);
glUniformMatrix4fv(projectionUniform, 1, 0, projectionMatrix.Pointer());
}

Und hier ist der Code übertragen. Wie man sehen kann ich die ModelVieMatrix geändert haben den Kegel auf der linken unteren Ecke des nahen Frustum Gesicht zu setzen.

void RenderingEngine2::Render() const
{
GLuint positionSlot = glGetAttribLocation(m_simpleProgram, "Position");
GLuint colorSlot = glGetAttribLocation(m_simpleProgram, "SourceColor");

glClearColor(0.5f, 0.5f, 0.5f, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnableVertexAttribArray(positionSlot);
glEnableVertexAttribArray(colorSlot);

MAT4 Rotation (m_animation.Current.ToMatrix ());     MAT4 Übersetzung = MAT4 :: Übersetzen (-1,6, -2,4, -5);

// Set the model-view matrix.
GLint modelviewUniform = glGetUniformLocation(m_simpleProgram, "Modelview");
mat4 modelviewMatrix = rotation * translation;
glUniformMatrix4fv(modelviewUniform, 1, 0, modelviewMatrix.Pointer());

// Draw the cone.
{
  GLsizei stride = sizeof(Vertex);
  const GLvoid* pCoords = &m_cone[0].Position.x;
  const GLvoid* pColors = &m_cone[0].Color.x;
  glVertexAttribPointer(positionSlot, 3, GL_FLOAT, GL_FALSE, stride, pCoords);
  glVertexAttribPointer(colorSlot, 4, GL_FLOAT, GL_FALSE, stride, pColors);
  glDrawArrays(GL_TRIANGLE_STRIP, 0, m_cone.size());
}

// Draw the disk that caps off the base of the cone.
{
  GLsizei stride = sizeof(Vertex);
  const GLvoid* pCoords = &m_disk[0].Position.x;
  const GLvoid* pColors = &m_disk[0].Color.x;
  glVertexAttribPointer(positionSlot, 3, GL_FLOAT, GL_FALSE, stride, pCoords);
  glVertexAttribPointer(colorSlot, 4, GL_FLOAT, GL_FALSE, stride, pColors);
  glDrawArrays(GL_TRIANGLE_FAN, 0, m_disk.size());
}

glDisableVertexAttribArray(positionSlot);
glDisableVertexAttribArray(colorSlot);
}
War es hilfreich?

Lösung

Sieht aus wie ich die Antwort auf meine eigene Frage gefunden.

Die Projektionsmatrix im Code O'Reilly falsch berechnet wird.

In ihrem Code, den sie haben:

T a = 2 * near / (right - left);
T b = 2 * near / (top - bottom);
T c = (right + left) / (right - left);
T d = (top + bottom) / (top - bottom);
T e = - (far + near) / (far - near);
T f = -2 * far * near / (far - near);
Matrix4 m;
m.x.x = a; m.x.y = 0; m.x.z = 0; m.x.w = 0;
m.y.x = 0; m.y.y = b; m.y.z = 0; m.y.w = 0;
m.z.x = c; m.z.y = d; m.z.z = e; m.z.w = -1;
m.w.x = 0; m.w.y = 0; m.w.z = f; m.w.w = 1;
return m;

Dies ist jedoch nicht die Projektionsmatrix. m.w.w sollte 0 nicht 1.

Matrix4 m;
m.x.x = a; m.x.y = 0; m.x.z = 0; m.x.w = 0;
m.y.x = 0; m.y.y = b; m.y.z = 0; m.y.w = 0;
m.z.x = c; m.z.y = d; m.z.z = e; m.z.w = -1;
m.w.x = 0; m.w.y = 0; m.w.z = f; m.w.w = 0;
return m;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top