-
Notifications
You must be signed in to change notification settings - Fork 0
/
house.cpp
executable file
·201 lines (182 loc) · 5.81 KB
/
house.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <iostream>
#include <GL/glut.h>
#include <math.h>
#include "imageloader.h"
using namespace std;
//GLfloat angle = 0.0f;
/*GLuint loadTexture(Image *image) {
GLuint textureId = 0;
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image->width, image->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->pixels);
return textureId;
}*/
class House {
public:
float length, width, height;
GLuint fTexture, bTexture, sTexture, tTexture;
House() {
length = 10.0f;
width = 10.0f;
height = 20.0f;
}
House(float h, float l, float w) {
length = l;
width = w;
height = h;
}
void setTexture(char *front, char *back, char *sides, char *top) {
Image *image = loadBMP(front);
fTexture = loadTexture(image);
delete image;
image = loadBMP(back);
bTexture = loadTexture(image);
delete image;
image = loadBMP(sides);
sTexture = loadTexture(image);
delete image;
image = loadBMP(top);
tTexture = loadTexture(image);
delete image;
}
void draw() {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glPushMatrix();
glRotatef(90, 0, 1, 0);
//top plane
glBegin(GL_QUADS);
glNormal3f(0.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0);
glVertex3f(-length / 2, height / 2, width / 2);
glTexCoord2f(0.0, 1.0);
glVertex3f(length / 2, height / 2, width / 2);
glTexCoord2f(0.0, 0.0);
glVertex3f(length / 2, height / 2, -width / 2);
glTexCoord2f(1.0, 0.0);
glVertex3f(-length / 2, height / 2, -width / 2);
glEnd();
glBindTexture(GL_TEXTURE_2D, sTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//side plane 1
glBegin(GL_QUADS);
glNormal3f(0.0, 0.0, 1.0);
glTexCoord2f(1.0, 1.0);
glVertex3f(-length / 2, height / 2, width / 2);
glTexCoord2f(0.0, 1.0);
glVertex3f(length / 2, height / 2, width / 2);
glTexCoord2f(0.0, 0.0);
glVertex3f(length / 2, -height / 2, width / 2);
glTexCoord2f(1.0, 0.0);
glVertex3f(-length / 2, -height / 2, width / 2);
glEnd();
//side plane2
glBegin(GL_QUADS);
glNormal3f(0.0, 0.0, -1.0);
glTexCoord2f(1.0, 1.0);
glVertex3f(-length / 2, height / 2, -width / 2);
glTexCoord2f(0.0, 1.0);
glVertex3f(length / 2, height / 2, -width / 2);
glTexCoord2f(0.0, 0.0);
glVertex3f(length / 2, -height / 2, -width / 2);
glTexCoord2f(1.0, 0.0);
glVertex3f(-length / 2, -height / 2, -width / 2);
glEnd();
//bottom plane
glBegin(GL_QUADS);
glNormal3f(0.0, -1.0, 0.0);
glVertex3f(-length / 2, -height / 2, width / 2);
glVertex3f(length / 2, -height / 2, width / 2);
glVertex3f(length / 2, -height / 2, -width / 2);
glVertex3f(-length / 2, -height / 2, -width / 2);
glEnd();
glBindTexture(GL_TEXTURE_2D, fTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//front plane
glBegin(GL_QUADS);
glNormal3f(-1.0, 0.0, 0.0);
glTexCoord2f(1.0, 1.0);
glVertex3f(-length / 2, height / 2, width / 2);
glTexCoord2f(0.0, 1.0);
glVertex3f(-length / 2, height / 2, -width / 2);
glTexCoord2f(0.0, 0.0);
glVertex3f(-length / 2, -height / 2, -width / 2);
glTexCoord2f(1.0, 0.0);
glVertex3f(-length / 2, -height / 2, width / 2);
glEnd();
glBindTexture(GL_TEXTURE_2D, bTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//back plane
glBegin(GL_QUADS);
glNormal3f(1.0, 0.0, 0.0);
glTexCoord2f(1.0, 1.0);
glVertex3f(length / 2, height / 2, width / 2);
glTexCoord2f(0.0, 1.0);
glVertex3f(length / 2, height / 2, -width / 2);
glTexCoord2f(0.0, 0.0);
glVertex3f(length / 2, -height / 2, -width / 2);
glTexCoord2f(1.0, 0.0);
glVertex3f(length / 2, -height / 2, width / 2);
glEnd();
glPopMatrix();
glDisable(GL_TEXTURE_2D);
}
};
/*
House *house;
void display(void) {
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLfloat ambientLight[] = {0.3f, 0.3f, 0.3f, 1.0f};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
GLfloat lightColor[] = {0.7f, 0.7f, 0.7f, 1.0f};
GLfloat lightPos[] = {-0.2f, 0.3f, -1, 0.0f};
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
glColor3f(1.0, 1.0, 1.0);
glTranslatef(0.0, 0.0, -2.0);
glRotatef(angle, 0, 1, 0);
// glScalef(0.1, 0.1, 0.1);
house->draw();
glutSwapBuffers();
angle += 0.02;
if (angle > 360) {
angle -= 360;
}
}
void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (float)w / (float)h, 1.0, 100.0);
}
void initRendering(void) {
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_NORMALIZE);
glEnable(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
house = new House(1, 1, 1);
house->setTexture("hfront.bmp", "hback.bmp", "hside.bmp", "htop.bmp");
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(500, 500);
glutCreateWindow("Test");
initRendering();
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}*/