Repository navigation
Expand file tree
/
Copy pathProgram2.cpp
More file actions
126 lines (90 loc) · 3.87 KB
/
Copy pathProgram2.cpp
File metadata and controls
126 lines (90 loc) · 3.87 KB
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
/*
Author: Saif Mahmud
Date : 1 / 25 / 2023
Program : 2
Description: This program draws a house with a triangle roof 5 times with translating and rotating.
*/
#include <iostream>
#include <GL/glut.h>
#include <cmath>
namespace Program2 {
static const double PI = atan(1) * 4;
static const int houseCount = 5;
//set width and height of the display
static double height = 500;
static double width = 500;
static double rect_line_length_half;
static double triLineLength;
static double degreeToRadian(double degree) {
return (degree * PI) / 180;
}
static void initialize() {
glClearColor(1.0, 1.0, 1.0, 0.0); //set display-window color-white
}
static void drawHouse(double centerX, double centerY) {
//draw house
glBegin(GL_LINE_LOOP);
glVertex2d(centerX - rect_line_length_half, centerY - rect_line_length_half); //bottom Left
glVertex2d(centerX + rect_line_length_half, centerY - rect_line_length_half); //bottom right
glVertex2d(centerX + rect_line_length_half, centerY + rect_line_length_half); //top right
glVertex2d(centerX - rect_line_length_half, centerY + rect_line_length_half); //top left
glEnd();
glBegin(GL_LINE_LOOP);
glVertex2d(centerX - rect_line_length_half - triLineLength, centerY + rect_line_length_half); //top left
glVertex2d(centerX + rect_line_length_half + triLineLength, centerY + rect_line_length_half); //top right
glVertex2d(((centerX - rect_line_length_half - triLineLength) + (centerX + rect_line_length_half + triLineLength)) / 2, centerY + rect_line_length_half + triLineLength * 5); // top middle
glEnd();
}
static void render() {
glColor3f(0.0, 0.0, 0.0); //set color to black
rect_line_length_half = 0.5;
triLineLength = 0.1;
double house_centerX = 0;
double house_centerY = 0;
double translateX = -4;
std::cout << translateX << std::endl;
double translateY = 0;
GLdouble theta = 45.0;
double sinTheta = 0;
for (int i = 0; i < houseCount; i++)
{
translateY = sin(sinTheta);
glPushMatrix();
glTranslatef(translateX, translateY, 0.0);
glRotatef(theta, 0.0, 0.0, 1.0);
drawHouse(house_centerX, house_centerY);
glPopMatrix();
theta = theta - 22.5;
translateX = translateX + 2;
sinTheta = sinTheta + degreeToRadian(45);
}
}
static void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0, 0.0, 8.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
render();
glFlush();
glutSwapBuffers();
}
static void reshape(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h); //area of the window where the renderer will be drawn
glMatrixMode(GL_PROJECTION); //any matrix operations that follow will affect the projection matrix.
glLoadIdentity(); //initialize the projection matrix
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 40.0); //map the 3D scene onto the 2D viewport
glMatrixMode(GL_MODELVIEW); //any matrix operations that follow will affect the modelview matrix.
}
static int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); //set display mode to double buffer
glutInitWindowSize(width, height); //set display-window width and height
glutInitWindowPosition(10, 10); //set top-left display-window position
glutCreateWindow("Program 2"); //create display window
glutDisplayFunc(display); //call drawHouse function
glutReshapeFunc(reshape);
initialize();
glutMainLoop(); //display everything and wait
return EXIT_SUCCESS;
}
}