Reputation: 545
I am having trouble loading the basic fragment, and vertex shaders. I have tried to run the example code from the OpenGL Red Book for OpenGL 4.3, which works fine except the triangles aren't blue.
I have loadShaders.h and loadShaders.cpp in the same directory as my main.cpp directory.
I am linking the files like this: g++ main.cpp LoadShaders.cpp -lGL -lGLEW -lGLU -lglut -o run
my: main.cpp file, triangles.vert file and triangles.frag file all look exactly the same as the sample code in the book.
Someone help me solve this problem.
main.cpp:
#include<GL/glew.h>
#include<GL/freeglut.h>
#include<GL/gl.h>
#include<iostream>
#include"LoadShaders.h"
#include"vgl.h"
using namespace std;
enum VAO_IDs {Triangles, NumVAOs };
enum Buffer_IDs {ArrayBuffer, NumBuffers };
enum Attrib_IDs {vPosition = 0};
GLuint VAOs[NumVAOs];
GLuint Buffers[NumBuffers];
const GLuint NumVertices = 6;
void init(void)
{
glGenVertexArrays(NumVAOs, VAOs);
glBindVertexArray(VAOs[Triangles]);
GLfloat vertices[NumVertices] [2] = {
{-0.90, -0.90},
{0.85, -0.90 },
{-0.90, 0.85 },
{0.90, -0.85 },
{0.90, 0.90 },
{-0.85, 0.90 }
};
glGenBuffers(NumBuffers, Buffers);
glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
ShaderInfo shaders[]={
{GL_VERTEX_SHADER, "triangles.vert"},
{GL_FRAGMENT_SHADER, "triangles.frag"},
{GL_NONE}
};
GLuint program = LoadShaders(shaders);
glUseProgram(program);
glVertexAttribPointer(vPosition, 2, GL_FLOAT,
GL_FALSE, 0, 0);
glEnableVertexAttribArray(vPosition);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAOs[Triangles]);
glDrawArrays(GL_TRIANGLES, 0, NumVertices);
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(512,512);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutCreateWindow(argv[0]);
if (glewInit()) {
cerr << "Unable to initialize GLEW"<<endl;
exit(EXIT_FAILURE);
}
init();
glutDisplayFunc(display);
glutMainLoop();
}
triangles.vert:
#version 430 core
layout(location = 0) in vec4 vPosition;
void main()
{
gl_position = vPosition;
}
triangles.frag:
#version 430 core
out vec4 fColor;
void main()
{
fColor = vec4(0.0, 0.0, 1.0, 1.0);
}
Upvotes: 0
Views: 1557
Reputation: 52082
gl_position
!= gl_Position
glutInitContextVersion()
) that meets or exceeds the GLSL version your shaders requireglewExperimental
to GL_TRUE
before glewInit()
This works on my (OpenGL 4.2) system:
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <iostream>
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( -1 );
}
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
(
420 core,
layout(location = 0) in vec4 vPosition;
void main()
{
gl_Position = vPosition;
}
);
const char* frag = GLSL
(
420 core,
out vec4 fColor;
void main()
{
fColor = vec4(0.0, 0.0, 1.0, 1.0);
}
);
GLuint vao = 0;
unsigned int numVerts = 0;
void init(void)
{
glGenVertexArrays( 1 , &vao );
glBindVertexArray( vao );
GLfloat vertices[] =
{
-0.90, -0.90,
0.85, -0.90,
-0.90, 0.85,
0.90, -0.85,
0.90, 0.90,
-0.85, 0.90,
};
numVerts = sizeof( vertices ) / sizeof( GLfloat ) / 2;
GLuint vbo = 0;
glGenBuffers( 1, &vbo );
glBindBuffer( GL_ARRAY_BUFFER, vbo );
glBufferData( GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW );
GLuint prog = Program::Load( vert, NULL, frag );
glUseProgram( prog );
glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 0, 0 );
glEnableVertexAttribArray( 0 );
}
void display()
{
glClear( GL_COLOR_BUFFER_BIT );
glBindVertexArray( vao );
glDrawArrays( GL_TRIANGLES, 0, numVerts );
glutSwapBuffers();
}
int main( int argc, char **argv )
{
glutInitWindowSize( 640, 480 );
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitContextVersion( 4, 2 );
glutInitContextProfile( GLUT_CORE_PROFILE );
glutCreateWindow( "GLUT" );
glewExperimental = GL_TRUE;
glewInit();
init();
glutDisplayFunc( display );
glutMainLoop();
return 0;
}
Version tester:
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <iostream>
int main( int argc, char **argv )
{
glutInitWindowSize( 640, 480 );
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitContextProfile( GLUT_CORE_PROFILE );
glutCreateWindow( "GLUT" );
glewExperimental = GL_TRUE;
glewInit();
std::cout << glGetString( GL_VERSION ) << std::endl;
return 0;
}
Upvotes: 3
Reputation: 3
Wipe out main's call to glutInitContextProfile. That and glutInitContextVersion(() gave me trouble and I was able to do without them by accepting the default profiles, I suppose. Then add this to the top of your fragment shader:
#version 300 es
precision mediump float;
... and your vertex shader will need to match it's version with this at the top:
#version 300 es
Also, be careful with the shader paths. I had to tweak mine with
ShaderInfo shaders[] = {
{ GL_VERTEX_SHADER, "../../triangles.vert" },
{ GL_FRAGMENT_SHADER, "../../triangles.frag" },
{ GL_NONE, NULL }
};
Upvotes: -1