OpenGL problem

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • [SOLVED]OpenGL problem

      I've been following the beginners tutorials from opengl-tutorial.org and decided to try and put it in to a class which would show a square with a random texture in the middle of the screen. However I seem to have hit a problem, whenever I run it, it just shows a white screen whereas it should show a square with a texture on a dark blue background. I assume I have missed a function or passed in a variable wrong, however I can't seem to find it. So I would really appreciate an experienced eye taking a look and pointing where I went wrong and other things that I have done badly.

      I will post my code first and the example that I modified and am effectively copying from below that.

      There may be some random and unused functions and variables about the place since I have been experimenting.

      Just previewed and it seems while the code is properly indented in the message editor, it does not seem to be when previewed. Is there anyway to do this?

      C Source Code

      1. #include <GL/glew.h>
      2. #include "glm/glm.hpp"
      3. #include "glm/gtc/matrix_transform.hpp"
      4. #include <Windows.h>
      5. #include "GLFW\glfw.h"
      6. #include <stdio.h>
      7. #include <typeinfo>
      8. #include <string>
      9. #include <iostream>
      10. #include <fstream>
      11. using namespace std;
      12. class Render;
      13. static Render* current;
      14. class Render
      15. {
      16. enum State
      17. {
      18. RUNNING,
      19. QUIT
      20. };
      21. State m_state;
      22. GLuint m_ProgramID;
      23. GLuint m_MatrixID;
      24. GLuint m_Texture;
      25. GLuint m_TextureID;
      26. GLuint m_VertexArrayID;
      27. GLuint m_VertexBuffer;
      28. GLuint m_UVBuffer;
      29. static const long m_Width;
      30. static const long m_Height;
      31. static const GLfloat g_vertex_buffer_data[12];
      32. static const GLfloat g_uv_buffer_data[8];
      33. glm::mat4 m_Model;
      34. glm::mat4 m_Projection;
      35. glm::mat4 m_View;
      36. glm::mat4 m_MVP;
      37. glm::vec2 m_speed; //units per sec
      38. public:
      39. static void GLFWCALL INPUTCALLBACK(int Key, int Action);
      40. Render();
      41. ~Render();
      42. void Init();
      43. void Destruct();
      44. void RenderNow();
      45. void Update(float deltaTime);
      46. int Run();
      47. private:
      48. GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path)
      49. {
      50. // Create the shaders
      51. GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
      52. GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
      53. // Read the Vertex Shader code from the file
      54. std::string VertexShaderCode;
      55. std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
      56. if(VertexShaderStream.is_open())
      57. {
      58. std::string Line = "";
      59. while(getline(VertexShaderStream, Line))
      60. VertexShaderCode += "\n" + Line;
      61. VertexShaderStream.close();
      62. }
      63. else
      64. {
      65. printf("Impossible to open %s. Are you in the right directory ? Don't forget to read the FAQ !\n", vertex_file_path);
      66. return 0;
      67. }
      68. // Read the Fragment Shader code from the file
      69. std::string FragmentShaderCode;
      70. std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
      71. if(FragmentShaderStream.is_open())
      72. {
      73. std::string Line = "";
      74. while(getline(FragmentShaderStream, Line))
      75. FragmentShaderCode += "\n" + Line;
      76. FragmentShaderStream.close();
      77. }
      78. }
      79. GLuint loadTGA_glfw(const char * imagepath){
      80. // Create one OpenGL texture
      81. GLuint textureID;
      82. glGenTextures(1, &textureID);
      83. // "Bind" the newly created texture : all future texture functions will modify this texture
      84. glBindTexture(GL_TEXTURE_2D, textureID);
      85. // Read the file, call glTexImage2D with the right parameters
      86. glfwLoadTexture2D(imagepath, 0);
      87. // Nice trilinear filtering.
      88. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
      89. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
      90. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
      91. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
      92. glGenerateMipmap(GL_TEXTURE_2D);
      93. // Return the ID of the texture we just created
      94. return textureID;
      95. }
      96. };
      97. #pragma region vertexData
      98. const GLfloat Render::g_vertex_buffer_data[12] =
      99. {
      100. -100.0f, 100.0f, 0.0f,
      101. -100.0f, -100.0f, 0.0f,
      102. 100.0f, 100.0f, 0.0f,
      103. 100.0f, -100.0f, 0.0f
      104. };
      105. const GLfloat Render::g_uv_buffer_data[8] =
      106. {
      107. 0.0f, 1.0f,
      108. 0.0f, 0.0f,
      109. 1.0f, 1.0f,
      110. 1.0f, 0.0f
      111. };
      112. #pragma endregion
      113. const long Render::m_Width = 1024;
      114. const long Render::m_Height = 768;
      115. int main()
      116. {
      117. current = new Render();
      118. current->Init();
      119. while (current->Run() != 0)
      120. {
      121. }
      122. current->Destruct();
      123. return 0;
      124. }
      125. Render::Render()
      126. {
      127. m_state = State::RUNNING;
      128. printf("Render Object created! \n");
      129. }
      130. void Render::Init()
      131. {
      132. if(!glfwInit())
      133. {
      134. fprintf(stderr, "Failed to initialize GLFW \n");
      135. }
      136. glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
      137. glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
      138. glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
      139. glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
      140. if(!glfwOpenWindow(m_Width, m_Height, 0, 0,0,0,32, 0, GLFW_WINDOW))
      141. {
      142. fprintf(stderr, "Open window failed :( \n");
      143. glfwTerminate();
      144. }
      145. glewExperimental = true;
      146. if(glewInit() != GLEW_OK)
      147. {
      148. fprintf(stderr, "Failed to initialize GLEW! :( ");
      149. }
      150. glfwSetWindowTitle("Render");
      151. glfwEnable(GLFW_STICKY_KEYS);
      152. glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
      153. glEnable(GL_DEPTH_TEST);
      154. glDepthFunc(GL_LESS);
      155. glGenVertexArrays(1, &m_VertexArrayID);
      156. glBindVertexArray(m_VertexArrayID);
      157. m_ProgramID = LoadShaders("Transform.vertexshader","Texture.fragmentshader");
      158. m_MatrixID = glGetUniformLocation(m_ProgramID, "MVP");
      159. m_Texture = loadTGA_glfw("textu.tga");
      160. m_TextureID = glGetUniformLocation(m_ProgramID, "myTextureSampler");
      161. glGenBuffers(1, &m_VertexBuffer);
      162. glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
      163. glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
      164. glGenBuffers(1, &m_UVBuffer);
      165. glBindBuffer(GL_ARRAY_BUFFER, m_UVBuffer);
      166. glBufferData(GL_ARRAY_BUFFER, sizeof(g_uv_buffer_data), g_uv_buffer_data, GL_STATIC_DRAW);
      167. m_Model = glm::mat4(1.0f);
      168. glfwSetKeyCallback(INPUTCALLBACK);
      169. m_Projection = glm::ortho(float(-(m_Width/2)),float( m_Width/2), float(-(m_Height/2)), float(m_Height/2), 1.0f, 100.0f);
      170. m_View = glm::lookAt(
      171. glm::vec3(0,0,3),
      172. glm::vec3(0,0,0),
      173. glm::vec3(0,1,0)
      174. );
      175. printf("INIT SUCCEEDED!");
      176. }
      177. void Render::Destruct()
      178. {
      179. glDeleteBuffers(1, &m_VertexBuffer);
      180. glDeleteBuffers(1, &m_UVBuffer);
      181. glDeleteProgram(m_ProgramID);
      182. glDeleteTextures(1, &m_TextureID);
      183. glDeleteVertexArrays(1, &m_VertexArrayID);
      184. }
      185. void Render::RenderNow()
      186. {
      187. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      188. glUseProgram(m_ProgramID);
      189. glUniformMatrix4fv(m_MatrixID, 1, GL_FALSE, &m_MVP[0][0]);
      190. glActiveTexture(GL_TEXTURE0);
      191. glBindTexture(GL_TEXTURE_2D, m_Texture);
      192. glUniform1i(m_TextureID, 0);
      193. glEnableVertexAttribArray(0);
      194. glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
      195. glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,(void*)0);
      196. glEnableVertexAttribArray(1);
      197. glBindBuffer(GL_ARRAY_BUFFER, m_UVBuffer);
      198. glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,0,(void*)0);
      199. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
      200. glDisableVertexAttribArray(0);
      201. glDisableVertexAttribArray(1);
      202. glfwSwapBuffers();
      203. }
      204. void Render::Update(float deltaTime)
      205. {
      206. m_Model[3].x += (m_speed.x * deltaTime);
      207. m_Model[3].y += (m_speed.y * deltaTime);
      208. m_MVP = m_Projection * m_View * m_Model;
      209. }
      210. Render::~Render()
      211. {
      212. }
      213. void Render::INPUTCALLBACK(int Key, int Action)
      214. {
      215. if(Key == GLFW_KEY_DOWN && Action == GLFW_PRESS)
      216. current->m_speed.y += -0.2;
      217. if(Key == GLFW_KEY_UP && Action == GLFW_PRESS)
      218. current->m_speed.y += 0.2;
      219. if(Key == GLFW_KEY_RIGHT && Action == GLFW_PRESS)
      220. current->m_speed.x += 0.2;
      221. if(Key == GLFW_KEY_LEFT && Action == GLFW_PRESS)
      222. current->m_speed.x += -0.2;
      223. }
      224. int Render::Run()
      225. {
      226. float lastTime = glfwGetTime();
      227. float deltaTime;
      228. while(m_state != State::QUIT)
      229. {
      230. deltaTime = glfwGetTime() - lastTime;
      231. lastTime = glfwGetTime();
      232. Update(deltaTime);
      233. RenderNow();
      234. }
      235. return 0;
      236. }
      Display All


      VertexShader:

      Source Code

      1. #version 330 core
      2. // Input vertex data, different for all executions of this shader.
      3. layout(location = 0) in vec3 vertexPosition_modelspace;
      4. layout(location = 1) in vec2 vertexUV;
      5. // Output data ; will be interpolated for each fragment.
      6. out vec2 UV;
      7. // Values that stay constant for the whole mesh.
      8. uniform mat4 MVP;
      9. void main(){
      10. // Output position of the vertex, in clip space : MVP * position
      11. gl_Position = MVP * vec4(vertexPosition_modelspace,1);
      12. // UV of the vertex. No special space for this one.
      13. UV = vertexUV;
      14. }
      Display All


      Fragment Shader:

      Source Code

      1. #version 330 core
      2. // Interpolated values from the vertex shaders
      3. in vec2 UV;
      4. // Ouput data
      5. out vec3 color;
      6. // Values that stay constant for the whole mesh.
      7. uniform sampler2D myTextureSampler;
      8. void main(){
      9. // Output color = color of the texture at the specified UV
      10. color = texture2D( myTextureSampler, UV ).rgb;
      11. }
      Display All
      Files
      • Main.cpp

        (7.04 kB, downloaded 1,323 times, last: )

      The post was edited 1 time, last by olivermd ().

    • C Source Code

      1. // Include standard headers
      2. #include <stdio.h>
      3. #include <stdlib.h>
      4. // Include GLEW
      5. #include <GL/glew.h>
      6. // Include GLFW
      7. #include <GL/glfw.h>
      8. // Include GLM
      9. #include <glm/glm.hpp>
      10. #include <glm/gtc/matrix_transform.hpp>
      11. using namespace glm;
      12. #include <common/shader.hpp>
      13. #include <common/texture.hpp>
      14. void GLFWCALL DownCallBack(int Key, int action);
      15. int main( void )
      16. {
      17. // Initialise GLFW
      18. if( !glfwInit() )
      19. {
      20. fprintf( stderr, "Failed to initialize GLFW\n" );
      21. return -1;
      22. }
      23. glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
      24. glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
      25. glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
      26. glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
      27. // Open a window and create its OpenGL context
      28. if( !glfwOpenWindow( 1024, 768, 0,0,0,0, 32,0, GLFW_WINDOW ) )
      29. {
      30. fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
      31. glfwTerminate();
      32. return -1;
      33. }
      34. // Initialize GLEW
      35. glewExperimental = true; // Needed for core profile
      36. if (glewInit() != GLEW_OK) {
      37. fprintf(stderr, "Failed to initialize GLEW\n");
      38. return -1;
      39. }
      40. glfwSetWindowTitle( "Tutorial 05" );
      41. // Ensure we can capture the escape key being pressed below
      42. glfwEnable( GLFW_STICKY_KEYS );
      43. // Dark blue background
      44. glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
      45. // Enable depth test
      46. glEnable(GL_DEPTH_TEST);
      47. // Accept fragment if it closer to the camera than the former one
      48. glDepthFunc(GL_LESS);
      49. GLuint VertexArrayID;
      50. glGenVertexArrays(1, &VertexArrayID);
      51. glBindVertexArray(VertexArrayID);
      52. // Create and compile our GLSL program from the shaders
      53. GLuint programID = LoadShaders( "TransformVertexShader.vertexshader", "TextureFragmentShader.fragmentshader" );
      54. // Get a handle for our "MVP" uniform
      55. GLuint MatrixID = glGetUniformLocation(programID, "MVP");
      56. // Load the texture using any two methods
      57. //GLuint Texture = loadBMP_custom("uvtemplate.bmp");
      58. GLuint Texture = loadTGA_glfw("textu.tga");
      59. // Get a handle for our "myTextureSampler" uniform
      60. GLuint TextureID = glGetUniformLocation(programID, "myTextureSampler");
      61. // Our vertices. Tree consecutive floats give a 3D vertex; Three consecutive vertices give a triangle.
      62. // A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices
      63. static const GLfloat g_vertex_buffer_data[] = {
      64. -100.0f, 100.0f, 0.0f,
      65. -100.0f, -100.0f, 0.0f,
      66. 100.0f, 100.0f, 0.0f,
      67. 100.0f, -100.0f, 0.0f
      68. };
      69. // Two UV coordinatesfor each vertex. They were created withe Blender.
      70. static const GLfloat g_uv_buffer_data[] = {
      71. 0.0f, 1.0f,
      72. 0.0f, 0.0f,
      73. 1.0f, 1.0f,
      74. 1.0f, 0.0f
      75. };
      76. GLuint vertexbuffer;
      77. glGenBuffers(1, &vertexbuffer);
      78. glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
      79. glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
      80. GLuint uvbuffer;
      81. glGenBuffers(1, &uvbuffer);
      82. glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
      83. glBufferData(GL_ARRAY_BUFFER, sizeof(g_uv_buffer_data), g_uv_buffer_data, GL_STATIC_DRAW);
      84. glm::mat4 Model = glm::mat4(1.0f);
      85. glfwSetKeyCallback(DownCallBack);
      86. do{
      87. static double lastTime = glfwGetTime();
      88. double deltaTime = glfwGetTime() - lastTime;
      89. // Clear the screen
      90. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      91. // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
      92. glm::mat4 Projection = glm::ortho(-1024.0f/2.0f, 1024.0f/2.0f, -768.0f/2.0f, 768.0f/2.0f, 1.0f, 100.0f);
      93. // Camera matrix
      94. glm::mat4 View = glm::lookAt(
      95. glm::vec3(0,0,3), // Camera is at (4,3,3), in World Space
      96. glm::vec3(0,0,0), // and looks at the origin
      97. glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
      98. );
      99. // Model matrix : an identity matrix (model will be at the origin)
      100. // Our ModelViewProjection : multiplication of our 3 matrices
      101. if(glfwGetKey(GLFW_KEY_DOWN ) == GLFW_PRESS)
      102. {
      103. Model[3].x += (4 * deltaTime);
      104. //printf();
      105. }
      106. glm::mat4 MVP = Projection * View * Model; // Remember, matrix multiplication is the other way around
      107. // Use our shader
      108. glUseProgram(programID);
      109. // Send our transformation to the currently bound shader,
      110. // in the "MVP" uniform
      111. glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
      112. // Bind our texture in Texture Unit 0
      113. glActiveTexture(GL_TEXTURE0);
      114. glBindTexture(GL_TEXTURE_2D, Texture);
      115. // Set our "myTextureSampler" sampler to user Texture Unit 0
      116. glUniform1i(TextureID, 0);
      117. // 1rst attribute buffer : vertices
      118. glEnableVertexAttribArray(0);
      119. glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
      120. glVertexAttribPointer(
      121. 0, // attribute. No particular reason for 0, but must match the layout in the shader.
      122. 3, // size
      123. GL_FLOAT, // type
      124. GL_FALSE, // normalized?
      125. 0, // stride
      126. (void*)0 // array buffer offset
      127. );
      128. // 2nd attribute buffer : UVs
      129. glEnableVertexAttribArray(1);
      130. glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
      131. glVertexAttribPointer(
      132. 1, // attribute. No particular reason for 1, but must match the layout in the shader.
      133. 2, // size : U+V => 2
      134. GL_FLOAT, // type
      135. GL_FALSE, // normalized?
      136. 0, // stride
      137. (void*)0 // array buffer offset
      138. );
      139. // Draw the triangle !
      140. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); // 12*3 indices starting at 0 -> 12 triangles
      141. glDisableVertexAttribArray(0);
      142. glDisableVertexAttribArray(1);
      143. // Swap buffers
      144. glfwSwapBuffers();
      145. } // Check if the ESC key was pressed or the window was closed
      146. while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
      147. glfwGetWindowParam( GLFW_OPENED ) );
      148. // Cleanup VBO and shader
      149. glDeleteBuffers(1, &vertexbuffer);
      150. glDeleteBuffers(1, &uvbuffer);
      151. glDeleteProgram(programID);
      152. glDeleteTextures(1, &TextureID);
      153. glDeleteVertexArrays(1, &VertexArrayID);
      154. // Close OpenGL window and terminate GLFW
      155. glfwTerminate();
      156. return 0;
      157. }
      158. void GLFWCALL DownCallBack(int Key, int action)
      159. {
      160. }
      Display All


      EDIT:
      Just done some more tests and discovered that if I copy the original source to my new file the white screen still appears and if I copy the source from my new file to the original the white screen also appears. This leads me to believe that it is due to an included file since I used the new file's include files for both tests. Fixed it by changing some header files although I'm not sure why it worked.

      The post was edited 2 times, last by olivermd ().

    • I was just about to go through your code, too late I guess!
      PC - Custom Built
      CPU: 3rd Gen. Intel i7 3770 3.4Ghz
      GPU: ATI Radeon HD 7959 3GB
      RAM: 16GB

      Laptop - Alienware M17x
      CPU: 3rd Gen. Intel i7 - Ivy Bridge
      GPU: NVIDIA GeForce GTX 660M - 2GB GDDR5
      RAM: 8GB Dual Channel DDR3 @ 1600mhz