Skip to content
Snippets Groups Projects
Commit 90cb8eb7 authored by tanguy.cavagna's avatar tanguy.cavagna :desktop:
Browse files

Revert opengl for master only

parent 1318bf08
No related branches found
No related tags found
No related merge requests found
Pipeline #18579 failed
...@@ -85,29 +85,6 @@ bool field_line_segment(struct gfx_context_t *ctxt, charge_t *charges, ...@@ -85,29 +85,6 @@ bool field_line_segment(struct gfx_context_t *ctxt, charge_t *charges,
return true; return true;
} }
bool field_line_segment_gl(charge_t *charges, int num_charges, double dx,
double x0, double x1, double y0, double y1, int w,
int h, vec2 old, vec2 p, bool side) {
while (pos_contrain_in_universe(p, x0, x1, y0, y1) &&
pos_not_too_close(p, charges, num_charges)) {
coordinates_t old_coord =
position_to_coordinates(w, h, x0, x1, y0, y1, old);
coordinates_t pi_coord =
position_to_coordinates(w, h, x0, x1, y0, y1, p);
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex2f(pi_coord.row, pi_coord.column);
glVertex2f(old_coord.row, old_coord.column);
glEnd();
old = p; // Save current point
compute_p_next(charges, num_charges, EPS, dx, &p, side);
}
return true;
}
void draw_field_line(struct gfx_context_t *ctxt, charge_t *charges, void draw_field_line(struct gfx_context_t *ctxt, charge_t *charges,
int num_charges, double dx, vec2 pos0, double x0, int num_charges, double dx, vec2 pos0, double x0,
double x1, double y0, double y1) { double x1, double y0, double y1) {
...@@ -123,21 +100,6 @@ void draw_field_line(struct gfx_context_t *ctxt, charge_t *charges, ...@@ -123,21 +100,6 @@ void draw_field_line(struct gfx_context_t *ctxt, charge_t *charges,
} }
} }
void draw_field_line_gl(charge_t *charges, int num_charges, double dx,
vec2 pos0, double x0, double x1, double y0, double y1,
int w, int h) {
vec2 p = pos0;
vec2 old = p;
bool ended = false;
for (int side = 0; side <= 1; side++) {
old = pos0; // Reset to start from the same point for both sides
field_line_segment_gl(charges, num_charges, dx, x0, x1, y0, y1, w, h,
old, p, (bool)side);
}
}
void draw_charges(struct gfx_context_t *ctxt, charge_t *charges, void draw_charges(struct gfx_context_t *ctxt, charge_t *charges,
int num_charges, double x0, double x1, double y0, double y1) { int num_charges, double x0, double x1, double y0, double y1) {
// Center coordinates // Center coordinates
...@@ -179,45 +141,7 @@ void draw_charges(struct gfx_context_t *ctxt, charge_t *charges, ...@@ -179,45 +141,7 @@ void draw_charges(struct gfx_context_t *ctxt, charge_t *charges,
} }
} }
void draw_charges_GL(charge_t *charges, int num_charges, double x0, double x1, void generate_points(vec2 points[], int nb_points) {
double y0, double y1, int w, int h) {
// Center coordinates
coordinates_t c;
// Charges coordinates
coordinates_t ch0;
coordinates_t ch1;
int chargePadding = CHARGE_RADIUS / 2;
// Draw charges
for (int i = 0; i < num_charges; i++) {
coordinates_t chCoord =
position_to_coordinates(w, h, x0, x1, y0, y1, charges[i].pos);
// Draw sign according to charge value
if (charges[i].q < 0) {
glBegin(GL_LINES);
glColor3f(0, 0, 255);
glVertex2f(chCoord.row - chargePadding, chCoord.column);
glVertex2f(chCoord.row + chargePadding, chCoord.column);
glEnd();
} else {
glBegin(GL_LINES);
glColor3f(255, 0, 0);
glVertex2f(chCoord.row, chCoord.column - chargePadding);
glVertex2f(chCoord.row, chCoord.column + chargePadding);
glVertex2f(chCoord.row - chargePadding, chCoord.column);
glVertex2f(chCoord.row + chargePadding, chCoord.column);
glEnd();
}
gl_draw_circle(chCoord, CHARGE_RADIUS, charges[i].q < 0 ? 0 : 255, 0,
charges[i].q < 0 ? 255 : 0);
}
}
void generate_points(vec2 points[], int nb_points, int w, int h) {
double x = 0; double x = 0;
double y = 0; double y = 0;
......
...@@ -18,8 +18,6 @@ ...@@ -18,8 +18,6 @@
#include "../gfx/gfx.h" #include "../gfx/gfx.h"
#include "../utilities/utils.h" #include "../utilities/utils.h"
#include "../vec2/vec2.h" #include "../vec2/vec2.h"
#include <GL/freeglut.h>
#include <GL/gl.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -100,10 +98,6 @@ void draw_field_line(struct gfx_context_t *ctxt, charge_t *charges, ...@@ -100,10 +98,6 @@ void draw_field_line(struct gfx_context_t *ctxt, charge_t *charges,
int num_charges, double dx, vec2 pos0, double x0, int num_charges, double dx, vec2 pos0, double x0,
double x1, double y0, double y1); double x1, double y0, double y1);
void draw_field_line_gl(charge_t *charges, int num_charges, double dx,
vec2 pos0, double x0, double x1, double y0, double y1,
int w, int h);
/** /**
* @brief Draw all charges * @brief Draw all charges
* A circle with a minus sign for negative charges * A circle with a minus sign for negative charges
...@@ -120,15 +114,12 @@ void draw_field_line_gl(charge_t *charges, int num_charges, double dx, ...@@ -120,15 +114,12 @@ void draw_field_line_gl(charge_t *charges, int num_charges, double dx,
void draw_charges(struct gfx_context_t *ctxt, charge_t *charges, void draw_charges(struct gfx_context_t *ctxt, charge_t *charges,
int num_charges, double x0, double x1, double y0, double y1); int num_charges, double x0, double x1, double y0, double y1);
void draw_charges_GL(charge_t *charges, int num_charges, double x0, double x1,
double y0, double y1, int w, int h);
/** /**
* @brief Generate a given amount of points * @brief Generate a given amount of points
* *
* @param points Points array * @param points Points array
* @param nb_points Number of point to generate * @param nb_points Number of point to generate
*/ */
void generate_points(vec2 points[], int nb_points, int w, int h); void generate_points(vec2 points[], int nb_points);
#endif // _CHARGE_H_ #endif // _CHARGE_H_
...@@ -118,22 +118,3 @@ void gfx_draw_circle(struct gfx_context_t *ctxt, coordinates_t c, int r, ...@@ -118,22 +118,3 @@ void gfx_draw_circle(struct gfx_context_t *ctxt, coordinates_t c, int r,
} }
} }
} }
<<<<<<< HEAD
=======
void gl_draw_circle(coordinates_t c, int rad, int r, int g, int b) {
int i;
int lineAmount = 100; //# of triangles used to draw circle
// GLfloat radius = 0.8f; //radius
GLfloat twicePi = 2.0f * 3.141592;
glBegin(GL_LINE_LOOP);
glColor3f(r, g, b);
for (i = 0; i <= lineAmount; i++) {
glVertex2f(c.row + (rad * cos(i * twicePi / lineAmount)),
c.column + (rad * sin(i * twicePi / lineAmount)));
}
glEnd();
}
>>>>>>> 438b2433ab31076f95d3a015859fa4f7594aa04f
...@@ -13,11 +13,10 @@ ...@@ -13,11 +13,10 @@
#define _DRAW_H_ #define _DRAW_H_
#include "../gfx/gfx.h" #include "../gfx/gfx.h"
#include <GL/freeglut.h>
#include <GL/gl.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
typedef struct { typedef struct {
uint32_t row; uint32_t row;
uint32_t column; uint32_t column;
...@@ -54,6 +53,4 @@ void gfx_draw_line(struct gfx_context_t *ctxt, coordinates_t p0, ...@@ -54,6 +53,4 @@ void gfx_draw_line(struct gfx_context_t *ctxt, coordinates_t p0,
void gfx_draw_circle(struct gfx_context_t *ctxt, coordinates_t c, int r, void gfx_draw_circle(struct gfx_context_t *ctxt, coordinates_t c, int r,
uint32_t color); uint32_t color);
void gl_draw_circle(coordinates_t c, int radius, int r, int g, int b);
#endif // _DRAW_H_ #endif // _DRAW_H_
...@@ -10,8 +10,6 @@ ...@@ -10,8 +10,6 @@
*/ */
#include "charge/charge.h" #include "charge/charge.h"
#include "gfx/gfx.h" #include "gfx/gfx.h"
#include <GL/freeglut.h>
#include <GL/glut.h>
#include <math.h> #include <math.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
...@@ -24,84 +22,83 @@ ...@@ -24,84 +22,83 @@
#define x1 1 // Maximal x of the universe #define x1 1 // Maximal x of the universe
#define y0 0 // Minimal y of the universe #define y0 0 // Minimal y of the universe
#define y1 1 // Maximal y of the universe #define y1 1 // Maximal y of the universe
#define NB_CHARGES 5 #define NB_CHARGES 3
#define NB_POINTS 200 #define NB_POINTS 200
#define DELTA (1 / sqrt(pow(WINDOW_WIDTH, 2) + pow(WINDOW_HEIGHT, 2))) #define DELTA (1 / sqrt(pow(WINDOW_WIDTH, 2) + pow(WINDOW_HEIGHT, 2)))
int charge_count = NB_CHARGES; void redraw_field(struct gfx_context_t *ctxt, int charge_count,
vec2 pos[30]; vec2 pos[charge_count], double chs[charge_count]) {
double chs[30];
void display() { // Display function will draw the image.
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
// Generate all the charges // Generate all the charges
charge_t charges[charge_count]; charge_t charges[charge_count];
for (int i = 0; i < charge_count; i++) for (int i = 0; i < charge_count; i++)
charges[i] = charge_create(chs[i], pos[i]); charges[i] = charge_create(chs[i], pos[i]);
draw_charges_GL(charges, charge_count, x0, x1, y0, y1, WINDOW_WIDTH, draw_charges(ctxt, charges, charge_count, x0, x1, y0, y1);
WINDOW_HEIGHT);
// Generate all the points // Generate all the points
vec2 points[NB_POINTS]; vec2 points[NB_POINTS];
generate_points(points, NB_POINTS, WINDOW_WIDTH, WINDOW_HEIGHT); generate_points(points, NB_POINTS);
for (int i = 0; i < NB_POINTS; i++) { for (int i = 0; i < NB_POINTS; i++) {
draw_field_line_gl(charges, charge_count, DELTA, points[i], x0, x1, y0, coordinates_t c = position_to_coordinates(WINDOW_WIDTH, WINDOW_HEIGHT,
y1, WINDOW_WIDTH, WINDOW_HEIGHT); x0, x1, y0, y1, points[i]);
} draw_field_line(ctxt, charges, charge_count, DELTA, points[i], x0, x1,
y0, y1);
glutSwapBuffers();
}
/**
* @brief Event that retrieves actions performed with the mouse.
*
* @param button
* @param state
* @param x
* @param y
*/
void handle_mouse_input(int button, int state, int x, int y) {
if (state == GLUT_UP)
return;
if (button == GLUT_LEFT_BUTTON) {
charge_count += 1;
pos[charge_count - 1] =
vec2_create((double)y / WINDOW_WIDTH, (double)x / WINDOW_HEIGHT);
chs[charge_count - 1] = rand_one() / (rand_one() * 10 - 5);
} }
glutPostRedisplay();
} }
int main(int argc, char **argv) { int main(void) {
srand(time(NULL)); srand(time(NULL));
int charge_count = NB_CHARGES;
vec2 pos[30];
double chs[30];
for (int i = 0; i < charge_count; i++) { for (int i = 0; i < charge_count; i++) {
chs[i] = rand_one() / (rand_one() * 10 - 5); chs[i] = rand_one() / (rand_one() * 10 - 5);
pos[i] = vec2_create(rand_one(), rand_one()); pos[i] = vec2_create(rand_one(), rand_one());
} }
glutInit(&argc, argv); // GFX initialization
glutSetOption(GLUT_MULTISAMPLE, 16); struct gfx_context_t *ctxt =
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | gfx_create("draw", WINDOW_WIDTH, WINDOW_HEIGHT);
GLUT_MULTISAMPLE); if (!ctxt) {
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); fprintf(stderr, "Graphics initialization failed !\n");
int window = glutCreateWindow("GL RGB Triangle"); return EXIT_FAILURE;
}
redraw_field(ctxt, charge_count, pos, chs);
bool running = true;
// GFX Draw loop
while (running) {
SDL_Event event = gfx_event();
glClearColor(0, 0, 0, 1); switch (event.type) {
glMatrixMode(GL_PROJECTION); case SDL_KEYDOWN:
glLoadIdentity(); if (event.key.keysym.sym == SDLK_ESCAPE)
gluOrtho2D(0, WINDOW_WIDTH, WINDOW_HEIGHT, 0); running = false;
break;
glutDisplayFunc(display); case SDL_MOUSEBUTTONDOWN:
glutMouseFunc(handle_mouse_input); if (event.button.button == SDL_BUTTON_LEFT) {
charge_count += 1;
gfx_clear(ctxt, 0x000000);
int m_x, m_y;
SDL_GetMouseState(&m_x, &m_y);
pos[charge_count - 1] = vec2_create(
(double)m_x / WINDOW_WIDTH, (double)m_y / WINDOW_HEIGHT);
chs[charge_count - 1] = rand_one() / (rand_one() * 10 - 5);
redraw_field(ctxt, charge_count, pos, chs);
}
break;
}
gfx_present(ctxt);
}
glutMainLoop(); gfx_destroy(ctxt);
glutDestroyWindow(window);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
CC = gcc CC = gcc
CFLAGS = -Wall -Wextra -std=gnu11 -g -fsanitize=address -fsanitize=leak CFLAGS = -Wall -Wextra -std=gnu11 -g -fsanitize=address -fsanitize=leak
LDFLAGS = -lm LDFLAGS = -lm
LIBS = -lSDL2 -lGL -lGLU -lglut LIBS = -lSDL2
VPATH:=vec2 gfx VPATH:=vec2 gfx
BIN = bin BIN = bin
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment