首页 > 其他分享 >OpenGL环境配置 和 测试代码(GLEW/GLFW VS2019)

OpenGL环境配置 和 测试代码(GLEW/GLFW VS2019)

时间:2023-07-02 11:56:11浏览次数:78  
标签:VS2019 int OpenGL vertexShader window version GLFW GLEW

1.配置

 

2.测试代码:

2.1 在黑板色的窗口上画一个三角形

  1 #include <iostream>
  2 
  3 // GLEW
  4 #define GLEW_STATIC
  5 #include <GL/glew.h>
  6 
  7 // GLFW
  8 #include <GLFW/glfw3.h>
  9 
 10 
 11 // Function prototypes
 12 void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
 13 
 14 // Window dimensions
 15 const GLuint WIDTH = 800, HEIGHT = 600;
 16 
 17 // Shaders
 18 const GLchar* vertexShaderSource = "#version 330 core\n"
 19 "layout (location = 0) in vec3 position;\n"
 20 "void main()\n"
 21 "{\n"
 22 "gl_Position = vec4(position.x, position.y, position.z, 1.0);\n"
 23 "}\0";
 24 const GLchar* fragmentShaderSource = "#version 330 core\n"
 25 "out vec4 color;\n"
 26 "void main()\n"
 27 "{\n"
 28 "color = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
 29 "}\n\0";
 30 
 31 // The MAIN function, from here we start the application and run the game loop
 32 int main()
 33 {
 34     // Init GLFW
 35     glfwInit();
 36     // Set all the required options for GLFW
 37     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
 38     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
 39     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
 40     glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
 41 
 42     // Create a GLFWwindow object that we can use for GLFW's functions
 43     GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr);
 44     glfwMakeContextCurrent(window);
 45 
 46     // Set the required callback functions
 47     glfwSetKeyCallback(window, key_callback);
 48 
 49     // Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
 50     glewExperimental = GL_TRUE;
 51     // Initialize GLEW to setup the OpenGL Function pointers
 52     glewInit();
 53 
 54     // Define the viewport dimensions
 55     int width, height;
 56     glfwGetFramebufferSize(window, &width, &height);
 57     glViewport(0, 0, width, height);
 58 
 59 
 60     // Build and compile our shader program
 61     // Vertex shader
 62     GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
 63     glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
 64     glCompileShader(vertexShader);
 65     // Check for compile time errors
 66     GLint success;
 67     GLchar infoLog[512];
 68     glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
 69     if (!success)
 70     {
 71         glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
 72         std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
 73     }
 74     // Fragment shader
 75     GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
 76     glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
 77     glCompileShader(fragmentShader);
 78     // Check for compile time errors
 79     glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
 80     if (!success)
 81     {
 82         glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
 83         std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
 84     }
 85     // Link shaders
 86     GLuint shaderProgram = glCreateProgram();
 87     glAttachShader(shaderProgram, vertexShader);
 88     glAttachShader(shaderProgram, fragmentShader);
 89     glLinkProgram(shaderProgram);
 90     // Check for linking errors
 91     glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
 92     if (!success) {
 93         glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
 94         std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
 95     }
 96     glDeleteShader(vertexShader);
 97     glDeleteShader(fragmentShader);
 98 
 99 
100     // Set up vertex data (and buffer(s)) and attribute pointers
101     GLfloat vertices[] = {
102         -0.5f, -0.5f, 0.0f, // Left  
103          0.5f, -0.5f, 0.0f, // Right 
104          0.0f,  0.5f, 0.0f  // Top   
105     };
106     GLuint VBO, VAO;
107     glGenVertexArrays(1, &VAO);
108     glGenBuffers(1, &VBO);
109     // Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
110     glBindVertexArray(VAO);
111 
112     glBindBuffer(GL_ARRAY_BUFFER, VBO);
113     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
114 
115     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
116     glEnableVertexAttribArray(0);
117 
118     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
119 
120     glBindVertexArray(0); // Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs)
121 
122     // Game loop
123     while (!glfwWindowShouldClose(window))
124     {
125         // Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
126         glfwPollEvents();
127 
128         // Render
129         // Clear the colorbuffer
130         glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
131         glClear(GL_COLOR_BUFFER_BIT);
132 
133         // Draw our first triangle
134         glUseProgram(shaderProgram);
135         glBindVertexArray(VAO);
136         glDrawArrays(GL_TRIANGLES, 0, 3);
137         glBindVertexArray(0);
138 
139         // Swap the screen buffers
140         glfwSwapBuffers(window);
141     }
142     // Properly de-allocate all resources once they've outlived their purpose
143     glDeleteVertexArrays(1, &VAO);
144     glDeleteBuffers(1, &VBO);
145     // Terminate GLFW, clearing any resources allocated by GLFW.
146     glfwTerminate();
147     return 0;
148 }
149 
150 // Is called whenever a key is pressed/released via GLFW
151 void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
152 {
153     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
154         glfwSetWindowShouldClose(window, GL_TRUE);
155 }
View Code

