VS2019 community + glew+ glfw搭建OpenGL 开发环境

1 篇文章 0 订阅
1 篇文章 0 订阅

1.下载glew-2.2.0和glfw-3.3.2.bin.WIN64
2.创建vsOpenGL工程;
3.分别在debug和release模式下的属性页面配置环境;
“C/C++”->“常规”->“附加包含目录”输入
$(SolutionDir)glew-2.2.0\include
$(SolutionDir)glfw-3.3.2.bin.WIN64\include
“链接器”->“输入”,输入
glfw3.lib
glew32s.lib
opengl32.lib

4.测试代码如下:

#include<iostream>
// GLEW    
#define GLEW_STATIC    
#include <GL/glew.h>

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

// Function prototypes    
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);

// Window dimensions    
const GLuint WIDTH = 800, HEIGHT = 600;

// The MAIN function, from here we start the application and run the game loop    
int main()
{
    std::cout << "Starting GLFW context, OpenGL 3.3" << std::endl;
    // 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_RESIZABLE, GL_FALSE);

    // Create a GLFWwindow object that we can use for GLFW's functions    
    GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr);
    if (window == nullptr)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    // Set the required callback functions    
    glfwSetKeyCallback(window, key_callback);

    // 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 (glewInit() != GLEW_OK)
    {
        std::cout << "Failed to initialize GLEW" << std::endl;
        return -1;
    }

    // Define the viewport dimensions    
    glViewport(0, 0, WIDTH, HEIGHT);

    // 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);
        // Swap the screen buffers    
        glfwSwapBuffers(window);
    }
    // Terminate GLFW, clearing any resources allocated by GLFW.    
    glfwTerminate();
    return 0;
}
// Is called whenever a key is pressed/released via GLFW    
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
    std::cout << key << std::endl;
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值