gluPerspective, glViewport, gluLookAt oraz GL_PROJECTION i GL_MODELVIEW Matricies

Pytanie oryginalne

Chcę użyć „gluPerspective”, „glViewport” i „gluLookAt”, aby manipulować moim aparatem i ekranem.

Jakie funkcje mam zastosować do jakiego trybu macierzy? A w jakiej kolejności powinienem / muszę z nich korzystać?

Na przykład próbuję skonfigurować mój ekran i aparat w ten sposób: (Ale to nie działa!)

glMatrixMode(GL_PROJECTION) // Apply following to projection matrix - is this correct?
glLoadIdentity(); // Reset first
glPerspective(45.0, (double)w/(double)h, 1.0, 200.0); // Set perspective
glViewport(0, 0, w, h); // Set viewport

glMatrixMode(GL_MODELVIEW); // Apply following to modelview - should glViewport come under here?
glLoadIdentity(); // Reset first
gluLookAt(px, py, pz, cx, cy, cz, ux, uy, uz); // Set position, centre and then up vectors
// This surely comes after calling GL_MODELVIEW?

Rozejrzałem się za dokumentacją online i rozumiem funkcje, po prostu nie tam, gdzie powinni iść i w jakiej kolejności!

Jakiś czas później...

Jest kilka miesięcy później i dodam szybką edycję, aby pokazać system, którego używam do renderowania rzeczy za pomocą OpenGL. Ma to pomóc innym, którzy zobaczą to pytanie w przyszłości.

Używam głównie dwóch metod.

Metoda 1:

Ta metoda grupuje wszystko razem.

// Firstly, the window may have been resized so re-create the viewing area
glViewport(0, 0, width_of_window_rendering_area, height_of_window_rendering area);

Powoduje to odtworzenie rzutni do renderowania na całym obszarze wnętrza okna. Z sfml możesz zrobić coś takiego jak window.width () lub window.height () lub coś podobnego w zależności od tego, jakiego zestawu narzędzi okienkowych używasz (glut, glfw, sdl itp.) ...

// The second step is to add a projection matrix. There are three main ones I like to use
// Uncomment the one you want
glMatrixMode(GL_PROJECTION); // Tell OpenGL to manipulate the correct matrix stack
glLoadIdentity(); // Reset the projection matrix, or bad things happen after multiple calls to below functions!
// glOrtho( ... ) // Uncomment to use 2D rendering
// gluPerspective( ... ) // Uncomment to use (easy) 3D rendering
glFrustrum( ... ) // Uncomment to use (harder/less intuitive?) 3D rendering
glMatrixMode(GL_MODELVIEW); // I always prefer to leave OpenGL in the modelview manipulation mode.
    // I consider it the "default" and safest mode to leave OpenGL in, as any rogue
    // calls to functions changing its contents is likely to mess up your geometry
    // which should be visible as a problem on the screen, which tells you you need
    // to fix something! Manipulating the other matrix stacks may not show obvious
    // problems.

Musisz wybrać jedną z trzech spośród „glOrtho”, „gluPerspective” i „glFrustrum”. Jest też „gluOrtho2D”, ale używaj go ostrożnie! (Wolę sam określać bliskie i dalekie płaszczyzny za pomocą 'glOrtho'.) Musisz także zastąpić '...' argumentami funkcji.

// The third step is to clear the screen and set your camera / geometry position
glClear(GL_COLOR_BUFFER_BIT); // use bitwise OR ('||') with 'GL_DEPTH_BUFFER_BIT' and 'GL_STENCIL_BUFFER_BIT' if required
gluLookAt( ... );
// I like to use gluLookAt, or at least I _italic_used to! Now I implement my own camera...
 // That is another fun thing you should try if you are comfortable with 3D geometry and hard math!

// The fourth step is to draw your scene. You may want to put lighting stuff first or in with the drawing
glutWireTeapot( ... );

Metoda 2:

Druga metoda jest taka sama jak powyżej, ale aby przenieść pierwszy krok do oddzielnej funkcji. Następnie musisz wykryć zdarzenia zmiany rozmiaru okna i wywołać tę funkcję. W trybie glut można określić wywołanie zwrotne. Dzięki SFML możesz wykryć zdarzenie po zmianie rozmiaru okna. Zapomniałem, jak działa SDL, ale jest podobny. Nie dowiedziałem się jeszcze, jak działa glfw.

Mam nadzieję, że ci to pomoże.

Niektórzy nowicjusze OpenGL (prawdopodobnie sam zawarty w tym samym czasie) próbują określić tłumaczenia kamer na macierzy PROJECTION.Nie rób tego - Kieruję nim, aż zaśmieca oświetlenie i ewentualnie inne rzeczy.

questionAnswers(1)

yourAnswerToTheQuestion