Oppure

Loading
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:


. . .

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
30/10/12 20:03
arack95
Un primo errore è questo:

*this *= tm;


Calcoli la matrice traslata(e ruotata) facendo (*this) = (*this) * tm, i fattori dovrebbero essere invertiti (e il prodotto tra matrici non è commutativo)
aaa
30/10/12 20:12
carlduke
accidenti!! quindi dovrei fare

 *this = tm * (*this)


?

grazie mille, non me ne ero accorto :k:
aaa
31/10/12 14:58
carlduke
Ho risolto..!
Il riscritto le funzioni delle matrici in questo modo:


void mat4::translate(vec3 t)
{
       mat4 tm;
	tm.LoadIdentity();

	tm.GetAt(3,0) = t.x;
	tm.GetAt(3,1) = t.y;
	tm.GetAt(3,2) = t.z;

	(*this) = tm * (*this);

}

void mat4::rotate(decimal angle,float x,float y,float z)
{
	

	vec3 axis(x,y,z);
	axis = axis.normalize();

	mat4 rm;

	float b = angle * PI /180;
	float c = cosf(b);
	float ac = 1.0 - c;
	float s = sinf(b);

	rm[0] = axis.x * axis.x * ac + c;
	rm[1] = axis.x * axis.y * ac + axis.z * s;
	rm[2] = axis.x * axis.z * ac - axis.y * s;

	rm[4] = axis.y * axis.x * ac - axis.z * s;
	rm[5] = axis.y * axis.y * ac + c;
	rm[6] = axis.y * axis.z * ac + axis.x * s;
	
	rm[8] = axis.z * axis.x * ac + axis.y * s;
	rm[9] = axis.z * axis.y * ac - axis.x * s;
	rm[10] = axis.z * axis.z * ac + c;

	(*this) = rm * (*this);
}

void mat4::buildProjection(float fovy,float aspect,float zNear,float zFar)
{
   decimal xmin,xmax,ymin,ymax;

   ymax = zNear * tan(fovy * PI / 360.0);
   ymin = -ymax;
   xmin = ymin * aspect;
   xmax = ymax * aspect;

   buildFrustrum(xmin,xmax,ymin,ymax,zNear,zFar);
}

void mat4::buildFrustrum(float left,float right,float bottom,float top,float zNear,float zFar)
{
	Clear();

	GetAt(0,0) = 2*zNear/(right-left);
	GetAt(0,2) = (right+left)/(right-left);
	GetAt(1,1) = 2*zNear/(top-bottom);
	GetAt(1,2) = (top+bottom)/(top-bottom);
	GetAt(2,2) = -((zFar+zNear)/(zFar-zNear));
	GetAt(2,3) = -1.0;
	GetAt(3,2) = -((2.0*zFar*zNear)/(zFar-zNear));
}



aand it works! img43.imageshack.us/img43/593/…

per la funzione Rotate, mi sono basato su questo talisman.org/opengl-1.1/Reference/…

:k:
aaa