I don't know where else to post... I cannot get external textures to feed into my GLSL shader... so frustrating!

main class for gl:

Qt Code:
  1. /*
  2.  * glviewer.cpp
  3.  * eternity-opengl
  4.  *
  5.  * Created by Chris Healer on 4/19/08.
  6.  * Copyright 2008 The Molecule. All rights reserved.
  7.  *
  8.  */
  9.  
  10. #include "glviewer.h"
  11.  
  12. void printProgramInfoLog(GLuint obj);
  13. void printShaderInfoLog(GLuint obj);
  14.  
  15. static GLint cubeArray[][3] = {
  16. {0, 0, 0}, {0, 1, 0}, {1, 1, 0}, {1, 0, 0},
  17. {0, 0, 1}, {1, 0, 1}, {1, 1, 1}, {0, 1, 1},
  18. {0, 0, 0}, {1, 0, 0}, {1, 0, 1}, {0, 0, 1},
  19. {0, 1, 0}, {0, 1, 1}, {1, 1, 1}, {1, 1, 0},
  20. {0, 1, 0}, {0, 0, 0}, {0, 0, 1}, {0, 1, 1},
  21. {1, 0, 0}, {1, 1, 0}, {1, 1, 1}, {1, 0, 1}
  22. };
  23.  
  24. static GLint cubeTextureArray[][2] = {
  25. {0, 0}, {1, 0}, {1, 1}, {0, 1},
  26. {0, 0}, {0, 1}, {1, 1}, {1, 0},
  27. {0, 0}, {1, 0}, {1, 1}, {0, 1},
  28. {1, 0}, {0, 0}, {0, 1}, {1, 1},
  29. {0, 0}, {1, 0}, {1, 1}, {0, 1},
  30. {1, 0}, {0, 0}, {0, 1}, {1, 1}
  31. };
  32.  
  33. static GLint faceArray[][2] = {
  34. {1, -1}, {1, 1}, {-1, 1}, {-1, -1}
  35. };
  36.  
  37. static GLfloat texCoordArray[][2] = {
  38. {0.0, 0.0}, {1.0,0.0}, {1.0,1.0}, {0.0,1.0}
  39. };
  40.  
  41. /*
  42. static GLubyte colorArray[][4] = {
  43.   {170, 202, 0, 255},
  44.   {120, 143, 0, 255},
  45.   {83, 102, 0, 255},
  46.   {120, 143, 0, 255}
  47. };
  48. */
  49. //glviewer::glviewer(QWidget *parent) : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
  50. glviewer::glviewer(QWidget *parent) : QGLWidget(parent)
  51. {
  52. shader = 0;
  53.  
  54. if (QGLFramebufferObject::hasOpenGLFramebufferObjects())
  55. printf("Has framebuffer objects.\n");
  56. }
  57.  
  58. void glviewer::createEternityShader(QString vertexShaderPath, QString fragmentShaderPath)
  59. {
  60. GLint compiled;
  61.  
  62. QFile vf(vertexShaderPath);
  63. QFile ff(fragmentShaderPath);
  64.  
  65. if (!vf.open(QFile::ReadOnly) || !ff.open(QFile::ReadOnly))
  66. {
  67. printf("ERROR! Current path is:\n");
  68. system("pwd");
  69. }
  70.  
  71. QByteArray qv = vf.readAll();
  72. QByteArray qf = ff.readAll();
  73.  
  74. const char * vd = qv.data();
  75. const char * fd = qf.data();
  76.  
  77. printf("%s\n%s\n",vd,fd);
  78.  
  79. v = glCreateShader(GL_VERTEX_SHADER);
  80. f = glCreateShader(GL_FRAGMENT_SHADER);
  81. g = 0; // no geometry shader
  82.  
  83. glShaderSource(f, 1, &fd,NULL);
  84. glShaderSource(v, 1, &vd,NULL);
  85.  
  86. glCompileShader(v);
  87. glGetShaderiv(v, GL_COMPILE_STATUS, &compiled);
  88. if (!compiled) printShaderInfoLog(v);
  89.  
  90. glCompileShader(f);
  91. glGetShaderiv(f, GL_COMPILE_STATUS, &compiled);
  92. if (!compiled) printShaderInfoLog(f);
  93.  
  94. shader = glCreateProgram();
  95. glAttachShader(shader,v);
  96. glAttachShader(shader,f);
  97.  
  98. glLinkProgram(shader);
  99. printProgramInfoLog(shader);
  100.  
  101. glUseProgram(shader);
  102.  
  103. defaultTextureSampler = glGetUniformLocation(shader,"Tex");
  104. glUniform1i(defaultTextureSampler,defaultTexture);
  105.  
  106. GL_CHECK_ERROR();
  107.  
  108. printf("Shaders were successfully created.\n");
  109. }
  110.  
  111. void glviewer::initializeEternityShader()
  112. {
  113. printf("would populate here.\n");
  114. }
  115.  
  116. void glviewer::initializeGL()
  117. {
  118. printf("initialize!\n");
  119.  
  120. makeCurrent();
  121. fbo = new QGLFramebufferObject(160, 160);
  122.  
  123. // QImage im(":res/images/default1.png");
  124. QImage im(":res/images/cubelogo.png");
  125. defaultTexture = bindTexture(im);
  126.  
  127. printf("Default texture number: %d\n",(int)defaultTexture);
  128.  
  129. createEternityShader(VERTEX_SHADER_PATH,FRAGMENT_SHADER_PATH);
  130.  
  131. glMatrixMode(GL_MODELVIEW);
  132.  
  133. glEnableClientState(GL_VERTEX_ARRAY);
  134. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  135. glTexCoordPointer(2, GL_INT, 0, cubeTextureArray);
  136.  
  137. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  138. glEnable(GL_BLEND);
  139. glEnable(GL_TEXTURE_2D);
  140. glEnable(GL_DEPTH_TEST);
  141.  
  142. glActiveTexture(GL_TEXTURE0);
  143. glEnable(GL_TEXTURE_2D);
  144. defaultTextureSampler = glGetUniformLocation(shader,"Tex");
  145. // glUniform1i(defaultTextureSampler,fbo->texture());
  146. glUniform1i(defaultTextureSampler,defaultTexture);
  147.  
  148. glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
  149. pbufferList = glGenLists(1);
  150. glNewList(pbufferList, GL_COMPILE);
  151. {
  152. glClear(GL_COLOR_BUFFER_BIT);
  153.  
  154. // draw shaded square
  155. glMatrixMode(GL_MODELVIEW);
  156. glPushMatrix();
  157. // glEnable(GL_TEXTURE_2D);
  158. glLoadIdentity();
  159. glTranslatef(0.5f, 0.5f, -2.0f);
  160. glActiveTexture(GL_TEXTURE0);
  161. glEnable(GL_TEXTURE_2D);
  162. glTexCoordPointer(2, GL_FLOAT, 0, texCoordArray);
  163. glVertexPointer(2, GL_INT, 0, faceArray);
  164. glDrawArrays(GL_QUADS, 0, 4);
  165. glPopMatrix();
  166.  
  167. // draw cube
  168. glTranslatef(0.5f, 0.5f, 0.5f);
  169. glRotatef(3.0f, 1.0f, 1.0f, 1.0f);
  170. glTranslatef(-0.5f, -0.5f, -0.5f);
  171. glColor4f(0.9f, 0.9f, 0.9f, 1.0f);
  172. glVertexPointer(3, GL_INT, 0, cubeArray);
  173. glDrawArrays(GL_QUADS, 0, 24);
  174.  
  175. }
  176. glEndList();
  177. }
  178.  
  179. void glviewer::paintGL()
  180. {
  181. printf("paint!\n");
  182.  
  183. glClear(GL_COLOR_BUFFER_BIT); //clear whole widget with clearColor
  184.  
  185. // glPopMatrix(); // pop the matrix pushed in the pbuffer list
  186.  
  187. // push the projection matrix and the entire GL state before
  188. // doing any rendering into our framebuffer object
  189. glPushAttrib(GL_ALL_ATTRIB_BITS);
  190. glMatrixMode(GL_PROJECTION);
  191. glPushMatrix();
  192. glViewport(0, 0, fbo->size().width(), fbo->size().height());
  193. glMatrixMode(GL_PROJECTION);
  194. glLoadIdentity();
  195. glOrtho(-1, 1, -1, 1, -99, 99);
  196. glTranslatef(-0.5f, -0.5f, 0.0f);
  197.  
  198. glMatrixMode(GL_MODELVIEW);
  199. glActiveTexture(GL_TEXTURE0);
  200. glEnable(GL_TEXTURE_2D);
  201.  
  202. // render to the framebuffer object
  203. fbo->bind();
  204. glCallList(pbufferList);
  205. fbo->release();
  206.  
  207. // pop the projection matrix and GL state back for rendering
  208. // to the actual widget
  209. glPopAttrib();
  210. glMatrixMode(GL_PROJECTION);
  211. glPopMatrix();
  212.  
  213. GL_CHECK_ERROR();
  214. printf("pt2\n");
  215.  
  216. glEnable(GL_TEXTURE_2D);
  217. glBindTexture(GL_TEXTURE_2D, fbo->texture());
  218. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  219.  
  220. // draw the background
  221. glMatrixMode(GL_MODELVIEW);
  222. glPushMatrix();
  223. glLoadIdentity();
  224. glMatrixMode(GL_PROJECTION);
  225. glPushMatrix();
  226. glLoadIdentity();
  227.  
  228. // glRotatef(180,0,0,1);
  229. // glRotatef(45,45,0,1);
  230. glVertexPointer(2, GL_INT, 0, faceArray);
  231. // glColor4f(0.5, 0.5, 0.5, 1.0);
  232. glDrawArrays(GL_QUADS, 0, 4);
  233. glPopMatrix();
  234. glMatrixMode(GL_MODELVIEW);
  235. glPopMatrix();
  236.  
  237. //glFlush();
  238.  
  239. GL_CHECK_ERROR();
  240. }
  241.  
  242. void glviewer::resizeGL(int w, int h)
  243. {
  244. printf("resize!\n");
  245.  
  246. glViewport(0, 0, (GLint)w, (GLint)h);
  247. glMatrixMode(GL_PROJECTION);
  248. glLoadIdentity();
  249. float aspect = w/(float)(h ? h : 1);
  250. glFrustum(-aspect, aspect, -1, 1, 10, 100);
  251. glTranslatef(-0.5f, -0.5f, -0.5f);
  252. glTranslatef(0.0f, 0.0f, -15.0f);
  253. }
  254.  
  255. /*!
  256. internal function used by constructor, to print out the compiling log. Critical errors are reported with popups.
  257. */
  258. void printShaderInfoLog(GLuint obj)
  259. {
  260. GLint infologLength = 0;
  261. GLsizei charsWritten = 0;
  262. char *infoLog;
  263.  
  264. glGetShaderiv(obj, GL_INFO_LOG_LENGTH,&infologLength);
  265.  
  266. if (infologLength > 0)
  267. {
  268. infoLog = (char *)malloc(infologLength);
  269. glGetShaderInfoLog(obj, infologLength, &charsWritten, infoLog);
  270. printf("%s\n",infoLog);
  271. free(infoLog);
  272. }
  273. }
  274.  
  275. void printProgramInfoLog(GLuint obj)
  276. {
  277. GLint infologLength = 0;
  278. GLsizei charsWritten = 0;
  279. char *infoLog;
  280.  
  281. glGetProgramiv(obj, GL_INFO_LOG_LENGTH,&infologLength);
  282.  
  283. if (infologLength > 0)
  284. {
  285. infoLog = (char *)malloc(infologLength);
  286. glGetProgramInfoLog(obj, infologLength, &charsWritten, infoLog);
  287. printf("%s\n",infoLog);
  288. free(infoLog);
  289. }
  290. }
