This repository has been archived by the owner on Apr 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tower.cpp
123 lines (104 loc) · 3.76 KB
/
Tower.cpp
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//===================================================================
// COSC363 Computer Graphics (2018). University of Canterbury.
//
// Tower.cpp
// See Lab04.pdf for details
//===================================================================
#include <cmath>
#include <GL/freeglut.h>
#include "loadBMP.h"
using namespace std;
GLuint txId; //Texture id
float viewAngle = 0.0; //Camera view angle
const int N = 18; //Number of vertices of the base polygon
//--------Loads a bitmap texture------------------------------------------
void loadTexture()
{
glGenTextures(1, &txId); // Create a Texture object
glBindTexture(GL_TEXTURE_2D, txId); //Use this texture
loadBMP("TowerTexture.bmp");
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //Set texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
}
//-----Computes surface normal vector-------------------------------
void normal(float x1, float y1, float z1,
float x2, float y2, float z2,
float x3, float y3, float z3 )
{
float nx, ny, nz;
nx = y1*(z2-z3)+ y2*(z3-z1)+ y3*(z1-z2);
ny = z1*(x2-x3)+ z2*(x3-x1)+ z3*(x1-x2);
nz = x1*(y2-y3)+ x2*(y3-y1)+ x3*(y1-y2);
glNormal3f(nx, ny, nz);
}
//-----------------------------------------------------------------------
void initialise(void)
{
//=====Uncomment the following 3 lines before implementing texture mapping=====
// loadTexture();
// glEnable(GL_TEXTURE_2D);
// glBindTexture(GL_TEXTURE_2D, txId);
//=============================================================================
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glColor3f(0., 0., 0.);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0, 1.0, 40.0, 1000.);;
}
//------------------------------------------------------------------
void display(void)
{
float lpos[4]={-100., 100., 0., 1.}; //light's position
float eye_x, eye_z; //Camera position
const float CDR = 3.14159265/180.0; //Degrees to radians conversion factor
float vx[N] = {0., 8., 11., 11., 10.4, 9.5, 8., 6., 3., 0., -3., -6., -8., -9.5, -10.4, -11., -11., -8.};
float vy[N] = {0};
float vz[N] = {19.4, 8., 5., 0., -4., -8., -11., -12., -12.4, -12.5, -12.4, -12., -11., -8., -4., 0., 5., 8.};
float wx[N], wy[N], wz[N];
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
eye_x = 150.0 * sin(viewAngle*CDR); //Update camera position
eye_z = 150.0 * cos(viewAngle*CDR);
gluLookAt(eye_x, 100.0, eye_z, 0.0, 100.0, 0.0, 0.0, 1.0, 0.0);
glLightfv(GL_LIGHT0, GL_POSITION, lpos);
glColor3f(0., 1., 1.);
// ==== Remove the following 5 lines of code and replace with a 3D model! ====
glNormal3f(0.0, 1.0, 0.0);
glBegin(GL_LINE_LOOP);
for(int i = 0; i < N; i++)
glVertex3f(vx[i], vy[i], vz[i]);
glEnd();
// =================================================================
glFlush();
}
//-------special keyboard function callback-------------------------------
void special(int key, int xp, int yp)
{
if(key==GLUT_KEY_LEFT)
viewAngle += 5.0;
else if(key==GLUT_KEY_RIGHT)
viewAngle -= 5.0;
glutPostRedisplay();
}
//-------------------------------------------------------------------
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_DEPTH);
glutInitWindowSize(600,600);
glutInitWindowPosition(0, 0);
glutCreateWindow("The Turning Torso");
initialise();
glutDisplayFunc(display);
glutSpecialFunc(special);
glutMainLoop();
return 0;
}