Hello there
I’m learning opengl 4 (4.5) and still a newbie, im trying to run this code however it doesn’t seems to be working correctly, here’s a code that i’ve written :
#include <iostream>
#include<glad/glad.h>
#include<GLFW/glfw3.h>
#include <stdlib.h>
#include <stdio.h>
#include<vector>
using namespace std;
void error_callback(int error, const char* description){
.....
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
.....
}
GLuint loadAndCompileShader(GLenum shaderType,const GLchar *sourceCode[]) {
// Create the shader
GLuint shader = glCreateShader(shaderType);
cout<<" Shader Creation : "<<shader<<endl;
if ( shader ) {
cout<<" Successfully created shader!\n";
// Pass the shader source code
glShaderSource(shader, 1, sourceCode, NULL);
// Compile the shader source code
glCompileShader(shader);
// Check the status of compilation
GLint compiled = 0;
glGetShaderiv(shader,GL_COMPILE_STATUS,&compiled);
cout<<" Compile Status : "<<compiled<<endl;
if (!compiled) {
// Get the info log for compilation failure
GLint infoLen = 0;
cout<<"failed to compile \n";
glGetShaderiv(shader,GL_INFO_LOG_LENGTH, &infoLen);
cout<<" Length of error message : "<<infoLen<<"\n";
if (infoLen) {
vector< char > buf(infoLen);
glGetShaderInfoLog(shader, infoLen, NULL, &buf[0]);
for(int i=0; i<buf.size(); i++)
cout<<buf[i];
cout<<endl;
glDeleteShader(shader);
shader = 0;
}
}
}
return shader;
}
int main(void)
{
ios_base::sync_with_stdio(0);
if(glfwInit())
cout<<"glfw success init\n";
else cout<<"error in glfw init\n";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwSetErrorCallback(error_callback);
GLFWwindow* window = glfwCreateWindow(640, 480, "My Title", NULL, NULL);
if (!window)
cout<<"Window (OPENGL Context) creation failed"<<endl;
else cout<<"Window (OPENGL Context) creation success"<<endl;
glfwMakeContextCurrent(window);
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
if(gladLoadGL())
cout<<"gladloadgl true\n";
else cout<<"gladloadgl false\n";
glfwSetKeyCallback(window, key_callback);
//opengl context established -------------------------------------
const GLubyte *renderer = glGetString( GL_RENDERER );
const GLubyte *vendor = glGetString( GL_VENDOR );
const GLubyte *version = glGetString( GL_VERSION );
const GLubyte *glslVersion = glGetString( GL_SHADING_LANGUAGE_VERSION );
GLint major, minor;
glGetIntegerv(GL_MAJOR_VERSION, &major);
glGetIntegerv(GL_MINOR_VERSION, &minor);
printf("GL Vendor : %s\n", vendor);
printf("GL Renderer : %s\n", renderer);
printf("GL Version (string) : %s\n", version);
printf("GL Version (integer) : %d.%d\n", major, minor);
printf("GLSL Version : %s\n", glslVersion);
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
double time = glfwGetTime();
cout<<"time = "<<time<<endl;
glfwSwapInterval(0);
//shader code
const char* vertexShader[] = {
"#version 450 core \n",
"layout(location=0) in vec2 pos; \n",
"void main(void){ \n",
"gl_Position = vec4(pos, 0.0, 1.0); } \n"
};
const char* fragmentShader[] = {
"#version 450 core \n",
"out vec4 color; \n",
"void main(void){ \n",
"color = vec4(0.0,0.0,0.0,1.0); } \n"
};
//Vertex Shader compilation
GLuint vertex_shader = loadAndCompileShader(GL_VERTEX_SHADER, vertexShader);
if(!vertex_shader){
cout<<" Vertex Shader Failed \n";
return -1;
}else cout<< " Vertex Shader Compiled \n";
//Fragment Shader compilation
GLuint fragment_shader = loadAndCompileShader(GL_FRAGMENT_SHADER, fragmentShader);
if(!fragment_shader){
cout<<" Fragment Shader Failed \n";
return -1;
} else cout<<" Fragment Shader Compiled \n";
GLuint program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
GLint isLinked = 0;
glGetProgramiv(program, GL_LINK_STATUS, &isLinked);
if (isLinked == GL_FALSE)
{
GLint maxLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength);
// The maxLength includes the NULL character
vector<GLchar> infoLog(maxLength);
glGetProgramInfoLog(program, maxLength, &maxLength, &infoLog[0]);
cout<<"\n linking failed reason : ";
for(int i=0; i<infoLog.size();i++)
cout<<infoLog[i];
// The program is useless now. So delete it.
glDeleteProgram(program);
// Provide the infolog in whatever manner you deem best.
// Exit with failure.
return -1;
}
glDetachShader(program, vertex_shader);
glDetachShader(program, fragment_shader);
float vertices[] = {
+0.0f, +1.0f,
-1.0f, -1.0f,
+1.0f, -1.0f,
};
//load vertices into GPU vertex buffer
GLuint vbo;
glCreateBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, 6*sizeof(float) , &vertices[0], GL_STATIC_DRAW);
//specify layout
GLuint vao;
glCreateVertexArrays(1, &vao);
glBindVertexArray(vao);
glVertexAttribPointer(0,2, GL_FLOAT, GL_FALSE,0, (void*)0);
glEnableVertexAttribArray(0);
glUseProgram(program);
while (!glfwWindowShouldClose(window)){
time = glfwGetTime();
float _ratio;
int width, height;
glfwGetFramebufferSize(window, &width, &height);
_ratio = width / (float) height;
glViewport(0, 0, width, height);
glUseProgram(program);
const float colors[] = {1.0f, 1.0f, 1.0f, 1.0f};
glClearBufferfv(GL_COLOR, 0, colors);
glDrawArrays(GL_TRIANGLES, 0, 3); //send vertices into opengl pipeline
glfwPollEvents();
glfwSwapBuffers(window);
cout<<(glfwGetTime() - time)<<"\n";
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
return 0;
}
as you can see, I’ve a function loadAndCompileShader() which does the work of compiling shader, however when i run this program, it fails to compile Vertex Shader as well as Fragment Shader, I’ve Intel HD 620 and Nvidia 940mx and tried on both, however though shaders seems to be getting compiled on 940MX, but no triangle is rendered in that case, only background color is visible.
also, when running on intel 620, when i remove #version 450 core statement from shaders, they seem to be getting compiled successfully but then linking fails and it shows following error in log
“defination of void main() not found”
I’ve searched it up on web and I’ve also tried all solutions, none worked for me.