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
| #include "graphic.h"
#include <string>
using namespace std;
graphic::graphic()
{
window = 0;
contexteOpenGL = 0;
initialisationGLEW = 0;
m_G = 0.0001;
m_vitX1 = 0;
m_vitX2 = 0;
m_vitY1 = 0;
m_vitY2 = 0;
}
graphic::~graphic()
{
}
int graphic::InitSDL()
{
//Init SDL
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
cout << "Erreur d'initialisation de la SDL : " << SDL_GetError() << endl;
Quit_SDL();
return -1;
}
//OpenGL version
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
//DoubleBuffer
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
return 0;
}
int graphic::InitOpenGl()
{
//Init OpenGL
//OpenGL context
contexteOpenGL = SDL_GL_CreateContext(window);
if (contexteOpenGL == 0)
{
cout << SDL_GetError() << endl;
Quit_SDL();
return -1;
}
//Init Glew
initialisationGLEW = glewInit();
if (initialisationGLEW != GLEW_OK)
{
cout << "Erreur d'initialisation de GLEW : " << glewGetErrorString(initialisationGLEW) << endl;
SDL_Delay(2000);
Quit_SDL();
return EXIT_FAILURE;
}
}
int graphic::Create_Window(const char *titre, int widht, int height)
{
window = SDL_CreateWindow(titre, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, widht, height, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
return 0;
}
void graphic::Quit_SDL()
{
SDL_GL_DeleteContext(contexteOpenGL);
SDL_DestroyWindow(window);
SDL_Quit();
}
void graphic::DrawPoint(float points[])
{
glClear(GL_COLOR_BUFFER_BIT);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, points);
glEnableVertexAttribArray(0);
glDrawArrays(GL_POINTS, 0, 2);
glDisableVertexAttribArray(0);
SDL_GL_SwapWindow(window);
}
void graphic::CalculePosition(float points[], float masse1, float masse2)
{
cout << m_DistX << " " << m_DistY << endl;
//Axe X
//Points 0
if (points[0] < points[2])
{
points[0] += m_vitX1;
}
else if (points[0] > points[2])
{
points[0] -= m_vitX1;
}
else
{
points[0] = 0;
}
//Points 2
if (points[2] < points[0])
{
points[2] += m_vitX2;
}
else if (points[2] > points[0])
{
points[2] -= m_vitX2;
}
else
{
points[2] = 0;
}
//Axe Y
//Points 0
if (points[1] < points[3])
{
points[1] += m_vitY1;
}
else if (points[1] > points[3])
{
points[1] -= m_vitY1;
}
else
{
points[1] = 0;
}
//Points 2
if (points[3] < points[1])
{
points[3] += m_vitY2;
}
else if (points[3] > points[1])
{
points[3] -= m_vitY2;
}
else
{
points[3] = 0;
}
CalculeDistance(points);
CalculeForce(points, masse1, masse2);
m_vitX1 = m_ForceX / masse1;
m_vitX2 = m_ForceX / masse2;
m_vitY1 = m_ForceY / masse1;
m_vitY2 = m_ForceY / masse2;
DrawPoint(points);
}
float graphic::CalculeForce(float points[], float masse1, float masse2)
{
//Calcule Force
m_ForceX = m_G*((masse1 * masse2) / pow(m_DistX, 2.0));
if (m_DistX < 0.01)
{
m_ForceX = 0;
m_DistX = 0;
}
m_ForceY = m_G*((masse1 * masse2) / pow(m_DistY, 2.0));
if (m_DistY < 0.01)
{
m_ForceY = 0;
m_DistY = 0;
}
return m_ForceX, m_ForceY;
}
float graphic::CalculeDistance(float points[])
{
//Calcule Distance X
if (points[0] < points[2])
{
m_DistX = fdim(points[2], points[0]);
}
else if (points[2] < points[0])
{
m_DistX = fdim(points[0], points[2]);
}
else if (points[0] == points[2])
{
m_DistX = 0;
}
//Calcule Distance Y
if (points[1] < points[3])
{
m_DistY = fdim(points[3], points[1]);
}
else if (points[3] < points[1])
{
m_DistY = fdim(points[1], points[3]);
}
else if (points[1] == points[3])
{
m_DistY = 0;
}
return m_DistX, m_DistY;
} |
Partager