Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8181419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:30:51+00:00 2026-06-07T00:30:51+00:00

Problem: Been struggling to get my code to load external shaders and it is

  • 0

Problem:

Been struggling to get my code to load external shaders and it is not working. No matter how i rewrite the code and try again i get the same error every time.

The shaders compile but will not link, is there anything that i am doing wrong in this function? The error catching blocks work great btw.

Fragment shader(s) failed to link,  vertex shader(s) failed to link.
ERROR: error(#280) Not all shaders have valid object code.
ERROR: error(#280) Not all shaders have valid object code.

I’m using freeglut, x64 windows 7 and mingw (and I am not using an IDE)

Usage:

In my mainloop I have GLuint programId = loadShader("vertex.shader", "frag.shader");
then I enable it with glUseProgram();

GLuint loadShader(const char * vertex_file_path, const char * fragment_file_path) {

    GLuint vertexShaderId = glCreateShader(GL_VERTEX_SHADER);
    GLuint fragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER);
    GLuint programId = glCreateProgram();

    GLint status = GL_FALSE;
    int infoLogLength;
    std::string line = "";

    //read in and compile
    std::string vertexShaderCode = "";
    std::ifstream vifs(vertex_file_path);
    if(!vifs.is_open()) {

        while(getline(vifs, line))
            vertexShaderCode += line +"\n";
        vifs.close();
    }

    const char * vertexStream = vertexShaderCode.c_str();

    std::string fragmentShaderCode = "";
    std::ifstream fifs(fragment_file_path);
    if(!fifs.is_open()) {

        while(getline(fifs, line))
            fragmentShaderCode += line +"\n";
        fifs.close();
    }

    const GLchar * fragmentStream = fragmentShaderCode.c_str();

    glShaderSource(vertexShaderId, 1, &vertexStream, NULL);
    glCompileShader(vertexShaderId);   

    glShaderSource(fragmentShaderId, 1, &fragmentStream, NULL);
    glCompileShader(fragmentShaderId);    

    glGetShaderiv(vertexShaderId, GL_COMPILE_STATUS, &status);
    glGetShaderiv(vertexShaderId, GL_INFO_LOG_LENGTH, &infoLogLength);
    std::vector<char> vertexShaderErrorMessage(infoLogLength);
    glGetShaderInfoLog(vertexShaderId, infoLogLength, NULL, &vertexShaderErrorMessage[0]);
    fprintf(stdout, "%s\n", &vertexShaderErrorMessage[0]);

    glGetShaderiv(fragmentShaderId, GL_COMPILE_STATUS, &status);
    glGetShaderiv(fragmentShaderId, GL_INFO_LOG_LENGTH, &infoLogLength);
    std::vector<char> fragmentShaderErrorMessage(infoLogLength);
    glGetShaderInfoLog(fragmentShaderId, infoLogLength, NULL, &fragmentShaderErrorMessage[0]);
    fprintf(stdout, "%s\n", &fragmentShaderErrorMessage[0]);

    glAttachShader(programId, vertexShaderId);
    glAttachShader(programId, fragmentShaderId);
    glLinkProgram(programId);
    glUseProgram(programId);

    glGetProgramiv(programId, GL_LINK_STATUS, &status);
    glGetProgramiv(programId, GL_INFO_LOG_LENGTH, &infoLogLength);
    std::vector<char> programErrorMessage(std::max(infoLogLength, int(1)) );
    glGetProgramInfoLog(programId, infoLogLength, NULL, &programErrorMessage[0]);
    fprintf(stdout, "%s\n", &programErrorMessage[0]);

    glDeleteShader(vertexShaderId);
    glDeleteShader(fragmentShaderId);

    return programId;
    }

[vertex.shader]

#version 400

layout(location=0) in vec4 in_Position;
layout(location=1) in vec4 in_Color;
out vec4 ex_Color;

void main(void) {
    gl_Position = in_Position;
    ex_Color = in_Color;
}

frag.shader

#version 400

in vec4 ex_Color;
out vec4 out_Color;

void main(void) {
   out_Color = ex_Color;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:30:52+00:00Added an answer on June 7, 2026 at 12:30 am

    You only read the files if they failed to open. Right now you’re passing empty strings to glShaderSource.

    Change the is_open test (better yet, check good() instead of is_open()), and things will probably work.

    Change

    std::ifstream vifs(vertex_file_path);
    if(!vifs.is_open()) { // <<=== BACKWARDS!
    
        while (getline(vifs, line))
            vertexShaderCode += line + "\n";
        vifs.close();
    }
    

    to

    {
        std::ifstream vifs(vertex_file_path);
        while(getline(vifs, line))
            vertexShaderCode += line +"\n";
    }
    

    Since you’re trying to read the whole file, look at some better ways in this question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been struggling to find a solution to this problem. In my code,
I have been struggling for days to get a working upload onto the admin
I've been struggling a long time with this problem and I can't get it
This is a problem I've been struggling to solve for a while. I need
Been struggling with this simple selector problem a couple of hours now and must
I've been struggling with this problem.. There is a wrapper div and it contains
I've been struggling a bit with this tiny problem - and I'm quite sure
I've been struggling with Zend_Navigation all weekend, and now I have another problem, which
This problem has been bugging me for a long time. For example, the code
I've been struggling with trying to get my commandButtons to perform an action (yet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.