create custom modelviewmatrix in GLSL

Started by
5 comments, last by ehmdjii 15 years, 9 months ago
hello, in GLSL how can i create a modelviewmatrix from a camerapoint and a targetpoint. (and possibly upvector) the two points are given as vec3. (pretty much the same as gluLookAt does) thanks!
Advertisement
You need to have a look at the source code. This is my own version of gluLookAt.
camerapoint is eyePosition3D
and
target would be center3D
Of course, you need to supply a upVector.

void glhLookAtf2(GLfloat *matrix, GLfloat *eyePosition3D, GLfloat *center3D, GLfloat *upVector3D){	GLfloat forward[3], side[3], up[3];	GLfloat matrix2[16], constant, resultMatrix[16];	forward[0]=center3D[0]-eyePosition3D[0];	forward[1]=center3D[1]-eyePosition3D[1];	forward[2]=center3D[2]-eyePosition3D[2];	NormalizeVectorFLOAT_1(forward, &constant);	//Side = forward x up	ComputeNormalOfPlaneFLOAT_2(side, forward, upVector3D);	NormalizeVectorFLOAT_1(side, &constant);	//Recompute up as: up = side x forward	ComputeNormalOfPlaneFLOAT_2(up, side, forward);	matrix2[0]=side[0];	matrix2[4]=side[1];	matrix2[8]=side[2];	matrix2[12]=0.0;	matrix2[1]=up[0];	matrix2[5]=up[1];	matrix2[9]=up[2];	matrix2[13]=0.0;	matrix2[2]=-forward[0];	matrix2[6]=-forward[1];	matrix2[10]=-forward[2];	matrix2[14]=0.0;	matrix2[3]=matrix2[7]=matrix2[11]=0.0;	matrix2[15]=1.0;	MultiplyMatrices4by4OpenGL_FLOAT(resultMatrix, matrix, matrix2);	glhTranslatef2(resultMatrix, -eyePosition3D[0], -eyePosition3D[1], -eyePosition3D[2]);		memcpy(matrix, resultMatrix, 16*sizeof(GLfloat));}
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
thanks,

wow its way more code than i expected.

Why aren't u using gluLookAt?

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

because i would like to do it in GLSL and there is no glulookat as far as i know.
Quote:Original post by ehmdjii
because i would like to do it in GLSL and there is no glulookat as far as i know.


huh? Why would you want to do it in GLSL?

[Edited by - V-man on June 28, 2008 7:53:20 PM]
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
because i would like to generate a depthmap for shadow mapping in ATIs rendermonkey software. and there you dont have access to the fixed function pipeline, only to the shaders, afaik.

This topic is closed to new replies.

Advertisement