I attach a vertex and fragment shader to a program object, then attempt to link the said program. GL_LINK_STATUS returns false. I check the info log, it's a bunch of gibberish characters. I check GL_INFO_LOG_LENGTH, it's 0. How do I debug this situation?
//this is the end of my LoadBasicShaders function
glAttachShader(program, vertShader);
glAttachShader(program, fragShader);
glLinkProgram(program);
GLint status;
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (status == GL_FALSE)
{
GLint logLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength);
std::vector<GLchar> log(logLength + 1);
glGetProgramInfoLog(program, logLength, &logLength, &log[0]);
fprintf(stderr, "%s\n\n", log);
//logLength returns 0, log returns seemingly random chars
return -3;//just my error code
}
My shaders are the simplest ones possible, since I'm just starting out.
Here's the vertex shader:
#version 330
layout(location = 0) in vec4 position;
void main()
{
gl_Position = position;
}
And here's the fragment shader:
#version 330
out vec4 outputColor;
void main()
{
outputColor = vec4(1.0, 1.0, 1.0, 1.0);
}
I use GLFW to create the OpenGL window, and GLEW to load the functions:
if (!glfwInit())
{/*error checking*/}
window = glfwCreateWindow(800, 600, "Swash", NULL, NULL);
if (!window)
{/*more error checking*/}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwSetKeyCallback(window, key_callback);
if (glewInit() != GLEW_OK)
{/*you guessed it*/}
//don't call anything that involves nonstandard OpenGL functions before this point
GLuint shaderProgram;
int shaderLoadResult = LoadBasicShaders(shaderProgram, "../res/vert.shader", "../res/frag.shader");
Use the GLFW error callback
Bullet-proof your shader loader
Use ARB_debug_output or KHR_debug
Request an explicit GL version and profile (Core/Compatibility) via glfwWindowHint()
All together (this is what I meant by posting a MCVE):
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <cstdlib>
struct GlDebugOutput
{
static void Install()
{
if( !glewIsSupported( "GL_ARB_debug_output" ) )
{
std::cerr << "GL_ARB_debug_output not supported" << std::endl;
return;
}
glEnable( GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB );
glDebugMessageCallbackARB( DebugCallback, 0 );
glDebugMessageControlARB( GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_TRUE );
}
private:
static const char* Source( GLenum source )
{
switch( source )
{
case GL_DEBUG_SOURCE_API_ARB : return "API";
case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB : return "WINDOW_SYSTEM";
case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB : return "SHADER_COMPILER";
case GL_DEBUG_SOURCE_THIRD_PARTY_ARB : return "THIRD_PARTY";
case GL_DEBUG_SOURCE_APPLICATION_ARB : return "APPLICATION";
case GL_DEBUG_SOURCE_OTHER_ARB : return "OTHER";
default : return "Unknown source";
}
}
static const char* Type( GLenum type )
{
switch( type )
{
case GL_DEBUG_TYPE_ERROR_ARB : return "ERROR";
case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB : return "DEPRECATED_BEHAVIOR";
case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB : return "UNDEFINED_BEHAVIOR";
case GL_DEBUG_TYPE_PORTABILITY_ARB : return "PORTABILITY";
case GL_DEBUG_TYPE_PERFORMANCE_ARB : return "PERFORMANCE";
case GL_DEBUG_TYPE_OTHER_ARB : return "OTHER";
default : return "Unknown type";
}
}
static const char* Severity( GLenum severity )
{
switch( severity )
{
case GL_DEBUG_SEVERITY_HIGH_ARB : return "HIGH";
case GL_DEBUG_SEVERITY_MEDIUM_ARB : return "MEDIUM";
case GL_DEBUG_SEVERITY_LOW_ARB : return "LOW";
default : return "Unknown severity";
}
}
static void APIENTRY DebugCallback
(
GLenum source,
GLenum type,
GLuint id,
GLenum severity,
GLsizei length,
const GLchar* message,
const void* userParam
)
{
std::cerr << "GL_DEBUG"
<< ": " << Source( source )
<< ": " << Type( type )
<< ": " << Severity( severity )
<< ": " << message
<< std::endl;
}
};
struct Program
{
static GLuint Load( const char* vert, const char* geom, const char* frag )
{
GLuint prog = glCreateProgram();
if( vert ) AttachShader( prog, GL_VERTEX_SHADER, vert );
if( geom ) AttachShader( prog, GL_GEOMETRY_SHADER, geom );
if( frag ) AttachShader( prog, GL_FRAGMENT_SHADER, frag );
glLinkProgram( prog );
CheckStatus( prog );
return prog;
}
private:
static void CheckStatus( GLuint obj )
{
GLint status = GL_FALSE;
if( glIsShader(obj) ) glGetShaderiv( obj, GL_COMPILE_STATUS, &status );
if( glIsProgram(obj) ) glGetProgramiv( obj, GL_LINK_STATUS, &status );
if( status == GL_TRUE ) return;
GLchar log[ 1 << 15 ] = { 0 };
if( glIsShader(obj) ) glGetShaderInfoLog( obj, sizeof(log), NULL, log );
if( glIsProgram(obj) ) glGetProgramInfoLog( obj, sizeof(log), NULL, log );
std::cerr << log << std::endl;
exit( EXIT_FAILURE );
}
static void AttachShader( GLuint program, GLenum type, const char* src )
{
GLuint shader = glCreateShader( type );
glShaderSource( shader, 1, &src, NULL );
glCompileShader( shader );
CheckStatus( shader );
glAttachShader( program, shader );
glDeleteShader( shader );
}
};
#define GLSL(version, shader) "#version " #version "\n" #shader
const char* vert = GLSL
(
330,
layout( location = 0 ) in vec4 position;
void main()
{
gl_Position = position;
}
);
const char* frag = GLSL
(
330,
out vec4 outputColor;
void main()
{
outputColor = vec4( 1.0, 1.0, 1.0, 1.0 );
}
);
GLuint VAO;
GLuint VBO;
GLuint prog;
void init()
{
glGenVertexArrays( 1, &VAO );
glBindVertexArray( VAO );
glGenBuffers( 1,&VBO );
glBindBuffer( GL_ARRAY_BUFFER, VBO );
float verts[] =
{
-1.0, -1.0,
1.0, -1.0,
0.0, 1.0,
};
glBufferData( GL_ARRAY_BUFFER, sizeof( verts ), verts, GL_STATIC_DRAW );
prog = Program::Load( vert, NULL, frag );
glUseProgram( prog );
glEnableVertexAttribArray( 0 );
glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 0, 0 );
}
void display()
{
glClearColor( 0, 0, 0, 1 );
glClear( GL_COLOR_BUFFER_BIT );
glUseProgram( prog );
glBindVertexArray( VAO );
glDrawArrays( GL_TRIANGLES, 0, 3 );
}
void glfwErrorCallback( int error, const char* description )
{
std::cerr << "GLFW error: " << description << std::endl;
}
int main( int argc, char** argv )
{
glfwSetErrorCallback( glfwErrorCallback );
if( GL_FALSE == glfwInit() )
{
return EXIT_FAILURE;
}
std::cout << "GLFW version : " << glfwGetVersionString() << std::endl;
glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 );
glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3 );
glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE );
glfwWindowHint( GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE );
GLFWwindow* window = glfwCreateWindow( 640, 480, "Test", NULL, NULL );
if( NULL == window )
{
glfwTerminate();
return EXIT_FAILURE;
}
glfwMakeContextCurrent( window );
glfwSwapInterval( 1 );
glewExperimental = GL_TRUE;
const GLenum glewErr = glewInit();
if( GLEW_OK != glewErr )
{
std::cerr << "glewInit() failed: " << glewGetErrorString( glewErr ) << std::endl;
glfwTerminate();
return EXIT_FAILURE;
}
// consume spurious GL error from GLEW init
glGetError();
GlDebugOutput::Install();
init();
while( !glfwWindowShouldClose( window ) )
{
glfwPollEvents();
int w, h;
glfwGetFramebufferSize( window, &w, &h );
glViewport( 0, 0, w, h );
display();
glfwSwapBuffers( window );
}
return EXIT_SUCCESS;
}
If anything is even just a little bit off with your OpenGL implementation and/or shaders that program should scream bloody murder to stderr and exit.
Sorry for the very slow update, I've been busy with other things.
Anyways, the problem was that while writing the code, I had included the line
program = glCreateProgram();
but at some point, while tidying up, I accidentally deleted it. That's why the shaders were compiling but weren't linking, they weren't being linked to a program object.
Related
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
Error Screenshot
You're missing a semicolon after the vec4 in your vertex shader
If 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.
I'm having classes of computer graphics on OpenGL. There we study and work with Visual Studio 2015 while I run Linux on my laptop. So there some differences between composing a program in our classes and in lessons I found on Wikibooks. I tried to run one tutorial code from there and everything was fine, while I couldn't run my code I wrote at my lesson.
So I tried manipulating with my and tutorial code. I succeeded a few. The program runs but not shows my figures. So it actually must show a chesstable. But, for my regret, it shows a background color only. What may be wrong with that?
The first part on onDisplay() is not displayed. The commented part is from tutorial which works and shows a triangle.
Full code:
#include <stdio.h>
#include <stdlib.h>
/* Use glew.h instead of gl.h to get all the GL prototypes declared */
#include <GL/glew.h>
/* Using the GLUT library for the base windowing setup */
#include <GL/freeglut.h>
GLuint program;
GLint attribute_coord2d;
int init_resources()
{
GLint compile_ok = GL_FALSE, link_ok = GL_FALSE;
GLuint vs = glCreateShader( GL_VERTEX_SHADER );
const char *vs_source =
#ifdef GL_ES_VERSION_2_0
"#version 100\n" // OpenGL ES 2.0
#else
"#version 120\n" // OpenGL 2.1
#endif
"attribute vec2 coord2d; "
"void main(void) { "
" gl_Position = vec4(coord2d, 0.0, 1.0); "
"}";
glShaderSource( vs, 1, &vs_source, NULL );
glCompileShader( vs );
glGetShaderiv( vs, GL_COMPILE_STATUS, &compile_ok );
if( !compile_ok )
{
fprintf( stderr, "Error in vertex shader\n" );
return 0;
}
GLuint fs = glCreateShader( GL_FRAGMENT_SHADER );
const char *fs_source =
#ifdef GL_ES_VERSION_2_0
"#version 100\n" // OpenGL ES 2.0
#else
"#version 120\n" // OpenGL 2.1
#endif
"void main(void) { "
" gl_FragColor[0] = 0.0; "
" gl_FragColor[1] = 0.0; "
" gl_FragColor[2] = 1.0; "
"}";
glShaderSource( fs, 1, &fs_source, NULL );
glCompileShader( fs );
glGetShaderiv( fs, GL_COMPILE_STATUS, &compile_ok );
if( !compile_ok )
{
fprintf( stderr, "Error in fragment shader\n" );
return 0;
}
program = glCreateProgram();
glAttachShader( program, vs );
glAttachShader( program, fs );
glLinkProgram( program );
glGetProgramiv( program, GL_LINK_STATUS, &link_ok );
if( !link_ok )
{
fprintf( stderr, "glLinkProgram:" );
return 0;
}
const char* attribute_name = "coord2d";
attribute_coord2d = glGetAttribLocation( program, attribute_name );
if( attribute_coord2d == -1 )
{
fprintf( stderr, "Could not bind attribute %s\n", attribute_name );
return 0;
}
return 1;
}
void chess( double x, double y, double w, double h, int k )
{
glColor3f( 0.6, 0.6, 0.6 );
glRecti( 20, 20, 480, 480 );
for( int i = 0; i < 8; i++ )
{
glRasterPos2i( ( i + 1 )*x + 20, 30 );
glutBitmapCharacter( GLUT_BITMAP_9_BY_15, 'A' + i );
}
for( int j = 0; j < 8; j++ )
{
for( int i = 0; i < 8; i++ )
{
if( k % 2 == 0 )
{
glColor3f( 1, 1, 1 );
}
else glColor3f( 0, 0, 0 );
glRecti( x + w*i, y + h*j, 2 * x + w*i, 2 * y + h*j );
k++;
}
k++;
}
}
void onDisplay()
{
glClearColor( 0.5, 1.0, 1.0, 1.0 );
glClear( GL_COLOR_BUFFER_BIT );
glUseProgram( program );
glEnableVertexAttribArray( attribute_coord2d );
double x, y, w, h;
int k;
x = 50;
y = 50;
h = 50;
w = 50;
k = 1;
chess( x, y, w, h, k );
glFlush();
/*
GLfloat triangle_vertices[] = {
0.0, 0.8,
-0.8, -0.8,
0.8, -0.8,
};
// Describe our vertices array to OpenGL (it can't guess its format automatically)
glVertexAttribPointer(
attribute_coord2d, // attribute
2, // number of elements per vertex, here (x,y)
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
0, // no extra data between each position
triangle_vertices // pointer to the C array
);
// Push each element in buffer_vertices to the vertex shader
glDrawArrays(GL_TRIANGLES, 0, 3);
*/
glDisableVertexAttribArray( attribute_coord2d );
glutSwapBuffers();
}
void free_resources()
{
glDeleteProgram( program );
}
int main( int argc, char* argv[] )
{
glutInit( &argc, argv );
glutInitContextVersion( 2, 0 );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowSize( 640, 640 );
glutCreateWindow( "chesstable" );
GLenum glew_status = glewInit();
if( glew_status != GLEW_OK )
{
fprintf( stderr, "Error: %s\n", glewGetErrorString( glew_status ) );
return 1;
}
if( init_resources() )
{
glutDisplayFunc( onDisplay );
glutMainLoop();
}
free_resources();
return 0;
}
I've been learning OpenGl, along with it "GLSL".
Currently i'm getting an error trying to compile my fragment shader:
Incompatible types (vec4 and vec3) in assignment (and no available implicit conversion)
Can someone please help me understand and resolve this error?
Code is below:
#version 330 core
in vec3 ourColor;
in vec2 TexCoord;
out vec4 color;
// Texture samplers
uniform sampler2D ourTexture1;
void main() {
// Linearly interpolate between both textures (second texture is only slightly combined)
color = texture(ourTexture1, TexCoord);
}
Thank you.
EDIT:
Here is my shader compiler:
class shaders_shader
{
public:
GLuint Program;
// Constructor generates the shader on the fly
shaders_shader( const GLchar *vertexPath, const GLchar *fragmentPath )
{
// 1. Retrieve the vertex/fragment source code from filePath
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
// ensures ifstream objects can throw exceptions:
vShaderFile.exceptions ( std::ifstream::badbit );
fShaderFile.exceptions ( std::ifstream::badbit );
try
{
// Open files
vShaderFile.open( vertexPath );
fShaderFile.open( fragmentPath );
std::stringstream vShaderStream, fShaderStream;
// Read file's buffer contents into streams
vShaderStream << vShaderFile.rdbuf( );
fShaderStream << fShaderFile.rdbuf( );
// close file handlers
vShaderFile.close( );
fShaderFile.close( );
// Convert stream into string
vertexCode = vShaderStream.str( );
fragmentCode = fShaderStream.str( );
}
catch ( std::ifstream::failure e )
{
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl;
}
const GLchar *vShaderCode = vertexCode.c_str( );
const GLchar *fShaderCode = fragmentCode.c_str( );
// 2. Compile shaders
GLuint vertex, fragment;
GLint success;
GLchar infoLog[512];
// Vertex Shader
vertex = glCreateShader( GL_VERTEX_SHADER );
glShaderSource( vertex, 1, &vShaderCode, NULL );
glCompileShader( vertex );
// Print compile errors if any
glGetShaderiv( vertex, GL_COMPILE_STATUS, &success );
if ( !success )
{
glGetShaderInfoLog( vertex, 512, NULL, infoLog );
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// Fragment Shader
fragment = glCreateShader( GL_FRAGMENT_SHADER );
glShaderSource( fragment, 1, &fShaderCode, NULL );
glCompileShader( fragment );
// Print compile errors if any
glGetShaderiv( fragment, GL_COMPILE_STATUS, &success );
if ( !success )
{
glGetShaderInfoLog( fragment, 512, NULL, infoLog );
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// Shader Program
this->Program = glCreateProgram( );
glAttachShader( this->Program, vertex );
glAttachShader( this->Program, fragment );
glLinkProgram( this->Program );
// Print linking errors if any
glGetProgramiv( this->Program, GL_LINK_STATUS, &success );
if (!success)
{
glGetProgramInfoLog( this->Program, 512, NULL, infoLog );
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
// Delete the shaders as they're linked into our program now and no longer necessery
glDeleteShader( vertex );
glDeleteShader( fragment );
}
// Uses the current shader
void Use( )
{
glUseProgram( this->Program );
}
};
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm trying to write a modern OpenGL application from scratch and I can't get a basic Triangle to render. This is odd because I've managed this in some older code and I cant find a difference in how I've done everything.
I'm using shaders, and as far as I can tell they're compiled and linked properly.
I'm using GLFW and gl3w.
All I get from this code is my window with the clear color on it.
I'll Dump my code
Main.cpp
#include <GL/gl3w.h>
#include "Display/Display.h"
#include "Shader/Shader.h"
#include "Mesh/Mesh.h"
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define WINDOW_TITLE "CompSci Coursework"
int main() {
//Create Display and Context
Display display( WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE );
//Create Basic Shaders
Shader basicShader( "BasicShader" );
basicShader.useShader();
//Create VAO
GLuint VAO;
glGenVertexArrays( 1, &VAO );
glBindVertexArray( VAO );
std::vector<float> verticies = {
-0.5f, -0.5f,
0.5f, -0.5f,
0.0f, 0.5f
};
//Create Mesh
Mesh triangle( verticies );
//While Open Loop
while ( !display.CheckShouldClose()) {
//Clear Buffer
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
//Draw Triangle
glDrawArrays( GL_TRIANGLES, 0, 3 );
//Update Display
display.Update();
}
return 0;
}
Display.cpp
#include <iostream>
#include <GL/gl3w.h>
#include "Display.h"
Display::Display( int width, int height, const char* title ) {
//Init GLFW
if ( glfwInit() == 0 ) {
std::cerr << "Could not initialise GLFW\n";
return;
} else {
std::cout << "GLFW initialised\n";
}
//Window Hints
glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 );
glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 2 );
glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE );
glfwWindowHint( GLFW_RESIZABLE, GL_FALSE );
//Create Window
m_window = glfwCreateWindow( width, height, title, NULL, NULL );
//Make Context
glfwMakeContextCurrent( m_window );
//Initialise GL3W
if ( gl3wInit() != 0 ) {
std::cerr << "Could not init GL3W\n";
return;
} else {
std::cout << "Initialised GL3W\n";
}
}
Display::~Display() {
//Delete Context
glfwMakeContextCurrent( 0 );
//Destroy Window
glfwDestroyWindow( m_window );
//Terminate GLFW
glfwTerminate();
}
bool Display::CheckShouldClose() {
return (bool)glfwWindowShouldClose( m_window );
}
void Display::Update() {
//Swap Buffers
glfwSwapBuffers( m_window );
//Poll Events
glfwPollEvents();
}
Mesh.cpp
#include <GL/gl3w.h>
#include <iostream>
#include "Mesh.h"
Mesh::Mesh( std::vector<float> verticies ) {
m_verticies = verticies;
//Generate VBO
glGenBuffers( 1, &m_vbo );
//Bind VBO
glBindBuffer( GL_ARRAY_BUFFER, m_vbo );;
//Pass Buffer Data
glBufferData( GL_ARRAY_BUFFER, m_verticies.size() * sizeof( float), &m_verticies[0], GL_STATIC_DRAW );
}
Mesh::~Mesh() {
glDeleteBuffers( 1, &m_vbo );
}
Shader.cpp
#include <fstream>
#include <iostream>
#include <vector>
#include <GL/gl3w.h>
#include "Shader.h"
#define SHADER_LIST { VERTEX_SHADER, FRAGMENT_SHADER }
Shader::Shader( std::string shaderName ) {
//Load Source Code
std::vector<std::string> shaderSources;
shaderSources.push_back( loadShaderSource( shaderName + ".vert" ));
shaderSources.push_back( loadShaderSource( shaderName + ".frag" ));
//Cast into C-Style Strings
const char* shaderSourcesCString[NUM_SHADER];
for ( int i : SHADER_LIST ) {
shaderSourcesCString[ i ] = shaderSources[ i ].c_str();
}
//Create Shaders
m_shaders[ VERTEX_SHADER ] = glCreateShader( GL_VERTEX_SHADER );
m_shaders[ FRAGMENT_SHADER ] = glCreateShader( GL_FRAGMENT_SHADER );
//Source Shaders
glShaderSource( m_shaders[ VERTEX_SHADER ], 1, &shaderSourcesCString[ VERTEX_SHADER ], NULL );
glShaderSource( m_shaders[ FRAGMENT_SHADER ], 1, &shaderSourcesCString[ FRAGMENT_SHADER ], NULL );
//Compile Shaders
for ( int i : SHADER_LIST ) {
glCompileShader( m_shaders[ i ] );
GLint status;
glGetShaderiv( m_shaders[ i ], GL_COMPILE_STATUS, &status );
if ( status != GL_TRUE ) {
char buffer[512];
glGetShaderInfoLog( m_shaders[ i ], 512, NULL, buffer );
std::cerr << "Shader Compilation Error on shader " << i << ": " << buffer << "\n";
return;
}
}
//Create Program
m_program = glCreateProgram();
//Attach Shaders
for ( int i : SHADER_LIST ) {
glAttachShader( m_program, m_shaders[ i ] );
}
//Link Shader
glLinkProgram( m_program );
//Find Link Error
{
GLint status;
glGetProgramiv(m_program, GL_LINK_STATUS, &status);
if (status != GL_TRUE){
std::cerr << "Unable to link shaders\n";
char buffer[512];
glGetProgramInfoLog(m_program, 512, NULL, buffer);
std::cerr << buffer;
return;
}
}
//Bind outColor
glBindFragDataLocation(m_program, 0, "outColor");
//Attributes
GLint posAttrib = glGetAttribLocation(m_program, "position");
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(posAttrib);
std::cout << "Shaders Compiled Successfully\n";
}
Shader::~Shader() {
for ( int i : SHADER_LIST ) {
//Detach Shader
glDetachShader( m_program, m_shaders[ i ] );
//Delete Shader
glDeleteShader( m_shaders[ i ] );
}
//Delete Program
glDeleteProgram( m_program );
}
std::string Shader::loadShaderSource( std::string filename ) {
//Locals
std::string output, line;
//Open File
std::fstream file;
file.open( "Shaders/" + filename );
//Check for error loading file
if ( file.bad()) {
std::cerr << "Could not load shader file: " << filename << "\n";
return NULL;
} else {
std::cout << "Loaded shader file: " << filename << "\n";
}
//Read file into locals
while ( file.good()) {
getline( file, line );
if ( line != "" )
output += line + "\n";
}
//Return C-String
return output;
}
void Shader::useShader() {
glUseProgram( m_program );
}
GLuint Shader::getProgram() {
return m_program;
}
These Are my shaders
Vertex Shader
#version 150 core
in vec2 position;
void main(){
gl_Position = vec4(position, 0.0, 1.0);
}
Fragment Shader
#version 150 core
out vec4 outColor;
void main(){
outColor = vec4(1.0, 1.0, 1.0, 1.0);
}
First you have to create the vertex array buffer:
std::vector< GLfloat > verticies;
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * (GLsizeiptr)verticies.size(), verticies.data(), GL_STATIC_DRAW);
Once your shader program is linked you have to determine the index of the vertex attribute(s):
GLuint prog = ...;
GLint posAttrLoc = glGetAttribLocation(prog, "position");
Before drawing, you have to bind and enable the vertex attribute(s):
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableVertexAttribArray(posAttrLoc );
glVertexAttribPointer(posAttrLoc , 2, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays( GL_TRIANGLES, 0, 3 );
I am trying to render a simple Triangle in OpenGL ES 2.x using c++ and SDL 2.
But the glCreateShader and glCreatProgram returning zero.
Bellow Is the code I am using
#include "SDL.h"
#include <OpenGLES/ES2/gl.h>
#include <OpenGLES/ES2/glext.h>
#include <iostream>
#include <string>
#include <memory>
using namespace std;
GLuint programObject;
class Graphics
{
private:
SDL_Window* _window;
public:
Graphics(SDL_Window* window)
{
_window = window;
}
void update()
{
glClearColor(255.0f, 0.0f, 255.0f, 1);
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
GLfloat vVertices[] = {
0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f
};
glViewport (0, 0, 320, 480);
glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
glClear (GL_COLOR_BUFFER_BIT);
glUseProgram (programObject);
glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
glEnableVertexAttribArray (0);
glDrawArrays (GL_TRIANGLES, 0, 3);
SDL_GL_SwapWindow(_window);
}
};
void UpdateFrame(void* param)
{
Graphics* graphics = (Graphics*)param;
graphics->update();
}
///
// Create a shader object, load the shader source, and
// compile the shader.
//
GLuint LoadShader(GLenum type, const GLchar *shaderSrc)
{
GLuint shader;
GLint compiled;
// Create the shader object
shader = glCreateShader(type);
if(shader == 0)
{
cerr << "Could not create OpenGL shader " << endl;
return 0;
}
// Load the shader source
glShaderSource(shader, 1, &shaderSrc, NULL);
// Compile the shader
glCompileShader(shader);
// Check the compile status
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if(!compiled)
{
GLint infoLen = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
if(infoLen > 1)
{
char* infoLog = (char*)malloc(sizeof(char) * infoLen);
glGetShaderInfoLog(shader, infoLen, NULL, infoLog);
SDL_Log("Error compiling shader:\n%s\n", infoLog);
free(infoLog);
}
glDeleteShader(shader);
return 0;
}
return shader;
}
///
// Initialize the shader and program object
//
int Init()
{
const char vertexShaderString[] =
"attribute vec4 vPosition; \n"
"void main() \n"
"{ \n"
" gl_Position = vPosition; \n"
"} \n";
const char fragmentShaderString[] =
"precision mediump float;\n"\
"void main() \n"
"{ \n"
" gl_FragColor = vec4 ( 1.0, 0.0, 0.0, 1.0 );\n"
"} \n";
GLuint vertexShader;
GLuint fragmentShader;
GLint linked;
vertexShader = LoadShader (GL_VERTEX_SHADER, vertexShaderString);
fragmentShader = LoadShader (GL_FRAGMENT_SHADER, fragmentShaderString);
programObject = glCreateProgram ();
if (programObject == 0) {
cerr << "Could not create OpenGL program" << endl;
return 0;
}
glAttachShader (programObject, vertexShader);
glAttachShader (programObject, fragmentShader);
glBindAttribLocation (programObject, 0, "vPosition");
glLinkProgram (programObject);
glGetProgramiv (programObject, GL_LINK_STATUS, &linked);
if (!linked) {
GLint infoLen = 0;
glGetProgramiv (programObject, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen > 1) {
char* infoLog = (char*) malloc (sizeof (char) * infoLen);
glGetProgramInfoLog (programObject, infoLen, NULL, infoLog);
cerr << "Error linking program: " << infoLog << endl;
free (infoLog);
}
glDeleteProgram (programObject);
return 0;
}
glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
return true;
}
int EventFilter(void* userdata, SDL_Event* event)
{
switch (event->type)
{
case SDL_FINGERMOTION:
SDL_Log("Finger Motion");
return 0;
case SDL_FINGERDOWN:
SDL_Log("Finger Down");
return 0;
case SDL_FINGERUP:
SDL_Log("Finger Up");
return 0;
}
return 1;
}
int main(int argc, char** argv)
{
/* initialize SDL */
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("Could not initialize SDL\n");
return 1;
}
SDL_DisplayMode displayMode;
SDL_GetDesktopDisplayMode(0, &displayMode);
/* create window and renderer */
SDL_Window* window = SDL_CreateWindow(NULL, 0, 0, displayMode.w, displayMode.h, SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN | SDL_WINDOW_RESIZABLE);
if (!window) {
printf("Could not initialize Window\n");
return 1;
}
auto gl = SDL_GL_CreateContext(window);
if (!Init()) {
cerr << "Error initializing OpenGL" << endl;
return 1;
}
unique_ptr<Graphics> graphics = unique_ptr<Graphics>(new Graphics(window));
SDL_iPhoneSetAnimationCallback(window, 1, UpdateFrame, graphics.get());
SDL_AddEventWatch(EventFilter, NULL);
//Game Loop
SDL_Event event;
auto done = false;
while (!done)
{
SDL_PumpEvents();
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
done = true;
break;
case SDL_APP_DIDENTERFOREGROUND:
SDL_Log("SDL_APP_DIDENTERFOREGROUND");
break;
case SDL_APP_DIDENTERBACKGROUND:
SDL_Log("SDL_APP_DIDENTERBACKGROUND");
break;
case SDL_APP_LOWMEMORY:
SDL_Log("SDL_APP_LOWMEMORY");
break;
case SDL_APP_TERMINATING:
SDL_Log("SDL_APP_TERMINATING");
break;
case SDL_APP_WILLENTERBACKGROUND:
SDL_Log("SDL_APP_WILLENTERBACKGROUND");
break;
case SDL_APP_WILLENTERFOREGROUND:
SDL_Log("SDL_APP_WILLENTERFOREGROUND");
break;
case SDL_WINDOWEVENT:
{
switch (event.window.event)
{
case SDL_WINDOWEVENT_RESIZED:
{
SDL_Log("Window %d resized to %dx%d", event.window.windowID, event.window.data1, event.window.data2);
break;
}
}
}
}
}
}
SDL_GL_DeleteContext(gl);
// Done! Close the window, clean-up and exit the program.
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Maybe I am missing something here, any help would be appreciated .
Thanks,
Khaled
This is most likely caused by a problem with the context created by SDL, specifically there were no attributes for the MAJOR/MINOR and MASK attributes which tend to be more important for iOS, OS X etc
What needs to be done is as follows. Before the creation of the SDL_Window do the following:
/* Set the correct attributes for MASK and MAJOR version */
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
/* create window and renderer */
SDL_Window* window = SDL_CreateWindow(NULL, 0, 0, displayMode.w, displayMode.h, SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN | SDL_WINDOW_RESIZABLE);
Another situation which can cause the same error output is calling glCreateShader or glCreateProgram inside of a glBegin / glEnd block.