与 2013 年相比,安装过剩

Posted

技术标签:

【中文标题】与 2013 年相比,安装过剩【英文标题】:Installation glut and glew on vs 2013 【发布时间】:2015-03-29 14:39:28 【问题描述】:

我有 vs 2013 社区版。使用 nuget 我安装了 nupengl.core 包。 我添加到我的项目中的唯一东西(除了 nupengl.core 包)是 opengl32.lib(到 c/c++-linker-input-Additional Dependencies)。我从一个非常简单的示例开始,但它只渲染一个点(着色器程序应该渲染三分)。另外奇怪的是,如果我在着色器代码中添加一些错误,而不是在我的着色器代码中添加“void main”“avoid main”,它会在没有信号错误的情况下编译(当我问 glew 是否有通常的错误glew - 功能 glew 说一切正常)。例如,此示例适用于 PyOpenGL,但不适用于 vs 2013。有人可以告诉我我的代码有什么问题吗? 代码是:

#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】:

在这里工作得很好:

#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() 中所做的旧固定函数矩阵的任何东西都不能在核心上下文中工作。

【讨论】:

感谢您的代码,现在一切正常。再次感谢! 我很乐意为你的答案投票,但他们说我没有 15 名声望可以投票。对不起! @Armagedon:嗯,你应该能够接受任何代表级别的答案。

以上是关于与 2013 年相比,安装过剩的主要内容,如果未能解决你的问题,请参考以下文章

docker简介与安装

我需要运行 devenv.exe /setup 两次吗? [与 2005 年和 2008 年相比]

将过剩与 Qt 联系起来

与 2013 相比,Visual Studio 2015 构建速度较慢

Exchange 2013 (多服务器多角色)安装部署

Anaconda 与 EPD Enthought 与 Python 的手动安装 [关闭]