20 #ifndef _STELQGL2RENDERER_HPP_
21 #define _STELQGL2RENDERER_HPP_
25 #include <QGLShaderProgram>
28 #include "StelApp.hpp"
29 #include "StelCore.hpp"
30 #include "StelQGLGLSLShader.hpp"
31 #include "StelQGLRenderer.hpp"
32 #include "StelProjector.hpp"
33 #include "StelProjectorClasses.hpp"
34 #include "StelQGL2InterleavedArrayVertexBufferBackend.hpp"
51 , floatTexturesDisabled(true)
72 Q_ASSERT_X(!initialized, Q_FUNC_INFO,
73 "StelQGL2Renderer is already initialized");
85 qWarning() <<
"StelQGL2Renderer::init : Failed because Qt paint engine is not OpenGL2 \n"
86 "If paint engine is OpenGL3 or higher, this code needs to be updated";
90 if(!QGLFormat::openGLVersionFlags().testFlag(QGLFormat::OpenGL_Version_2_1) &&
91 !QGLFormat::openGLVersionFlags().testFlag(QGLFormat::OpenGL_ES_Version_2_0))
93 qWarning() <<
"StelQGL2Renderer::init : StelQGL2Renderer needs OpenGL 2.1 or OpenGL ES 2.0";
98 if(!
gl.hasOpenGLFeature(QGLFunctions::Shaders))
100 qWarning() <<
"StelQGL2Renderer::init : Required feature not supported: shaders";
103 if(!
gl.hasOpenGLFeature(QGLFunctions::Buffers))
105 qWarning() <<
"StelQGL2Renderer::init : Required feature not supported: VBOs/IBOs";
111 if(!
gl.hasOpenGLFeature(QGLFunctions::Multitexture))
113 qWarning() <<
"StelQGL2Renderer::init : Required feature not supported: Multitexturing";
119 qWarning() <<
"StelQGL2Renderer::init : parent init failed";
148 plainShader = loadBuiltinShader
152 "vec4 project(in vec4 v);\n"
153 "attribute mediump vec4 vertex;\n"
154 "uniform mediump mat4 projectionMatrix;\n"
157 " gl_Position = projectionMatrix * project(vertex);\n"
160 "uniform mediump vec4 globalColor;\n"
163 " gl_FragColor = globalColor;\n"
166 if(NULL == plainShader)
168 qWarning() <<
"StelQGL2Renderer::init failed to load plain shader";
171 builtinShaders.append(plainShader);
174 colorShader = loadBuiltinShader
178 "vec4 project(in vec4 v);\n"
179 "attribute highp vec4 vertex;\n"
180 "attribute mediump vec4 color;\n"
181 "uniform mediump mat4 projectionMatrix;\n"
182 "varying mediump vec4 outColor;\n"
185 " outColor = color;\n"
186 " gl_Position = projectionMatrix * project(vertex);\n"
189 "varying mediump vec4 outColor;\n"
192 " gl_FragColor = outColor;\n"
195 if(NULL == colorShader)
197 qWarning() <<
"StelQGL2Renderer::init failed to load color shader program";
200 builtinShaders.append(colorShader);
203 textureShader = loadBuiltinShader
207 "vec4 project(in vec4 v);\n"
208 "attribute highp vec4 vertex;\n"
209 "attribute mediump vec2 texCoord;\n"
210 "uniform mediump mat4 projectionMatrix;\n"
211 "varying mediump vec2 texc;\n"
214 " gl_Position = projectionMatrix * project(vertex);\n"
215 " texc = texCoord;\n"
218 "varying mediump vec2 texc;\n"
219 "uniform sampler2D tex;\n"
220 "uniform mediump vec4 globalColor;\n"
223 " gl_FragColor = texture2D(tex, texc) * globalColor;\n"
226 if(NULL == textureShader)
228 qWarning() <<
"StelQGL2Renderer::init failed to load texture shader";
231 builtinShaders.append(textureShader);
234 colorTextureShader = loadBuiltinShader
238 "vec4 project(in vec4 v);\n"
239 "attribute highp vec4 vertex;\n"
240 "attribute mediump vec2 texCoord;\n"
241 "attribute mediump vec4 color;\n"
242 "uniform mediump mat4 projectionMatrix;\n"
243 "varying mediump vec2 texc;\n"
244 "varying mediump vec4 outColor;\n"
247 " gl_Position = projectionMatrix * project(vertex);\n"
248 " texc = texCoord;\n"
249 " outColor = color;\n"
252 "varying mediump vec2 texc;\n"
253 "varying mediump vec4 outColor;\n"
254 "uniform sampler2D tex;\n"
257 " gl_FragColor = texture2D(tex, texc) * outColor;\n"
260 if(NULL == colorTextureShader)
262 qWarning() <<
"StelQGL2Renderer::init failed to load colorTexture shader";
265 builtinShaders.append(colorTextureShader);
270 floatTexturesDisabled =
289 return QGLFormat::openGLVersionFlags().testFlag(QGLFormat::OpenGL_Version_3_0)
290 && !floatTexturesDisabled;
316 if(NULL != customShader) {
return customShader;}
319 bool position, texCoord, normal, color;
320 position = texCoord = normal = color =
false;
321 for(
int attrib = 0; attrib < attributeCount; ++attrib)
326 case AttributeInterpretation_Position: position =
true;
break;
327 case AttributeInterpretation_TexCoord: texCoord =
true;
break;
328 case AttributeInterpretation_Normal: normal =
true;
break;
329 case AttributeInterpretation_Color: color =
true;
break;
331 Q_ASSERT_X(
false, Q_FUNC_INFO,
"Unknown vertex interpretation");
335 Q_ASSERT_X(position, Q_FUNC_INFO,
336 "Vertex formats without vertex position are not supported");
340 if(!texCoord && !normal && !color) {
return plainShader;}
341 if(!texCoord && !normal && color) {
return colorShader;}
342 if(texCoord && !normal && !color) {
return textureShader;}
343 if(texCoord && !normal && color) {
return colorTextureShader;}
348 qDebug() <<
"position: " << position <<
" texCoord: " << texCoord
349 <<
" normal: " << normal <<
" color: " << color;
351 Q_ASSERT_X(
false, Q_FUNC_INFO,
352 "Shader for vertex format not (yet) implemented");
369 Q_ASSERT_X(NULL == customShader, Q_FUNC_INFO,
370 "Trying to bind() a shader without releasing the previous shader.");
372 customShader = shader;
387 Q_ASSERT_X(shader == customShader, Q_FUNC_INFO,
388 "Trying to release() a shader when no shader or a different shader is bound.");
395 (
const PrimitiveType primitiveType,
const QVector<StelVertexAttribute>& attributes)
405 bool dontProject =
false)
411 Q_ASSERT_X(backend != NULL, Q_FUNC_INFO,
412 "StelQGL2Renderer: Vertex buffer created by different renderer backend "
416 if(indexBuffer != NULL)
419 Q_ASSERT_X(glIndexBuffer != NULL, Q_FUNC_INFO,
420 "StelQGL2Renderer: Index buffer created by different renderer "
421 "backend or uninitialized");
422 if(indexBuffer->length() == 0)
428 else if(backend->
length() == 0)
440 if(NULL == customShader) {shader->
bind();}
449 projector = (NULL != projector ? projector : &(*(projector2DDummy)));
451 if(!dontProject && (NULL == dynamic_cast<StelProjector2d*>(projector)))
454 glslProjector = projector->getGLSLProjector();
455 if(NULL == glslProjector || !glslProjector->
init(shader))
458 glslProjector = NULL;
459 statistics[BATCH_PROJECTIONS_CPU_TOTAL] += 1.0;
464 statistics[BATCH_PROJECTIONS_GPU_TOTAL] += 1.0;
470 statistics[BATCH_PROJECTIONS_NONE_TOTAL] += 1.0;
476 Q_ASSERT_X(
false, Q_FUNC_INFO,
"Failed to bind shader program");
478 if(NULL != glslProjector)
481 glslProjector->
preDraw(shader);
489 const Mat4f& projectionMatrix = projector->getProjectionMatrix();
495 const Vec4i viewXywh = projector->getViewportXywh();
496 glViewport(viewXywh[0], viewXywh[1], viewXywh[2], viewXywh[3]);
499 backend->
draw(*
this, projectionMatrix, glIndexBuffer, shader);
506 if(NULL != glslProjector)
512 if(NULL == customShader) {shader->
release();}
520 int textureUnitCount;
522 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &textureUnitCount);
523 return std::max(textureUnitCount, STELQGLRENDERER_MAX_TEXTURE_UNITS);
529 Q_ASSERT_X(initialized, Q_FUNC_INFO,
"uninitialized StelQGL2Renderer");
539 bool floatTexturesDisabled;
544 QVector<StelQGLGLSLShader *> builtinShaders;
575 const char*
const vSrc,
const char*
const fSrc)
585 qWarning() <<
"Failed to compile vertex shader of builtin shader \""
586 << name <<
"\" : " << result->
log();
593 qWarning() <<
"Failed to compile fragment shader of builtin shader \""
594 << name <<
"\" : " << result->
log();
601 qWarning() <<
"Failed to link builtin shader \""
602 << name <<
"\" : " << result->
log();
606 if(!result->
log().isEmpty())
608 qWarning() <<
"Log of the compilation of builtin shader \"" << name
609 <<
"\" :" << result->
log();
616 #endif // _STELQGL2RENDERER_HPP_