17 const float zLet[] = {
25 const int axisSegCount[] = {2, 3, 3};
26 const float* axisLabels[] = {xLet, yLet, zLet};
30 initializeOpenGLFunctions();
32 shader.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/gl/colored_lines.vert");
33 shader.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/gl/colored_lines.frag");
35 const int ptSize = 6*sizeof(float);
36 for(int lIdx = 0; lIdx < 3; lIdx++)
38 const float* l = axisLabels[lIdx];
39 const int ptCount = axisSegCount[lIdx]*2;
41 c[lIdx] = 1.0;//set color
42 QOpenGLBuffer b = flowerLabelVertices[lIdx];
45 b.allocate(ptCount*ptSize);
46 for(int pIdx = 0; pIdx < ptCount; pIdx++)
48 b.write(pIdx*ptSize, &(l[pIdx*3]), ptSize/2);//write coords
49 b.write(pIdx*ptSize + ptSize/2, c, ptSize/2);//write color
53 //Axis buffer: 6 floats per vertex, 2 vert per line, 3 lines
54 float aBuf[6*2*3] = {0.0};
55 for(int aIdx = 0; aIdx < 3; aIdx++)
57 aBuf[(2*aIdx)*6+3+aIdx] = 1.0;//Set color (last 3 floats)
58 aBuf[(2*aIdx+1)*6+3+aIdx] = 1.0;//Set color (last 3 floats)
59 aBuf[(2*aIdx+1)*6+aIdx] = 1.0;//Extend line in axis
61 //The lines which form the 'axis-flower' in the corner
62 flowerAxisVertices.create();
63 flowerAxisVertices.bind();
64 flowerAxisVertices.allocate(aBuf, sizeof(aBuf));
65 flowerAxisVertices.release();
66 //The lines which form the model-space axes
69 vertices.allocate(aBuf, sizeof(aBuf));
72 void Axis::setScale(QVector3D min, QVector3D max)
74 //Max function. not worth importing <algorithm> just for max
75 auto Max = [](float a, float b)
77 return (a > b) ? a : b;
79 //This is how much the axes extend beyond the model
80 //We want it to be dependent on the model's size, but uniform on all axes.
81 const float axismargin = 0.25*Max(Max(max[0]-min[0], max[1]-min[1]), max[2]-min[2]);
83 //Manually rewrite coordinates to control axis draw lengths
84 float s = sizeof(float);
85 //aIdx*12+aIdx gets us to the set of 2 points of the axis line, plus the offset for that dimension
86 //+6 gets us to the other end of the line in that dimension
87 for(int aIdx = 0; aIdx < 3; aIdx++)
89 float t = min[aIdx]-axismargin;
90 vertices.write(s*(aIdx*12+aIdx), &t, s);
91 t = max[aIdx]+axismargin;
92 vertices.write(s*(aIdx*12+aIdx+6), &t, s);
96 void Axis::draw(QMatrix4x4 transMat, QMatrix4x4 viewMat,
97 QMatrix4x4 orientMat, QMatrix4x4 aspectMat, float aspectRatio)
101 // Load the transform and view matrices into the shader
102 auto loadMatrixUniforms = [&](QMatrix4x4 transform, QMatrix4x4 view)
105 shader.uniformLocation("transform_matrix"),
106 1, GL_FALSE, transform.data());
108 shader.uniformLocation("view_matrix"),
109 1, GL_FALSE, view.data());
111 const GLuint vp = shader.attributeLocation("vertex_position");
112 const GLuint vc = shader.attributeLocation("vertex_color");
113 glEnableVertexAttribArray(vp);
114 glEnableVertexAttribArray(vc);
115 auto loadAttribPtr = [&]()
117 glVertexAttribPointer(vp, 3, GL_FLOAT, false,
118 6 * sizeof(GLfloat), 0);
119 glVertexAttribPointer(vc, 3, GL_FLOAT, false,
121 (GLvoid*)(3 * sizeof(GLfloat)));
123 loadMatrixUniforms(transMat, viewMat);
126 glDrawArrays(GL_LINES, 0, 3*6);
129 //Next, we draw the hud axis-flower
130 flowerAxisVertices.bind();
131 glClear(GL_DEPTH_BUFFER_BIT);//Ensure hud draws over everything
132 const float hudSize = 0.2;
134 //Move the hud to the bottom left corner with margin
135 if (aspectRatio > 1.0)
137 hudMat.translate(aspectRatio-2*hudSize, -1.0+2*hudSize, 0);
141 hudMat.translate(1.0-2*hudSize, -1.0/aspectRatio+2*hudSize, 0);
143 //Scale the hud to be small
144 hudMat.scale(hudSize, hudSize, 1);
145 loadMatrixUniforms(orientMat, aspectMat*hudMat);
147 glDrawArrays(GL_LINES, 0, 3*6);
148 flowerAxisVertices.release();
149 for(int aIdx = 0; aIdx < 3; aIdx++){
150 QVector3D transVec = QVector3D();
151 transVec[aIdx] = 1.25;//This is how far we want the letters to be extended out
152 QOpenGLBuffer b = flowerLabelVertices[aIdx];
153 //The only transform we want is to translate the letters to the ends of the axis lines
154 QMatrix4x4 labelTransMat = QMatrix4x4();
155 labelTransMat.translate(orientMat * transVec);
157 loadMatrixUniforms(labelTransMat, aspectMat * hudMat);
159 glDrawArrays(GL_LINES, 0, axisSegCount[aIdx]*2*6);