最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I rotate around a scene in webgl on mousedrag (emulating a camera moving around a position) - Stack Overflo

programmeradmin0浏览0评论

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 putation, 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.multiplyVec3(
    rM,
    position,
    position
)

*Note this uses the glMatrix Library (/)

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

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 putation, 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.multiplyVec3(
    rM,
    position,
    position
)

*Note this uses the glMatrix Library (http://code.google./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/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

Share Improve this question asked Apr 1, 2011 at 10:29 Josh McJosh Mc 10.3k8 gold badges56 silver badges68 bronze badges 1
  • Can't help you on this one, but maybe you can get some info from github./mrdoob/three.js – Han Dijk Commented Apr 1, 2011 at 11:50
Add a ment  | 

1 Answer 1

Reset to default 4

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);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论