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
|
#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();
}
|