-
Notifications
You must be signed in to change notification settings - Fork 3
/
DataItem.cpp
159 lines (134 loc) · 4.87 KB
/
DataItem.cpp
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*******************************************************************************
DataItem: OpenGL context data.
Copyright (c) 2006-2008 Jordan Van Aalsburg
This file is part of the Dynamics Toolset.
The Dynamics Toolset is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option) any
later version.
The Dynamics Toolset is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License
along with the Dynamics Toolset. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
#include "DataItem.h"
#include <iostream>
// Vrui includes
//
#include <Misc/ThrowStdErr.h>
#include <GL/Extensions/GLARBVertexShader.h>
#include <GL/Extensions/GLARBFragmentShader.h>
// External includes
//
#include "VruiStreamManip.h"
#include "IO/ansi-color.h"
#include <string.h>
namespace DTS
{
//
// DataItem methods
//
DataItem::DataItem(void)
: hasPointParameterExtension(GLARBPointParameters::isSupported()),
hasVertexBufferObjectExtension(GLARBVertexBufferObject::isSupported()),
hasShaders(GLARBShaderObjects::isSupported()&&GLARBVertexShader::isSupported()&&GLARBFragmentShader::isSupported()),
vertexBufferId(0), spriteTextureObjectId(0),
version(0),
vertexShaderObject(0),fragmentShaderObject(0),programObject(0),
numParticles(0)
{
master::filter masterout(std::cout);
masterout() << "Initializing OpenGL extensions..." << std::endl;
masterout() << "\tGL_ARB_POINT_PARAMETERS : ";
if(hasPointParameterExtension)
{
GLARBPointParameters::initExtension();
masterout() << ansi::green(ansi::BOLD) << "OK" << ansi::endl;
}
else
{
masterout() << ansi::red(ansi::BOLD) << "NOT SUPPORTED" << ansi::endl;
}
masterout() << "\tGL_ARB_VERTEX_BUFFER_OBJECT : ";
if (hasVertexBufferObjectExtension)
{
// initialize the vertex buffer object extension
GLARBVertexBufferObject::initExtension();
// create a vertex buffer object
glGenBuffersARB(1,&vertexBufferId);
masterout() << ansi::green(ansi::BOLD) << "OK" << ansi::endl;
}
else
{
masterout() << ansi::red(ansi::BOLD) << "NOT SUPPORTED" << ansi::endl;
}
glGenTextures(1, &spriteTextureObjectId);
masterout() << "\tGL_ARB_SHADER_OBJECTS : ";
if(hasShaders)
{
/* Initialize the OpenGL extensions: */
GLARBShaderObjects::initExtension();
GLARBVertexShader::initExtension();
GLARBFragmentShader::initExtension();
/* Source code for vertex and fragment programs: */
static const char* vertexProgram="\
uniform float scaledParticleRadius; \
\
void main() \
{ \
vec4 vertexEye; \
\
/* Transform the vertex to eye coordinates: */ \
vertexEye=gl_ModelViewMatrix*gl_Vertex; \
\
/* Calculate point size based on vertex' eye distance along z direction: */ \
gl_PointSize=scaledParticleRadius*2.0*vertexEye.w/vertexEye.z; \
\
/* Use standard vertex position for fragment generation: */ \
gl_FrontColor=gl_Color; \
gl_Position=ftransform(); \
}";
static const char* fragmentProgram="\
uniform sampler2D tex0; \
\
void main() \
{ \
gl_FragColor=texture2D(tex0,gl_TexCoord[0].xy)*gl_Color; \
}";
/* Compile and link the point size computation shader program: */
vertexShaderObject=glCompileVertexShaderFromString(vertexProgram);
fragmentShaderObject=glCompileFragmentShaderFromString(fragmentProgram);
programObject=glLinkShader(vertexShaderObject,fragmentShaderObject);
scaledParticleRadiusLocation=glGetUniformLocationARB(programObject,"scaledParticleRadius");
tex0Location=glGetUniformLocationARB(programObject,"tex0");
masterout() << ansi::green(ansi::BOLD) << "OK" << ansi::endl;
}
else
{
masterout() << ansi::red(ansi::BOLD) << "NOT SUPPORTED" << ansi::endl;
}
/* Display list for StaticSolverTool */
dataDisplayListVersion = 0;
dataDisplayListId=glGenLists(1);
}
DataItem::~DataItem(void)
{
if(vertexBufferId>0)
{
// delete the vertex buffer object
glDeleteBuffersARB(1,&vertexBufferId);
}
// delete texture object(s)
glDeleteTextures(1, &spriteTextureObjectId);
if(hasShaders)
{
glDeleteObjectARB(programObject);
glDeleteObjectARB(vertexShaderObject);
glDeleteObjectARB(fragmentShaderObject);
}
/* Display list for StaticSolverTool */
glDeleteLists(dataDisplayListId, 1);
}
} // namspace::DTS