-
Notifications
You must be signed in to change notification settings - Fork 0
/
forcedpendulum.cpp
221 lines (182 loc) · 7.36 KB
/
forcedpendulum.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* FORCED PENDULUM
Forced damped pendulum simulation.
Made by ouz81. 25/02/2022
github.com/oguz81
*/
#include <stdio.h>
#include <iostream>
#include "shader.h"
#include <cmath>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
GLFWwindow* window;
#define PI 3.141592 //Holy Pi!
#define h 0.025 //step length for Runge-Kutta
#define k 0.67 //driving force frequency (radian)
#define THETA_0 0 //initial angle(radian)
#define omega_0 0 //initial angular velocity
#define A 1.4 //driving force amplitude
#define b 0.4 //damping constant
#define m 1 //mass of pendulum
#define R 1 //length of rod
#define grav 1 //gravitational constant
//functions of the differential equation
float f(float time, float tht,float omega){
return omega;
}
float g(float time, float tht, float omega){
return -(grav/R)*sin(tht)-((b/(m*R*R))*omega)+((A/(m*R*R))*cos(k*time));
}
void drawCircle(float array[]){
int corner_one, corner_two, corner_three;//corners of triangles. GL_TRIANGLES starts to draw counterclockwise.
corner_one = -6;
corner_two = -4;
corner_three = -2;
float radius = 0.1f;
for(int angle = 1; angle <= 360; angle ++){
corner_one = corner_one + 6;
corner_two = corner_two + 6;
corner_three = corner_three + 6;
array[corner_one] = 0.0f;
array[corner_one + 1] = 0.0f;
array[corner_two] = radius * cos((angle -1) * 3.1416 / 180);
array[corner_two + 1] = radius * sin((angle -1) * 3.1416 / 180);
array[corner_three] = radius * cos((angle) * 3.1416 / 180);
array[corner_three + 1] = radius * sin((angle) * 3.1416 / 180);
}
}
int main( void )
{
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW\n" );
getchar();
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow( 630, 600, "PENDULUM", NULL, NULL);
if( window == NULL ){
fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
getchar();
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = true;
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
getchar();
glfwTerminate();
return -1;
}
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
glClearColor(1.0f, 0.8f, 0.0f, 0.0f);
Shader myshader("pendulum.vs" , "pendulum.fs");
unsigned int shaderProgram = myshader.programID();
float vertices[2160];
float vertices2[] = { //vertices2 gives us the rod of the pendulum.
-0.01f, 0.0f,
0.01f, 0.0f,
0.01f, 0.8f,
-0.01f, 0.8f,
-0.01, 0.0f
};
float arrows[] = {
-0.5f, 0.0f,
-0.1f, 0.0f,
-0.5f, 0.0f,
-0.3f, 0.1f,
-0.5f, 0.0f,
-0.3f, -0.1f
};
drawCircle(vertices); //draws the pendulum ball.
unsigned int VBO, VAO, VAO2, VAOArrow;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
//position attribute for 'vertices'(ball of the pendulum)
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glGenVertexArrays(1, &VAO2);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO2);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices2), vertices2, GL_STATIC_DRAW);
//position attribute for 'vertices2'(rod of the pendulum)
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glGenVertexArrays(1, &VAOArrow);
glGenBuffers(1, &VBO);
glBindVertexArray(VAOArrow);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(arrows), arrows, GL_STATIC_DRAW);
//position attribute for 'arrows'(force arrow)
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
float theta= THETA_0;
float omg= omega_0;
float time = 0;
float k1,k2,k3,k4,l1,l2,l3,l4;
float driving_force;
//Initialize f and g functions.
f(time,theta,omg);
g(time,theta,omg);
float current_angle;
do{
driving_force = A * cos(k * time);
current_angle = theta * 180 / PI; //converts theta(radian) to degree
glClear( GL_COLOR_BUFFER_BIT );
glUseProgram(shaderProgram);
glm::mat4 model = glm::mat4(1.0f);
glm::mat4 projection = glm::mat4(1.0f);
glm::mat4 view = glm::mat4(1.0f);
view = glm::rotate(view, glm::radians(current_angle), glm::vec3(0.0f, 0.0f, 1.0f));
view = glm::translate(view, glm::vec3(0.0f, -0.8f, 0.0f));
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "view"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "model"), 1, GL_FALSE, glm::value_ptr(model));
k1= h*f(time,theta,omg);
l1= h*g(time,theta,omg);
k2= h*f(time+(0.5*h),theta+(0.5*k1),omg+(0.5*l1));
l2= h*g(time+(0.5*h),theta+(0.5*k1),omg+(0.5*l1));
k3= h*f(time+(0.5*h),theta+(0.5*k2),omg+(0.5*l2));
l3= h*g(time+(0.5*h),theta+(0.5*k2),omg+(0.5*l2));
k4= h*f(time+h,theta+k3,omg+l3);
l4= h*g(time+h,theta+k3,omg+l3);
theta = theta+(k1 + (2*k2) + (2*k3) + k4)/6;
omg = omg+(l1 + (2*l2) + (2*l3) + l4)/6;
//Below two lines keep the theta in range of -2PI to 2PI.
if(theta>2*PI) theta= theta-(2*PI);
if(theta<-2*PI) theta= theta+(2*PI);
time =time+h;
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 1080);
glBindVertexArray(VAO2);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 5);
//Create the driving force arrow. It shows magnitude and direction side of the force.
glm::mat4 modelArrow= glm::mat4(1.0f);
glm::mat4 projectionArrow = glm::mat4(1.0f);
glm::mat4 viewArrow = glm::mat4(1.0f);
viewArrow = glm::translate(view, glm::vec3(0.0f, 0.0f, 0.0f));
viewArrow = glm::scale(viewArrow, glm::vec3(-driving_force * 0.5, 1.0f, 1.0f));
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "projection"), 1, GL_FALSE, glm::value_ptr(projectionArrow));
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "view"), 1, GL_FALSE, glm::value_ptr(viewArrow));
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "model"), 1, GL_FALSE, glm::value_ptr(modelArrow));
glBindVertexArray(VAOArrow);
glDrawArrays(GL_LINES, 0, 6);
glfwSwapBuffers(window);
glfwPollEvents();
}
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0);
glfwTerminate();
return 0;
}