-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.cpp
More file actions
142 lines (115 loc) · 4.64 KB
/
template.cpp
File metadata and controls
142 lines (115 loc) · 4.64 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// main.cpp
// GML
//
// Created by csea39 on 10/20/22.
//
#include <GL/glut.h> // GLUT, include glu.h and gl.h
#include<math.h>
double theta;
/* Global variables */
char title[] = "3D Shapes";
/* Initialize OpenGL Graphics */
void initGL() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
glClearDepth(1.0f); // Set background depth to farthest
glEnable(GL_DEPTH_TEST); // Enable depth testing for z-culling
glDepthFunc(GL_LEQUAL); // Set the type of depth-test
glShadeModel(GL_SMOOTH); // Enable smooth shading
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Nice perspective corrections
}
void transformAndPlot(double T[4][4], double P[4][1]){
double new_point[4][1] = {0,0,0, 0};
for (int i=0;i<4;i++){
for (int j=0;j<1;j++){
new_point[i][j] = 0;
for (int k=0;k<4;k++){
new_point[i][j]+= T[i][k]*P[k][j];
}
}
}
glVertex3f(new_point[0][0], new_point[1][0], new_point[2][0]);
}
void rotateZ(double x, double y,double z, double theta){
double angle = theta*3.14/180;
double rotation_Z_matrix[4][4] = {
cos(angle), -sin(angle), 0, 0,
sin(angle), cos(angle), 0,0,
0,0,1,0,
0,0,0,1
};
double point_matrix[4][1] = {x, y, z, 1};
transformAndPlot(rotation_Z_matrix, point_matrix);
}
/* Handler for window-repaint event. Called back when the window first appears and
whenever the window needs to be re-painted. */
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers
glMatrixMode(GL_MODELVIEW); // To operate on model-view matrix
theta+=5;
// Render a pyramid consists of 4 triangles
// Move left and into the screen
glLoadIdentity(); // Reset the model-view matrix
glTranslatef(0.0f, 0.0f, -7.0f); // Move right and into the screen
glBegin(GL_TRIANGLES);
// Begin drawing the pyramid with 4 triangles
// Front
// Front
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(0.0, 1.0, 0.0);
glVertex3f( -1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
// Right
glColor3f(0.0f, 1.0f, 0.0f); // Green
rotateZ(0.0f, 1.0f, 0.0f, theta);
rotateZ(1.0f, -1.0f, 1.0f, theta);
rotateZ(1.0f, -1.0f, -1.0f, theta);
// Back
glColor3f(0.0f, 0.0f, 1.0f); // Blue
rotateZ(0.0f, 1.0f, 0.0f, theta);
rotateZ(1.0f, -1.0f, -1.0f, theta);
rotateZ(-1.0f, -1.0f, -1.0f, theta);
// Left
glColor3f(1.0f,0.0f,0.0f); // Red
rotateZ( 0.0f, 1.0f, 0.0f, theta);
glColor3f(0.0f,0.0f,1.0f); // Blue
rotateZ(-1.0f,-1.0f,-1.0f, theta);
glColor3f(0.0f,1.0f,0.0f); // Green
rotateZ(-1.0f,-1.0f, 1.0f, theta);
glEnd(); // Done drawing the pyramid
glutSwapBuffers(); // Swap the front and back frame buffers (double buffering)
}
/* Handler for window re-size event. Called back when the window first appears and
whenever the window is re-sized with its new width and height.
*/
void reshape(GLsizei width, GLsizei height) { // GLsizei for non-negative integer
// Compute aspect ratio of the new window
if (height == 0) height = 1; // To prevent divide by 0
GLfloat aspect = (GLfloat)width / (GLfloat)height;
// Set the viewport to cover the new window
glViewport(0, 0, width, height);
// Set the aspect ratio of the clipping volume to match the viewport
glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix
glLoadIdentity(); // Reset
// Enable perspective projection with fovy, aspect, zNear and zFar
gluPerspective(45.0f, aspect, 0.1f, 100.0f);
}
void Timer(int value){
glutPostRedisplay();
glutTimerFunc(500, Timer, 0);
}
/* Main function: GLUT runs as a console application starting at main() */
int main(int argc, char** argv) {
theta = 0.5;
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_DOUBLE); // Enable double buffered mode
glutInitWindowSize(640, 480); // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutCreateWindow(title); // Create window with the given title
glutDisplayFunc(display); // Register callback handler for window re-paint event
glutReshapeFunc(reshape); // Register callback handler for window re-size event
initGL();// Our own OpenGL initialization
Timer(0);
glutMainLoop(); // Enter the infinite event-processing loop
return 0;
}