Zacznij od poniższego programu. Zwróć uwagę, że używamy w nim rzutowania perspektywicznego.
#include <gl/glut.h>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
const double PI = 4*atan(1);
double eyex;
double eyey;
double eyez;
struct Vec3 {
double x, y, z;
};
void cube()
{
vector<Vec3> v {
{-1,-1, 1}, // 0
{ 1,-1, 1}, // 1
{ 1, 1, 1}, // 2
{-1, 1, 1}, // 3
{-1,-1,-1}, // 4
{ 1,-1,-1}, // 5
{ 1, 1,-1}, // 6
{-1, 1,-1}, // 7
};
// 7 - 6
// / /|
// 3 - 2 5
// | |/
// 0 - 1
vector<vector<int>> f {
{0,1,2,3},
{5,4,7,6},
{0,3,7,4},
{1,5,6,2},
{3,2,6,7},
{1,0,4,5}
};
for(int i=0; i<(int)f.size(); ++i)
{
glBegin(GL_LINE_LOOP);
for(int j=0; j<(int)f[i].size(); ++j)
{
glVertex3d(v[f[i][j]].x, v[f[i][j]].y, v[f[i][j]].z);
}
glEnd();
}
}
// Funkcja update modyfikuje kat angle
double angle = PI/2;
void update()
{
angle += 0.0001;
}
// Funkcja wywołuje update i wymusza odrysowanie
void idlefunc()
{
update();
glutPostRedisplay();
}
void myDisplay() {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
eyex = 3*cos(angle);
eyey = 1.5;
eyez = 3*sin(angle);
gluLookAt(eyex,eyey,eyez, 0,0,0, 0,1,0);
cube();
glutSwapBuffers();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("GLUT WINDOW");
// Perspective projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90, 1, 0.1, 100);
glutDisplayFunc(myDisplay);
glutIdleFunc(idlefunc);
glutMainLoop();
}
Zadanie - Flat Shading
Twoim celem jest uzyskanie poniższego efektu: