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 3 of 3

Thread: Shaders are not working

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2015
    Posts
    27

    Shaders are not working

    Hi everyone,
    I am struggling with shaders right now. Below are my shaders.
    #version 440 core
    void main(void)
    {
    const vec4 vertices[3]=vec4[3](vec4(10,-10,15,1.0),vec4(-10,-10,10,1.0),vec4(10,10,10,1.0));
    gl_Position=vertices[gl_VertexID];
    }

    #version 440 core
    out vec4 color;
    void main(void)
    {
    color=vec4(1.0f,0.0f,0.0f,1.0f);
    }

    In render function, I used glDrawArrays(GL_TRIANGLES,0,3). They draw nothing. The problem is I dont know even where to start troubleshooting. They are compiled, linked and even validated successfully. All I get is a blank scene. Several issues, I believe, are also related to how I get the text into the shader source in the first place. So the code below is how load it.

    ifstream input(****.txt);
    string line, content;
    while(!input.eof())
    {
    getline(input,line);
    content. append(line+"\n");
    }
    const GLchar *shader_source=content.c_str();
    glShaderSource(vertex_shader,1,&shader_source,NULL );

    Fragment shader is similar to the above one. If I print shader_source, I get whatever is in my text. So no problem there. If everything is fine here, then I need to examine the code elsewhere.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,480
    Quote Originally Posted by ajith.mk View Post
    Hi everyone,
    I am struggling with shaders right now. Below are my shaders.
    Code :
    const vec4 vertices[3]=vec4[3](vec4(10,-10,15,1.0),vec4(-10,-10,10,1.0),vec4(10,10,10,1.0));
    gl_Position=vertices[gl_VertexID];
    All of those vertices are beyond the far plane, so the triangle will be discarded. Bear in mind that gl_Position is in clip space.

    Also, please use [code] tags around code.

    Quote Originally Posted by ajith.mk View Post
    Hi everyone,
    Several issues, I believe, are also related to how I get the text into the shader source in the first place.
    For simple test cases, using a string literal is an option. The main thing is to remember the "\n" at the end of any preprocessor directive (it doesn't matter if the rest of the code is all on one line).

    Otherwise, the simplest way to read a file into a string in C++ is probably
    Code :
    std::string readfile(std::istream& s)
    {
        std::ostringstream ss;
        ss << s.rdbuf();
        return ss.str();
    }

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2015
    Posts
    27
    Got it. I concentrated too much on syntax errors, string loading etc.

Posting Permissions

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