我无法加载基本片段和顶点着色器。我试图从OpenGL红皮书for OpenGL 4.3运行示例代码,除了三角形不是蓝色外,它的工作正常。
我将loadShaders.h和loadShaders.cpp放在与main.cpp目录相同的目录中。
我正在链接这样的文件:g++ main.cpp LoadShaders.cpp -lGL -lGLEW -lGLU -lglut -o run
my:main.cpp文件,triangles.vert文件和triangles.frag文件看起来与书中的示例代码完全相同。
有人帮我解决了这个问题。
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);
}
答案 0 :(得分:3)
gl_position
!= gl_Position
glutInitContextVersion()
)glewExperimental
GL_TRUE
设为glewInit()
这适用于我的(OpenGL 4.2)系统:
#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;
}
版本测试员:
#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;
}
答案 1 :(得分:-1)
清除主要对glutInitContextProfile的调用。那个和glutInitContextVersion(()给了我麻烦,我想通过接受默认配置文件我没有它们。我想。然后将它添加到片段着色器的顶部:
#version 300 es
precision mediump float;
...你的顶点着色器需要在顶部与它的版本匹配:
#version 300 es
另外,请注意着色器路径。我不得不用
调整我的ShaderInfo shaders[] = {
{ GL_VERTEX_SHADER, "../../triangles.vert" },
{ GL_FRAGMENT_SHADER, "../../triangles.frag" },
{ GL_NONE, NULL }
};