so I'm basically a beginner in OpenGL and I was trying to create my own shaders to form a simple triangle. In doing that I used SDL to create the window (as you can see in the screenshot attached that its being successfully loaded) even though the tutorial made use of glfw3 and glad (I had to resort to SDL because I was unable to effectively include the latter libraries). It keeps giving me the following error
"GLSL is not supported. Supported version are: 1.10, 1.20, 1.30, ... 3.20 ES"
Is the SDL the cause of the problem?
Operating system: Ubuntu 18.04; IDE: CodeBlocks;
main.cpp
#include <iostream>
#include <SDL2/SDL.h>
#include <GL/glew.h>
#include "display.h"
using namespace std;
const char *vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
"gl_Position = vec4(aPos.x,aPos.y,aPos.z,1.0f)\n"
"}\0";
const char *fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
"FragColor = vec4(1.0f,0.5f,0.2f,1.0f);\n"
"}\0";
int main()
{
Display display(800,600,"Moiz");
while(!display.isClosed())
{
glClearColor(0.0f,0.15f,0.3f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);
display.Update();
int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1 , &vertexShaderSource, NULL);
glCompileShader(vertexShader);
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource,NULL);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
}
return 0;
}
Display.cpp
#include <iostream>
#include <string>
#include <SDL2/SDL.h>
#include <GL/glew.h>
#include "display.h"
using namespace std;
Display::Display(int width, int height,const string &title)
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
m_window = SDL_CreateWindow(title.c_str(),SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height,SDL_WINDOW_OPENGL);
m_glContext = SDL_GL_CreateContext(m_window);
GLenum status = glewInit();
if(status != GLEW_OK)
{
std::cerr << "GLEW FAILED TO INITIALIZE" << endl;
}
}
bool Display::isClosed(){
return m_isClosed;
}
void Display::Update()
{
SDL_GL_SwapWindow(m_window);
SDL_Event e;
while(SDL_PollEvent(&e))
{
if(e.type == SDL_QUIT)
{
m_isClosed = true;
}
}
}
Display::~Display()
{
SDL_GL_DeleteContext(m_glContext);
SDL_DestroyWindow(m_window);
SDL_Quit();
}
Display::Display(const Display& other)
{
//copy ctor
}
Display& Display::operator=(const Display& rhs)
{
if (this == &rhs) return *this; // handle self assignment
//assignment operator
return *this;
}
:Error Image
vec4
in your vertex shaderIf you're using Mesa be aware (historically; this has been changing for some drivers in the past few weeks/months) it only hands out GL contexts >3.0 if you request a Core context. You can use SDL_GL_SetAttribute()
to make SDL_CreateWindow()
request one:
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 3 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE );
All together (this is what I meant by MCVE: a single file, depending only on well-known libraries like SDL/GLEW/GLM/GLFW):
#include <iostream>
#include <SDL2/SDL.h>
#include <GL/glew.h>
#include <iostream>
#include <string>
class Display
{
public:
Display( int width, int height, const std::string &title )
: m_isClosed( false )
{
SDL_Init( SDL_INIT_EVERYTHING );
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_BUFFER_SIZE, 32 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 3 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE );
m_window = SDL_CreateWindow
(
title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
width, height,
SDL_WINDOW_OPENGL
);
m_glContext = SDL_GL_CreateContext( m_window );
GLenum status = glewInit();
if( status != GLEW_OK )
{
std::cerr << "GLEW FAILED TO INITIALIZE" << std::endl;
}
}
bool isClosed()
{
return m_isClosed;
}
void Update()
{
SDL_GL_SwapWindow( m_window );
SDL_Event e;
while( SDL_PollEvent( &e ) )
{
if( e.type == SDL_QUIT )
{
m_isClosed = true;
}
}
}
~Display()
{
SDL_GL_DeleteContext( m_glContext );
SDL_DestroyWindow( m_window );
SDL_Quit();
}
Display( const Display& other )
{
//copy ctor
}
Display& operator=( const Display& rhs )
{
if( this == &rhs ) return *this; // handle self assignment
//assignment operator
return *this;
}
private:
SDL_Window* m_window;
SDL_GLContext m_glContext;
bool m_isClosed;
};
const char *vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
"gl_Position = vec4(aPos.x,aPos.y,aPos.z,1.0f);\n"
"}\0";
const char *fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
"FragColor = vec4(1.0f,0.5f,0.2f,1.0f);\n"
"}\0";
int main( int argc, char** argv )
{
Display display( 800, 600, "Moiz" );
while( !display.isClosed() )
{
glClearColor( 0.0f, 0.15f, 0.3f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT );
display.Update();
int vertexShader = glCreateShader( GL_VERTEX_SHADER );
glShaderSource( vertexShader, 1, &vertexShaderSource, NULL );
glCompileShader( vertexShader );
int success;
char infoLog[ 512 ];
glGetShaderiv( vertexShader, GL_COMPILE_STATUS, &success );
if( !success )
{
glGetShaderInfoLog( vertexShader, 512, NULL, infoLog );
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
int fragmentShader = glCreateShader( GL_FRAGMENT_SHADER );
glShaderSource( fragmentShader, 1, &fragmentShaderSource, NULL );
glCompileShader( fragmentShader );
glGetShaderiv( fragmentShader, GL_COMPILE_STATUS, &success );
if( !success )
{
glGetShaderInfoLog( fragmentShader, 512, NULL, infoLog );
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
}
return 0;
}
Also, make sure to check that SDL_CreateWindow()
and SDL_GL_CreateContext()
return valid values.