C++ 用c++;不起作用

C++ 用c++;不起作用,c++,xcode,macos,opengl,glfw,C++,Xcode,Macos,Opengl,Glfw,我有一个基本的OpenGL程序,使用GLFW和GLEW渲染三角形 我使用的是带有Xcode、GLFW 3.3和GLEW 2.1的Mac 我遇到了glfwCreateWindow()的一个问题,如果我将OpenGL配置文件设置为core配置文件(因为据我所知,Mac需要它),该问题将返回null,我通过将前向兼容性设置为true来解决它 有人能向我解释一下这个解决方案是否正确吗 另一件事是,现在创建的窗口没有问题,但现在我在屏幕上看不到任何东西 代码如下: #include <GL/glew

我有一个基本的OpenGL程序,使用GLFW和GLEW渲染三角形

我使用的是带有Xcode、GLFW 3.3和GLEW 2.1的Mac


我遇到了
glfwCreateWindow()
的一个问题,如果我将OpenGL配置文件设置为core配置文件(因为据我所知,Mac需要它),该问题将返回null,我通过将前向兼容性设置为true来解决它

有人能向我解释一下这个解决方案是否正确吗

另一件事是,现在创建的窗口没有问题,但现在我在屏幕上看不到任何东西

代码如下:

#include <GL/glew.h> 
#include <GLFW/glfw3.h>

#include <iostream>

static unsigned int CompileShader(unsigned int type, const std::string& source)
{
unsigned int id = glCreateShader(type);
const char* src = source.c_str();
glShaderSource(id, 1, &src, nullptr);
// Compiles the actual shader
glCompileShader(id);

// Error handeling of the compilation
int result;
glGetShaderiv(id, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE)
{
    int length;
    // Gets the lenght of the message
    glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
    // This is used to allocate memory on the stack dynamically
    char* message = (char*)alloca(length * sizeof(char));
    // Gets the log
    glGetShaderInfoLog(id, length, &length, message);
    std::cout << "Failed to compile " << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << std::endl;
    std::cout << message << std::endl;
    glDeleteShader(id);
    return 0;
}

return id;
}
static unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader)
{
unsigned int program = glCreateProgram();
// Creating the two shader
unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
// Attaches the two shaders to the program and validate everything
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program); // This links the shader executable to the actual processor
glValidateProgram(program);
glDeleteShader(vs);
glDeleteShader(fs);
return program;
}
int main(void)
{
// to initialize glew go to c++ preprocessor and add GLEW_STATIC

GLFWwindow* window;

/* Initialize the library */
if (!glfwInit())
    return -1;

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

/* Create a window and its OpenGl context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);

if (!window)
{
    glfwTerminate();
    return -1;
}

/* Make the window's context current */
glfwMakeContextCurrent(window);
// Glew init HAS to be put after making the context
if (glewInit() != GLEW_OK)
    std::cout << "GlewInit Error" << std::endl;


std::cout << "VERSION:" << std::endl;
std::cout << glGetString(GL_VERSION) << std::endl;
std::cout << "GL Version: " << (char *)glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;

// Defining the position of the vertices
float positions[6] = {
    -0.5f, -0.5f,
    0.0f,  0.5f,
    0.5f, -0.5f
};


unsigned int buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);

std::string vertexShader =
"#version 330 core\n"
"\n"
// here we are saying that the attribute at the position 0 (see attribPointer)
// which is the position itself is a input value put in a vec4 because
// glPosition needs a vec4
"layout(location = 0) in vec4 position;\n"
"\n"
"void main()\n"
"{\n"
"    gl_Position = position;\n"
"}\n";

std::string fragmentShader =
"#version 330 core\n"
"\n"
"layout(location = 0) out vec4 color;\n"
"\n"
"void main()\n"
"{\n"
"    color = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n";

unsigned int shader = CreateShader(vertexShader, fragmentShader);
glUseProgram(shader);

/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
    /* Render here */
    glClear(GL_COLOR_BUFFER_BIT);

    // second parameter is the starting index and the third is the number of indexes
    glDrawArrays(GL_TRIANGLES, 0, 3);

    /* Swap front and back buffers */
    glfwSwapBuffers(window);

    /* Poll for and process events */
    glfwPollEvents();
}

