trying some shader stuff, but segfaults
This commit is contained in:
parent
293a814ad3
commit
c5e45ef31a
6
Makefile
6
Makefile
@ -21,10 +21,10 @@ SOURCES += $(wildcard $(IMGUI_DIR)/*.cpp)
|
||||
SOURCES += $(wildcard $(IMGUI_DIR)/backends/*.cpp)
|
||||
OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES))))
|
||||
UNAME_S := $(shell uname -s)
|
||||
LINUX_GL_LIBS = -lGL
|
||||
LINUX_GL_LIBS = -lGL -lGLEW
|
||||
|
||||
CXXFLAGS = -std=c++20 -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends
|
||||
CXXFLAGS += -g -DIMGUI_DEFINE_MATH_OPERATORS -Ofast
|
||||
CXXFLAGS += -g -DIMGUI_DEFINE_MATH_OPERATORS
|
||||
LIBS =
|
||||
|
||||
##---------------------------------------------------------------------
|
||||
@ -52,7 +52,7 @@ endif
|
||||
|
||||
ifeq ($(UNAME_S), Darwin) #APPLE
|
||||
ECHO_MESSAGE = "Mac OS X"
|
||||
LIBS += -framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo `sdl2-config --libs`
|
||||
LIBS += -framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo `sdl2-config --libs` -lGLEW
|
||||
LIBS += -L/usr/local/lib -L/opt/local/lib
|
||||
|
||||
CXXFLAGS += `sdl2-config --cflags`
|
||||
|
@ -32,7 +32,7 @@ EMS =
|
||||
##---------------------------------------------------------------------
|
||||
|
||||
# ("EMS" options gets added to both CPPFLAGS and LDFLAGS, whereas some options are for linker only)
|
||||
EMS += -s USE_SDL=2 -flto -O2
|
||||
EMS += -s USE_SDL=2 -flto -O2 -s USE_WEBGL2=1
|
||||
EMS += -s DISABLE_EXCEPTION_CATCHING=1
|
||||
LDFLAGS += -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 -s NO_EXIT_RUNTIME=0 -s ASSERTIONS=1 -sEXPORTED_FUNCTIONS=[_main,_malloc,_free] -sEXPORTED_RUNTIME_METHODS=[ccall]
|
||||
|
||||
|
58
lib/FrameBuffer.cpp
Normal file
58
lib/FrameBuffer.cpp
Normal file
@ -0,0 +1,58 @@
|
||||
// FrameBuffer.cpp
|
||||
|
||||
#include "FrameBuffer.h"
|
||||
|
||||
FrameBuffer::FrameBuffer(float width, float height) {
|
||||
glGenFramebuffers(1, &fbo);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
||||
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
|
||||
GL_UNSIGNED_BYTE, NULL);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
|
||||
texture, 0);
|
||||
|
||||
glGenRenderbuffers(1, &rbo);
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, rbo);
|
||||
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
|
||||
GL_RENDERBUFFER, rbo);
|
||||
|
||||
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
|
||||
std::cout << "ERROR::FRAMEBUFFER:: Framebuffer is not complete!"
|
||||
<< std::endl;
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, 0);
|
||||
}
|
||||
|
||||
FrameBuffer::~FrameBuffer() {
|
||||
glDeleteFramebuffers(1, &fbo);
|
||||
glDeleteTextures(1, &texture);
|
||||
glDeleteRenderbuffers(1, &rbo);
|
||||
}
|
||||
|
||||
unsigned int FrameBuffer::getFrameTexture() { return texture; }
|
||||
|
||||
void FrameBuffer::RescaleFrameBuffer(float width, float height) {
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
|
||||
GL_UNSIGNED_BYTE, NULL);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
|
||||
texture, 0);
|
||||
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, rbo);
|
||||
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
|
||||
GL_RENDERBUFFER, rbo);
|
||||
}
|
||||
|
||||
void FrameBuffer::Bind() const { glBindFramebuffer(GL_FRAMEBUFFER, fbo); }
|
||||
|
||||
void FrameBuffer::Unbind() const { glBindFramebuffer(GL_FRAMEBUFFER, 0); }
|
38
lib/FrameBuffer.h
Normal file
38
lib/FrameBuffer.h
Normal file
@ -0,0 +1,38 @@
|
||||
// FrameBuffer.h
|
||||
#ifdef __EMSCRIPTEN__
|
||||
#else
|
||||
#include "GL/glew.h"
|
||||
#endif
|
||||
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#if defined(__APPLE__)
|
||||
#include <SDL2/SDL.h>
|
||||
#if defined(IMGUI_IMPL_OPENGL_ES2)
|
||||
#include <SDL2/SDL_opengles.h>
|
||||
#else
|
||||
#include <SDL2/SDL_opengl.h>
|
||||
#endif
|
||||
#else
|
||||
#include <SDL.h>
|
||||
#if defined(IMGUI_IMPL_OPENGL_ES2)
|
||||
#include <SDL_opengles2.h>
|
||||
#else
|
||||
#include <SDL_opengl.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
class FrameBuffer {
|
||||
public:
|
||||
FrameBuffer(float width, float height);
|
||||
~FrameBuffer();
|
||||
unsigned int getFrameTexture();
|
||||
void RescaleFrameBuffer(float width, float height);
|
||||
void Bind() const;
|
||||
void Unbind() const;
|
||||
|
||||
private:
|
||||
unsigned int fbo;
|
||||
unsigned int texture;
|
||||
unsigned int rbo;
|
||||
};
|
@ -1,6 +1,6 @@
|
||||
|
||||
#include "../ImNodeFlow.h"
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <ostream>
|
||||
|
||||
@ -20,12 +20,11 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include "../ImNodeFlow.h"
|
||||
#include "../image_model.h"
|
||||
#include "../imfilebrowser.h"
|
||||
#include "../stb_image.h"
|
||||
#include "../uuid.h"
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
#include "../emscripten_browser_file.h"
|
||||
@ -54,7 +53,11 @@ public:
|
||||
});
|
||||
}
|
||||
|
||||
~Exposure() { stbi_image_free(returnBundle.image); }
|
||||
~Exposure() {
|
||||
if (returnBundle.loaded) {
|
||||
stbi_image_free(returnBundle.image);
|
||||
}
|
||||
}
|
||||
|
||||
void draw() override {
|
||||
ImGui::SetNextItemWidth(100);
|
||||
@ -158,7 +161,11 @@ public:
|
||||
});
|
||||
}
|
||||
|
||||
~Contrast() { stbi_image_free(returnBundle.image); }
|
||||
~Contrast() {
|
||||
if (returnBundle.loaded) {
|
||||
stbi_image_free(returnBundle.image);
|
||||
}
|
||||
}
|
||||
|
||||
void draw() override {
|
||||
ImGui::SetNextItemWidth(100);
|
||||
@ -263,7 +270,11 @@ public:
|
||||
});
|
||||
}
|
||||
|
||||
~SimpleBrightness() { stbi_image_free(returnBundle.image); }
|
||||
~SimpleBrightness() {
|
||||
if (returnBundle.loaded) {
|
||||
stbi_image_free(returnBundle.image);
|
||||
}
|
||||
}
|
||||
|
||||
void draw() override {
|
||||
ImGui::SetNextItemWidth(100);
|
||||
|
143
main.cpp
143
main.cpp
@ -8,6 +8,13 @@
|
||||
// - Documentation https://dearimgui.com/docs (same as your local docs/
|
||||
// folder).
|
||||
// - Introduction, links and more at the top of imgui.cpp
|
||||
//
|
||||
|
||||
#include <cstdint>
|
||||
#ifdef __EMSCRIPTEN__
|
||||
#else
|
||||
#include "GL/glew.h"
|
||||
#endif
|
||||
|
||||
#include <cstddef>
|
||||
#define IMGUI_DEFINE_MATH_OPERATORS
|
||||
@ -43,10 +50,136 @@
|
||||
#include "lib/emscripten/emscripten_mainloop_stub.h"
|
||||
#endif
|
||||
|
||||
#include "lib/FrameBuffer.h"
|
||||
#include "lib/ImNodeFlow.h"
|
||||
|
||||
static bool init = true;
|
||||
|
||||
GLuint initShader(const char *vShader, const char *fShader,
|
||||
const char *outputAttributeName) {
|
||||
struct Shader {
|
||||
GLenum type;
|
||||
const char *source;
|
||||
} shaders[2] = {{GL_VERTEX_SHADER, vShader}, {GL_FRAGMENT_SHADER, fShader}};
|
||||
|
||||
GLuint program = glCreateProgram();
|
||||
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
Shader &s = shaders[i];
|
||||
GLuint shader = glCreateShader(s.type);
|
||||
glShaderSource(shader, 1, (const GLchar **)&s.source, NULL);
|
||||
glCompileShader(shader);
|
||||
|
||||
GLint compiled;
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
|
||||
if (!compiled) {
|
||||
std::cerr << " failed to compile:" << std::endl;
|
||||
GLint logSize;
|
||||
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logSize);
|
||||
char *logMsg = new char[logSize];
|
||||
glGetShaderInfoLog(shader, logSize, NULL, logMsg);
|
||||
std::cerr << logMsg << std::endl;
|
||||
delete[] logMsg;
|
||||
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
glAttachShader(program, shader);
|
||||
}
|
||||
|
||||
/* Link output */
|
||||
#ifndef EMSCRIPTEN
|
||||
glBindFragDataLocation(program, 0, outputAttributeName);
|
||||
#endif
|
||||
/* link and error check */
|
||||
glLinkProgram(program);
|
||||
|
||||
GLint linked;
|
||||
glGetProgramiv(program, GL_LINK_STATUS, &linked);
|
||||
if (!linked) {
|
||||
std::cerr << "Shader program failed to link" << std::endl;
|
||||
GLint logSize;
|
||||
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logSize);
|
||||
char *logMsg = new char[logSize];
|
||||
glGetProgramInfoLog(program, logSize, NULL, logMsg);
|
||||
std::cerr << logMsg << std::endl;
|
||||
delete[] logMsg;
|
||||
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* use program object */
|
||||
glUseProgram(program);
|
||||
|
||||
return program;
|
||||
}
|
||||
|
||||
uint8_t loadShader() {
|
||||
#ifdef EMSCRIPTEN
|
||||
const char *vert = R"(#version 100
|
||||
uniform vec2 offset;
|
||||
attribute vec4 position;
|
||||
attribute vec2 uv;
|
||||
varying vec2 vUV;
|
||||
void main (void)
|
||||
{
|
||||
vUV = uv;
|
||||
gl_Position = position + vec4(offset,0.0,0.0);
|
||||
})";
|
||||
#else
|
||||
const char *vert = R"(#version 150
|
||||
uniform vec2 offset;
|
||||
in vec4 position;
|
||||
in vec2 uv;
|
||||
|
||||
out vec2 vUV;
|
||||
|
||||
void main (void)
|
||||
{
|
||||
vUV = uv;
|
||||
gl_Position = position + vec4(offset,0.0,0.0);
|
||||
})";
|
||||
#endif
|
||||
#ifdef EMSCRIPTEN
|
||||
const char *frag = R"(#version 100
|
||||
precision mediump float;
|
||||
|
||||
varying vec2 vUV;
|
||||
uniform sampler2D tex;
|
||||
|
||||
void main(void)
|
||||
{
|
||||
gl_FragColor = texture2D(tex,vUV);
|
||||
})";
|
||||
#else
|
||||
const char *frag = R"(#version 150
|
||||
in vec2 vUV;
|
||||
out vec4 fragColor;
|
||||
uniform sampler2D tex;
|
||||
void main(void)
|
||||
{
|
||||
fragColor = texture(tex,vUV);
|
||||
})";
|
||||
#endif
|
||||
std::cout << "Starting to init" << std::endl;
|
||||
uint8_t shaderProgram = initShader(vert, frag, "fragColor");
|
||||
std::cout << "done to init" << std::endl;
|
||||
|
||||
uint8_t uvAttribute = glGetAttribLocation(shaderProgram, "uv");
|
||||
if (uvAttribute < 0) {
|
||||
std::cerr << "Shader did not contain the 'color' attribute." << std::endl;
|
||||
}
|
||||
uint8_t positionAttribute = glGetAttribLocation(shaderProgram, "position");
|
||||
if (positionAttribute < 0) {
|
||||
std::cerr << "Shader did not contain the 'position' attribute."
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
return shaderProgram;
|
||||
}
|
||||
|
||||
uint8_t setup() { return loadShader(); }
|
||||
|
||||
// Main code
|
||||
int main(int, char **) {
|
||||
// Setup SDL
|
||||
@ -139,7 +272,9 @@ int main(int, char **) {
|
||||
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
|
||||
ImFlow::ImNodeFlow editor = ImFlow::ImNodeFlow("Nodes");
|
||||
|
||||
uint8_t shader = 0;
|
||||
// Main loop
|
||||
|
||||
bool done = false;
|
||||
#ifdef __EMSCRIPTEN__
|
||||
// For an Emscripten build we are disabling file-system access, so let's not
|
||||
@ -201,6 +336,8 @@ int main(int, char **) {
|
||||
ImGui::DockBuilderSetNodeSize(dockspace_id, viewport->Size);
|
||||
ImGui::DockBuilderDockWindow("Editor", dockspace_id);
|
||||
ImGui::DockBuilderFinish(dockspace_id);
|
||||
|
||||
// setup_shaders();
|
||||
}
|
||||
if (ImGui::BeginMainMenuBar()) {
|
||||
if (ImGui::BeginMenu("Add")) {
|
||||
@ -230,6 +367,12 @@ int main(int, char **) {
|
||||
ImGui::End();
|
||||
ImGui::End();
|
||||
ImGui::PopStyleVar();
|
||||
|
||||
if (shader != 0) {
|
||||
ImGui::Begin("Shader test");
|
||||
ImGui::Text("Shader ID: %d", shader);
|
||||
ImGui::End();
|
||||
}
|
||||
}
|
||||
|
||||
// Rendering
|
||||
|
Loading…
Reference in New Issue
Block a user