forked from capnramses/antons_opengl_tutorials_book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
138 lines (119 loc) · 5.5 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/******************************************************************************\
| OpenGL 4 Example Code. |
| Accompanies written series "Anton's OpenGL 4 Tutorials" |
| Email: anton at antongerdelan dot net |
| First version 27 Jan 2014 |
| Dr Anton Gerdelan, Trinity College Dublin, Ireland. |
| See individual libraries separate legal notices |
|******************************************************************************|
| Phong Illumination |
| Apple: remember to uncomment version number hint in start_gl() |
\******************************************************************************/
#include "gl_utils.h"
#include "maths_funcs.h"
#include <GL/glew.h> // include GLEW and new version of GL on Windows
#include <GLFW/glfw3.h> // GLFW helper library
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define _USE_MATH_DEFINES
#include <math.h>
#define GL_LOG_FILE "gl.log"
// keep track of window size for things like the viewport and the mouse cursor
int g_gl_width = 640;
int g_gl_height = 480;
GLFWwindow* g_window = NULL;
int main() {
restart_gl_log();
// start GL context and O/S window using the GLFW helper library
start_gl();
// tell GL to only draw onto a pixel if the shape is closer to the viewer
glEnable( GL_DEPTH_TEST ); // enable depth-testing
glDepthFunc( GL_LESS ); // depth-testing interprets a smaller value as "closer"
/* OTHER STUFF GOES HERE NEXT */
GLfloat points[] = { 0.0f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, -0.5f, -0.5f, 0.0f };
float normals[] = {
0.0f,
0.0f,
1.0f,
0.0f,
0.0f,
1.0f,
0.0f,
0.0f,
1.0f,
};
GLuint points_vbo;
glGenBuffers( 1, &points_vbo );
glBindBuffer( GL_ARRAY_BUFFER, points_vbo );
glBufferData( GL_ARRAY_BUFFER, 9 * sizeof( GLfloat ), points, GL_STATIC_DRAW );
GLuint normals_vbo;
glGenBuffers( 1, &normals_vbo );
glBindBuffer( GL_ARRAY_BUFFER, normals_vbo );
glBufferData( GL_ARRAY_BUFFER, 9 * sizeof( GLfloat ), normals, GL_STATIC_DRAW );
GLuint vao;
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );
glBindBuffer( GL_ARRAY_BUFFER, points_vbo );
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, NULL );
glBindBuffer( GL_ARRAY_BUFFER, normals_vbo );
glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, 0, NULL );
glEnableVertexAttribArray( 0 );
glEnableVertexAttribArray( 1 );
GLuint shader_programme = create_programme_from_files( "test_vs.glsl", "test_fs.glsl" );
#define ONE_DEG_IN_RAD ( 2.0 * M_PI ) / 360.0 // 0.017444444
// input variables
float near = 0.1f; // clipping plane
float far = 100.0f; // clipping plane
float fov = 67.0f * ONE_DEG_IN_RAD; // convert 67 degrees to radians
float aspect = (float)g_gl_width / (float)g_gl_height; // aspect ratio
// matrix components
float inverse_range = 1.0f / tan( fov * 0.5f );
float Sx = inverse_range / aspect;
float Sy = inverse_range;
float Sz = -( far + near ) / ( far - near );
float Pz = -( 2.0f * far * near ) / ( far - near );
GLfloat proj_mat[] = { Sx, 0.0f, 0.0f, 0.0f, 0.0f, Sy, 0.0f, 0.0f, 0.0f, 0.0f, Sz, -1.0f, 0.0f, 0.0f, Pz, 0.0f };
/* create VIEW MATRIX */
float cam_pos[] = { 0.0f, 0.0f, 2.0f }; // don't start at zero, or we will be too close
float cam_yaw = 0.0f; // y-rotation in degrees
mat4 T = translate( identity_mat4(), vec3( -cam_pos[0], -cam_pos[1], -cam_pos[2] ) );
mat4 R = rotate_y_deg( identity_mat4(), -cam_yaw );
mat4 view_mat = R * T;
/* matrix for moving the triangle */
mat4 model_mat = identity_mat4();
glUseProgram( shader_programme );
int view_mat_location = glGetUniformLocation( shader_programme, "view_mat" );
glUniformMatrix4fv( view_mat_location, 1, GL_FALSE, view_mat.m );
int proj_mat_location = glGetUniformLocation( shader_programme, "projection_mat" );
glUniformMatrix4fv( proj_mat_location, 1, GL_FALSE, proj_mat );
int model_mat_location = glGetUniformLocation( shader_programme, "model_mat" );
glUniformMatrix4fv( model_mat_location, 1, GL_FALSE, model_mat.m );
glEnable( GL_CULL_FACE ); // cull face
glCullFace( GL_BACK ); // cull back face
glFrontFace( GL_CW ); // GL_CCW for counter clock-wise
while ( !glfwWindowShouldClose( g_window ) ) {
_update_fps_counter( g_window );
double current_seconds = glfwGetTime();
// wipe the drawing surface clear
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glViewport( 0, 0, g_gl_width, g_gl_height );
glUseProgram( shader_programme );
model_mat.m[12] = sinf( current_seconds );
glUniformMatrix4fv( model_mat_location, 1, GL_FALSE, model_mat.m );
glBindVertexArray( vao );
// draw points 0-3 from the currently bound VAO with current in-use shader
glDrawArrays( GL_TRIANGLES, 0, 3 );
// update other events like input handling
glfwPollEvents();
if ( GLFW_PRESS == glfwGetKey( g_window, GLFW_KEY_ESCAPE ) ) { glfwSetWindowShouldClose( g_window, 1 ); }
// put the stuff we've been drawing onto the display
glfwSwapBuffers( g_window );
}
// close GL context and any other GLFW resources
glfwTerminate();
return 0;
}