Hello,
Thank you for your replies, here is the code I have in main, NOTE: it does not matter what I do to the glsl files it just says “Linker failure: Error:…” maybe its what is in my code that is the problem, and I’m only a beginner at programming and OpenGL, I am not sure what part of the code should be set to null although I do see that there are pointers that have been set to null in the “Gluint CreateProgram” Function as well as the “GLuint CreateShader” function.
I apologise if I am not very specific as I mentioned I am still learning.
#include "GLEW/GL/glew.h"
#include "GLEW/GL/glxew.h"
#include "glfw3.h"
#include <vector>
#include <string>
#include <fstream>
GLuint CreateShader(GLenum a_eShaderType, const char *a_strShaderFile)
{
std::string strShaderCode;
//open shader file
std::ifstream shaderStream(a_strShaderFile);
//if that worked ok, load file line by line
if(shaderStream.is_open())
{
std::string Line = "";
while(std::getline(shaderStream, Line))
{
strShaderCode += "
" + Line;
}
shaderStream.close();
}
//convert to cstring
char const *szShaderSourcePointer = strShaderCode.c_str();
//create shader ID
GLuint uiShader = glCreateShader(a_eShaderType);
//load source code
glShaderSource(uiShader, 1, &szShaderSourcePointer, NULL);
//compile shader
glCompileShader(uiShader);
//check for compilation errors and output them
GLint iStatus;
glGetShaderiv(uiShader, GL_COMPILE_STATUS, &iStatus);
if (iStatus == GL_FALSE)
{
GLint infoLogLength;
glGetShaderiv(uiShader, GL_INFO_LOG_LENGTH, &infoLogLength);
GLchar *strInfoLog = new GLchar[infoLogLength + 1];
glGetShaderInfoLog(uiShader, infoLogLength, NULL, strInfoLog);
const char *strShaderType = NULL;
switch(a_eShaderType)
{
case GL_VERTEX_SHADER: strShaderType = "vertex"; break;
case GL_FRAGMENT_SHADER: strShaderType = "fragment"; break;
}
fprintf(stderr, "Compile failure in %s shader:
%s
", strShaderType, strInfoLog);
delete[] strInfoLog;
}
return uiShader;
}
GLuint CreateProgram(const char *a_vertex, const char *a_frag)
{
std::vector<GLuint> shaderList;
shaderList.push_back(CreateShader(GL_VERTEX_SHADER, a_vertex));
shaderList.push_back(CreateShader(GL_FRAGMENT_SHADER, a_frag));
//create shader program ID
GLuint uiProgram = glCreateProgram();
//attach shaders
for(auto shader = shaderList.begin(); shader != shaderList.end(); shader++)
glAttachShader(uiProgram, *shader);
//link program
glLinkProgram(uiProgram);
//check for link errors and output them
GLint status;
glGetProgramiv (uiProgram, GL_LINK_STATUS, &status);
if (status == GL_FALSE)
{
GLint infoLogLength;
glGetProgramiv(uiProgram, GL_INFO_LOG_LENGTH, &infoLogLength);
GLchar *strInfoLog = new GLchar[infoLogLength + 1];
glGetProgramInfoLog(uiProgram, infoLogLength, NULL, strInfoLog);
fprintf(stderr, "Linker failure: %s
", strInfoLog);
delete[] strInfoLog;
}
for(auto shader = shaderList.begin(); shader != shaderList.end(); shader++)
{
glDetachShader(uiProgram, *shader);
glDeleteShader(*shader);
}
return uiProgram;
}
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(800, 600, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK)
{
// OpenGL didn't start-up! shutdown GLFW and return an error code
glfwTerminate();
return -1;
}
const float vertexPositions[] =
{
-0.3f, 0.10f, 0.0f, 1.0f, //height and left of right position
-0.50f, -0.05f, 0.0f, 1.0f,// right point position of triangle
-0.10f, -0.05f, 0.0f, 1.0f, // left point position of triangle
0.0f, 0.03f, 0.0f, 1.0f,
-0.025f, -0.05f, 0.0f, 1.0f,
0.025f, -0.05f, 0.0f, 1.0f,
};
const float vertexColours[] =
{
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
};
//create shader program
GLuint uiProgramFlat = CreateProgram("VertexShader.glsl", "FlatFragmentShader.glsl");
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
glClearColor(0.0f,0.0f,0.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT);
/* Render here */
//enable shaders
glUseProgram(uiProgramFlat);
//enable the vertex array state, since we're sending in an array of vertices
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
//specify where our vertex array is, how many components each vertex has,
//the data type of each component and whether the data is normalised or not
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, vertexPositions);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, vertexColours);
//draw to the screen
glDrawArrays(GL_TRIANGLES, 0, 3);
glDrawArrays(GL_TRIANGLES, 0, 6);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
Regards
Theodore.