Computergrafik
OpenGL MVP Anwendungs Beispiel
← Viewport Transformation | ● | MVP Szene →
Beispiel für ein rotiertes und transliertes Dreieck, dessen Geometrie in lokalen Objektkoordinaten gegeben ist:
mat4 M = mat4::translate(0, 0, -10); // translation along z-axis
mat4 V = mat4::lookat(vec4(0, 0, 0), vec3(0, -10 , 0), vec3(0, 1, 0));
mat4 P = mat4::perspective(60, (float)width()/height(), 1, 100);
lglPerspective(P);
lglModelView(V * M);
lglBegin(LGL_TRIANGLES);
lglVertex(0,0,0);
lglVertex(-1,1,0);
lglVertex(1,1,0);
lglEnd();
mat4 V = mat4::lookat(vec4(0, 0, 0), vec3(0, -10 , 0), vec3(0, 1, 0));
mat4 P = mat4::perspective(60, (float)width()/height(), 1, 100);
lglPerspective(P);
lglModelView(V * M);
lglBegin(LGL_TRIANGLES);
lglVertex(0,0,0);
lglVertex(-1,1,0);
lglVertex(1,1,0);
lglEnd();
Alternative Spezifikation der Projektion mit LGL:
lglProjection(60, (float)width()/height(), 1, 100); // perspective
lglProjection(-(float)width()/height(), (float)width()/height(), -1, 1, -1, 1); // orthographic
lglProjection(-(float)width()/height(), (float)width()/height(), -1, 1, -1, 1); // orthographic
Entsprechender Legacy-Code (OpenGL compatibility profile):
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (float)width()/height(), 1, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslated(0,0,-10); // translation along z-axis
glBegin(GL_TRIANGLES);
glVertex3d(0,0,0);
glVertex3d(-1,1,0);
glVertex3d(1,1,0);
glEnd();
glLoadIdentity();
gluPerspective(60, (float)width()/height(), 1, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslated(0,0,-10); // translation along z-axis
glBegin(GL_TRIANGLES);
glVertex3d(0,0,0);
glVertex3d(-1,1,0);
glVertex3d(1,1,0);
glEnd();
Live Demo: Model-View Teapot (T#05)
← Viewport Transformation | ● | MVP Szene →