在 vs 2013 上安装过剩和 glew

Installation glut and glew on vs 2013

我有 vs 2013 社区 edition.Using 我安装了 nuget nupengl.core 包。 我添加到我的项目中的唯一东西(nupengl.core 包除外)是 opengl32.lib(到 c/c++-linker-input-Additional Dependencies)。我从非常简单的示例开始,但它呈现只有一个点(着色器程序应该渲染三个点)。同样奇怪的是,如果我向着色器代码添加一些错误,而是说 "void main" 放入我的着色器代码 "avoid main",它编译时没有信号错误(当我问 glew 是否有错误通过通常的 glew - 函数 glew 说一切正常)。例如,此示例适用于 PyOpenGL,但不适用于 vs 2013.Can 有人告诉我我的代码有什么问题吗? 代码是:

#include <GL\glew.h>
#include <GL\freeglut.h>
#include <iostream>

using namespace std;
static const GLchar * vertex_shader_source[] = {
"#version 440 core \n ",
"void main(void){ \n",
"vec4   ves[3]=vec4[3]    (vec4(0.0,-0.5,0.5,1.0),
vec4(0.0,0.5,0.5,1.0),vec4(-0.5,-0.5,0.6,1.0)) ; \n"
"    gl_Position=ves[gl_VertexID] ; \n",

"} \n"
};

static const GLchar * fragment_shader_source[] = {
"#version 440 core \n ",
"out vec4 color ; \n",
"void main(void){ \n",
"    color=vec4(0.0,0.1,0.0,1.0) ; \n",
"}  \n"
};

static GLfloat g_nearPlane = 1;
static GLfloat g_farPlane = 1000;

void reshape(GLint width, GLint height){
static int g_Width = width;
static int g_Height = height;
glViewport(0, 0, g_Width, g_Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.0, (float)g_Width / g_Height, g_nearPlane,       g_farPlane);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

}
GLuint gf;
GLuint program;

void display(void){
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPointSize(10.0f);
const static GLfloat red[] = { 1.0f, 0.0f, 0.0f, 1.0f };
glClearBufferfv(GL_COLOR, 0, red);


glDrawArrays(GL_POINTS, 0, 3);
glutSwapBuffers();
glutPostRedisplay();
}

void init(void){
GLuint vertex_shader, fragment_shader ;
glEnable(GL_DEPTH_TEST);

vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, vertex_shader_source, NULL);
glCompileShader(vertex_shader);

fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, fragment_shader_source, NULL);
glCompileShader(fragment_shader);

program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);

glUseProgram(program);

glGenVertexArrays(1, &gf);
glBindVertexArray(gf);

glPointSize(10.0f);
}

int main(int argc, char **argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

glutInitContextVersion(4, 4);
glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutSetOption(
    GLUT_ACTION_ON_WINDOW_CLOSE,
    GLUT_ACTION_GLUTMAINLOOP_RETURNS
    );

glutInitWindowSize(250, 250);
glutInitWindowPosition(100, 100);
glutCreateWindow("shader doesn't work");

glewExperimental = GL_TRUE;
glewInit();

glutDisplayFunc(display);
glutReshapeFunc(reshape);
init();
glDrawArrays(GL_POINTS, 0, 3);
glutMainLoop();
return 0;             /* ANSI C requires main to return int. */
}

在这里工作很好:


#include <GL/glew.h>
#include <GL/freeglut.h>
#include <iostream>
#include <cstdarg>

struct Program
{
    static GLuint Load( const char* shader, ... )
    {
        GLuint prog = glCreateProgram();
        va_list args;
        va_start( args, shader );
        while( shader )
        {
            const GLenum type = va_arg( args, GLenum );
            AttachShader( prog, type, shader );
            shader = va_arg( args, const char* );
        }
        va_end( args );
        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
(
    440 core,
    void main(void)
    {
        vec4 ves[3] = vec4[3]
        (
            vec4(  0.0, -0.5, 0.5, 1.0 ),
            vec4(  0.0,  0.5, 0.5, 1.0 ),
            vec4( -0.5, -0.5, 0.6, 1.0 )
        );

        gl_Position = ves[ gl_VertexID ];
    }
);

const char* frag = GLSL
(
    440 core,
    out vec4 color;
    void main(void)
    {
        color = vec4( 0.0, 1.0, 0.0, 1.0 );
    }
);

GLuint gf;
GLuint program;
void init()
{
    glEnable(GL_DEPTH_TEST);

    program = Program::Load
        (
        vert, GL_VERTEX_SHADER,
        frag, GL_FRAGMENT_SHADER,
        NULL
        );
    glUseProgram(program);

    glGenVertexArrays(1, &gf);
    glBindVertexArray(gf);

    glPointSize(10.0f);
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    const static GLfloat red[] = { 1.0f, 0.0f, 0.0f, 1.0f };
    glClearBufferfv(GL_COLOR, 0, red);

    glPointSize(10.0f);
    glDrawArrays(GL_POINTS, 0, 3);

    glutSwapBuffers();
}

int main( int argc, char** argv )
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

    glutInitContextVersion(4, 4);
    glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
    glutInitContextProfile(GLUT_CORE_PROFILE);
    glutSetOption
        (
        GLUT_ACTION_ON_WINDOW_CLOSE,
        GLUT_ACTION_GLUTMAINLOOP_RETURNS
        );

    glutInitWindowSize(250, 250);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("shader doesn't work");

    glewExperimental = GL_TRUE;
    glewInit();

    glutDisplayFunc(display);
    init();
    glutMainLoop();
    return 0;
}

仅供参考,您在 reshape()display() 中所做的旧固定函数矩阵中的 none 将在核心上下文中工作。