Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F103566152
shape.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Mon, Mar 3, 01:07
Size
4 KB
Mime Type
text/x-c
Expires
Wed, Mar 5, 01:07 (1 d, 11 h)
Engine
blob
Format
Raw Data
Handle
24576024
Attached To
rSYMKIT symkit
shape.cpp
View Options
#include "shape.h"
#include <QMatrix4x4>
#include <QGLShaderProgram>
#include "camera.h"
#include "shaderid.h"
Shape
::
Shape
(
const
QString
&
label
,
flags_t
flags
)
:
_label
(
label
),
flags
(
flags
),
_scale
(
1.0
)
{
// empty
}
Shape
::~
Shape
()
{
}
QString
Shape
::
label
()
const
{
return
_label
;
}
void
Shape
::
setGLBuffer
(
const
GLBuffer
&
buffer
)
{
this
->
buffer
=
buffer
;
}
const
GLBuffer
&
Shape
::
getGLBuffer
()
const
{
return
buffer
;
}
bool
Shape
::
isUpdating
()
const
{
return
(
flags
&
S_UPDATE
)
!=
0
;
}
void
Shape
::
setUpdate
(
bool
value
)
{
if
(
value
)
flags
|=
S_UPDATE
;
else
flags
&=
~
S_UPDATE
;
}
void
Shape
::
toggleUpdate
()
{
flags
^=
S_UPDATE
;
// Switch first bit of flags (Voir operateur ^(xor))
}
bool
Shape
::
isRendering
()
const
{
return
(
flags
&
S_RENDER
)
!=
0
;
}
bool
Shape
::
isFillMode
()
const
{
return
(
flags
&
S_FILL_MODE
)
!=
0
;
}
flags_t
Shape
::
getFlags
()
const
{
return
flags
;
}
void
Shape
::
setRender
(
bool
value
)
{
if
(
value
)
flags
|=
S_RENDER
;
else
flags
&=
~
S_RENDER
;
}
void
Shape
::
toggleRender
()
{
flags
^=
S_RENDER
;
}
void
Shape
::
setFlags
(
flags_t
flags
)
{
this
->
flags
=
flags
;
}
void
Shape
::
toggleFillMode
()
{
flags
^=
S_FILL_MODE
;
}
void
Shape
::
setFillMode
(
bool
value
)
{
if
(
value
)
flags
|=
S_FILL_MODE
;
else
flags
&=
~
S_FILL_MODE
;
}
bool
Shape
::
isTranslating
()
const
{
return
(
flags
&
S_TRANSLATE
)
!=
0
;
}
void
Shape
::
toggleTranslate
()
{
flags
^=
S_TRANSLATE
;
}
void
Shape
::
setTranslate
(
bool
value
)
{
if
(
value
)
flags
|=
S_TRANSLATE
;
else
flags
&=
~
S_TRANSLATE
;
}
bool
Shape
::
isRotating
()
const
{
return
(
flags
&
S_ROTATE
)
!=
0
;
}
void
Shape
::
toggleRotate
()
{
flags
^=
S_ROTATE
;
}
void
Shape
::
setRotate
(
bool
value
)
{
if
(
value
)
flags
|=
S_ROTATE
;
else
flags
&=
~
S_ROTATE
;
}
bool
Shape
::
isScaling
()
const
{
return
(
flags
&
S_SCALE
)
!=
0
;
}
void
Shape
::
toggleScale
()
{
flags
^=
S_SCALE
;
}
void
Shape
::
setScale
(
bool
value
)
{
if
(
value
)
flags
|=
S_SCALE
;
else
flags
&=
~
S_SCALE
;
}
bool
Shape
::
isUsingTexture
()
const
{
return
(
flags
&
S_TEXTURE
)
!=
0
;
}
void
Shape
::
toggleUseTexture
()
{
flags
^=
S_TEXTURE
;
}
void
Shape
::
useTexture
(
bool
value
)
{
if
(
value
)
flags
|=
S_TEXTURE
;
else
flags
&=
~
S_TEXTURE
;
}
double
Shape
::
scaling
()
const
{
return
_scale
;
}
void
Shape
::
setScaling
(
const
double
&
scale
)
{
if
(
scale
>
0
)
this
->
_scale
=
scale
;
}
void
Shape
::
scale
(
const
double
&
delta
)
{
if
(
delta
>
0
)
this
->
_scale
*=
delta
;
}
#define _USE_MATH_DEFINES
#include <cmath>
void
Shape
::
render
(
render_s
&
args
)
{
args
.
matrix
->
setToIdentity
();
if
(
this
->
isTranslating
())
args
.
matrix
->
translate
(
qtVector
(
-
this
->
position
()));
if
(
this
->
isRotating
())
{
SVector
<
3
>
angles
=
-
this
->
orientation
();
args
.
matrix
->
rotate
((
float
)
angles
[
0
],
{
1
,
0
,
0
});
args
.
matrix
->
rotate
((
float
)
angles
[
1
],
{
0
,
1
,
0
});
args
.
matrix
->
rotate
((
float
)
angles
[
2
],
{
0
,
0
,
1
});
}
if
(
this
->
isScaling
())
args
.
matrix
->
scale
(
(
float
)
this
->
scaling
()
);
args
.
shaders
->
setUniformValue
(
args
.
modelview_name
,
args
.
camera
->
resolve
(
*
args
.
matrix
));
/* set polygon mode */
if
(
this
->
isFillMode
()
&&
!
args
.
polygonMode
)
{
glPolygonMode
(
GL_FRONT_AND_BACK
,
GL_FILL
);
args
.
polygonMode
=
true
;
}
else
if
(
!
this
->
isFillMode
()
&&
args
.
polygonMode
)
{
glPolygonMode
(
GL_FRONT_AND_BACK
,
GL_LINE
);
args
.
polygonMode
=
false
;
}
#define COLOR_ARGS(buf) buf.red, buf.green, buf.blue, buf.alpha
if
(
this
->
isUsingTexture
()
)
{
// TODO bind texture to the graphics engine
}
else
// use the color
args
.
shaders
->
setAttributeValue
(
ShaderID
::
color
,
COLOR_ARGS
(
buffer
.
color
));
/* bind buffers */
buffer
.
vbo
.
bind
();
buffer
.
ibo
.
bind
();
/*
* enable GL_FLOAT formatting
* set a ibo offset, ! qt magical memory workaround !
*/
args
.
shaders
->
setAttributeBuffer
(
ShaderID
::
vertex
,
GL_FLOAT
,
0
,
3
);
args
.
shaders
->
enableAttributeArray
(
ShaderID
::
vertex
);
#define BUFFER_OFFSET(a) ((char*)nullptr + (a))
GLuint
counter
=
0
;
/* loop through GLGroups */
for
(
auto
group
:
buffer
.
groups
)
{
/* Finally draw onto the graphics OpenGL engine */
glDrawElements
(
group
.
primitive
,
group
.
ibo_size
/
sizeof
(
GLuint
),
GL_UNSIGNED_INT
,
BUFFER_OFFSET
(
counter
));
/* Increase elements memory counter */
counter
+=
group
.
ibo_size
;
}
/* Disable GL_FLOAT formatting */
args
.
shaders
->
disableAttributeArray
(
ShaderID
::
vertex
);
/* release buffers */
buffer
.
ibo
.
release
();
buffer
.
vbo
.
release
();
}
QVector3D
qtVector
(
const
SVector
<
3
>&
v
)
{
return
QVector3D
((
float
)
v
[
0
],
(
float
)
v
[
1
],
(
float
)
v
[
2
]);
}
Event Timeline
Log In to Comment