Lab2-GeometryPositionColor (using Model-View-Projection matrix)
lab2-moglclassGeometryPositionColor
Geometry – Triangle, Quad, Circle, Sphere, Cube, Cylinder, Torus, Parallelpiped
int main( int argc, char **argv )
{
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH );
…
}
void init( void )
{
…
glEnable(GL_DEPTH_TEST);
glClearColor( 1.0, 1.0, 1.0, 1.0 ); // white background
}
void display( void )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // clear the window
…
glFlush();
}
// using Uniform variables to shader
float g_fovy = 45.0f;
float g_aspect = 1.0f;
float g_zNear = 1.0f;
float g_zFar = 100.f;
glm::vec3 g_eye(0, 0, 3.0f);
glm::vec3 g_at(0, 0, 0);
glm::vec3 g_up(0, 1, 0);
// Model View Projection matrix
glm::mat4 World(1.0f);
glm::mat4 View(1.0f);
glm::mat4 Projection(1.0f);
glm::mat4 MVP(1.0f);
void display( void )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // clear the window
// set drawmode
if (g_wiremode)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // draw wireframe
else
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // draw filled
// Model-View-Projection matrix
Projection = glm::perspective(g_fovy, g_aspect, g_zNear, g_zFar);
View = glm::lookAt(g_eye, g_at, g_up);
World = yRotation;
MVP = Projection * View * World;
spMain.useProgram();
spMain.setUniform(“gMVP“, MVP);
…
}
void reshape(int w, int h)
{
g_aspect = (float) (w/h);
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glutPostRedisplay();
}
void specialkey(int key, int x, int y)
{
switch (key) {
case GLUT_KEY_UP:
g_eye.z += 2;
break;
case GLUT_KEY_DOWN:
g_eye.z -= 2;
break;
case GLUT_KEY_LEFT:
g_at.x -= 1;
g_eye.x -= 1;
break;
case GLUT_KEY_RIGHT:
g_at.x += 1;
g_eye.x += 1;
break;
}
glutPostRedisplay();
}
//////////////////////////////////////////////
simple3.vs
//////////////////////////////////////////////
uniform mat4 gMVP;
in vec3 vPosition;
in vec3 inColor;
out vec4 Color;
void
main()
{
gl_Position = gMVP * vec4(vPosition, 1);
Color = vec4(inColor, 1);
}