diff options
| author | NPScript <nathan@reinerweb.ch> | 2022-08-18 17:04:05 +0200 |
|---|---|---|
| committer | NPScript <nathan@reinerweb.ch> | 2022-08-18 17:04:05 +0200 |
| commit | 95361c8c2229840a5e05f49622be629cf654371d (patch) | |
| tree | 280a6d7706b349563959852057e1741f846d5f60 /opengl.c | |
first sketch
Diffstat (limited to 'opengl.c')
| -rw-r--r-- | opengl.c | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/opengl.c b/opengl.c new file mode 100644 index 0000000..2b0b70c --- /dev/null +++ b/opengl.c @@ -0,0 +1,138 @@ +#include <stdio.h> +#include <stdlib.h> +#include <sys/time.h> +#include <math.h> + +#include <GL/freeglut.h> + +void draw_axis() { + glColor3f(1, 1, 1); + glBegin(GL_LINES); + + glVertex2f(0, 0); + glVertex2f(1, 0); + glVertex2f(0, 0); + glVertex2f(0, 1); + + glVertex2f(1, 0); + glVertex2f(0.9, 0.1); + glVertex2f(1, 0); + glVertex2f(0.9, -0.1); + + glVertex2f(0, 1); + glVertex2f(0.1, 0.9); + glVertex2f(0, 1); + glVertex2f(-0.1, 0.9); + + glEnd(); +} + +void draw_grid() { + glColor3f(0.2, 0.2, 0.2); + + glBegin(GL_LINES); + + for (int i = -10; i < 10; ++i) { + glVertex2f(i, -10); + glVertex2f(i, 10); + } + + for (int i = -10; i < 10; ++i) { + glVertex2f(-10, i); + glVertex2f(10, i); + } + + glEnd(); + + glColor3f(0.5, 0.5, 0.5); + + glBegin(GL_LINES); + + glVertex2f(-10, 0); + glVertex2f(10, 0); + + glVertex2f(0, -10); + glVertex2f(0, 10); + + glEnd(); +} + +void draw_function(double from, double to, double step, double(*fun)(double)) { + glColor3f(0.9f, 0.5f, 0.2f); + glLineWidth(2); + glBegin(GL_LINES); + + for (float i = from; i < to; i += step) { + double current = fun(i); + double next = fun(i + step); + + glVertex2f(i, current); + glVertex2f(i + step, next); + } + + glEnd(); +} + +double tp(double x, double a, double b, double c, double d) { + return a * x * x * x + b * x * x + c * x + d; +} + +double example_function(double x) { + return tp(x, 1, 5, 5, -1); +} + +static void display(void) { + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + + draw_grid(); + draw_axis(); + draw_function(-10, 10, 0.01, &example_function); + + glutSwapBuffers(); +} + +static void keyboard(unsigned char key, int x, int y) { + printf("Pressed %c key\n", key); + if(key == 'q') { + exit(0); + } +} + +void enableMultisample(int msaa) { + if (msaa) { + glEnable(GL_MULTISAMPLE); + glHint(GL_MULTISAMPLE_FILTER_HINT_NV, GL_NICEST); + + // detect current settings + GLint iMultiSample = 0; + GLint iNumSamples = 0; + glGetIntegerv(GL_SAMPLE_BUFFERS, &iMultiSample); + glGetIntegerv(GL_SAMPLES, &iNumSamples); + printf("MSAA on, GL_SAMPLE_BUFFERS = %d, GL_SAMPLES = %d\n", iMultiSample, iNumSamples); + } else { + glDisable(GL_MULTISAMPLE); + printf("MSAA off\n"); + } +} + +int main(int argc, char **argv) { + glutInit(&argc, argv); + glutInitWindowSize(600, 600); + glutSetOption(GLUT_MULTISAMPLE, 8); + glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE); + glutCreateWindow("cme"); + + enableMultisample(1); + + printf("OpenGL version = %s\n", glGetString(GL_VERSION)); + + glutDisplayFunc(display); + glutKeyboardFunc(keyboard); + + glScalef(0.1, 0.1, 0.1); + glClearColor(0.0, 0.0, 0.0, 1.0); + + + glutMainLoop(); +} |