glDeleteProgram(shader);

glfwTerminate();
return 0;
}
#包括
#包括
#包括
静态无符号整数编译器标头(无符号整数类型,常量std::string和source)
{
unsigned int id=glCreateShader(类型);
const char*src=source.c_str();
glShaderSource(id,1,&src,nullptr);
//编译实际着色器
glCompileShader(id);
//编译的错误处理
int结果;
glGetShaderiv(id、GL\u编译状态和结果);
如果(结果==GL\u假)
{
整数长度;
//获取消息的长度
glGetShaderiv(标识、总账信息、日志长度和长度);
//这用于在堆栈上动态分配内存
char*message=(char*)alloca(长度*sizeof(char));
//获取日志
glGetShaderInfoLog(id、长度和长度、消息);
std::cout在核心上下文中不是可选的,就像在兼容性中一样。必须绑定一个才能绘制任何内容

在设置顶点布局和图形之前,创建一个并绑定它:

GLuint vao = 0;
glCreateVertexArrays( 1, &vao );
glBindVertexArray( vao );
...
glEnableVertexAttribArray( ... );
glVertexAttribPointer( ... );
...
glDrawArrays( ... );
总而言之:

#include <GL/glew.h> 
#include <GLFW/glfw3.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;
        std::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 );
    }
};

const char* vert = 1 + R"GLSL(
#version 330 core
// here we are saying that the attribute at the position 0 (see attribPointer)
// which is the position itself is a input value put in a vec4 because
// glPosition needs a vec4
layout(location = 0) in vec4 position;
void main()
{
    gl_Position = position;
};
)GLSL";

const char* frag = 1 + R"GLSL(
#version 330 core
layout(location = 0) out vec4 color;
void main()
{
    color = vec4(1.0, 0.0, 0.0, 1.0);
};
)GLSL";

int main( void )
{
    if( !glfwInit() )
        return -1;

    glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 );
    glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3 );
    glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE );
    glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
    GLFWwindow* window = glfwCreateWindow( 640, 480, "Hello World", NULL, NULL );
    if( !window )
    {
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent( window );

    // Glew init HAS to be put after making the context
    if( glewInit() != GLEW_OK )
        std::cout << "GlewInit Error" << std::endl;

    GLuint vao = 0;
    glCreateVertexArrays( 1, &vao );
    glBindVertexArray( vao );

    // Defining the position of the vertices
    float positions[ 6 ] = {
        -0.5f, -0.5f,
        0.0f,  0.5f,
        0.5f, -0.5f
    };

    unsigned int buffer;
    glGenBuffers( 1, &buffer );
    glBindBuffer( GL_ARRAY_BUFFER, buffer );
    glBufferData( GL_ARRAY_BUFFER, 6 * sizeof( float ), positions, GL_STATIC_DRAW );
    glEnableVertexAttribArray( 0 );
    glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, sizeof( float ) * 2, 0 );

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

    while( !glfwWindowShouldClose( window ) )
    {
        glClear( GL_COLOR_BUFFER_BIT );
        glDrawArrays( GL_TRIANGLES, 0, 3 );
        glfwSwapBuffers( window );
        glfwPollEvents();
    }

    glDeleteProgram( shader );

    glfwTerminate();
    return 0;
}
#包括
#包括
#包括
#包括
结构程序
{
静态GLuint加载(常量字符*着色器,…)
{
GLuint prog=glCreateProgram();
va_列表参数;
va_开始(参数、着色器);
while(着色器)
{
恒盂类型=va_arg(args,盂);
附加着色器(程序、类型、着色器);
着色器=va_arg(args,const char*);
}
va_端(args);
GLLINK计划(prog);
检查状态(prog);
返回程序;
}
私人:
静态无效检查状态(GLuint obj)
{
闪烁状态=GLU FALSE;
if(glishader(obj))glGetShaderiv(obj、GL\u编译状态和状态);
if(glIsProgram(obj))glGetProgramiv(obj、GL链接状态和状态);
if(status==GL_TRUE)返回;

GLchar log[1“将前向兼容性设置为true”是的,这是@genpfault好的,谢谢!知道为什么它没有显示三角形吗?