-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOGLUtils.cpp
More file actions
145 lines (132 loc) · 3.66 KB
/
Copy pathOGLUtils.cpp
File metadata and controls
145 lines (132 loc) · 3.66 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
134
135
136
137
138
139
140
141
142
143
144
145
#include "OGLUtils.h"
#ifndef WIN32
#include <GL/gl.h>
#include <GL/glx.h>
#include <GL/glxext.h>
#endif
#include <GL/glu.h>
using namespace std;
using namespace cml;
#ifndef WIN32
#ifdef USE_LOCAL_HEADERS
# include "SDL.h"
# include "SDL_cpuinfo.h"
#else
# include <SDL.h>
# include <SDL_cpuinfo.h>
#endif
#endif
namespace utils
{
COGLUtils::COGLUtils()
{
}
COGLUtils::~COGLUtils()
{
}
vector3ub COGLUtils::getColor(vector2i mousePos)
{
/* get color information from frame buffer */
unsigned char pixel[3];
int viewport[4];
glGetIntegerv(GL_VIEWPORT,viewport);
glReadPixels(mousePos[0],viewport[3]-mousePos[1],1,1,GL_RGB,GL_UNSIGNED_BYTE,pixel);
return vector3ub(pixel[0],pixel[1],pixel[2]);
}
GLvoid COGLUtils::toGLOrtho(GLint screenWidth, GLint screenHeight, bool invertY)
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0,screenWidth,invertY?screenHeight:0,invertY?0:screenHeight,-1,1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
}
GLvoid COGLUtils::restoreProjection()
{
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
string COGLUtils::errorToString(GLint errorCode)
{
switch(errorCode)
{
case GL_INVALID_ENUM:
{
return "An unacceptable value is specified for an enumerated argument. The offending function is ignored, having no side effect other than to set the error flag.";
}
case GL_INVALID_VALUE:
{
return "A numeric argument is out of range. The offending function is ignored, having no side effect other than to set the error flag.";
}
case GL_INVALID_OPERATION:
{
return "The specified operation is not allowed in the current state. The offending function is ignored, having no side effect other than to set the error flag.";
}
case GL_STACK_OVERFLOW:
{
return "This function would cause a stack overflow. The offending function is ignored, having no side effect other than to set the error flag.";
}
case GL_STACK_UNDERFLOW:
{
return "This function would cause a stack underflow. The offending function is ignored, having no side effect other than to set the error flag.";
}
case GL_OUT_OF_MEMORY:
{
return "There is not enough memory left to execute the function. The state of OpenGL is undefined, except for the state of the error flags, after this error is recorded.";
}
default:
{
return "No error has been recorded. The value of this symbolic constant is guaranteed to be zero.";
}
}
}
GLvoid COGLUtils::setVSync(bool val)
{
#ifdef WIN32
char *extensions = (char*)glGetString(GL_EXTENSIONS);
if (strstr(extensions,"WGL_EXT_swap_control")==0)
{
return;
}
else
{
PFNWGLSWAPINTERVALFARPROC wglSwapIntervalEXT = (PFNWGLSWAPINTERVALFARPROC)wglGetProcAddress("wglSwapIntervalEXT");
if(wglSwapIntervalEXT)
{
wglSwapIntervalEXT(val?1:0);
}
}
#else
#ifdef USE_SDL
#if SDL_VERSION_ATLEAST(1,3,0)
SDL_GL_SetSwapInterval(val);
#else /* SDL_VERSION_ATLEAST(1,3,0) */
#ifdef SDL_GL_SWAP_CONTROL
SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, val);
#else /* SDL_GL_SWAP_CONTROL */
printf("VSync unsupported on old SDL versions (before 1.2.10).\n");
#endif /* SDL_GL_SWAP_CONTROL */
#endif /* SDL_VERSION_ATLEAST(1,3,0) */
#endif
#endif
}
GLvoid COGLUtils::clear()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
#ifdef WIN32
GLvoid COGLUtils::swapDC(HDC dc)
{
SwapBuffers(dc);
}
#else
GLvoid COGLUtils::swapDC()
{
SDL_GL_SwapBuffers();
}
#endif
};