src/primitives.c

Sun, 11 Aug 2024 16:29:09 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 11 Aug 2024 16:29:09 +0200
changeset 70
81a089a47eff
parent 65
9c44c55d327a
permissions
-rw-r--r--

redesign FPS counter

3
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 * Copyright 2023 Mike Becker. All rights reserved.
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 *
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 * Redistribution and use in source and binary forms, with or without
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * modification, are permitted provided that the following conditions are met:
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 *
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 * 1. Redistributions of source code must retain the above copyright
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * notice, this list of conditions and the following disclaimer.
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 *
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 * 2. Redistributions in binary form must reproduce the above copyright
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * notice, this list of conditions and the following disclaimer in the
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * documentation and/or other materials provided with the distribution.
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 *
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * POSSIBILITY OF SUCH DAMAGE.
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 */
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
28 #include "ascension/primitives.h"
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
29 #include "ascension/error.h"
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
30 #include "ascension/context.h"
3
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
32 #include <string.h>
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
33 #include <GL/glew.h>
4
b7acda6a4476 add simple color data types
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
34
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
35 static void asc_primitives_init_plane(AscMesh *mesh) {
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
36 asc_dprintf("Create primitive plane in VBO %u and VAO %u", mesh->vbo, mesh->vao);
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
37 mesh->vertices = 4;
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
38 float data[8] = {
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
39 0.0f, 0.0f, // bottom left
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
40 0.0f, 1.0f, // top left
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
41 1.0f, 0.0f, // bottom right
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
42 1.0f, 1.0f // top right
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
43 };
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
44 glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo);
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
45 glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
46 glBindVertexArray(mesh->vao);
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
47 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
48 glEnableVertexAttribArray(0);
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
49 }
3
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
51 bool asc_primitives_init(AscPrimitives *primitives) {
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
52 asc_dprintf("Create primitives for the GL context of active window.");
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
53 // TODO: more primitives
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
54
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
55 GLuint buffers[1];
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
56 GLuint arrays[1];
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
57 glGenBuffers(1, buffers);
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
58 glGenVertexArrays(1, arrays);
4
b7acda6a4476 add simple color data types
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
59
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
60 AscMesh *plane = &(primitives->plane);
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
61 plane->vbo = buffers[0];
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
62 plane->vao = arrays[0];
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
63 asc_primitives_init_plane(plane);
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
64
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
65 GLenum error = glGetError();
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
66 if (error == GL_NO_ERROR) {
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
67 return true;
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
68 } else {
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
69 asc_error_gl(error, CX_STR("Initialization of primitive meshes failed."));
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
70 return false;
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
71 }
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
72 }
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
73
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
74 void asc_primitives_destroy(AscPrimitives *primitives) {
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
75 asc_dprintf("Destroy primitives in GL context of active window.");
11
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
76
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
77 GLuint buffers[1];
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
78 GLuint arrays[1];
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
79
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
80 buffers[0] = primitives->plane.vbo;
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
81 arrays[0] = primitives->plane.vao;
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
82
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
83 glDeleteBuffers(1, buffers);
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
84 glDeleteVertexArrays(1, arrays);
11
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
85
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
86 memset(primitives, 0, sizeof(AscPrimitives));
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
87 }
3
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
89 void asc_primitives_draw_plane(void) {
65
9c44c55d327a consistently refer to windows by ID - fixes #381
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
90 AscMesh const *mesh = &(asc_active_window->glctx.primitives.plane);
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
91 glBindVertexArray(mesh->vao);
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
92 glDrawArrays(GL_TRIANGLE_STRIP, 0, mesh->vertices);
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
93 }

mercurial