'k Ben reeds enige tijd aan't zoeken hoe ik een probleem met m'n view matrix kan oplossen.
[edit]Heb alles aangepast op 7/11/04, 19.50u
M'n viewport wordt eerst ingesteld als volgt:
De render loop is als volgt:
De detailcode van het renderen en de camera rotatie code:
[edit] Het probleem is dat de camerabeweging totaal niet klopt en ik vroeg me af waarom...
Hier is de tot waar ik gekomen ben(nieuwe demo):
http://users.pandora.be/novanet/coding/AlterNovaProb4.zip
[edit]Heb alles aangepast op 7/11/04, 19.50u
M'n viewport wordt eerst ingesteld als volgt:
C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
| void CGraphicsOGL::Set_ViewportSize(const int iX, const int iY) { iViewportX = iX; iViewportY = iY; glViewport(0, 0, iViewportX, iViewportY); glMatrixMode(GL_PROJECTION); glLoadIdentity(); // gluPerspective(view angle, aspect ratio of the width to the height, closest distance to the camera // before it clips, farthest distance before it stops drawing) gluPerspective(70.0f,(GLfloat)iViewportX/(GLfloat)iViewportY, 5.0f, 10000.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void CGraphicsOGL::Set_ViewMatrix(const int iMatrixType) { switch (iMatrixType) { case VIEWMATRIX_ORTHOGONAL: glMatrixMode(GL_PROJECTION); glLoadIdentity(); // last parameters are near and far planes //0.1 used to be 0.0, but then text comes after bitmaps in menu's glOrtho(0, iViewportX, iViewportY, 0, 0.1f, -0.1f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glDisable(GL_DEPTH_TEST); break; case VIEWMATRIX_PERSPECTIVE: glMatrixMode(GL_PROJECTION); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glEnable(GL_DEPTH_TEST); break; default: break; } } |
De render loop is als volgt:
C++:
1
2
3
4
5
6
7
| bridgePtr->BeginScene(false); rendererPtr->Set_Camera(camera.GetPosition(), camera.GetLookAt(), camera.GetUpPosition()); sceneObjectManagerPtr->RenderAll(); rendererPtr->Start_GuiViewMatrix(); guiObjectManagerPtr->RenderAll(); rendererPtr->End_GuiViewMatrix(); bridgePtr->EndScene(); |
De detailcode van het renderen en de camera rotatie code:
C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
| void CGraphicsBridgeSDLOGL::BeginScene(bool clearBackBuffer) { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // clear screen and depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); } void CGraphicsBridgeSDLOGL::EndScene() { // swap the graphics buffers SDL_GL_SwapBuffers(); } void CCameraMover::SetViewByRelMouse(const int iMouseRelX, const int iMouseRelY, const float fMouseSpeed) { float fAngleY, fAngleZ; static float fCurrentRotX = 0.0f; if (!camPtr) return; fAngleY = (float)iMouseRelX * fMouseSpeed; fAngleZ = (float)iMouseRelY * fMouseSpeed; fCurrentRotX -= fAngleZ; // restrict camera up/down view if (fCurrentRotX > 1.5f) // in radians { fCurrentRotX = 1.5f; } else if(fCurrentRotX < -1.5f) { fCurrentRotX = -1.5f; } else { // to find the axis we need to rotate around for up and down // movements and need to get a perpendicular vector from the // camera's view vector and up vector which will be the axis CVector3 vAxis = Cross(camPtr->GetLookAt(), camPtr->GetUpVector()); NormalizeFast(vAxis); // rotate around perpendicular axis and along y-axis RotateView(fAngleZ, vAxis.coordinates[0], vAxis.coordinates[1], vAxis.coordinates[2]); RotateView(fAngleY, 0.0f, 1.0f, 0.0f); } } void CCameraMover::RotateView(const float fAngle, const float fX, const float fY, const float fZ) { CVector3 vNewView; if (!camPtr) return; // Get the view vector (The direction we are facing) CVector3 vView = camPtr->GetLookAt(); // Calculate the sine and cosine of the angle once float fCosTheta = (float)cos(fAngle); float fSinTheta = (float)sin(fAngle); // Find the new x position for the new rotated point vNewView.coordinates[0] = (fCosTheta + (1.0f - fCosTheta) * fX * fX) * vView.coordinates[0]; vNewView.coordinates[0] += ((1.0f - fCosTheta) * fX * fY - fZ * fSinTheta) * vView.coordinates[1]; vNewView.coordinates[0] += ((1.0f - fCosTheta) * fX * fZ + fY * fSinTheta) * vView.coordinates[2]; // Find the new y position for the new rotated point vNewView.coordinates[1] = ((1.0f - fCosTheta) * fX * fY + fZ * fSinTheta) * vView.coordinates[0]; vNewView.coordinates[1] += (fCosTheta + (1.0f - fCosTheta) * fY * fY) * vView.coordinates[1]; vNewView.coordinates[1] += ((1.0f - fCosTheta) * fY * fZ - fX * fSinTheta) * vView.coordinates[2]; // Find the new z position for the new rotated point vNewView.coordinates[2] = ((1.0f - fCosTheta) * fX * fZ - fY * fSinTheta) * vView.coordinates[0]; vNewView.coordinates[2] += ((1.0f - fCosTheta) * fY * fZ + fX * fSinTheta) * vView.coordinates[1]; vNewView.coordinates[2] += (fCosTheta + (1.0f - fCosTheta) * fZ * fZ) * vView.coordinates[2]; // add the newly rotated vector to the position to set new rotated camera view camPtr->SetLookAt(vNewView); } |
[edit] Het probleem is dat de camerabeweging totaal niet klopt en ik vroeg me af waarom...
Hier is de tot waar ik gekomen ben(nieuwe demo):
http://users.pandora.be/novanet/coding/AlterNovaProb4.zip
[ Voor 175% gewijzigd door Verwijderd op 08-11-2004 22:57 . Reden: set_viewmatrix aangepast ]