simple.c
Go to the documentation of this file.
1 //========================================================================
2 // Simple GLFW example
3 // Copyright (c) Camilla Löwy <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 //========================================================================
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 "#version 110\n"
48 "uniform mat4 MVP;\n"
49 "attribute vec3 vCol;\n"
50 "attribute vec2 vPos;\n"
51 "varying vec3 color;\n"
52 "void main()\n"
53 "{\n"
54 " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
55 " color = vCol;\n"
56 "}\n";
57 
58 static const char* fragment_shader_text =
59 "#version 110\n"
60 "varying vec3 color;\n"
61 "void main()\n"
62 "{\n"
63 " gl_FragColor = vec4(color, 1.0);\n"
64 "}\n";
65 
66 static void error_callback(int error, const char* description)
67 {
68  fprintf(stderr, "Error: %s\n", description);
69 }
70 
71 static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
72 {
73  if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
75 }
76 
77 int main(void)
78 {
80  GLuint vertex_buffer, vertex_shader, fragment_shader, program;
81  GLint mvp_location, vpos_location, vcol_location;
82 
84 
85  if (!glfwInit())
86  exit(EXIT_FAILURE);
87 
90 
91  window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
92  if (!window)
93  {
94  glfwTerminate();
95  exit(EXIT_FAILURE);
96  }
97 
99 
100  glfwMakeContextCurrent(window);
102  glfwSwapInterval(1);
103 
104  // NOTE: OpenGL error checks have been omitted for brevity
105 
106  glGenBuffers(1, &vertex_buffer);
107  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
109 
110  vertex_shader = glCreateShader(GL_VERTEX_SHADER);
111  glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
112  glCompileShader(vertex_shader);
113 
114  fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
115  glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
116  glCompileShader(fragment_shader);
117 
118  program = glCreateProgram();
119  glAttachShader(program, vertex_shader);
120  glAttachShader(program, fragment_shader);
121  glLinkProgram(program);
122 
123  mvp_location = glGetUniformLocation(program, "MVP");
124  vpos_location = glGetAttribLocation(program, "vPos");
125  vcol_location = glGetAttribLocation(program, "vCol");
126 
127  glEnableVertexAttribArray(vpos_location);
128  glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
129  sizeof(vertices[0]), (void*) 0);
130  glEnableVertexAttribArray(vcol_location);
131  glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE,
132  sizeof(vertices[0]), (void*) (sizeof(float) * 2));
133 
134  while (!glfwWindowShouldClose(window))
135  {
136  float ratio;
137  int width, height;
138  mat4x4 m, p, mvp;
139 
140  glfwGetFramebufferSize(window, &width, &height);
141  ratio = width / (float) height;
142 
143  glViewport(0, 0, width, height);
145 
146  mat4x4_identity(m);
147  mat4x4_rotate_Z(m, m, (float) glfwGetTime());
148  mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 1.f, -1.f);
149  mat4x4_mul(mvp, p, m);
150 
151  glUseProgram(program);
152  glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
153  glDrawArrays(GL_TRIANGLES, 0, 3);
154 
155  glfwSwapBuffers(window);
156  glfwPollEvents();
157  }
158 
159  glfwDestroyWindow(window);
160 
161  glfwTerminate();
162  exit(EXIT_SUCCESS);
163 }
164 
#define glBufferData
#define glCreateProgram
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
Definition: simple.c:71
#define GL_VERTEX_SHADER
vec4 mat4x4[4]
Definition: linmath.h:83
#define GL_STATIC_DRAW
khronos_float_t GLfloat
The header of the GLFW 3 API.
GLFWAPI GLFWglproc glfwGetProcAddress(const char *procname)
Returns the address of the specified function for the current context.
Definition: context.c:741
static void mat4x4_ortho(mat4x4 M, float l, float r, float b, float t, float n, float f)
Definition: linmath.h:341
#define glGetAttribLocation
static void mat4x4_identity(mat4x4 M)
Definition: linmath.h:84
GLfloat GLfloat p
Definition: glext.h:12687
const GLfloat * m
Definition: glext.h:6814
void *(* GLADloadproc)(const char *name)
static GLFWwindow * window
Definition: joysticks.c:55
#define GLFW_CONTEXT_VERSION_MAJOR
Context client API major version hint and attribute.
Definition: glfw3.h:921
#define glDrawArrays
GLFWAPI GLFWerrorfun glfwSetErrorCallback(GLFWerrorfun cbfun)
Sets the error callback.
Definition: init.c:309
#define glUniformMatrix4fv
static void mat4x4_rotate_Z(mat4x4 Q, mat4x4 M, float angle)
Definition: linmath.h:247
float g
Definition: simple.c:38
static const char * vertex_shader_text
Definition: simple.c:46
#define glUseProgram
static const struct @20 vertices[3]
[code]
#define glCreateShader
GLuint64 key
Definition: glext.h:8966
GLFWAPI int glfwInit(void)
Initializes the GLFW library.
Definition: init.c:198
GLdouble f
#define glVertexAttribPointer
static void error_callback(int error, const char *description)
Definition: simple.c:66
#define GL_FLOAT
#define glEnableVertexAttribArray
#define GL_COLOR_BUFFER_BIT
GLbitfield GLuint program
Definition: glext.h:1889
int GLint
GLFWAPI void glfwSwapInterval(int interval)
Sets the swap interval for the current context.
Definition: context.c:658
#define glBindBuffer
#define GLFW_KEY_ESCAPE
Definition: glfw3.h:412
#define glClear
GLint GLsizei GLsizei height
GLFWAPI void glfwSwapBuffers(GLFWwindow *window)
Swaps the front and back buffers of the specified window.
Definition: context.c:641
GLFWAPI void glfwMakeContextCurrent(GLFWwindow *window)
Makes the context of the specified window current for the calling thread.
Definition: context.c:611
float x
Definition: simple.c:37
float y
Definition: simple.c:37
#define glGetUniformLocation
#define GL_FRAGMENT_SHADER
#define glShaderSource
action
Definition: enums.py:62
#define glViewport
GLAPI int gladLoadGLLoader(GLADloadproc)
Definition: glad/glad.c:1697
static const textual_icon exit
Definition: model-views.h:254
#define glAttachShader
GLFWAPI void glfwSetWindowShouldClose(GLFWwindow *window, int value)
Sets the close flag of the specified window.
Definition: window.c:486
#define glCompileShader
GLFWAPI GLFWwindow * glfwCreateWindow(int width, int height, const char *title, GLFWmonitor *monitor, GLFWwindow *share)
Creates a window and its associated context.
Definition: window.c:151
#define GLFW_TRUE
One.
Definition: glfw3.h:279
int main(void)
Definition: simple.c:77
#define GL_FALSE
float r
Definition: simple.c:38
GLFWAPI double glfwGetTime(void)
Returns the value of the GLFW timer.
Definition: input.c:1275
unsigned int GLuint
#define glGenBuffers
GLFWAPI void glfwDestroyWindow(GLFWwindow *window)
Destroys the specified window and its context.
Definition: window.c:444
GLFWAPI void glfwGetFramebufferSize(GLFWwindow *window, int *width, int *height)
Retrieves the size of the framebuffer of the specified window.
Definition: window.c:647
#define glLinkProgram
GLFWAPI void glfwTerminate(void)
Terminates the GLFW library.
Definition: init.c:243
float b
Definition: simple.c:38
GLFWAPI GLFWkeyfun glfwSetKeyCallback(GLFWwindow *window, GLFWkeyfun cbfun)
Sets the key callback.
Definition: input.c:791
GLFWAPI void glfwPollEvents(void)
Processes all pending events.
Definition: window.c:1072
#define NULL
Definition: tinycthread.c:47
static void mat4x4_mul(mat4x4 M, mat4x4 a, mat4x4 b)
Definition: linmath.h:145
#define GLFW_CONTEXT_VERSION_MINOR
Context client API minor version hint and attribute.
Definition: glfw3.h:927
#define GLFW_PRESS
The key or mouse button was pressed.
Definition: glfw3.h:304
#define GL_ARRAY_BUFFER
struct GLFWwindow GLFWwindow
GLFWAPI void glfwWindowHint(int hint, int value)
Sets the specified window hint to the desired value.
Definition: window.c:291
GLint GLsizei width
static const char * fragment_shader_text
Definition: simple.c:58
#define GL_TRIANGLES
GLFWAPI int glfwWindowShouldClose(GLFWwindow *window)
Checks the close flag of the specified window.
Definition: window.c:477


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:41