To copy to clipboard, switch view to plain text mode 

vertex GLSL:

Qt Code:
  1. /* Just passthrough */
  2.  
  3. //varying vec2 TexCoord;
  4.  
  5. void main(void)
  6. {
  7. // TexCoord = gl_MultiTexCoord0.st;
  8. // TexCoord = gl_TexCoord[0.st;
  9. gl_TexCoord[0] = gl_MultiTexCoord0;
  10. gl_Position = ftransform();
  11. }
To copy to clipboard, switch view to plain text mode 

fragment GLSL:

Qt Code:
  1. uniform sampler2D Tex;
  2. //varying vec2 TexCoord;
  3.  
  4. void main()
  5. {
  6. vec4 color = texture2D(Tex, gl_TexCoord[0].st);
  7. gl_FragColor = color;
  8. // gl_FragColor = texture2D(Tex, gl_TexCoord[0].st);
  9. // gl_FragColor = vec4(0.0,0.0,1.0,1.0);
  10. // gl_FragColor = texture2D(Tex,TexCoord);
  11. // gl_FragColor = gl_FragCoord;
  12. // gl_FragColor = TexCoord;
  13. // gl_FragColor += vec4(.5,.01,.01,1.0);
  14. }
To copy to clipboard, switch view to plain text mode 

I feel like I'm making some simple mistake... most everything is based on the framebuffer2 opengl example.

It just won't work on my ATI (MacBook pro) -- now I'm wondering if I'm using nvidia commands.

HELP!