git-svn-id: svn://db.shs.com.ru/libs@540 a8b55f48-bf90-11e4-a774-851b48703e85

This commit is contained in:
2019-05-27 16:36:51 +00:00
parent 09e7268779
commit bb75525a3b
37 changed files with 926 additions and 683 deletions

View File

@@ -110,8 +110,8 @@ typedef unsigned long ulong;
typedef unsigned long long ullong;
typedef long double ldouble;
const double deg2rad = atan(1.) / 45.;
const double rad2deg = 45. / atan(1.);
const float deg2rad = atanf(1.f) / 45.f;
const float rad2deg = 45.f / atanf(1.f);
# ifdef WINDOWS
inline int random() {return rand();}
@@ -121,9 +121,9 @@ inline int random() {return rand();}
#endif
#ifdef CC_VC
inline double round(const double & v) {return floor(v + 0.5);}
inline float round(const float & v) {return floor(v + 0.5);}
#endif
inline double randomu() {return double(random()) / RAND_MAX;}
inline float randomu() {return float(random()) / RAND_MAX;}
inline const QSizeF operator *(const QSizeF & f, const QSizeF & s) {return QSizeF(f.width() * s.width(), f.height() * s.height());}
#ifndef PIP_VERSION
@@ -136,13 +136,13 @@ template<typename Type> inline Type piClamp(const Type & v, const Type & min, co
inline ushort letobe_s(ushort v) {return (v << 8) | (v >> 8);}
#endif
// return [-1, 1]
inline double urand(const double & scale = 1.) {return ((double)rand() / RAND_MAX - .5) * (scale + scale);}
inline float urand(const float & scale = 1.) {return ((float)rand() / RAND_MAX - .5f) * (scale + scale);}
// return [0, 1]
inline double uprand(const double & scale = 1.) {return ((double)rand() / RAND_MAX) * scale;}
inline float uprand(const float & scale = 1.) {return ((float)rand() / RAND_MAX) * scale;}
QString readCharsUntilNull(QDataStream & s);
QString findFile(const QString & file, const QStringList & pathes);
inline QColor operator *(const QColor & c, double v) {return QColor(c.red() * v, c.green() * v, c.blue() * v, c.alpha() * v);}
inline QColor operator /(const QColor & c, double v) {return QColor(c.red() / v, c.green() / v, c.blue() / v, c.alpha() / v);}
inline QColor operator *(const QColor & c, float v) {return QColor(c.red() * v, c.green() * v, c.blue() * v, c.alpha() * v);}
inline QColor operator /(const QColor & c, float v) {return QColor(c.red() / v, c.green() / v, c.blue() / v, c.alpha() / v);}
//extern __GLWidget__ * currentQGLView;
@@ -158,26 +158,26 @@ inline void glSetCapEnabled(GLenum cap, bool on = true) {if (on) glEnable(cap);
inline void glSetLightEnabled(bool on) {if (on) glEnable(GL_LIGHTING); else glDisable(GL_LIGHTING);}
inline void glSetFogEnabled(bool on) {if (on) glEnable(GL_FOG); else glDisable(GL_FOG);}
inline void glSetPolygonMode(GLenum mode) {glPolygonMode(GL_FRONT_AND_BACK, mode);}
void glDrawQuad(QOpenGLShaderProgram * prog = 0, QVector4D * corner_dirs = 0, GLfloat x = -1.f, GLfloat y = -1.f, GLfloat w = 2.f, GLfloat h = 2.f);
void glDrawQuad(QOpenGLShaderProgram * prog = nullptr, QVector4D * corner_dirs = nullptr, GLfloat x = -1.f, GLfloat y = -1.f, GLfloat w = 2.f, GLfloat h = 2.f);
QMatrix4x4 getGLMatrix(GLenum matrix);
void setGLMatrix(QMatrix4x4 matrix);
inline void deleteGLTexture(GLuint & tex) {if (tex != 0) glDeleteTextures(1, &tex); tex = 0;}
# define QGLCI if (!QOpenGLContext::currentContext()) return; QOpenGLExtraFunctions gf(QOpenGLContext::currentContext());
# define QGLC gf.
inline void glActiveTextureChannel(int channel) {QGLCI gf.glActiveTexture(GL_TEXTURE0 + channel);}
inline void glDisableTextures(int channels = 8) {QGLCI for (int i = channels - 1; i >= 0; --i) {glActiveTextureChannel(i); glDisable(GL_TEXTURE_2D); glDisable(GL_TEXTURE_CUBE_MAP);}}
inline void glReleaseTextures(int channels = 8) {QGLCI for (int i = channels - 1; i >= 0; --i) {glActiveTextureChannel(i); glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_CUBE_MAP, 0);}}
inline void glReleaseFramebuffer() {QGLCI gf.glBindFramebuffer(GL_FRAMEBUFFER, 0);}
inline void glReleaseShaders() {QGLCI gf.glUseProgram(0);}
inline void deleteGLFramebuffer(GLuint & fbo) {QGLCI if (fbo != 0) gf.glDeleteFramebuffers(1, &fbo); fbo = 0;}
inline void deleteGLRenderbuffer(GLuint & drbo) {QGLCI if (drbo != 0) gf.glDeleteRenderbuffers(1, &drbo); drbo = 0;}
inline void deleteGLBuffer(GLuint & bo) {QGLCI if (bo != 0) gf.glDeleteBuffers(1, &bo); bo = 0;}
inline void deleteGLVertexArray(GLuint & va) {QGLCI if (va != 0) gf.glDeleteVertexArrays(1, &va); va = 0;}
//# define QGLCI if (!QOpenGLContext::currentContext()) return; QOpenGLExtraFunctions gf(QOpenGLContext::currentContext());
//# define QGLC gf.
//inline void glActiveTextureChannel(int channel) {QGLCI gf.glActiveTexture(GL_TEXTURE0 + channel);}
//inline void glDisableTextures(int channels = 8) {QGLCI for (int i = channels - 1; i >= 0; --i) {glActiveTextureChannel(i); glDisable(GL_TEXTURE_2D); glDisable(GL_TEXTURE_CUBE_MAP);}}
//inline void glReleaseTextures(int channels = 8) {QGLCI for (int i = channels - 1; i >= 0; --i) {glActiveTextureChannel(i); glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_CUBE_MAP, 0);}}
//inline void glReleaseFramebuffer() {QGLCI gf.glBindFramebuffer(GL_FRAMEBUFFER, 0);}
//inline void glReleaseShaders() {QGLCI gf.glUseProgram(0);}
//inline void deleteGLFramebuffer(GLuint & fbo) {QGLCI if (fbo != 0) gf.glDeleteFramebuffers(1, &fbo); fbo = 0;}
//inline void deleteGLRenderbuffer(GLuint & drbo) {QGLCI if (drbo != 0) gf.glDeleteRenderbuffers(1, &drbo); drbo = 0;}
//inline void deleteGLBuffer(GLuint & bo) {QGLCI if (bo != 0) gf.glDeleteBuffers(1, &bo); bo = 0;}
//inline void deleteGLVertexArray(GLuint & va) {QGLCI if (va != 0) gf.glDeleteVertexArrays(1, &va); va = 0;}
void createGLTexture(GLuint & tex, int width, int height, const GLenum & format = GL_RGBA8, const GLenum & target = GL_TEXTURE_2D);
void createGLTexture(GLuint & tex, const QImage & image, const GLenum & format = GL_RGBA8, const GLenum & target = GL_TEXTURE_2D);
inline void qglTranslate(const QVector3D & v) {glTranslated(v.x(), v.y(), v.z());}
inline void qglScale(const QVector3D & v) {glScaled(v.x(), v.y(), v.z());}
QMatrix4x4 glMatrixPerspective(double angle, double aspect, double near_, double far_);
inline void qglTranslate(const QVector3D & v) {glTranslatef(v.x(), v.y(), v.z());}
inline void qglScale(const QVector3D & v) {glScalef(v.x(), v.y(), v.z());}
QMatrix4x4 glMatrixPerspective(float angle, float aspect, float near_, float far_);
QImage rotateQImageLeft(const QImage & im);
QImage rotateQImageRight(const QImage & im);
inline QImage rotateQImage180(const QImage & im) {return im.mirrored(true, true);}
@@ -197,11 +197,11 @@ struct Box3D {
Box3D() {x = y = z = width = length = height = angle_z = angle_xy = angle_roll = 0.f;}
Box3D(const QVector3D & center, GLfloat hwid, GLfloat hlen, GLfloat hhei) {x = center.x() - hwid; y = center.y() - hlen; z = center.z() - hhei; width = 2 * hwid; length = 2 * hlen; height = 2 * hhei; angle_z = angle_xy = angle_roll = 0.f;}
Box3D(const QVector<QVector3D> & points);
bool isEmpty() const {return (qAbs(width) < 1E-6) || (qAbs(length) < 1E-6) || (qAbs(height) < 1E-6);}
bool isEmpty() const {return (qAbs(width) < 1E-6f) || (qAbs(length) < 1E-6f) || (qAbs(height) < 1E-6f);}
QVector3D randomPoint() const {return QVector3D(uprand(length) + x, uprand(width) + y, uprand(height) + z);}
QVector3D pos() const {return QVector3D(x, y, z);}
QVector3D size() const {return QVector3D(length, width, height);}
QVector3D center() const {return QVector3D(length / 2. + x, width / 2. + y, height / 2. + z);}
QVector3D center() const {return QVector3D(length / 2.f + x, width / 2.f + y, height / 2.f + z);}
QVector3D angles() const {return QVector3D(angle_xy, angle_roll, angle_z);}
QVector<QVector3D> corners() const;
void setPos(const QVector3D & p) {x = p.x(); y = p.y(); z = p.z();}
@@ -228,14 +228,14 @@ struct Vector3d {
Vector3d(const QVector3D & v) {x = v.x(); y = v.y(); z = v.z();}
Vector3d(const QString & str);
inline void clear() {x = y = z = 0.;}
inline GLfloat length() const {return sqrt(x*x + y*y + z*z);}
inline GLfloat length() const {return sqrtf(x*x + y*y + z*z);}
inline GLfloat lengthSquared() const {return x*x + y*y + z*z;}
Vector3d & normalize() {
GLfloat l = length();
if (l == 0.) return *this;
if (l == 0.f) return *this;
x /= l; y /= l; z /= l;
return *this;
};
}
Vector3d normalized() {return Vector3d(*this).normalize();}
Vector3d projectTo(Vector3d dir) {dir.normalize(); return dir * dot(dir, *this);}
Vector3d operator *(const GLfloat v) {return Vector3d(x*v, y*v, z*v);}
@@ -309,19 +309,19 @@ inline QDataStream & operator <<(QDataStream & s, const Vector3i & v) {s << v.p0
inline QDataStream & operator >>(QDataStream & s, Vector3i & v) {s >> v.p0 >> v.p1 >> v.p2; return s;}
QColor colorFromString(const QString & str);
inline double cosABV(const QVector3D & v0, const QVector3D & v1) {
double l = v0.length() * v1.length();
if (l == 0.) return 0.;
inline float cosABV(const QVector3D & v0, const QVector3D & v1) {
float l = v0.length() * v1.length();
if (l == 0.f) return 0.;
return (QVector3D::dotProduct(v0, v1)) / l;
}
inline QVector3D projection(const QVector3D & v, const QVector3D & to) {return to.normalized() * v.length() * cosABV(v, to);}
QVector3D orthToVector(const QVector3D & v, const double & scale = 1.);
QVector3D orthToVector(const QVector3D & v, const float & scale = 1.);
QVector3D rotateVector(const QVector3D & v, const QVector3D & a);
void setVectorLength(QVector3D & v, const double & l);
void lengthenVector(QVector3D & v, const double & l);
inline double squareLength(const QVector3D & from, const QVector3D & to) {return (to.x() - from.x())*(to.x() - from.x()) + (to.y() - from.y())*(to.y() - from.y()) + (to.z() - from.z())*(to.z() - from.z());}
void setVectorLength(QVector3D & v, const float & l);
void lengthenVector(QVector3D & v, const float & l);
inline float squareLength(const QVector3D & from, const QVector3D & to) {return (to.x() - from.x())*(to.x() - from.x()) + (to.y() - from.y())*(to.y() - from.y()) + (to.z() - from.z())*(to.z() - from.z());}
inline QVector3D directionFromAngles(const QVector3D & a) {return rotateVector(QVector3D(1., 0., 0.), a);}
inline double frac(const double & x, const double & b) {return x - int(x / b) * b;}
inline float frac(const float & x, const float & b) {return x - int(x / b) * b;}
class GLObjectBase;
class QGLView;
@@ -334,14 +334,15 @@ class QGLViewBase
friend class GLObjectBase;
public:
QGLViewBase();
Camera & camera();
const Camera & camera() const;
void setCamera(const Camera & camera);
virtual ~QGLViewBase();
Camera * camera();
const Camera * camera() const;
void setCamera(Camera * camera);
GLTextureManagerBase * textureManager();
protected:
virtual void collectLights() = 0;
Camera * camera_;
GLTextureManagerBase * m_texture_manager;
GLTextureManagerBase * textures_manager;
};
#endif // GLTYPES_H