-
Notifications
You must be signed in to change notification settings - Fork 0
/
painter.h
95 lines (75 loc) · 1.6 KB
/
painter.h
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
#ifndef PAINTER_HEADER
#define PAINTER_HEADER
#include <inttypes.h>
#include <stdio.h>
#include <SDL.h>
#include <SDL_opengl.h>
#include "dmath.h"
class Painter
{
private:
Vec ref;
real scale;
real invScale;
public:
Painter(uintptr_t w, uintptr_t h)
{
ref.zero();
scale = 100;
invScale = real(1.0) / scale;
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_SetVideoMode(w, h, 0, SDL_OPENGL | SDL_HWSURFACE);
glShadeModel(GL_SMOOTH);
glClearColor(0, 0, 0, 0);
glEnable(GL_DEPTH_TEST);
//glEnable(GL_POINT_SMOOTH);
//glEnable(GL_LINE_SMOOTH);
glPointSize(1);
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40, w/(real)h, 1, 100);//near/far plane
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(3, 3, -5, 0, 0, 0, 0, 1, 0);
}
void setScale(const Vec &min, const Vec &max)
{
ref = min;
Vec size = max - min;
scale = size.x > size.y ? size.x : size.y;
scale = scale > size.z ? scale : size.z;
invScale = real(1.0) / scale;
}
void paint(Boid *boids, uintptr_t nBoids)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,0,0);
glBegin(GL_POINTS);
for(uintptr_t i = 0; i < nBoids; ++i) {
Vec pos = boids[i].pos - ref;
real x = pos.x * invScale;
real y = pos.y * invScale;
real z = pos.z * invScale;
glVertex3f(x,y,z);
}
glEnd();
SDL_GL_SwapBuffers();
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
exit(0);
break;
}
}
}
~Painter()
{
SDL_Quit();
}
};
#endif /* PAINTER_HEADER */