-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
122 lines (97 loc) · 3.38 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <glad/gl.h>
#include <GLFW/glfw3.h>
#include "renderer.h"
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window,int width,int height);
void processInput(GLFWwindow *window);
void error_callback(int errorcode,const char* description);
void GLAPIENTRY
MessageCallback( GLenum source,
GLenum type,
GLuint id,
GLenum severity,
GLsizei length,
const GLchar* message,
const void* userParam );
int main(void)
{
GLFWwindow* window;
glfwSetErrorCallback(error_callback);
/* Initialize the library */
if (!glfwInit())
return -1;
// 设置窗口属性
// glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(1920, 1080, "LearnOpenGLES", NULL, NULL);
if (!window)
{
const char* error_message;
int errorcode = glfwGetError(&error_message);
std::cout << "Error Exit " << error_message << " error code : " << errorcode << std::endl;
glfwTerminate();
return -1;
}
// 设置framebuffer callback
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
/* Make the window's context current */
glfwMakeContextCurrent(window);
// glad: load all OpenGL function pointers
int version = gladLoadGL(glfwGetProcAddress);
printf("GL %d.%d\n", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version));
// // 设置GL 错误信息捕获
// glEnable ( GL_DEBUG_OUTPUT );
// glDebugMessageCallback( MessageCallback, 0 );
glClearColor( 0.4f, 0.3f, 0.4f, 0.0f );
Renderer* renderer = new Renderer();
renderer->init();
// uncomment this call to draw in wireframe polygons.
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
// handle input
processInput(window);
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
renderer->render();
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
delete renderer;
renderer = NULL;
glfwTerminate();
return 0;
}
void framebuffer_size_callback(GLFWwindow* window,int width,int height)
{
glViewport(0,0,width,height);
}
void processInput(GLFWwindow *window)
{
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
void error_callback(int errorcode,const char* description)
{
fprintf(stderr, "errorcode %d , description: %s\n", errorcode, description);
}
void GLAPIENTRY
MessageCallback( GLenum source,
GLenum type,
GLuint id,
GLenum severity,
GLsizei length,
const GLchar* message,
const void* userParam )
{
fprintf( stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
( type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : "" ),
type, severity, message );
}