一起来学OpenGL-2.Getting started-3.Hello Window

Hello Window

GLFW API:http://www.glfw.org/docs/3.3/group__init.html
英文:
https://learnopengl.com/Getting-started/Hello-Window
中文:
https://learnopengl-cn.github.io/01%20Getting%20started/03%20Hello%20Window/

时间空闲才搞这个= =;
画窗口的流程就是:
初始化——创建窗口——初始化OpenGL——设置OpenGL状态——绘制窗口——结束绘制

注意事项
  1. 头文件glad.h一定要在glfw3..之前包含。
相关API详解
glfwInit
int glfwInit (void):

http://www.glfw.org/docs/latest/group__init.html#ga317aac130a235ab08c6db0834907d85e

This function initializes the GLFW library. Before most GLFW functions can be used, GLFW must be initialized, and before an application terminates GLFW should be terminated in order to free any resources allocated during or after initialization.
If this function fails, it calls glfwTerminate before returning. If it succeeds, you should call glfwTerminate before the application exits.
Additional calls to this function after successful initialization but before termination will return GLFW_TRUE immediately.
Returns
GLFW_TRUE if successful, or GLFW_FALSE if an error occurred.
Errors
Possible errors include GLFW_PLATFORM_ERROR.
Remarks
OS X: This function will change the current directory of the application to the Contents/Resources subdirectory of the application’s bundle, if present. This can be disabled with a compile-time option.
Thread safety
This function must only be called from the main thread.
See also
Initialization and termination
glfwTerminate
Since
Added in version 1.0.

此方法主要就是初始化GLFW库,使用GLFW库函数之前必须是已经初始化。会返回一个int值查看是否成功:

    int success= glfwInit();
    std::cout << success <<std::endl;
glfwWindowHint
void glfwWindowHint (int hint,int value )

This function sets hints for the next call to glfwCreateWindow. The hints, once set, retain their values until changed by a call to glfwWindowHint or glfwDefaultWindowHints, or until the library is terminated.
This function does not check whether the specified hint values are valid. If you set hints to invalid values this will instead be reported by the next call to glfwCreateWindow.
Parameters
[in] hint The window hint to set.
[in] value The new value of the window hint.
Errors
Possible errors include GLFW_NOT_INITIALIZED and GLFW_INVALID_ENUM.
Thread safety
This function must only be called from the main thread.
See also
Window creation hints
glfwDefaultWindowHints
Since
Added in version 3.0. Replaces glfwOpenWindowHint.
详见API 没啥可说的。
在代码里的三句指定版本和模式。
OpenGL3.3是Core-profile不是Immediate mode;
下面三句说明CONTEXT版本为3.3,模式为CORE_PROFILE。
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

glfwCreateWindow
GLFWwindow* glfwCreateWindow (int width,int height,const char * title,GLFWmonitor * monitor,GLFWwindow * share )

