Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// H File Follows
- /**
- * Copyright (c) 2023 Jonathan Metzgar
- *
- * This software is released under the MIT License.
- * https://opensource.org/licenses/MIT
- */
- #include <SDL2/SDL_opengl.h>
- #include <SDL2/SDL_opengl_glext.h>
- #include "object.h"
- struct GfFunction_t
- {
- Object_t self;
- } GfFunction_t, *GfFunction;
- typedef struct GfPipelineState_t
- {
- Object_t self;
- GLuint program;
- } GfPipelineState_t, *GfPipelineState;
- struct GfBuffer_t
- {
- } GfBuffer_t, *GfBuffer;
- GfPipelineState GfNewPipeline(const char *vertSource, const char *fragSource);
- void GfUsePipeline(GfPipelineState pso);
- /// C File follows
- /**
- * Copyright (c) 2023 Jonathan Metzgar
- *
- * This software is released under the MIT License.
- * https://opensource.org/licenses/MIT
- */
- #include <stdio.h>
- #define GL_SILENCE_DEPRECATION 1
- #include <OpenGL/gl3.h>
- #include "goldfish.h"
- void Log(const char *label, const char *str)
- {
- fprintf(stderr, "%s: %s\n", label, str);
- }
- GLuint CompileShader(const char *source, GLenum type)
- {
- if (source == NULL)
- return 0;
- GLuint shader = glCreateShader(type);
- glShaderSource(shader, 1, &source, NULL);
- glCompileShader(shader);
- GLint status;
- glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
- if (status != GL_TRUE)
- {
- GLchar infoLog[1024];
- GLsizei length;
- glGetShaderInfoLog(shader, 1024, &length, infoLog);
- Log("error", infoLog);
- glDeleteShader(shader);
- return 0;
- }
- return shader;
- }
- GLuint CompileProgram(const char *vertSource, const char *fragSource)
- {
- GLuint vs = CompileShader(vertSource, GL_VERTEX_SHADER);
- GLuint fs = CompileShader(fragSource, GL_FRAGMENT_SHADER);
- GLuint program = glCreateProgram();
- if (vs != 0)
- {
- glAttachShader(program, vs);
- }
- if (fs != 0)
- {
- glAttachShader(program, fs);
- }
- glLinkProgram(program);
- GLint status;
- glGetProgramiv(program, GL_LINK_STATUS, &status);
- if (status != GL_TRUE)
- {
- GLchar infoLog[1024];
- GLsizei length;
- glGetProgramInfoLog(program, 1024, &length, infoLog);
- Log("error", infoLog);
- glDeleteProgram(program);
- program = 0;
- }
- glDeleteShader(vs);
- glDeleteShader(fs);
- return program;
- }
- GfPipelineState GfNewPipeline(const char *vertSource, const char *fragSource)
- {
- GfPipelineState pso = (GfPipelineState)allocObject(sizeof(GfPipelineState_t));
- pso->program = CompileProgram(vertSource, fragSource);
- return pso;
- }
- void GfUsePipeline(GfPipelineState pso)
- {
- if (pso == NULL)
- {
- return;
- }
- glUseProgram(pso->program);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement