所以我基本上是OpenGL的初学者,我试图创建自己的着色器以形成一个简单的三角形。在此过程中,尽管本教程使用glfw3并感到高兴(我不得不求助于SDL,因为我无法有效地包含该窗口),但我还是使用SDL创建了一个窗口(如附件屏幕截图中所示,窗口已成功加载)。后一个库)。它不断给我以下错误
“不支持GLSL。受支持的版本为:1.10、1.20、1.30,... 3.20 ES”
SDL是问题的原因吗?
操作系统: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;
}
:错误图片
答案 0 :(得分:0)
Content-Length
之后缺少分号如果您使用的是Mesa,请注意(从历史上看;在过去的几周/几个月中,这种变化已在某些驱动程序中有所改变),如果您请求Core上下文,则只会分发GL上下文> 3.0。您可以使用vec4
发出SDL_GL_SetAttribute()
一个请求:
SDL_CreateWindow()
一起(这就是MCVE的意思:一个文件,仅取决于SDL / GLEW / GLM / GLFW等知名库):
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 );
此外,请确保检查#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;
}
和SDL_CreateWindow()
返回有效值。