• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //========================================================================
2 // Simple GLFW example
3 // Copyright (c) Camilla Berglund <elmindreda@glfw.org>
4 //
5 // This software is provided 'as-is', without any express or implied
6 // warranty. In no event will the authors be held liable for any damages
7 // arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it
11 // freely, subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented; you must not
14 //    claim that you wrote the original software. If you use this software
15 //    in a product, an acknowledgment in the product documentation would
16 //    be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such, and must not
19 //    be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source
22 //    distribution.
23 //
24 //========================================================================
25 //! [code]
26 
27 #include <glad/glad.h>
28 #include <GLFW/glfw3.h>
29 
30 #include "linmath.h"
31 
32 #include <stdlib.h>
33 #include <stdio.h>
34 
35 static const struct
36 {
37     float x, y;
38     float r, g, b;
39 } vertices[3] =
40 {
41     { -0.6f, -0.4f, 1.f, 0.f, 0.f },
42     {  0.6f, -0.4f, 0.f, 1.f, 0.f },
43     {   0.f,  0.6f, 0.f, 0.f, 1.f }
44 };
45 
46 static const char* vertex_shader_text =
47 "uniform mat4 MVP;\n"
48 "attribute vec3 vCol;\n"
49 "attribute vec2 vPos;\n"
50 "varying vec3 color;\n"
51 "void main()\n"
52 "{\n"
53 "    gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
54 "    color = vCol;\n"
55 "}\n";
56 
57 static const char* fragment_shader_text =
58 "varying vec3 color;\n"
59 "void main()\n"
60 "{\n"
61 "    gl_FragColor = vec4(color, 1.0);\n"
62 "}\n";
63 
error_callback(int error,const char * description)64 static void error_callback(int error, const char* description)
65 {
66     fprintf(stderr, "Error: %s\n", description);
67 }
68 
key_callback(GLFWwindow * window,int key,int scancode,int action,int mods)69 static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
70 {
71     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
72         glfwSetWindowShouldClose(window, GLFW_TRUE);
73 }
74 
main(void)75 int main(void)
76 {
77     GLFWwindow* window;
78     GLuint vertex_buffer, vertex_shader, fragment_shader, program;
79     GLint mvp_location, vpos_location, vcol_location;
80 
81     glfwSetErrorCallback(error_callback);
82 
83     if (!glfwInit())
84         exit(EXIT_FAILURE);
85 
86     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
87     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
88 
89     window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
90     if (!window)
91     {
92         glfwTerminate();
93         exit(EXIT_FAILURE);
94     }
95 
96     glfwSetKeyCallback(window, key_callback);
97 
98     glfwMakeContextCurrent(window);
99     gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
100     glfwSwapInterval(1);
101 
102     // NOTE: OpenGL error checks have been omitted for brevity
103 
104     glGenBuffers(1, &vertex_buffer);
105     glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
106     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
107 
108     vertex_shader = glCreateShader(GL_VERTEX_SHADER);
109     glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
110     glCompileShader(vertex_shader);
111 
112     fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
113     glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
114     glCompileShader(fragment_shader);
115 
116     program = glCreateProgram();
117     glAttachShader(program, vertex_shader);
118     glAttachShader(program, fragment_shader);
119     glLinkProgram(program);
120 
121     mvp_location = glGetUniformLocation(program, "MVP");
122     vpos_location = glGetAttribLocation(program, "vPos");
123     vcol_location = glGetAttribLocation(program, "vCol");
124 
125     glEnableVertexAttribArray(vpos_location);
126     glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
127                           sizeof(float) * 5, (void*) 0);
128     glEnableVertexAttribArray(vcol_location);
129     glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE,
130                           sizeof(float) * 5, (void*) (sizeof(float) * 2));
131 
132     while (!glfwWindowShouldClose(window))
133     {
134         float ratio;
135         int width, height;
136         mat4x4 m, p, mvp;
137 
138         glfwGetFramebufferSize(window, &width, &height);
139         ratio = width / (float) height;
140 
141         glViewport(0, 0, width, height);
142         glClear(GL_COLOR_BUFFER_BIT);
143 
144         mat4x4_identity(m);
145         mat4x4_rotate_Z(m, m, (float) glfwGetTime());
146         mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 1.f, -1.f);
147         mat4x4_mul(mvp, p, m);
148 
149         glUseProgram(program);
150         glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
151         glDrawArrays(GL_TRIANGLES, 0, 3);
152 
153         glfwSwapBuffers(window);
154         glfwPollEvents();
155     }
156 
157     glfwDestroyWindow(window);
158 
159     glfwTerminate();
160     exit(EXIT_SUCCESS);
161 }
162 
163 //! [code]
164