How can I rotate around a scene in webgl on mousedrag (emulating a camera moving around a position)
Okay, So I have been reading for the past few hours and I have managed to get the mouse drag to work on the x axis using the following matrix computation, but no luck with the y axis: where newX = new mouse X coord previousX = mouse X coord at last update position = camera position mvMatrix = model view matrix or 'world matrix'
angle = 0.01*(newX-previousX);
rM = mat4.create();
mat4.identity(rM);
rM[0] = Math.cos(angle);
rM[2] = Math.sin(angle);
rM[8] = -Math.sin(angle);
rM[10] = Math.cos(angle);
mat4.mul开发者_运维问答tiplyVec3(
rM,
position,
position
)
*Note this uses the glMatrix Library (http://code.google.com/p/glmatrix/)
And also in order to always face the position 0,0,0
mat4.lookAt(
position,
vec3.create([0, 0, 0]),
vec3.create([position[0], position[1]+1, position[2]]),
mvMatrix
);
I got the matrix from http://en.wikipedia.org/wiki/Rotation_matrix I have used the matrix under 'basic rotations' and Ry
I am sure this has been done before, any help would be apreciated.
Cheers, Josh
Assuming you want a free-moving camera, with the Z-axis as vertical - each frame, you can do something like this:
mat4.identity(viewMatrix); mat4.translate(viewMatrix, [x,y,z]); mat4.rotate(viewMatrix, degToRad(90-pitch), [-1, 0, 0]); mat4.rotate(viewMatrix, degToRad(yaw), [0, 0, 1]); mat4.multiply(viewMatrix,modelMatrix,modelViewMatrix);
Where degToRad transforms degrees to radians. Then pass the modelViewMatrix and the projection matrix to the vertex shader, which can use:
attribute vec3 aVertexPosition; uniform mat4 modelViewMatrix; uniform mat4 projectionMatrix; gl_Position = projectionMatrix* modelViewMatrix* vec4(aVertexPosition, 1.0);
精彩评论