This function creates a window and its associated OpenGL or OpenGL ES context. Most of the options controlling how the window and its context should be created are specified with window hints.
Successful creation does not change which context is current. Before you can use the newly created context, you need to make it current. For information about the share parameter, see Context object sharing.
The created window, framebuffer and context may differ from what you requested, as not all parameters and hints are hard constraints. This includes the size of the window, especially for full screen windows. To query the actual attributes of the created window, framebuffer and context, see glfwGetWindowAttrib, glfwGetWindowSize and glfwGetFramebufferSize.
To create a full screen window, you need to specify the monitor the window will cover. If no monitor is specified, the window will be windowed mode. Unless you have a way for the user to choose a specific monitor, it is recommended that you pick the primary monitor. For more information on how to query connected monitors, see Retrieving monitors.
For full screen windows, the specified size becomes the resolution of the window’s desired video mode. As long as a full screen window is not iconified, the supported video mode most closely matching the desired video mode is set for the specified monitor. For more information about full screen windows, including the creation of so called windowed full screen or borderless full screen windows, see “Windowed full screen” windows.
Once you have created the window, you can switch it between windowed and full screen mode with glfwSetWindowMonitor. If the window has an OpenGL or OpenGL ES context, it will be unaffected.
By default, newly created windows use the placement recommended by the window system. To create the window at a specific position, make it initially invisible using the GLFW_VISIBLE window hint, set its position and then show it.
As long as at least one full screen window is not iconified, the screensaver is prohibited from starting.
Window systems put limits on window sizes. Very large or very small window dimensions may be overridden by the window system on creation. Check the actual size after creation.
The swap interval is not set during window creation and the initial value may vary depending on driver settings and defaults.
Parameters
[in] width The desired width, in screen coordinates, of the window. This must be greater than zero.
[in] height The desired height, in screen coordinates, of the window. This must be greater than zero.
[in] title The initial, UTF-8 encoded window title.
[in] monitor The monitor to use for full screen mode, or NULL for windowed mode.
[in] share The window whose context to share resources with, or NULL to not share resources.
Returns
The handle of the created window, or NULL if an error occurred.
Errors
Possible errors include GLFW_NOT_INITIALIZED, GLFW_INVALID_ENUM, GLFW_INVALID_VALUE, GLFW_API_UNAVAILABLE, GLFW_VERSION_UNAVAILABLE, GLFW_FORMAT_UNAVAILABLE and GLFW_PLATFORM_ERROR.
Remarks
Windows: Window creation will fail if the Microsoft GDI software OpenGL implementation is the only one available.
Windows: If the executable has an icon resource named GLFW_ICON, it will be set as the initial icon for the window. If no such icon is present, the IDI_WINLOGO icon will be used instead. To set a different icon, see glfwSetWindowIcon.
Windows: The context to share resources with must not be current on any other thread.
OS X: The GLFW window has no icon, as it is not a document window, but the dock icon will be the same as the application bundle’s icon. For more information on bundles, see the Bundle Programming Guide in the Mac Developer Library.
OS X: The first time a window is created the menu bar is populated with common commands like Hide, Quit and About. The About entry opens a minimal about dialog with information from the application’s bundle. The menu bar can be disabled with a compile-time option.
OS X: On OS X 10.10 and later the window frame will not be rendered at full resolution on Retina displays unless the NSHighResolutionCapable key is enabled in the application bundle’s Info.plist. For more information, see High Resolution Guidelines for OS X in the Mac Developer Library. The GLFW test and example programs use a custom Info.plist template for this, which can be found as CMake/MacOSXBundleInfo.plist.in in the source tree.
X11: Some window managers will not respect the placement of initially hidden windows.
X11: Due to the asynchronous nature of X11, it may take a moment for a window to reach its requested state. This means you may not be able to query the final size, position or other attributes directly after window creation.
Reentrancy
This function must not be called from a callback.
Thread safety
This function must only be called from the main thread.
See also
Window creation
glfwDestroyWindow
Since
Added in version 3.0. Replaces glfwOpenWindow.
目前只需要知道此API会创建一个窗口,和对应的参数列表意义。

glClearColor
void glClearColor( GLfloat red,GLfloat green,GLfloat blue,GLfloat alpha);

glClearColor specifies the red, green, blue, and alpha values used by glClear to clear the color buffers. Values specified by glClearColor are clamped to the range [0,1].
glClearColor只起到Set的作用,并不Clear任何,指定刷新颜色缓冲区时所用的颜色。与glClear配对使用。glClearColor 仅仅是设置 glClear 的时候的颜色值,本身不会进行任何 clear 操作。

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

#include <iostream>

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);

// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

int main()
{
    // glfw: initialize and configure
    // ------------------------------
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

#ifdef __APPLE__
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
#endif

                                                         // glfw window creation
                                                         // --------------------
    GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
    if (window == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    // glad: load all OpenGL function pointers
    // ---------------------------------------
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }

    // render loop
    // -----------
    while (!glfwWindowShouldClose(window))
    {
        // input
        // -----
        processInput(window);

        // render
        // ------
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
        // -------------------------------------------------------------------------------
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    // glfw: terminate, clearing all previously allocated GLFW resources.
    // ------------------------------------------------------------------
    glfwTerminate();
    return 0;
}

// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
}

// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
    // make sure the viewport matches the new window dimensions; note that width and 
    // height will be significantly larger than specified on retina displays.
    glViewport(0, 0, width, height);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值