与2013年相比,安装过剩和缺口

时间:2015-03-29 14:39:28

标签: opengl glut glew

我有vs 2013社区版。使用nuget我安装了nupengl.core包。 我添加到项目中的唯一内容(nupengl.core包除外)是opengl32.lib(到c / c ++ - linker-input-Additional Dependencies)。我从非常简单的示例开始,但它只呈现一个点(着色器程序应该渲染)还有一点奇怪的是,如果我在着色器代码中添加一些错误,请说明#34; void 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. */
}

1 个答案:

答案 0 :(得分:0)

在这里工作得很好:

screenshot

#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()中执行的旧固定功能矩阵内容都不会在Core上下文中运行。