3.ps: glew32s.lib 和  glew32.dll 区别:

I haven't looked at any of the documentation, but I believe glew32s.lib is
the static version and glew32.lib is the dynamically linked DLL version --
the static version links the lib into the .exe, while the dynamic version
requires the glew32.dll at runtime. You can use #define GLEW_STATIC before
#include <GL/glew.h> to use the static version

标签:VS2019,int,OpenGL,vertexShader,window,version,GLFW,GLEW
From: https://www.cnblogs.com/sansuiwantong/p/17520549.html

相关文章

  • OpenGL不够用?为何还要开发Vulkan?
    相信不少朋友和我一样有这样的疑问既然已经有广泛应用的OpenGL图形接口,为什么Khronos还要花费精力重新开发一套Vulkan图形API接口呢?查询相关资料后,总结为一句话为了更高的性能、更低的驱动程序开销。对于许多图形开发者来说,OpenGL是一个非常熟悉和广泛使用的图形API接口。然而......
  • C#C++联合debug-vs2019
    转自:https://blog.csdn.net/qq_41375318/article/details/127717701vs2019中C#和c++的dll进行联合调试*Major*已于2022-11-0617:29:51修改462收藏2文章标签:c#版权vs2019中C#和c++的dll进行联合调试一C++这边属性的输出目录设置为C#的Debug目录二调试设为C#的exe路径......
  • 创建WinFrom项目,生成桌面exe安装包 【VS2019】
    一:添加WinFrom开发需要的工具  a)打开VS-->选择随意项目或继续无需代码-->工具-->获取工具和功能-->选中【.NET桌面开发】进行安装即可。二:创建WinFrom项目  a)打开VS-->创建新项目-->搜索窗体应用,选择Windows窗体应用(.NETFramework)如图所示-->修改项目名称、位置......
  • OpenGL 环境搭建(VS2019)
    1.GLFW下载:https://sourceforge.net/projects/glfw/官网:https://www.glfw.org/download.html(可能登录不了)下载后解压glfw-3.3.8.bin.WIN32.zip将include和lib-vc2019配置到VS的工程内需要的lib:openGL32.lib ---不要忘了glfw3.lib测试代码:#include<GLFW/glfw3.......
  • OpenGL 初识
    1.能干什么 2.各种库关系  ......
  • VS2019调用Matlab2019b生成的dll时初始化异常
    在VisualStudio中使用C++调用Matlab程序我目前在写一个用VS和Matlab混编的程序,由于之前的版本(VS2005+MATLAB2010b)太老了,现在想换用VS2019+MATLAB2019b的版本。我原本以为会很简单就能完成配置,没想到在运行时会出现下面的问题。(帧不在模块中。在加载的模块中未找到当前堆栈帧。无......
  • 【OpenGL修行】点线面模式设置
    文章目录点点的大小切换线线的宽窄设置虚线绘制多边形多边形正反绘制&反转多边形表面剔除多边形表面镂空通过初始化数组设置镂空样式通过读取bmp文件设置镂空样式点点的大小切换通过glPointSize()函数即可实现点的大小控制,我画了两个大小不同的点,还是挺明显的。voiddisplay1(){......
  • OpenGL ES 和 OpenGL 版本关系
    本文记录了OpenGL和OpenGLES的版本关系。 ES2.0不向后兼容(notbackwardcompatible),也就是不兼容老版本。ES2.0已经没有固定管线了。ES3.0兼容ES2.0。ES不支持Geometry和Tessellation着色器,但3.0支持feedback模式。ES不支持Antialiasedline. OpenGL版本......
  • QGIS3.22.0+VS2019 window10编译
    首先感谢博客 济南友泉软件有限公司提供的顺序教程。博客地址:https://blog.csdn.net/qq_26221775/article/details/122792445这篇博客主要是表示编译时遇到的坑。1.一定使用vs2019进行编译。我刚开始想使用vs2017编译。因此遇到了两个坑。(1)vs2017编译qgis_cor......
  • [OpenGL]环境搭建以及OpenGL初识
    想往游戏行业发展的话,经常被提及到的就是OpenGL和DirectX,这两者听起来感觉是一门挺高深的技术,今天我也开始摸索学习OpenGL,那么OpenGL到底是什么?它和DirectX有什么区别和联系?OpenGL初识OpenGL只是一套图形函数库DirectX包含图形、声音、输入、网络等模块。但就图形而论,DirectX的图形......