working first window with vertex+fragment
This commit is contained in:
+89
-2
@@ -5,8 +5,42 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "linmath.h"
|
||||
|
||||
#define GLAD_GL_IMPLEMENTATION
|
||||
#include <glad/gl.h>
|
||||
#define GLFW_INCLUDE_NONE
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
typedef struct Vertex {
|
||||
vec2 pos;
|
||||
} Vertex;
|
||||
|
||||
static const Vertex vertices[6] = {{{0.0f, 0.0f}}, {{0.0f, 1.0f}},
|
||||
{{1.0f, 1.0f}}, {{0.0f, 0.0f}},
|
||||
{{1.0f, 1.0f}}, {{1.0f, 0.0f}}};
|
||||
|
||||
static const char *vertex_shader_text =
|
||||
"#version 330\n"
|
||||
"uniform mat4 mvp;\n"
|
||||
"in vec2 vPos;\n"
|
||||
"out vec2 vUV;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" gl_Position = mvp * vec4(vPos, 0.0, 1.0);\n"
|
||||
" vUV = vPos;\n"
|
||||
"}\n";
|
||||
|
||||
static const char *fragment_shader_text =
|
||||
"#version 330\n"
|
||||
"uniform float iTime;\n"
|
||||
"in vec2 vUV;\n"
|
||||
"out vec4 fragColor;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" fragColor = vec4(vUV.x, vUV.y, abs(sin(iTime)), 1.0);\n"
|
||||
"}\n";
|
||||
|
||||
void error_callback(int error, const char *description) {
|
||||
fprintf(stderr, "Error %d: %s\n", error, description);
|
||||
}
|
||||
@@ -20,29 +54,82 @@ static void key_callback(GLFWwindow *window, int key, int scancode, int action,
|
||||
|
||||
void forge_run(parameters params) {
|
||||
GLFWwindow *window;
|
||||
|
||||
glfwSetErrorCallback(error_callback);
|
||||
|
||||
fprintf(stdout, "[GLFW] %s\n", glfwGetVersionString());
|
||||
|
||||
if (!glfwInit()) {
|
||||
fprintf(stderr, "[GLFW] Initialization failed\n");
|
||||
exit(1);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
|
||||
window = glfwCreateWindow(640, 480, PACKAGE " " VERSION, NULL, NULL);
|
||||
|
||||
if (!window) {
|
||||
fprintf(stderr, "[GLFW] Window or context creation failed\n");
|
||||
glfwTerminate();
|
||||
exit(1);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent(window);
|
||||
gladLoadGL(glfwGetProcAddress);
|
||||
glfwSetKeyCallback(window, key_callback);
|
||||
glfwSwapInterval(1);
|
||||
|
||||
GLuint vertex_buffer;
|
||||
glGenBuffers(1, &vertex_buffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
|
||||
const GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
|
||||
glCompileShader(vertex_shader);
|
||||
|
||||
const GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
|
||||
glCompileShader(fragment_shader);
|
||||
|
||||
const GLuint program = glCreateProgram();
|
||||
glAttachShader(program, vertex_shader);
|
||||
glAttachShader(program, fragment_shader);
|
||||
glLinkProgram(program);
|
||||
|
||||
const GLint mvp_location = glGetUniformLocation(program, "mvp");
|
||||
const GLint itime_location = glGetUniformLocation(program, "iTime");
|
||||
const GLint vpos_location = glGetAttribLocation(program, "vPos");
|
||||
|
||||
GLuint vertex_array;
|
||||
glGenVertexArrays(1, &vertex_array);
|
||||
glBindVertexArray(vertex_array);
|
||||
glEnableVertexAttribArray(vpos_location);
|
||||
glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
|
||||
(void *)offsetof(Vertex, pos));
|
||||
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
int width, height;
|
||||
glfwGetFramebufferSize(window, &width, &height);
|
||||
const float ratio = width / (float)height;
|
||||
|
||||
glViewport(0, 0, width, height);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
mat4x4 m, p, mvp;
|
||||
mat4x4_identity(m);
|
||||
// mat4x4_rotate_Z(m, m, (float) glfwGetTime());
|
||||
mat4x4_ortho(p, 0, ratio, 0.0f, 1.0f, 1.0f, 0.0f);
|
||||
mat4x4_mul(mvp, p, m);
|
||||
|
||||
glUseProgram(program);
|
||||
glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat *)&mvp);
|
||||
glUniform1f(itime_location, (const GLfloat)glfwGetTime());
|
||||
glBindVertexArray(vertex_array);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user