-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathglut_wrap.cpp
More file actions
133 lines (115 loc) · 3.25 KB
/
Copy pathglut_wrap.cpp
File metadata and controls
133 lines (115 loc) · 3.25 KB
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
////////////////////////////////////////////////////////////////////////
//
// glut_wrap.cpp: Wrap up GLUT-calling code to increase reuse.
//
// Copyright (c) Simon Frankau 2018
//
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <cstdlib>
#include <exception>
#include "glut_wrap.h"
static void doNothing()
{
}
int gwTransferSetup(int size)
{
// Configure window
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(size, size);
int win = glutCreateWindow("Transfer calculator");
// This is needed as otherwise any glutMainLoop called gets
// unhappy, even if we've already destroyed the window.
glutDisplayFunc(doNothing);
// Flat shading.
glEnable(GL_COLOR_MATERIAL);
// Use depth buffering for hidden surface elimination.
glEnable(GL_DEPTH_TEST);
// Back-face culling.
glEnable(GL_CULL_FACE);
// To read from the scene...
glReadBuffer(GL_BACK);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Set up the view to be one face of the cube-map.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0, // Field of view in degrees
1.0, // Aspect ratio
0.001, // Z near
10.0); // Z far
return win;
}
// Not re-entrant but I don't think GLUT is.
static void (*dispFn)() = NULL;
class BreakException : public std::exception {
};
static void idleFn()
{
if (dispFn == NULL) {
throw BreakException();
}
}
static void dispFnWrapper()
{
if (dispFn) {
dispFn();
dispFn = NULL;
}
}
void gwRenderOnce(void (*f)())
{
#ifdef VANILLA_GLUT
// Reading the definition of 'glutMainLoop', unwinding the stack
// from the idle function shouldn't break anything. Horrible way
// to escape, but... meh.
//
// While vanilla GLUT allows this, the MacOS GLUT calls into
// [GLUTApplication run] and gets unhappy if you try to run it
// twice as it sets up menus etc.
//
// Set up helper functions to display once and then throw int the
// idle function...
dispFn = f;
glutDisplayFunc(dispFnWrapper);
glutIdleFunc(idleFn);
try {
glutMainLoop();
} catch (BreakException &e) {
}
#else
// On MacOS glutCheckLoop looks like an alternative. However, this
// just manages the event loop. It doesn't render unless
// necessary, and rendering for the purposes of reading the buffer
// back out works fine by just making OpenGL calls directly,
// outside the event loop. So, let's just do that.
f();
#endif
}
Camera::Camera(Vertex const &eyePos, Vertex const &lookAt, Vertex const &upDir)
: m_eyePos(eyePos), m_lookAt(lookAt), m_upDir(upDir)
{
}
Camera Camera::baseCamera = Camera(Vertex(0.0, 0.0, 0.0),
Vertex(0.0, 0.0, 1.0),
Vertex(0.0, 1.0, 0.0));
Vertex Camera::getEyePos() const
{
return m_eyePos;
}
Vertex Camera::getLookAt() const
{
return m_lookAt;
}
Vertex Camera::getUpDir() const
{
return m_upDir;
}
void Camera::applyViewTransform() const
{
gluLookAt(m_eyePos.x(), m_eyePos.y(), m_eyePos.z(),
m_lookAt.x(), m_lookAt.y(), m_lookAt.z(),
m_upDir.x(), m_upDir.y(), m_upDir.z());
}