Reputation: 57
I want to display a rotated text in QGLWidget
, Qt, C++, but I haven't been able to find a working code so far. Is it even possible to render a rotated text in OpenGL?
what I want is something like this Rotated Picture but every time I get the original one:
This is my code:
Header file:
#pragma once
#include "QGLWidget"
#include "QOpenGLFunctions_3_0"
#include "QOpenGLFunctions"
#include "QOpenGLShaderProgram"
#include "QOpenGLShader"
#include "QOpenGLBuffer"
class GLPlotter : public QOpenGLWidget,
protected QOpenGLFunctions/*_3_0*/
{
public:
GLPlotter(QWidget* Parent = nullptr);
protected:
void initializeGL();
void resizeGL(int w, int h);
void paintGL();
inline void Draw_Axis();
inline void Draw_Text(QString Text, double x,double y, double Angle);
private:
void InitVertexShaderSource();
void InitFragmentShaderSource();
void InitVertexShader();
void InitFragmentShader();
void InitShaderProgram();
public:
protected:
private:
QString m_VertexShaderSource;
QString m_FragmentShaderSource;
QString m_TextVertexShaderSource;
QString m_TextFragmentShaderSource;
QOpenGLShader* m_VertexShader;
QOpenGLShader* m_FragmentShader;
QOpenGLShaderProgram* m_ShaderProgram;
double m_BoundaryLeft = -100.0;
double m_BoundaryRight = +100.0;
double m_BoundaryBottom = -100.0;
double m_BoundaryTop = +100.0;
double m_AxisLeft = -0.7;
double m_AxisRight = +0.999;
double m_AxisBottom = -0.8;
double m_AxisTop = +0.999;
};
.cpp
file:
#include "GLPlotter.h"
GLPlotter::GLPlotter(QWidget* Parent ) : QOpenGLWidget(Parent){}
void GLPlotter::initializeGL()
{
makeCurrent();
initializeOpenGLFunctions();
InitShaderProgram();
glEnable(GL_LINE_SMOOTH);
glEnable(GL_LINE_SMOOTH_HINT);
}
void GLPlotter::resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
}
void GLPlotter::paintGL()
{
glClear(GL_COLOR | GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0, 0, 0, 0);
m_ShaderProgram->bind();
Draw_Axis();
Draw_Text("Hello", 0, 0, 30.0);
m_ShaderProgram->release();
}
void GLPlotter::Draw_Axis()
{
GLfloat Vertices[] =
{
static_cast<GLfloat>(m_AxisLeft) , static_cast<GLfloat>(m_AxisBottom),
static_cast<GLfloat>(m_AxisRight) , static_cast<GLfloat>(m_AxisBottom),
static_cast<GLfloat>(m_AxisRight) , static_cast<GLfloat>(m_AxisTop),
static_cast<GLfloat>(m_AxisLeft) , static_cast<GLfloat>(m_AxisTop)
};
QOpenGLBuffer VBO(QOpenGLBuffer::VertexBuffer);
VBO.create();
VBO.bind();
VBO.allocate(Vertices, sizeof(Vertices));
m_ShaderProgram->setAttributeBuffer("posAttr", GL_FLOAT, 0, 2 );
m_ShaderProgram->enableAttributeArray("posAttr");
QMatrix4x4 Matrix;
Matrix.ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
m_ShaderProgram->setUniformValue("matrix", Matrix);
m_ShaderProgram->setUniformValue("colorUniform", 255.0, 150, 0.0, 255.0);
glLineWidth(4);
glDrawArrays(GL_LINE_LOOP, 0, 4);
m_ShaderProgram->disableAttributeArray("posAttr");
VBO.release();
VBO.destroy();
}
void GLPlotter::Draw_Text(QString Text, double x, double y, double Angle)
{
glPushMatrix();
glRotatef(Angle, 0.0, 0.0, 1.0);
renderText(x, y, 0.0, Text, QFont("Timew New Roman Bold Italic",20));
glPopMatrix();
}
void GLPlotter::InitVertexShaderSource()
{
m_VertexShaderSource = "attribute highp vec4 posAttr;\n"
"uniform highp mat4 matrix;\n"
"void main()"
"{"
"gl_Position = matrix * posAttr;\n"
"}\n";
}
void GLPlotter::InitFragmentShaderSource()
{
m_FragmentShaderSource = "uniform lowp vec4 colorUniform;"
"void main()"
"{"
"gl_FragColor = colorUniform;\n"
"}";
}
void GLPlotter::InitVertexShader()
{
m_VertexShader = new QOpenGLShader(QOpenGLShader::Vertex, this);
m_VertexShader->compileSourceCode(m_VertexShaderSource);
}
void GLPlotter::InitFragmentShader()
{
m_FragmentShader = new QOpenGLShader(QOpenGLShader::Fragment, this);
m_FragmentShader->compileSourceCode(m_FragmentShaderSource);
}
void GLPlotter::InitShaderProgram()
{
InitVertexShaderSource();
InitFragmentShaderSource();
InitVertexShader();
InitFragmentShader();
m_ShaderProgram = new QOpenGLShaderProgram(this);
m_ShaderProgram->addShader(m_VertexShader);
m_ShaderProgram->addShader(m_FragmentShader);
m_ShaderProgram->link();
}
Upvotes: 2
Views: 105