Results 1 to 12 of 12

Thread: How to sue glMapBuffers/glUnmapBuffer with QGLWidget?

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

    Angry How to sue glMapBuffers/glUnmapBuffer with QGLWidget?

    I am trying to follow the OpenGL Superbible (4th Edition) example (ThunderbirdGL) and they have a function glMapBuffers() in a VBOMesh.cpp file and I can't get that function to be recognized by any header that is included with GL. Am I missing something simple?

    Qt Code:
    1. ///////////////////////////////////////////////////////////////////////////
    2.  
    3. // Scale of the vertices. The only way to do this is to map the VBO back
    4.  
    5. // into client memory, then back again
    6.  
    7. void CVBOMesh::Scale(GLfloat fScaleValue)
    8.  
    9. {
    10.  
    11. glFuncs.glBindBuffer(GL_ARRAY_BUFFER, bufferObjects[VERTEX_DATA]);
    12.  
    13. M3DVector3f *pVertexData = (M3DVector3f *)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
    14.  
    15.  
    16.  
    17. if(pVertexData != NULL)
    18.  
    19. {
    20.  
    21. for(unsigned int i = 0; i < nNumVerts; i++)
    22.  
    23. m3dScaleVector3(pVertexData[i], fScaleValue);
    24.  
    25.  
    26.  
    27. glUnmapBuffer(GL_ARRAY_BUFFER);
    28.  
    29. }
    30.  
    31. }
    To copy to clipboard, switch view to plain text mode 

    The error I see from the compiler is:
    ../shared/VBOMesh.cpp:204: error: 'glMapBuffer' was not declared in this scope
    ../shared/VBOMesh.cpp:211: error: 'glUnmapBuffer' was not declared in this scope

    where shared is the folder that VBOMesh is in.

    http://www.opengl.org/sdk/docs/man/x...lMapBuffer.xml reference for glMapBuffer
    http://www.opengl.org/sdk/docs/man4/...lMapBuffer.xml reference for glUnmapBuffer

    Any help would be awesome! Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to sue glMapBuffers/glUnmapBuffer with QGLWidget?

    Did you include GL.h?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    jshafferman (25th October 2013)

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

    Default Re: How to sue glMapBuffers/glUnmapBuffer with QGLWidget?

    Yup I still get the same error after I use this include
    #include <GL/gl.h>

    VBOMesh.h has #include <QtOpenGL> passed into it from a different header in case your curious.

    Just an FYI, I am trying to use mostly Qt related stuff because I am doing all my opengl stuff in QGLWidget. Also I am using Qt 4.8.4 because that is the same version we use at my office and eventually we will be doing Opengl stuff in our widget. Finally the reason I am using Opengl Superbible 4th edition is because that is the superbible edition that matches our products opengl capability set (2.0, 2.1, and ES 2.0)
    Last edited by jshafferman; 25th October 2013 at 14:13.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to sue glMapBuffers/glUnmapBuffer with QGLWidget?

    I never used glMapBuffer so I might be wrong but don't you have to query for this function using dlsym/getprocaddress first?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    jshafferman (25th October 2013)

  7. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to sue glMapBuffers/glUnmapBuffer with QGLWidget?

    Yes, this function needs to be loaded. I have always used GLEW (The OpenGL Extension Wrangler Library) to load additional functions, simply include glew before gl.h:
    Qt Code:
    1. #include <GL/glew.h>
    2. #include <GL/gl.h>
    3. ...
    To copy to clipboard, switch view to plain text mode 
    and link with -lGLEW.
    More info here : GLEW

  8. The following user says thank you to stampede for this useful post:

    jshafferman (25th October 2013)

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

    Default Re: How to sue glMapBuffers/glUnmapBuffer with QGLWidget?

    Ok, so I haven't used glew before but if I am guessing I can't use QGLFunctions anymore... Is there a way around that? I will show the whole class just so you can see how I am using QGLFunctions and the current problem is the final function in this .cpp

    Qt Code:
    1. #include "VBOMesh.h" (in VBOMesh.h I have #inlcude <GL/glew.h>)
    2.  
    3. #include <QGLFunctions>
    4.  
    5. QGLFunctions glFuncs(QGLContext::currentContext());
    6.  
    7. ///////////////////////////////////////////////////////////
    8. // Constructor, does what constructors do... set everything to zero or NULL
    9. CVBOMesh::CVBOMesh(void)
    10. {
    11. pIndexes = NULL;
    12. pVerts = NULL;
    13. pNorms = NULL;
    14. pTexCoords = NULL;
    15.  
    16. nMaxIndexes = 0;
    17. nNumIndexes = 0;
    18. nNumVerts = 0;
    19. }
    20.  
    21. ////////////////////////////////////////////////////////////
    22. // Free any dynamically allocated memory. For those C programmers
    23. // coming to C++, it is perfectly valid to delete a NULL pointer.
    24. CVBOMesh::~CVBOMesh(void)
    25. {
    26. // Just in case these still are allocated when the object is destroyed
    27. delete [] pIndexes;
    28. delete [] pVerts;
    29. delete [] pNorms;
    30. delete [] pTexCoords;
    31.  
    32. // Delete buffer objects
    33. glFuncs.glDeleteBuffers(4, bufferObjects);
    34. }
    35.  
    36. ////////////////////////////////////////////////////////////
    37. // Start assembling a mesh. You need to specify a maximum amount
    38. // of indexes that you expect. The EndMesh will clean up any uneeded
    39. // memory. This is far better than shreading your heap with STL containers...
    40. // At least that's my humble opinion.
    41. void CVBOMesh::BeginMesh(GLuint nMaxVerts)
    42. {
    43. // Just in case this gets called more than once...
    44. delete [] pIndexes;
    45. delete [] pVerts;
    46. delete [] pNorms;
    47. delete [] pTexCoords;
    48.  
    49. nMaxIndexes = nMaxVerts;
    50. nNumIndexes = 0;
    51. nNumVerts = 0;
    52.  
    53. // Allocate new blocks
    54. pIndexes = new GLushort[nMaxIndexes];
    55. pVerts = new M3DVector3f[nMaxIndexes];
    56. pNorms = new M3DVector3f[nMaxIndexes];
    57. pTexCoords = new M3DVector2f[nMaxIndexes];
    58. }
    59.  
    60. /////////////////////////////////////////////////////////////////
    61. // Add a triangle to the mesh. This searches the current list for identical
    62. // (well, almost identical - these are floats you know...) verts. If one is found, it
    63. // is added to the index array. If not, it is added to both the index array and the vertex
    64. // array grows by one as well.
    65. void CVBOMesh::AddTriangle(M3DVector3f verts[3], M3DVector3f vNorms[3], M3DVector2f vTexCoords[3])
    66. {
    67. const float e = 0.000001; // How small a difference to equate
    68.  
    69. // First thing we do is make sure the normals are unit length!
    70. // It's almost always a good idea to work with pre-normalized normals
    71. m3dNormalizeVector(vNorms[0]);
    72. m3dNormalizeVector(vNorms[1]);
    73. m3dNormalizeVector(vNorms[2]);
    74.  
    75.  
    76. // Search for match - triangle consists of three verts
    77. for(GLuint iVertex = 0; iVertex < 3; iVertex++)
    78. {
    79. GLuint iMatch = 0;
    80. for(iMatch = 0; iMatch < nNumVerts; iMatch++)
    81. {
    82. // If the vertex positions are the same
    83. if(m3dCloseEnough(pVerts[iMatch][0], verts[iVertex][0], e) &&
    84. m3dCloseEnough(pVerts[iMatch][1], verts[iVertex][1], e) &&
    85. m3dCloseEnough(pVerts[iMatch][2], verts[iVertex][2], e) &&
    86.  
    87. // AND the Normal is the same...
    88. m3dCloseEnough(pNorms[iMatch][0], vNorms[iVertex][0], e) &&
    89. m3dCloseEnough(pNorms[iMatch][1], vNorms[iVertex][1], e) &&
    90. m3dCloseEnough(pNorms[iMatch][2], vNorms[iVertex][2], e) &&
    91.  
    92. // And Texture is the same...
    93. m3dCloseEnough(pTexCoords[iMatch][0], vTexCoords[iVertex][0], e) &&
    94. m3dCloseEnough(pTexCoords[iMatch][1], vTexCoords[iVertex][1], e))
    95. {
    96. // Then add the index only
    97. pIndexes[nNumIndexes] = iMatch;
    98. nNumIndexes++;
    99. break;
    100. }
    101. }
    102.  
    103. // No match for this vertex, add to end of list
    104. if(iMatch == nNumVerts)
    105. {
    106. memcpy(pVerts[nNumVerts], verts[iVertex], sizeof(M3DVector3f));
    107. memcpy(pNorms[nNumVerts], vNorms[iVertex], sizeof(M3DVector3f));
    108. memcpy(pTexCoords[nNumVerts], &vTexCoords[iVertex], sizeof(M3DVector2f));
    109. pIndexes[nNumIndexes] = nNumVerts;
    110. nNumIndexes++;
    111. nNumVerts++;
    112. }
    113. }
    114. }
    115.  
    116. //////////////////////////////////////////////////////////////////
    117. // Compact the data. This is a nice utility, but you should really
    118. // save the results of the indexing for future use if the model data
    119. // is static (doesn't change).
    120. void CVBOMesh::EndMesh(void)
    121. {
    122. // Create the buffer objects
    123. glFuncs.glGenBuffers(4, bufferObjects);
    124.  
    125. // Copy data to video memory
    126. // Vertex data
    127. glFuncs.glBindBuffer(GL_ARRAY_BUFFER, bufferObjects[VERTEX_DATA]);
    128. glFuncs.glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*nNumVerts*3, pVerts, GL_STATIC_DRAW);
    129.  
    130. // Normal data
    131. glFuncs.glBindBuffer(GL_ARRAY_BUFFER, bufferObjects[NORMAL_DATA]);
    132. glFuncs.glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*nNumVerts*3, pNorms, GL_STATIC_DRAW);
    133.  
    134. // Texture coordinates
    135. glFuncs.glBindBuffer(GL_ARRAY_BUFFER, bufferObjects[TEXTURE_DATA]);
    136. glFuncs.glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*nNumVerts*2, pTexCoords, GL_STATIC_DRAW);
    137.  
    138. // Indexes
    139. glFuncs.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferObjects[INDEX_DATA]);
    140. glFuncs.glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort)*nNumIndexes, pIndexes, GL_STATIC_DRAW);
    141.  
    142. // Free older, larger arrays
    143. delete [] pIndexes;
    144. delete [] pVerts;
    145. delete [] pNorms;
    146. delete [] pTexCoords;
    147.  
    148. // Reasign pointers so they are marked as unused
    149. pIndexes = NULL;
    150. pVerts = NULL;
    151. pNorms = NULL;
    152. pTexCoords = NULL;
    153. }
    154.  
    155. //////////////////////////////////////////////////////////////////////////
    156. // Draw - make sure you call glEnableClientState for these arrays
    157. void CVBOMesh::Draw(void)
    158. {
    159. // Here's where the data is now
    160. glFuncs.glBindBuffer(GL_ARRAY_BUFFER, bufferObjects[VERTEX_DATA]);
    161. glVertexPointer(3, GL_FLOAT,0, 0);
    162.  
    163. // Normal data
    164. glFuncs.glBindBuffer(GL_ARRAY_BUFFER, bufferObjects[NORMAL_DATA]);
    165. glNormalPointer(GL_FLOAT, 0, 0);
    166.  
    167. // Texture coordinates
    168. glFuncs.glBindBuffer(GL_ARRAY_BUFFER, bufferObjects[TEXTURE_DATA]);
    169. glTexCoordPointer(2, GL_FLOAT, 0, 0);
    170.  
    171. // Indexes
    172. glFuncs.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferObjects[INDEX_DATA]);
    173. glDrawElements(GL_TRIANGLES, nNumIndexes, GL_UNSIGNED_SHORT, 0);
    174. }
    175.  
    176.  
    177. ///////////////////////////////////////////////////////////////////////////
    178. // Scale of the vertices. The only way to do this is to map the VBO back
    179. // into client memory, then back again
    180. void CVBOMesh::Scale(GLfloat fScaleValue)
    181. {
    182. glFuncs.glBindBuffer(GL_ARRAY_BUFFER, bufferObjects[VERTEX_DATA]);
    183. M3DVector3f *pVertexData = (M3DVector3f *)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
    184.  
    185. if(pVertexData != NULL)
    186. {
    187. for(unsigned int i = 0; i < nNumVerts; i++)
    188. m3dScaleVector3(pVertexData[i], fScaleValue);
    189.  
    190. glUnmapBuffer(GL_ARRAY_BUFFER);
    191. }
    192. }
    To copy to clipboard, switch view to plain text mode 

    If I add #include <GL/glew.h> before VBOMesh/QGLFunctions I get the following errors

    Compile Errors:
    /opt/Qt/include/QtOpenGL/qglfunctions.h:24:2: warning: #warning qglfunctions.h is not compatible with GLEW, GLEW defines will be undefined
    /opt/Qt/include/QtOpenGL/qglfunctions.h:25:2: warning: #warning To use GLEW with Qt, do not include <QtOpenGL> or <QGLFunctions> after glew.h
    ....

    Edit:
    This disappears when I include the lib -lGLEW

    /home/jshafferman/OpenGL/build-ThunderbirdGL-Desktop-Debug/../shared/VBOMesh.cpp:204: undefined reference to `__glewMapBuffer'
    /home/jshafferman/OpenGL/build-ThunderbirdGL-Desktop-Debug/../shared/VBOMesh.cpp:211: undefined reference to `__glewUnmapBuffer'

    So by that compiler error I am not allowed to use QGLFunctions so all the places where glFuncs."" are used won't work anymore but does glew have that functionality built into it?

    Thanks for all your help! You are all rockstars


    Added after 6 minutes:


    I build 'green' however I still have the above warnings and when I try to run the program I get this error/crash response:

    ASSERT: "context" in file qglfunctions.cpp, line 143
    The program has unexpectedly finished.

    Any ideas?
    Last edited by jshafferman; 25th October 2013 at 16:51.

  10. #7
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to sue glMapBuffers/glUnmapBuffer with QGLWidget?

    I never used QGLFunctions, but I think you shouldn't mix it with GLEW. I'd just stick to plain OpenGL calls and remove all occurences of QGLFunctions from the project. GLEW should have all functions you need.

  11. The following user says thank you to stampede for this useful post:

    jshafferman (25th October 2013)

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

    Default Re: How to sue glMapBuffers/glUnmapBuffer with QGLWidget?

    Thank you very much for your comments! Yeah it was the QGLFunctions not working properly or not having the set of functions I wanted. The glew library worked perfectly for me so once I removed the QGLFunction the glew library allowed me to handle all the functions that I wanted it to. Thanks stampede and wysota!

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

    Default Re: How to sue glMapBuffers/glUnmapBuffer with QGLWidget?

    Boo! So I am now running into an issue that is related to using glew (or so I think). I compile green inside of Qt Creator no problem but when I run my executable I get this output:

    texobj = 2
    level = 0
    The program has unexpectedly finished.
    I will put the .cpp file in the next thread...

    Where thunderBirdBody/Glass are the VBOMesh previously discussed.


    Added after 5 minutes:


    Part 1:

    Qt Code:
    1. #include "glwidget.h"
    2.  
    3. #include "../shared/gltools.h"
    4.  
    5. #include <math.h>
    6. #include <QKeyEvent>
    7.  
    8. // Thunderbird body
    9. extern short face_indicies[3704][9];
    10. extern GLfloat vertices [1898][3];
    11. extern GLfloat normals [2716][3];
    12. extern GLfloat textures [2925][2];
    13.  
    14. // Glass cock-pit
    15. extern short face_indiciesGlass[352][9];
    16. extern GLfloat verticesGlass[197][3];
    17. extern GLfloat normalsGlass [227][3];
    18. extern GLfloat texturesGlass [227][2];
    19.  
    20. namespace
    21. {
    22. // Storeage for two texture objects
    23. GLuint textureObjects[3];
    24. #define CUBE_MAP 0
    25. #define BODY_TEXTURE 1
    26. #define GLASS_TEXTURE 2
    27.  
    28. // Six sides of a cube map
    29. const char *szCubeFaces[6] = { "../Vbo/pos_x.tga", "../Vbo/neg_x.tga", "../Vbo/pos_y.tga", "../Vbo/neg_y.tga",
    30. "../Vbo/pos_z.tga", "../Vbo/neg_z.tga" };
    31.  
    32. GLenum cube[6] = { GL_TEXTURE_CUBE_MAP_POSITIVE_X,
    33. GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
    34. GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
    35. GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
    36. GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
    37. GL_TEXTURE_CUBE_MAP_NEGATIVE_Z };
    38. }
    39.  
    40. GLWidget::GLWidget(QWidget *parent) :
    41. QGLWidget(QGLFormat(QGL::DoubleBuffer | QGL::DepthBuffer | QGL::SampleBuffers), parent)
    42. {
    43. animationTimer.setSingleShot(false);
    44. connect(&animationTimer, SIGNAL(timeout()), this, SLOT(TimerFunction()));
    45. animationTimer.start(0);
    46. }
    47.  
    48. GLWidget::~GLWidget()
    49. {
    50. glDeleteTextures(2, textureObjects);
    51. }
    52.  
    53. void GLWidget::initializeGL()
    54. {
    55.  
    56. GLfloat fScale = 0.01f;
    57. GLbyte *pBytes;
    58. GLint iWidth, iHeight, iComponents;
    59. GLenum eFormat;
    60. int i;
    61.  
    62. // Cull backs of polygons
    63. glCullFace(GL_BACK);
    64. glFrontFace(GL_CCW);
    65. glEnable(GL_CULL_FACE);
    66. glEnable(GL_DEPTH_TEST);
    67.  
    68. glGenTextures(2, textureObjects);
    69.  
    70. // Set up texture maps
    71. // Cube Map
    72. glBindTexture(GL_TEXTURE_CUBE_MAP, textureObjects[CUBE_MAP]);
    73. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    74. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    75. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    76. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    77. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
    78.  
    79. // Load Cube Map images
    80. for(i = 0; i < 6; i++)
    81. {
    82. // Load this texture map
    83. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_GENERATE_MIPMAP, GL_TRUE);
    84. pBytes = gltLoadTGA(szCubeFaces[i], &iWidth, &iHeight, &iComponents, &eFormat);
    85. glTexImage2D(cube[i], 0, iComponents, iWidth, iHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBytes);
    86. free(pBytes);
    87. }
    88.  
    89.  
    90. // Load the body texture
    91. glBindTexture(GL_TEXTURE_2D, textureObjects[BODY_TEXTURE]);
    92. glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
    93. pBytes = gltLoadTGA("../Vbo/body.tga", &iWidth, &iHeight, &iComponents, &eFormat);
    94. glTexImage2D(GL_TEXTURE_2D, 0, iComponents, iWidth, iHeight, 0, eFormat, GL_UNSIGNED_BYTE, (void *)pBytes);
    95. free(pBytes);
    96.  
    97. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    98. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    99. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    100. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    101.  
    102. GLfloat fLargest;
    103. glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &fLargest);
    104. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, fLargest);
    105.  
    106.  
    107. glBindTexture(GL_TEXTURE_2D, textureObjects[GLASS_TEXTURE]);
    108. glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
    109.  
    110. pBytes = gltLoadTGA("../Vbo/glass.tga", &iWidth, &iHeight, &iComponents, &eFormat);
    111. glTexImage2D(GL_TEXTURE_2D, 0, iComponents, iWidth, iHeight, 0, eFormat, GL_UNSIGNED_BYTE, (void *)pBytes);
    112. free(pBytes);
    113.  
    114. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    115. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    116. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    117. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    118.  
    119.  
    120. /////////////////////////////////////////////////////////////////////
    121. // Set up the texture units
    122.  
    123. // First texture unit contains the color map
    124. glActiveTexture(GL_TEXTURE0);
    125. glEnable(GL_TEXTURE_2D);
    126. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); // Decal tarnish
    127.  
    128. // Second texture unit contains the cube map
    129. glActiveTexture(GL_TEXTURE1);
    130. glBindTexture(GL_TEXTURE_CUBE_MAP, textureObjects[CUBE_MAP]);
    131. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
    132. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
    133. glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
    134. glEnable(GL_TEXTURE_CUBE_MAP);
    135.  
    136. // Multiply this texture by the one underneath
    137. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    138.  
    139. // Load Thunderbird body and canopy
    140. // Temporary workspace
    141. M3DVector3f vVerts[3];
    142. M3DVector3f vNorms[3];
    143. M3DVector2f vTex[3];
    144.  
    145. // Start assembling the body mesh, set maximum size
    146. thunderBirdBody.BeginMesh(3704*3);
    147.  
    148. // Loop through all the faces
    149. for(int iFace = 0; iFace < 3704; iFace++)
    150. {
    151. // Assemble the triangle
    152. for(int iPoint = 0; iPoint < 3; iPoint++)
    153. {
    154. memcpy(&vVerts[iPoint][0], &vertices[face_indicies[iFace][iPoint]][0], sizeof(M3DVector3f));
    155. memcpy(&vNorms[iPoint][0], &normals[face_indicies[iFace][iPoint+3]][0], sizeof(M3DVector3f));
    156. memcpy(&vTex[iPoint][0], &textures[face_indicies[iFace][iPoint+6]][0], sizeof(M3DVector2f));
    157. }
    158.  
    159. thunderBirdBody.AddTriangle(vVerts, vNorms, vTex);
    160. }
    161.  
    162. // Close the mesh and scale it (it's a little BIG in it's original format)
    163. thunderBirdBody.EndMesh();
    164. thunderBirdBody.Scale(fScale);
    165.  
    166. // Now do the same for the canopy
    167. thunderBirdGlass.BeginMesh(352*3);
    168.  
    169. for(int iFace = 0; iFace < 352; iFace++)
    170. {
    171. // Assemble the triangle
    172. for(int iPoint = 0; iPoint < 3; iPoint++)
    173. {
    174. memcpy(&vVerts[iPoint][0], &verticesGlass[face_indiciesGlass[iFace][iPoint]][0], sizeof(M3DVector3f));
    175. memcpy(&vNorms[iPoint][0], &normalsGlass[face_indiciesGlass[iFace][iPoint+3]][0], sizeof(M3DVector3f));
    176. memcpy(&vTex[iPoint][0], &texturesGlass[face_indiciesGlass[iFace][iPoint+6]][0], sizeof(M3DVector2f));
    177. }
    178.  
    179. thunderBirdGlass.AddTriangle(vVerts, vNorms, vTex);
    180. }
    181.  
    182. thunderBirdGlass.EndMesh();
    183. thunderBirdGlass.Scale(fScale);
    184.  
    185. GLfloat fAmbLight[] = { 0.075f, 0.075f, 0.075f, 0.0f };
    186. GLfloat fDiffLight[] = { 1.0f, 1.0f, 1.0f, 0.0f };
    187. GLfloat fSpecLight[] = { 0.25f, 0.25f, 0.25f, 0.0f };
    188. GLfloat lightPos[] = { -50.0f, 100.0f, 100.0f, 1.0f };
    189.  
    190. // Set up lighting
    191. glEnable(GL_LIGHTING);
    192. glEnable(GL_LIGHT0);
    193. glEnable(GL_COLOR_MATERIAL);
    194. glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
    195. glMaterialfv(GL_FRONT, GL_SPECULAR, fDiffLight);
    196. glMateriali(GL_FRONT, GL_SHININESS, 128);
    197.  
    198. glLightfv(GL_LIGHT0, GL_AMBIENT, fAmbLight);
    199. glLightfv(GL_LIGHT0, GL_DIFFUSE, fDiffLight);
    200. glLightfv(GL_LIGHT0, GL_SPECULAR, fSpecLight);
    201. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, fAmbLight);
    202. glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
    203.  
    204. // Light never changes, put it here
    205. glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
    206.  
    207. frameCamera.MoveUp(20.0f);
    208. }
    To copy to clipboard, switch view to plain text mode 

    Part 2

    Qt Code:
    1. void GLWidget::DrawSkyBox()
    2. {
    3. GLfloat fExtent = 50.0f;
    4.  
    5. glBegin(GL_QUADS);
    6. //////////////////////////////////////////////
    7. // Negative X
    8. // Note, we must now use the multi-texture version of glTexCoord
    9. glMultiTexCoord3f(GL_TEXTURE1, -1.0f, -1.0f, 1.0f);
    10. glVertex3f(-fExtent, -fExtent, fExtent);
    11.  
    12. glMultiTexCoord3f(GL_TEXTURE1, -1.0f, -1.0f, -1.0f);
    13. glVertex3f(-fExtent, -fExtent, -fExtent);
    14.  
    15. glMultiTexCoord3f(GL_TEXTURE1, -1.0f, 1.0f, -1.0f);
    16. glVertex3f(-fExtent, fExtent, -fExtent);
    17.  
    18. glMultiTexCoord3f(GL_TEXTURE1, -1.0f, 1.0f, 1.0f);
    19. glVertex3f(-fExtent, fExtent, fExtent);
    20.  
    21.  
    22. ///////////////////////////////////////////////
    23. // Postive X
    24. glMultiTexCoord3f(GL_TEXTURE1, 1.0f, -1.0f, -1.0f);
    25. glVertex3f(fExtent, -fExtent, -fExtent);
    26.  
    27. glMultiTexCoord3f(GL_TEXTURE1, 1.0f, -1.0f, 1.0f);
    28. glVertex3f(fExtent, -fExtent, fExtent);
    29.  
    30. glMultiTexCoord3f(GL_TEXTURE1, 1.0f, 1.0f, 1.0f);
    31. glVertex3f(fExtent, fExtent, fExtent);
    32.  
    33. glMultiTexCoord3f(GL_TEXTURE1, 1.0f, 1.0f, -1.0f);
    34. glVertex3f(fExtent, fExtent, -fExtent);
    35.  
    36.  
    37. ////////////////////////////////////////////////
    38. // Negative Z
    39. glMultiTexCoord3f(GL_TEXTURE1, -1.0f, -1.0f, -1.0f);
    40. glVertex3f(-fExtent, -fExtent, -fExtent);
    41.  
    42. glMultiTexCoord3f(GL_TEXTURE1, 1.0f, -1.0f, -1.0f);
    43. glVertex3f(fExtent, -fExtent, -fExtent);
    44.  
    45. glMultiTexCoord3f(GL_TEXTURE1, 1.0f, 1.0f, -1.0f);
    46. glVertex3f(fExtent, fExtent, -fExtent);
    47.  
    48. glMultiTexCoord3f(GL_TEXTURE1, -1.0f, 1.0f, -1.0f);
    49. glVertex3f(-fExtent, fExtent, -fExtent);
    50.  
    51.  
    52. ////////////////////////////////////////////////
    53. // Positive Z
    54. glMultiTexCoord3f(GL_TEXTURE1, 1.0f, -1.0f, 1.0f);
    55. glVertex3f(fExtent, -fExtent, fExtent);
    56.  
    57. glMultiTexCoord3f(GL_TEXTURE1, -1.0f, -1.0f, 1.0f);
    58. glVertex3f(-fExtent, -fExtent, fExtent);
    59.  
    60. glMultiTexCoord3f(GL_TEXTURE1, -1.0f, 1.0f, 1.0f);
    61. glVertex3f(-fExtent, fExtent, fExtent);
    62.  
    63. glMultiTexCoord3f(GL_TEXTURE1, 1.0f, 1.0f, 1.0f);
    64. glVertex3f(fExtent, fExtent, fExtent);
    65.  
    66.  
    67. //////////////////////////////////////////////////
    68. // Positive Y
    69. glMultiTexCoord3f(GL_TEXTURE1, -1.0f, 1.0f, 1.0f);
    70. glVertex3f(-fExtent, fExtent, fExtent);
    71.  
    72. glMultiTexCoord3f(GL_TEXTURE1, -1.0f, 1.0f, -1.0f);
    73. glVertex3f(-fExtent, fExtent, -fExtent);
    74.  
    75. glMultiTexCoord3f(GL_TEXTURE1, 1.0f, 1.0f, -1.0f);
    76. glVertex3f(fExtent, fExtent, -fExtent);
    77.  
    78. glMultiTexCoord3f(GL_TEXTURE1, 1.0f, 1.0f, 1.0f);
    79. glVertex3f(fExtent, fExtent, fExtent);
    80.  
    81.  
    82. ///////////////////////////////////////////////////
    83. // Negative Y
    84. glMultiTexCoord3f(GL_TEXTURE1, -1.0f, -1.0f, -1.0f);
    85. glVertex3f(-fExtent, -fExtent, -fExtent);
    86.  
    87. glMultiTexCoord3f(GL_TEXTURE1, -1.0f, -1.0f, 1.0f);
    88. glVertex3f(-fExtent, -fExtent, fExtent);
    89.  
    90. glMultiTexCoord3f(GL_TEXTURE1, 1.0f, -1.0f, 1.0f);
    91. glVertex3f(fExtent, -fExtent, fExtent);
    92.  
    93. glMultiTexCoord3f(GL_TEXTURE1, 1.0f, -1.0f, -1.0f);
    94. glVertex3f(fExtent, -fExtent, -fExtent);
    95. glEnd();
    96. }
    97.  
    98. void GLWidget::DrawThunderBird()
    99. {
    100.  
    101. glEnableClientState(GL_VERTEX_ARRAY);
    102. glEnableClientState(GL_NORMAL_ARRAY);
    103. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    104.  
    105. glActiveTexture(GL_TEXTURE1);
    106. glDisable(GL_TEXTURE_CUBE_MAP);
    107. glActiveTexture(GL_TEXTURE0);
    108.  
    109. glPushMatrix();
    110. glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
    111. glTexEnvi(GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    112. glBindTexture(GL_TEXTURE_2D, textureObjects[BODY_TEXTURE]);
    113. thunderBirdBody.Draw();
    114. glPopMatrix();
    115.  
    116. glActiveTexture(GL_TEXTURE1);
    117. glEnable(GL_TEXTURE_CUBE_MAP);
    118. glActiveTexture(GL_TEXTURE0);
    119.  
    120. glEnable(GL_BLEND);
    121. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    122. glColor4f(1.0f, 1.0f, 1.0f, 0.25f);
    123. glBindTexture(GL_TEXTURE_2D, textureObjects[GLASS_TEXTURE]);
    124.  
    125. glTranslatef(0.0f, 0.132f, 0.555f);
    126.  
    127. glFrontFace(GL_CW);
    128. thunderBirdGlass.Draw();
    129. glFrontFace(GL_CCW);
    130. thunderBirdGlass.Draw();
    131. glDisable(GL_BLEND);
    132.  
    133. glDisableClientState(GL_VERTEX_ARRAY);
    134. glDisableClientState(GL_NORMAL_ARRAY);
    135. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    136. }
    137.  
    138.  
    139. // Called to draw scene
    140. void GLWidget::paintGL()
    141. {
    142. // Clear the window
    143. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    144.  
    145. glPushMatrix();
    146. frameCamera.ApplyCameraTransform(); // Move the camera about
    147.  
    148. // Sky Box is manually textured
    149. glActiveTexture(GL_TEXTURE0);
    150. glDisable(GL_TEXTURE_2D);
    151. glActiveTexture(GL_TEXTURE1);
    152.  
    153. glEnable(GL_TEXTURE_CUBE_MAP);
    154. glDisable(GL_TEXTURE_GEN_S);
    155. glDisable(GL_TEXTURE_GEN_T);
    156. glDisable(GL_TEXTURE_GEN_R);
    157. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
    158. DrawSkyBox();
    159.  
    160. // Use texgen to apply cube map
    161. glEnable(GL_TEXTURE_GEN_S);
    162. glEnable(GL_TEXTURE_GEN_T);
    163. glEnable(GL_TEXTURE_GEN_R);
    164. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    165. glDisable(GL_TEXTURE_CUBE_MAP);
    166.  
    167. glActiveTexture(GL_TEXTURE0);
    168. glEnable(GL_TEXTURE_2D);
    169.  
    170.  
    171. glActiveTexture(GL_TEXTURE1);
    172. glMatrixMode(GL_TEXTURE);
    173. glPushMatrix();
    174.  
    175. // Invert camera matrix (rotation only) and apply to
    176. // texture coordinates
    177. M3DMatrix44f m, invert;
    178. frameCamera.GetCameraOrientation(m);
    179. m3dInvertMatrix44(invert, m);
    180. glMultMatrixf(invert);
    181. glActiveTexture(GL_TEXTURE0);
    182. glMatrixMode(GL_MODELVIEW);
    183.  
    184. // This displays a rotating ThunderBird Model
    185. static GLfloat yRot = 0.0f;
    186. yRot += 1.1f;
    187. glTranslatef(0.0f, 19.6f, -3.0f);
    188. glRotatef(yRot, 0.0f, 1.0f, 0.0f);
    189. DrawThunderBird();
    190.  
    191. // This shows four ThunderBirds flying in formation
    192. // Coment out the above section, and uncomment
    193. // the following section to see the four ThunderBirds
    194. // in formation.
    195. /*
    196.   glPushMatrix();
    197.   glTranslatef(0.0f, 21.5f, -7.0f);
    198.   glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
    199.   glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
    200.   glRotatef(-10.0f, 1.0f, 0.0f, 0.0f);
    201.   DrawThunderBird();
    202.   glPopMatrix();
    203.  
    204.   glPushMatrix();
    205.   glTranslatef(0.85f, 20.75f, -6.5f);
    206.   glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
    207.   glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
    208.   glRotatef(-10.0f, 1.0f, 0.0f, 0.0f);
    209.   DrawThunderBird();
    210.   glPopMatrix();
    211.  
    212.   glPushMatrix();
    213.   glTranslatef(-1.0f, 19.75f, -7.0f);
    214.   glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
    215.   glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
    216.   glRotatef(-10.0f, 1.0f, 0.0f, 0.0f);
    217.   DrawThunderBird();
    218.   glPopMatrix();
    219.  
    220.   glPushMatrix();
    221.   glTranslatef(-0.15f, 19.0f, -6.5f);
    222.   glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
    223.   glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
    224.   glRotatef(-10.0f, 1.0f, 0.0f, 0.0f);
    225.   DrawThunderBird();
    226.   glPopMatrix();
    227.   */
    228.  
    229. glMatrixMode(GL_TEXTURE);
    230. glActiveTexture(GL_TEXTURE1);
    231. glPopMatrix();
    232. glActiveTexture(GL_TEXTURE0);
    233. glMatrixMode(GL_MODELVIEW);
    234. glPopMatrix();
    235.  
    236. // Do the buffer Swap
    237. swapBuffers();
    238. }
    239.  
    240. void GLWidget::keyPressEvent(QKeyEvent *event)
    241. {
    242.  
    243. if(event->key() == Qt::Key_Up)
    244. frameCamera.MoveForward(0.1f);
    245.  
    246. if(event->key() == Qt::Key_Down)
    247. frameCamera.MoveForward(-0.1f);
    248.  
    249. if(event->key() == Qt::Key_Left)
    250. frameCamera.RotateLocalY(0.1);
    251.  
    252. if(event->key() == Qt::Key_Right)
    253. frameCamera.RotateLocalY(-0.1);
    254.  
    255. if(event->key() == Qt::Key_PageUp)
    256. frameCamera.RotateLocalX(0.1);
    257.  
    258. if(event->key() == Qt::Key_PageDown)
    259. frameCamera.RotateLocalX(-0.1);
    260.  
    261. // Refresh the Window
    262. update();
    263. }
    264.  
    265. void GLWidget::TimerFunction()
    266. {
    267. update();
    268. }
    269.  
    270. void GLWidget::resizeGL(int width, int height)
    271. {
    272. GLfloat fAspect;
    273.  
    274. // Prevent a divide by zero, when window is too short
    275. // (you cant make a window of zero width).
    276. if(height == 0)
    277. height = 1;
    278.  
    279. glViewport(0, 0, width, height);
    280.  
    281. fAspect = (GLfloat)width / (GLfloat)height;
    282.  
    283. // Reset the coordinate system before modifying
    284. glMatrixMode(GL_PROJECTION);
    285. glLoadIdentity();
    286.  
    287. // Set the clipping volume
    288. gluPerspective(35.0f, fAspect, 1.0f, 1000.0f);
    289.  
    290. glMatrixMode(GL_MODELVIEW);
    291. glLoadIdentity();
    292. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jshafferman; 25th October 2013 at 21:39.

  14. #10
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to sue glMapBuffers/glUnmapBuffer with QGLWidget?

    1. Did you call glewInit() ?
    2. Did you try to debug the app ?
    btw. read here the part about GLEW

  15. The following user says thank you to stampede for this useful post:

    jshafferman (29th October 2013)

  16. #11
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: How to sue glMapBuffers/glUnmapBuffer with QGLWidget?

    Qt Code:
    1. #include <GL/glew.h>
    2. #include <GL/gl.h>
    3. #include <GL/glext.h>
    4.  
    5. void GlWidget::exec()
    6. {
    7. initializeGLFunctions();
    8.  
    9. GLuint vboId = ppi->getVBOID();
    10.  
    11. glBindBuffer(GL_ARRAY_BUFFER, vboId);
    12.  
    13. //glBufferData( GL_ARRAY_BUFFER, (ANGLE_COUNT/24 * RANGE_COUNT * 6 * sizeof(float)), buffer0, GL_DYNAMIC_DRAW );
    14.  
    15. if(segmentCount % 2 == 0)
    16. glBufferSubData(GL_ARRAY_BUFFER, segmentIndex * RANGE_COUNT * 6 * sizeof(float), 5 * RANGE_COUNT * 6 * sizeof(float), &buffer0[0]);
    17. else
    18. glBufferSubData(GL_ARRAY_BUFFER, segmentIndex * RANGE_COUNT * 6 * sizeof(float), 5 * RANGE_COUNT * 6 * sizeof(float), &buffer1[0]);
    19.  
    20. glBindBuffer(GL_ARRAY_BUFFER, 0);
    21. }
    To copy to clipboard, switch view to plain text mode 

  17. The following user says thank you to saman_artorious for this useful post:

    jshafferman (29th October 2013)

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

    Default Re: How to sue glMapBuffers/glUnmapBuffer with QGLWidget?

    Thanks to both comments! I was able to figure out that I hadn't initialized glew with the glewInit() function. I haven't used glew so I didn't realize you had to initialize it similair to glut. I put the glewInit() funciton as the first line in the the GLWidget::initializeGL() and everything is working as it should. I will most likely have more questions in the future but thanks for everyone and there help!

Similar Threads

  1. One QGLWidget that manage QGLWidget childs
    By polch in forum Qt Programming
    Replies: 4
    Last Post: 12th December 2012, 12:26
  2. QGLWidget and VBO
    By kaszewczyk in forum Newbie
    Replies: 1
    Last Post: 6th May 2010, 11:04
  3. QGLWidget
    By manmohan in forum Newbie
    Replies: 2
    Last Post: 5th June 2009, 13:54
  4. Can not use QGLWidget.
    By Teuniz in forum Installation and Deployment
    Replies: 3
    Last Post: 25th September 2007, 01:27
  5. QGLWidget bug
    By Wizard in forum Qt Programming
    Replies: 11
    Last Post: 31st August 2007, 12:23

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.