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
| #include "graphic.h"
#include <string>
using namespace std;
graphic::graphic()
{
window = 0;
contexteOpenGL = 0;
initialisationGLEW = 0;
m_G = 0.001;
m_accX1 = 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_ForceX << endl;
//Points X
//Points 0
if (points[0] < 0)
{
points[0] += m_accX1;
}
else if (points[0] > 0)
{
points[0] -= m_accX1;
}
else
{
points[0] = 0;
}
//Points 2
if (points[2] < 0)
{
points[2] += m_accX1;
}
else if (points[2] > 0)
{
points[2] -= m_accX1;
}
else
{
points[2] = 0;
}
CalculeDistance(points);
CalculeForce(points, masse1, masse2);
m_accX1 = m_ForceX / masse1;
DrawPoint(points);
}
float graphic::CalculeForce(float points[], float masse1, float masse2)
{
//Calcule Force
m_ForceX = m_G*((masse1 * masse2) / pow(m_DistX, 2.0));
return m_ForceX;
}
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;
}
return m_DistX;
} |
Partager