Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Suspected fragment shader problem, No color, OpenGL 4, GLFW 3, GLEW

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    6

    Suspected fragment shader problem, No color, OpenGL 4, GLFW 3, GLEW

    I am using OpenGL 4.1, GLFW 3 and GLEW. I am trying to draw a simple triangle with the color purple.

    The triangle is drawn, the color is not.

    This code is from a tutorial, I had to update the GLFW calls to reflect GLFW 3.x. As far as I can tell, the color shader appears to be correct.


    EDIT: Solved a problem where the GLFW window is unresponsive to key presses and mouse clicks. Need to include call to glfwPollEvents() within the drawing loop. Still no color.

    Code :
    #include <GL/glew.h> // include GLEW and new version of GL on Windows
    #include <GL/glfw3.h> // GLFW helper library
    #include <stdio.h>
    #include <string>
    #include <iostream>
     
    int main () {
    	GLFWwindow* window;
     
      // start GL context and O/S window using the GLFW helper library
      if(!glfwInit ()){
     
    	  std::cout << "Failed at glfwInit()!" << std::endl;
    	  glfwTerminate();
    	  return -1;
      }
     
     
      // "hint" that the version 4.2 should be run with no back-compat stuff
      int major = 4;
    	int minor = 1;
    	glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, major);
    	glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, minor);
    	glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    	glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
     
      // open a window of sixe 640x480 
      window = glfwCreateWindow(640, 480, "Window!!!", NULL, NULL);
     
      if(!window){
     
    	  std::cout << "Failed at window creation!" << std::endl;
    	  glfwTerminate();
    	  return -1;
      }
     
      glfwMakeContextCurrent(window);
     
      // start GLEW extension handler
      GLenum err = glewInit ();
     
      if(GLEW_OK != err){
     
    	  std::cout << "glewInit failed!!!" << std::endl;
    	  glfwTerminate();
    	  return -1;
      }
     
      // get version info
      const GLubyte* renderer = glGetString (GL_RENDERER); // get renderer string
      const GLubyte* version = glGetString (GL_VERSION); // version as a string
      printf("Renderer: %s\n", renderer);
      printf("OpenGL version supported %s\n", version);
     
    	// tell GL to only draw onto a pixel if the shape is closer to the viewer
      glEnable (GL_DEPTH_TEST); // enable depth-testing
      glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer"
     
      int i;
     
      //A triangle's vertices
      float points[] = {
     
    	  0.0f, 0.5f, 0.0f,
    	  0.5f, -0.5f, 0.0f,
    	  -0.5f, -0.5f, 0.0f
      };
     
      //Create and setup vertex buffer object
      unsigned int vbo = 0;
      glGenBuffers(1, &vbo);
      glBindBuffer(GL_ARRAY_BUFFER, vbo);
      glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), &points[0], GL_STATIC_DRAW);
     
      //Create and setup vertex array (or attribute?) object to store vbo point data
      unsigned int vao = 0;
      glGenVertexArrays(1, &vao);
      glBindVertexArray(vao);
      glEnableVertexAttribArray(0);
      glBindBuffer(GL_ARRAY_BUFFER, vbo);
      glVertexAttribPointer(0,3, GL_FLOAT, GL_FALSE, 0, (GLubyte*)NULL);
     
      std::string vertex_shader = 
    	  "in vec3 vp;"
    	  "void main () {"
    	  "	gl_Position = vec4(vp, 1.0);"
    	  "}";
     
      std::string fragment_shader = 
    	  "out vec4 frag_color;"
    	  "void main() {"
    	  "	frag_color = vec4 (0.5, 0.0, 0.5, 1.0f);"
    	  "}";
     
      unsigned int vs = glCreateShader(GL_VERTEX_SHADER);
      const char* str = vertex_shader.c_str();
      glShaderSource(vs, 1, &str, NULL);
      glCompileShader(vs);
      unsigned int fs = glCreateShader (GL_FRAGMENT_SHADER);
      const char* strb = fragment_shader.c_str();
      glShaderSource (fs, 1, &strb, NULL);
      glCompileShader(fs);
     
      unsigned int shader_program = glCreateProgram();
      glAttachShader(shader_program, fs);
      glAttachShader(shader_program, vs);
      glLinkProgram(shader_program);
     
      bool running = true;
      while (running){
     
    	  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	  glUseProgram(shader_program);
    	  glBindVertexArray(vao);
    	  glDrawArrays(GL_TRIANGLES, 0, 3);
    	  glfwSwapBuffers(window);
     
    	  running = !glfwGetKey(window, GLFW_KEY_ESCAPE) && !glfwWindowShouldClose(window);
    	  glfwPollEvents(); //Needs to be called or else window is completely unresponsive to move, resize, close, key strokes, etc
     
      }
     
      // close GL context and any other GLFW resources
      glfwTerminate();
      return 0;
    }
    Last edited by WaywardSquanderer; 07-21-2013 at 07:43 PM.

  2. #2
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    6
    Forgot to include version number and layout statement in shader files/strings! So does this mean the tutorial is wrong or do some shaders compile fine with

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •