OpenGL ES 2.0 (specifically for the iphone) rendering is slightly off. Best guess is it's a projection matrix problem -


so bought o'reilly's iphone 3d programming , found believe bug in there code. can't figure out problem is, , unless can't move forward own code.

i paste consider appropriate code post luckily code available online at: http://examples.oreilly.com/9780596804831/hellocone/

the problem having opengl es 2.0 renderer, not show in es 1.1 renderer.

so have been noticing cone not render in correct position. test changed modelviewmatrix render on frustumnear plane. cone should appear cut in two. when es 1.1 render case, when same in opengl es 2.0 not. cone part there, shaved off. meaning not landing on fustrum's near face.

here initialization code projection matrix created , set up:

void renderingengine2::initialize(int width, int height) { const float coneradius = 0.5f; const float coneheight = 1.0f; const int coneslices = 40;  {     // allocate space cone vertices.     m_cone.resize((coneslices + 1) * 2);      // initialize vertices of triangle strip.     vector<vertex>::iterator vertex = m_cone.begin();     const float dtheta = twopi / coneslices;     (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 disk vertices.     m_disk.resize(coneslices + 2);      // initialize center vertex of 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 rim vertices of triangle fan.     const float dtheta = twopi / coneslices;     (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 depth buffer. glgenrenderbuffers(1, &m_depthrenderbuffer); glbindrenderbuffer(gl_renderbuffer, m_depthrenderbuffer); glrenderbufferstorage(gl_renderbuffer,                       gl_depth_component16,                       width,                       height);  // create framebuffer object; attach depth , 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 color buffer rendering. glbindrenderbuffer(gl_renderbuffer, m_colorrenderbuffer);  // set gl state. glviewport(0, 0, width, height); glenable(gl_depth_test);  // build glsl program. m_simpleprogram = buildprogram(simplevertexshader, simplefragmentshader); gluseprogram(m_simpleprogram);  // set 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()); } 

and here render code. can see have changed modelviematrix place cone on bottom left corner of near frustum face.

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 translation = mat4::translate(-1.6, -2.4, -5);

// set model-view matrix. glint modelviewuniform = glgetuniformlocation(m_simpleprogram, "modelview"); mat4 modelviewmatrix = rotation * translation; gluniformmatrix4fv(modelviewuniform, 1, 0, modelviewmatrix.pointer());  // draw 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 disk caps off base of 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); } 

looks found answer own question.

the projection matrix in o'reilly code being calculated incorrectly.

in code have:

t = 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; 

however not projection matrix. m.w.w should 0 not 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; 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -