Results 1 to 2 of 2

Thread: How to properly use QGLShader and QGLShaderProgram

  1. #1
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Angry How to properly use QGLShader and QGLShaderProgram

    I am unable to get the following code to render anything . I am not sure where I might be going wrong, however it appears that when I attempt to compile the vertex shader the shader fails to compile and my program exits. I am not sure as to why it would be failing so I am hoping someone sees something that I do not.

    Qt Code:
    1. #include "glwidget.h"
    2.  
    3. #include <GL/glut.h>
    4.  
    5. #include <QAction>
    6. #include <QContextMenuEvent>
    7. #include <QDebug>
    8. #include <QKeyEvent>
    9. #include <QMenu>
    10. #include <QWheelEvent>
    11.  
    12. namespace
    13. {
    14. GLint windowWidth = 1024; // window size
    15. GLint windowHeight = 768;
    16.  
    17. GLboolean useVertexShader = GL_TRUE;
    18. GLboolean useFragmentShader = GL_TRUE;
    19. GLboolean doBlink = GL_FALSE;
    20.  
    21. GLboolean needsValidation = GL_TRUE;
    22.  
    23. GLint flickerLocation = -1;
    24.  
    25. GLfloat cameraPos[] = {100.0f, 150.0f, 200.0f, 1.0f};
    26. GLdouble cameraZoom = 0.6;
    27.  
    28. const int MAX_INFO_LOG_SIZE = 2048;
    29. }
    30.  
    31. GLWidget::GLWidget(QWidget *parent)
    32. : QGLWidget(QGLFormat(QGL::DoubleBuffer | QGL::Rgba | QGL::DepthBuffer), parent)
    33. , vShader(QGLShader::Vertex)
    34. , fShader(QGLShader::Fragment)
    35. , program(this)
    36. {
    37. CreateActions();
    38. CreateMenu();
    39.  
    40. int argc;
    41. char** argv = NULL;
    42.  
    43. glutInit(&argc, argv);
    44. }
    45.  
    46. void GLWidget::toggleVertexShader()
    47. {
    48. useVertexShader = !useVertexShader;
    49.  
    50. if (useVertexShader)
    51. {
    52. toggleVertexShaderAction->setText(tr("Toggle vertex shader (currently ON)"));
    53. program.addShader(&vShader);
    54. }
    55. else
    56. {
    57. toggleVertexShaderAction->setText(tr("Toggle vertex shader (currently OFF)"));
    58. program.removeShader(&vShader);
    59. }
    60.  
    61. Relink();
    62.  
    63. update();
    64. }
    65.  
    66. void GLWidget::toggleFragmentShader()
    67. {
    68. useFragmentShader = !useFragmentShader;
    69.  
    70. if (useFragmentShader)
    71. {
    72. toggleFragmentShaderAction->setText(tr("Toggle framgent shader (currently ON)"));
    73. program.addShader(&fShader);
    74. }
    75. else
    76. {
    77. toggleFragmentShaderAction->setText(tr("Toggle framgent shader (currently OFF)"));
    78. program.removeShader(&fShader);
    79. }
    80.  
    81. Relink();
    82.  
    83. update();
    84. }
    85.  
    86. void GLWidget::toggleFlicker()
    87. {
    88. doBlink = !doBlink;
    89.  
    90. if (doBlink)
    91. {
    92. toggleFlickerAction->setText(tr("Toggle flicker (currently ON)"));
    93. }
    94. else
    95. {
    96. toggleFlickerAction->setText(tr("Toggle flicker (currently OFF)"));
    97. }
    98. if (!doBlink && (flickerLocation != -1))
    99. program.setUniformValue(flickerLocation, 1.0f);
    100.  
    101. update();
    102. }
    103.  
    104. void GLWidget::initializeGL()
    105. {
    106. bool success;
    107.  
    108. GLchar vsString[] =
    109. "void main(void)\n"
    110. "{\n"
    111. " // This is our Hello World vertex shader\n"
    112. " // notice how comments are preceded by '//'\n"
    113. "\n"
    114. " // normal MVP transform\n"
    115. " vec4 clipCoord = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
    116. " gl_Position = clipCoord;\n"
    117. "\n"
    118. " // Copy the primary color\n"
    119. " gl_FrontColor = gl_Color;\n"
    120. "\n"
    121. " // Calculate NDC\n"
    122. " vec4 ndc = vec4(clipCoord.xyz, 0) / clipCoord.w;\n"
    123. "\n"
    124. " // Map from [-1,1] to [0,1] before outputting\n"
    125. " gl_FrontSecondaryColor = (ndc * 0.5) + 0.5;\n"
    126. "}\n";
    127.  
    128. GLchar fsString[] =
    129. "uniform float flickerFactor;\n"
    130. "\n"
    131. "void main(void)\n"
    132. "{\n"
    133. " // Mix primary and secondary colors, 50/50\n"
    134. " vec4 temp = mix(gl_Color, vec4(vec3(gl_SecondaryColor), 1.0), 0.5);\n"
    135. "\n"
    136. " // Multiply by flicker factor\n"
    137. " gl_FragColor = temp * flickerFactor;\n"
    138. "}\n";
    139.  
    140. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    141. glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
    142.  
    143. // Black background
    144. glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
    145. // glSecondaryColor3f(1.0f, 1.0f, 1.0f);
    146.  
    147. // Hidden surface removal
    148. glEnable(GL_DEPTH_TEST);
    149. glDepthFunc(GL_LEQUAL);
    150.  
    151. glShadeModel(GL_SMOOTH);
    152.  
    153. qDebug() << "Source code\n" << vsString;
    154. success = vShader.compileSourceCode(vsString);
    155. qDebug() << "Source code" << vShader.sourceCode();
    156. qDebug() << "isCompiled? " << vShader.isCompiled();
    157.  
    158. if (!success)
    159. {
    160. qDebug() << "Vertex Shader Failed Why?\n" << vShader.log();
    161. swapBuffers();
    162. exit(0);
    163. }
    164.  
    165. success = fShader.compileSourceCode(fsString);
    166. if (!success)
    167. {
    168. qDebug() << "Fragment Shader Failed Why?\n" << fShader.log();
    169. swapBuffers();
    170. exit(0);
    171. }
    172.  
    173. QGLShaderProgram program(context());
    174.  
    175. if (useVertexShader)
    176. program.addShader(&vShader);
    177. if (useFragmentShader)
    178. program.addShader(&fShader);
    179.  
    180. Link(GL_TRUE);
    181. }
    182.  
    183. void GLWidget::resizeGL(int w, int h)
    184. {
    185. windowWidth = w;
    186. windowHeight = h;
    187. }
    188.  
    189. void GLWidget::paintGL()
    190. {
    191. static GLfloat flickerFactor = 1.0f;
    192.  
    193. // Track camera angle
    194. glMatrixMode(GL_PROJECTION);
    195. glLoadIdentity();
    196. if (windowWidth > windowHeight)
    197. {
    198. GLdouble ar = (GLdouble)windowWidth / (GLdouble)windowHeight;
    199. glFrustum(-ar * cameraZoom, ar * cameraZoom, -cameraZoom, cameraZoom, 1.0, 1000.0);
    200. }
    201. else
    202. {
    203. GLdouble ar = (GLdouble)windowHeight / (GLdouble)windowWidth;
    204. glFrustum(-cameraZoom, cameraZoom, -ar * cameraZoom, ar * cameraZoom, 1.0, 1000.0);
    205. }
    206. glMatrixMode(GL_MODELVIEW);
    207. glLoadIdentity();
    208. gluLookAt(cameraPos[0], cameraPos[1], cameraPos[2],
    209. 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
    210.  
    211. glViewport(0, 0, windowWidth, windowHeight);
    212.  
    213. // Clear the window with current clearing color
    214. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    215.  
    216. if (doBlink && (flickerLocation != -1))
    217. {
    218. // Pick a random flicker factor
    219. flickerFactor += ((((GLfloat)rand())/((GLfloat)RAND_MAX)) - 0.5f) * 0.1f;
    220. if (flickerFactor > 1.0f) flickerFactor = 1.0f;
    221. if (flickerFactor < 0.0f) flickerFactor = 0.0f;
    222. program.setUniformValue(flickerLocation, flickerFactor);
    223. }
    224.  
    225. // Validate our shader before first use
    226. if (needsValidation)
    227. {
    228. needsValidation = GL_FALSE;
    229. }
    230.  
    231. // Draw objects in the scene
    232. DrawModels();
    233.  
    234. glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
    235. glDisable(GL_DEPTH_TEST);
    236. glEnable(GL_DEPTH_TEST);
    237.  
    238. if (glGetError() != GL_NO_ERROR)
    239. {
    240. glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
    241. }
    242.  
    243. // Flush drawing commands
    244. swapBuffers();
    245.  
    246. if (doBlink && (flickerLocation != -1))
    247. update();
    248. }
    249.  
    250. void GLWidget::Link(GLboolean firstTime)
    251. {
    252. bool success;
    253.  
    254. success = program.link();
    255.  
    256. if (!success)
    257. {
    258. qDebug() << "Program link failed, Why?\n" << program.log();
    259. glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
    260. swapBuffers();
    261. exit(0);
    262. }
    263.  
    264. if (firstTime)
    265. program.bind();
    266.  
    267. flickerLocation = program.uniformLocation("flickerFactor");
    268.  
    269. // Initially set the blink parameter to 1 (no flicker)
    270. if (flickerLocation != -1)
    271. program.setUniformValue(flickerLocation, 1.0f);
    272.  
    273. // Program object has changed, so we should revalidate
    274. needsValidation = GL_TRUE;
    275. }
    276.  
    277. void GLWidget::Relink()
    278. {
    279. Link(GL_FALSE);
    280. }
    To copy to clipboard, switch view to plain text mode 

    In initializeGL() you can see where I call vShader.compileSourceCode(vsString) and I can see the debug statement Vertex Shader Failed Why? followed by an empty string "", so I am not sure how to proceed when the vShader.log() doesn't tell me anything. Any ideas???? Thanks for the help!

  2. #2
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to properly use QGLShader and QGLShaderProgram

    Ok so apparantly I was doing things very wrong. I was able to get the program to work perfectly fine when I changed the members vShader, fShader, and program to pointers. I simply created them inside of the initializeGL and the program started working. I am not sure but maybe it both shaders and the program need to explicity call the QGLContext that is the QGLWidget's context. Either it works now! If anyone else has a question feel free to ask.

Similar Threads

  1. Replies: 8
    Last Post: 26th September 2013, 19:35
  2. Applying a QGLShader to a whole QGraphicsView
    By abarral in forum Qt Programming
    Replies: 0
    Last Post: 26th August 2012, 18:51
  3. Replies: 0
    Last Post: 16th April 2012, 22:58
  4. QGLShaderprogram, multiple textures
    By Mixpicles in forum Newbie
    Replies: 0
    Last Post: 21st May 2011, 11:21
  5. Replies: 3
    Last Post: 28th April 2011, 14:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.