【发布时间】:2021-08-08 14:58:20
【问题描述】:
帮我解决下一个问题:
-
我们有下一个文件序列:
Four_Triangles01.cpp
#include <iostream> // GLEW #define GLEW_STATIC #include <GL/glew.h> // GLFW #include <GLFW/glfw3.h> const GLint WIDTH = 800, HEIGHT = 600; // Shaders // for line width: "gl_LineWidth = 1.5;\n" const GLchar* vertexShaderSource = "#version 330 core\n" "layout (location = 0) in vec3 position;\n" "void main()\n" "{\n" "gl_Position = vec4(position.x, position.y, position.z, 1.0);\n" "gl_PointSize = 50.0;\n" "}\0"; const GLchar* fragmentShaderSource = "#version 330 core\n" "out vec4 color;\n" "void main()\n" "{\n" "color = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n" "}\n\0"; GLFWwindow* init_window () { // Init GLFW glfwInit( ); // Set all the required options for GLFW glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 ); glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3 ); glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE ); glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE ); glfwWindowHint( GLFW_RESIZABLE, GL_FALSE ); // Create a GLFWwindow object that we can use for GLFW's functions GLFWwindow *window = glfwCreateWindow( WIDTH, HEIGHT, "Four Triangles", nullptr, nullptr ); int screenWidth, screenHeight; glfwGetFramebufferSize( window, &screenWidth, &screenHeight ); if ( nullptr == window ) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate( ); } glfwMakeContextCurrent( window ); // Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions glewExperimental = GL_TRUE; // Initialize GLEW to setup the OpenGL Function pointers if ( GLEW_OK != glewInit( ) ) { std::cout << "Failed to initialize GLEW" << std::endl; } // Define the viewport dimensions glViewport( 0, 0, screenWidth, screenHeight ); return window; } // The MAIN function, from here we start the application and run the game loop int main() { GLFWwindow* window = init_window(); glEnable(GL_PROGRAM_POINT_SIZE); glEnable(GL_LINE_SMOOTH); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // Build and compile our shader program // Vertex shader GLuint vertexShader = glCreateShader( GL_VERTEX_SHADER ); glShaderSource( vertexShader, 1, &vertexShaderSource, NULL ); glCompileShader( vertexShader ); // Check for compile time errors GLint success; GLchar infoLog[512]; glGetShaderiv( vertexShader, GL_COMPILE_STATUS, &success ); if ( !success ) { glGetShaderInfoLog( vertexShader, 512, NULL, infoLog ); std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl; } // Fragment shader GLuint fragmentShader = glCreateShader( GL_FRAGMENT_SHADER ); glShaderSource( fragmentShader, 1, &fragmentShaderSource, NULL ); glCompileShader( fragmentShader ); // Check for compile time errors glGetShaderiv( fragmentShader, GL_COMPILE_STATUS, &success ); if ( !success ) { glGetShaderInfoLog( fragmentShader, 512, NULL, infoLog ); std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl; } // Link shaders GLuint shaderProgram = glCreateProgram( ); glAttachShader( shaderProgram, vertexShader ); glAttachShader( shaderProgram, fragmentShader ); glLinkProgram( shaderProgram ); // Check for linking errors glGetProgramiv( shaderProgram, GL_LINK_STATUS, &success ); if ( !success ) { glGetProgramInfoLog( shaderProgram, 512, NULL, infoLog ); std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl; } glDeleteShader( vertexShader ); glDeleteShader( fragmentShader ); float lineWidth[2]; glGetFloatv(GL_LINE_WIDTH_RANGE, lineWidth); // Set up vertex data (and buffer(s)) and attribute pointers GLfloat vertices[] = { 0.0, 0.0, 0.5, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 0.5, -0.5, 0.5, 0.0, 0.0, -0.5, 0.0, -0.5, -0.5, 0.0, 0.0, 0.0, -0.5, 0.5, -0.5, }; GLuint VBO, VAO; glGenVertexArrays( 1, &VAO ); glGenBuffers( 1, &VBO ); // Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s). glBindVertexArray( VAO ); glBindBuffer( GL_ARRAY_BUFFER, VBO ); glBufferData( GL_ARRAY_BUFFER, sizeof( vertices ), vertices, GL_STATIC_DRAW ); GLint position_attribute = glGetAttribLocation(shaderProgram, "position"); glVertexAttribPointer(position_attribute, 2, GL_FLOAT, GL_FALSE, 0, 0); //glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof( GLfloat ), ( GLvoid * ) 0 ); glEnableVertexAttribArray(position_attribute ); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glBindBuffer( GL_ARRAY_BUFFER, 0 ); // Note that this is allowed, the call to glVertexAttribPointer registered VBO as the currently bound vertex buffer object so afterwards we can safely unbind glBindVertexArray( 0 ); // Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs) //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // Game loop while ( !glfwWindowShouldClose( window ) ) { // Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions glfwPollEvents( ); // Render // Clear the colorbuffer glClearColor( 0.2f, 0.3f, 0.3f, 1.0f ); glClear( GL_COLOR_BUFFER_BIT ); // Draw our first triangle glUseProgram( shaderProgram ); glBindVertexArray( VAO ); glDrawArrays( GL_TRIANGLES, 0, 12); glBindVertexArray( 0 ); // Swap the screen buffers glfwSwapBuffers( window ); } // Properly de-allocate all resources once they've outlived their purpose glDeleteVertexArrays( 1, &VAO ); glDeleteBuffers( 1, &VBO ); // Terminate GLFW, clearing any resources allocated by GLFW. glfwTerminate( ); return EXIT_SUCCESS; }task.json
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "type": "shell", "label": "clang++ build active file", "command": "/usr/bin/clang++", "args": [ "-std=c++17", "-stdlib=libc++", "-g", "${file}", "-I/Users/Armonicus/MyProjects/C++VSCodeProjects/projects/helloworld01/include", "/usr/local/Cellar/glfw/3.3.3/lib/libglfw.3.3.dylib", "/usr/local/Cellar/glew/2.2.0_1/lib/libGLEW.2.2.0.dylib", "-o", "${fileDirname}/src/${fileBasenameNoExtension}", "-Wno-deprecated", "-Wno-pragma-once-outside-header" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": ["$gcc"], "group": { "kind": "build", "isDefault": true } } ] }c_cpp_properties.json
{ "configurations": [ { "name": "Mac", "includePath": [ "${workspaceFolder}/**", "${workspaceFolder}/include" ], "defines": [], "macFrameworkPath": [ "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks" ], "compilerPath": "/usr/bin/clang++", "intelliSenseMode": "macos-gcc-x64", "configurationProvider": "go2sh.cmake-integration" } ], "version": 4 } -
这些是我在编译时收到的错误:
所以,我寻求帮助来解决这个问题,并最终能够直接从 VSCode IDE 在 MacO 上运行用 C++ 编写的 OpenGL 应用程序。
【问题讨论】:
-
为什么不使用 Xcode?span>
-
在 Xcode 上完美运行。但我喜欢打开 VSCode,因为智能感知性能更高。
标签: c++ macos visual-studio-code opengl glfw