29/10/12 18:59
carlduke
Ho questo problema nell'implementare le mie matrici (projection matrix, view matrix e model matrix) per le applicazioni opengl.
Se uso il codice per le versioni piu' vecchie delle opengl:
tutto funziona come dovrebbe, cioe' : img204.imageshack.us/img204/3036/…
mentre usando le mie matrici come vogliono opengl e glsl 3+
non disegna piu' il modello, cioe' lo disegna ma non so dove: img543.imageshack.us/img543/5228/…
Deduco che il problema sia nei calcoli delle matrici, quindi in buildProjection() o translate() o rotate()
posto i sorgenti:
dove GetAt e' definito nel seguente modo (ma e' giusto, lo posto per completezza) :
per costruire la projection matrix ho seguito questo: msdn.microsoft.com/en-us/library/…
Se uso il codice per le versioni piu' vecchie delle opengl:
. . . void DrawAxis(vec3 center = vec3(-1.0,-1.0,-6.0),float scale = 4); . . . while(running) { start_time = SDL_GetTicks(); EventHandler(); GLXSDLRenderPipeline::ClearScreen(); GLXSDLRenderPipeline::SetViewport(); //takes default parameters glMatrixMode(GL_PROJECTION); //reset matrices for drawing gluPerspective(60,800/600,0.1,200); glMatrixMode(GL_MODELVIEW); //reset modelview matrix glLoadIdentity(); DrawAxis(); //Draw axis of the scene glPushMatrix(); glTranslatef(0.0,-0.5,-5.0); glRotatef(angle,0.50,1.0,0.50); DrawAxis(vec3(0.0),2.0); //Draw axis of the model glColor3f(0.70,1.0,1.0); glBindVertexArray(vao[1]); glDrawArrays(GL_TRIANGLES,0,6); glBindVertexArray(0); glPopMatrix(); GLXSDLRenderPipeline::SwapBuffer(); Update(); }
tutto funziona come dovrebbe, cioe' : img204.imageshack.us/img204/3036/…
mentre usando le mie matrici come vogliono opengl e glsl 3+
. . . gl3Math::mat4 glProjectionMatrix; gl3Math::mat4 glViewMatrix; glProjectionMatrix.buildProjection(60,800/600,0.1,200); while(running) { start_time = SDL_GetTicks(); EventHandler(); GLXSDLRenderPipeline::ClearScreen(); GLXSDLRenderPipeline::SetViewport(); glViewMatrix.translate(vec3(0.0,0.0,5.0)); gl3Math::mat4 glModelMatrix; glModelMatrix.LoadIdentity(); glModelMatrix.translate(vec3(0.0,-0.5,-5.0)); glModelMatrix.rotate(angle,0.0,1.0,0.0); DrawAxis(); glMatrixMode(GL_PROJECTION); glLoadMatrixf(glProjectionMatrix); glMatrixMode(GL_MODELVIEW); glLoadMatrixf(glViewMatrix); glPushMatrix(); glMatrixMode(GL_MODELVIEW); glMultMatrixf(glModelMatrix); DrawAxis(vec3(0.0),2.0); glColor3f(0.70,1.0,1.0); glBindVertexArray(vao[1]); glDrawArrays(GL_TRIANGLES,0,6); glBindVertexArray(0); glPopMatrix(); GLXSDLRenderPipeline::SwapBuffer(); Update(); }
non disegna piu' il modello, cioe' lo disegna ma non so dove: img543.imageshack.us/img543/5228/…
Deduco che il problema sia nei calcoli delle matrici, quindi in buildProjection() o translate() o rotate()
posto i sorgenti:
void mat4::translate(vec3 t) { mat4 tm; //translation matrix tm.LoadIdentity(); tm.GetAt(0,3) = t.x; tm.GetAt(1,3) = t.y; tm.GetAt(2,3) = t.z; *this *= tm; } void mat4::rotate(decimal angle,float x,float y,float z) { mat4 rm; rm.LoadIdentity(); decimal s = sin(angle); decimal c = cos(angle); rm.GetAt(0,0) = x*x*(1-c) + c; rm.GetAt(0,1) = x*y*(1-c) - z*s; rm.GetAt(0,2) = x*z*(1-c) + y*s; rm.GetAt(1,0) = x*y*(1-c) + z*s; rm.GetAt(1,1) = y*y*(1-c) + c; rm.GetAt(1,2) = y*z*(1-c) - x*s; rm.GetAt(2,0) = x*z*(1-c) - y*s; rm.GetAt(2,1) = y*z*(1-c) + x*s; rm.GetAt(2,2) = z*z*(1-c) + c; *this *= rm; } void mat4::buildProjection(float fovy,float aspect,float zNear,float zFar) { decimal y = cotan(fovy/2.0); decimal x = y/aspect; Clear(); //set all elements to 0.0 GetAt(0,0) = x; GetAt(1,1) = y; GetAt(2,2) = zFar/(zNear-zFar); GetAt(2,3) = -1; GetAt(3,2) = (zNear*zFar)/(zNear-zFar); }
dove GetAt e' definito nel seguente modo (ma e' giusto, lo posto per completezza) :
class gl3Math::mat4 { public : mat4(); mat4(decimal m[16]); inline decimal& GetAt(int i,int j){ return mat[i*4 + j]; } . . . protected : decimal mat[16]; };
per costruire la projection matrix ho seguito questo: msdn.microsoft.com/en-us/library/…
Ultima modifica effettuata da carlduke 30/10/12 18:45
aaa