我的OpenGL程序坏了,我不明白为什么。
我的代码如下。
我使用的是Mac OS X 10.9。 这是复制bug的最小程序
片段着色器
#version 410 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
}
顶点着色器
#version 410 core
layout(location=0) in vec2 position;
void main()
{
gl_Position = vec4(position, 0.0f, 1.0f);
}
绘制功能
void draw()
{
glUseProgram(program);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
初始化功能
void initData()
{
unsigned int indexes[] =
{
0,1,2
};
float data[] =
{
0.0f,0.8f,
-0.8f, 0.0f,
0.8f,0.0f
};
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data,GL_STATIC_DRAW);
glGenBuffers(1, &index_buffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexes), indexes, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0,2,GL_FLOAT, GL_FALSE,0,0);
glBindVertexArray(0);
}
UPD
以下程序采用此输出
GL_VERSION : 4.1 INTEL-8.18.29
GL_VENDOR : Intel Inc.
GL_RENDERER : Intel HD Graphics 5000 OpenGL Engine
GL_SHADING_LANGUAGE_VERSION : 4.10
UPD 2
解决了,在着色器编译功能中找到了错误
答案 0 :(得分:1)
在这里工作得很好:
GLFW version : 3.0.4 Win32 WGL VisualC LoadLibrary(winmm)
GLEW_VERSION : 1.10.0
GL_VERSION : 4.1.0
GL_VENDOR : NVIDIA Corporation
GL_RENDERER : GeForce GTX 765M/PCIe/SSE2
GL_SHADING_LANGUAGE_VERSION : 4.10 NVIDIA via Cg compiler
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <vector>
#include <iostream>
using namespace std;
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 << 16 ] = { 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 );
}
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 );
}
GLuint LoadProgram( 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;
}
#define GLSL(version, shader) "#version " #version "\n" #shader
GLuint program;
GLuint VAO;
void initData()
{
const char* vert = GLSL
(
410 core,
layout( location = 0 ) in vec2 position;
void main()
{
gl_Position = vec4( position, 0.0, 1.0 );
}
);
const char* frag = GLSL
(
410 core,
out vec4 FragColor;
void main()
{
FragColor = vec4( 0.6, 1.0, 1.0, 1.0 );
}
);
program = LoadProgram( vert, NULL, frag );
glGenVertexArrays( 1, &VAO );
glBindVertexArray( VAO );
GLuint vertex_buffer = 0;
glGenBuffers( 1, &vertex_buffer );
glBindBuffer( GL_ARRAY_BUFFER, vertex_buffer );
float data[] =
{
0.0f,0.8f,
-0.8f, 0.0f,
0.8f,0.0f
};
glBufferData( GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW );
GLuint index_buffer = 0;
glGenBuffers( 1, &index_buffer );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, index_buffer );
unsigned int indexes[] =
{
0,1,2
};
glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof(indexes), indexes, GL_STATIC_DRAW );
glEnableVertexAttribArray( 0 );
glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 0, 0 );
glBindVertexArray( 0 );
}
int main( int argc, char** argv )
{
if( !glfwInit() )
return -1;
glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 4 );
glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 1 );
glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE );
glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
GLFWwindow* window = glfwCreateWindow( 640, 480, "Hello window", NULL, NULL );
if( !window )
{
cerr << "Error on window creating" << endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent( window );
cout << "GLFW version : " << glfwGetVersionString() << endl;
cout << "GLEW_VERSION : " << glewGetString( GLEW_VERSION ) << endl;
cout << "GL_VERSION : " << glGetString( GL_VERSION ) << endl;
cout << "GL_VENDOR : " << glGetString( GL_VENDOR ) << endl;
cout << "GL_RENDERER : " << glGetString( GL_RENDERER ) << endl;
cout << "GL_SHADING_LANGUAGE_VERSION : " << glGetString( GL_SHADING_LANGUAGE_VERSION ) << endl;
glewExperimental = true;
GLenum err = glewInit();
if( err!= GLEW_OK )
{
cerr << "Glew init failed!" << endl;
cerr << "Error: " << glewGetErrorString( err ) << endl;
}
initData();
while( !glfwWindowShouldClose(window) )
{
glClearColor( 0, 0, 0, 0 );
glClear( GL_COLOR_BUFFER_BIT );
glUseProgram( program );
glBindVertexArray( VAO );
glDrawElements( GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0 );
glBindVertexArray( 0 );
glfwSwapBuffers( window );
glfwPollEvents();
}
glfwTerminate();
return 0;
}
确保卡上有supports OpenGL 4.1 Core个。{/ p>