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
/
yard.cpp
153 lines (119 loc) · 3.79 KB
/
yard.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// ========================================================================
// COSC363: Computer Graphics (2018); University of Canterbury.
//
// FILE NAME: Yard.cpp
// See Lab03.pdf for details.
// ========================================================================
#include <iostream>
#include <cmath>
#include <GL/glut.h>
#include "loadTGA.h"
using namespace std;
GLuint txId[2]; //Texture ids
float angle = 0, look_x, look_z = -1., eye_x, eye_z; //Camera parameters
//--------------------------------------------------------------------------------
void loadTexture()
{
glGenTextures(2, txId); // Create 2 texture ids
glBindTexture(GL_TEXTURE_2D, txId[0]); //Use this texture
loadTGA("Wall.tga");
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //Set texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, txId[1]); //Use this texture
loadTGA("Floor.tga");
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);
}
//--------------------------------------------------------------------------------
void special(int key, int x, int y)
{
if (key == GLUT_KEY_LEFT) angle -= 0.1; //Change direction
else if (key == GLUT_KEY_RIGHT) angle += 0.1;
else if (key == GLUT_KEY_DOWN)
{ //Move backward
eye_x -= 0.1*sin(angle);
eye_z += 0.1*cos(angle);
}
else if (key == GLUT_KEY_UP)
{ //Move forward
eye_x += 0.1*sin(angle);
eye_z -= 0.1*cos(angle);
}
look_x = eye_x + 100 * sin(angle);
look_z = eye_z - 100 * cos(angle);
glutPostRedisplay();
}
//--------------------------------------------------------------------------------
void initialise()
{
// loadTexture();
// glEnable(GL_TEXTURE_2D);
glClearColor(0., 1., 1., 1.); //Background colour
}
//--------------------------------------------------------------------------------
void walls()
{
glColor3f(0.8, 0.7, 0.3); //replace with a texture
glBegin(GL_QUADS);
////////////////////// BACK WALL ///////////////////////
glVertex3f(-15, 1, -15);
glVertex3f(-15, -1, -15);
glVertex3f(15, -1, -15);
glVertex3f(15, 1, -15);
////////////////////// FRONT WALL ///////////////////////
glVertex3f(-15, 1, 15);
glVertex3f(-15, -1, 15);
glVertex3f(15, -1, 15);
glVertex3f(15, 1, 15);
////////////////////// LEFT WALL ///////////////////////
glVertex3f(-15, 1, -15);
glVertex3f(-15, -1, -15);
glVertex3f(-15, -1, 15);
glVertex3f(-15, 1, 15);
////////////////////// RIGHT WALL ///////////////////////
glVertex3f(15, 1, -15);
glVertex3f(15, -1, -15);
glVertex3f(15, -1, 15);
glVertex3f(15, 1, 15);
glEnd();
}
//--------------------------------------------------------------------------------
void floor()
{
glColor3d(0.6, 0.25, 0.25); //replace with a texture
glBegin(GL_QUADS);
glVertex3f(-15, -1, -15);
glVertex3f(-15, -1, 15);
glVertex3f(15, -1, 15);
glVertex3f(15, -1, -15);
glEnd();
}
//--------------------------------------------------------------------------------
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45., 1., 1., 100.);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eye_x, 0, eye_z, look_x, 0, look_z, 0, 1, 0);
walls();
floor();
glutSwapBuffers();
}
//--------------------------------------------------------------------------------
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(600, 600);
glutInitWindowPosition(10, 10);
glutCreateWindow("The Yard");
initialise();
glutDisplayFunc(display);
glutSpecialFunc(special);
glutMainLoop();
return 0;
}