Add demo shader and spinning colored cube

This commit is contained in:
josh committed 2024-01-25 20:57:33 -05:00
1 parent 8e6ec5f820
commit 8dc6e97585
8 files changed
+149 -38

No files matched your search

+60
View File
@@ -3,10 +3,17 @@ import sdl;
import gl;
import glad.gl.loader;
import gltk;
import gl3n.linalg;
enum int WIDTH = 800;
enum int HEIGHT = 600;
alias ColorShader = Program!(["view": "Matrix4fv"], ["position": 0, "color": 1]);
ColorShader color_shader;
Array spinny_vao;
GLuint program_id;
mat4 view_matrix;
void init()
{
glActiveTexture(GL_TEXTURE0);
@@ -15,12 +22,65 @@ void init()
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glClearColor (1.0, 0.6, 0.0, 0.0);
glViewport(0, 0, WIDTH, HEIGHT);
spinny_vao = new Array();
spinny_vao.bind();
string color_program_src = `
#version 450
uniform mat4 view;
vertex:
in vec2 position;
in vec4 color;
out vec4 color_i;
void main(void)
{
gl_Position = view * vec4(position, 0.0, 1.0);
color_i = color;
}
fragment:
in vec4 color_i;
out vec4 frag_color;
void main(void)
{
frag_color = color_i;
}`;
color_shader = new ColorShader(color_program_src);
color_shader.use();
float[] vertices = [0.4, 0.4, -0.4, 0.4, -0.4, -0.4, 0.4, -0.4];
float[] colors = [
1.0, 0.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 0.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0];
ushort[] indices = [0, 1, 2, 3];
Buffer vbo = new Buffer(vertices);
vbo.bind();
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, null);
Buffer cbo = new Buffer(colors);
cbo.bind();
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, null);
ElementBuffer ibo = new ElementBuffer(indices);
ibo.bind();
}
void display(SDL_Window * window)
{
glClear(GL_COLOR_BUFFER_BIT);
color_shader.use();
spinny_vao.bind();
view_matrix.make_identity();
view_matrix.scale(HEIGHT / cast(float)WIDTH, 1.0, 1.0);
view_matrix.rotatez(SDL_GetTicks() / 500.0);
color_shader.set_view(view_matrix.value_ptr);
glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_SHORT, null);
SDL_GL_SwapWindow(window);
}