Browse Source

Merge branch 'master' into 'master'

# Conflicts:
#   Makefile
master
R Odili 5 years ago
parent
commit
6f2df5d028
  1. 58
      Makefile
  2. 0
      res/DejaVuSerif-Bold.ttf
  3. 0
      res/DejaVuSerif.ttf
  4. 0
      res/anime.pnm
  5. 0
      res/shaders/FontShader.frag
  6. 0
      res/shaders/FontShader.vert
  7. 0
      res/shaders/TextureShader.frag
  8. 0
      res/shaders/TextureShader.vert
  9. 35
      src/environment/Environment.cpp
  10. 16
      src/environment/Environment.h
  11. 21
      src/environment/Path.cpp
  12. 13
      src/environment/Path.h
  13. 19
      src/graphics/components/DocumentComponent.cpp
  14. 12
      src/graphics/components/InputComponent.cpp
  15. 17
      src/graphics/components/MultiComponent.cpp
  16. 37
      src/graphics/opengl/Shader.cpp
  17. 37
      src/graphics/opengl/Shader.h
  18. 152
      src/graphics/opengl/ShaderLoader.cpp
  19. 34
      src/graphics/opengl/ShaderLoader.h
  20. 92
      src/graphics/opengl/Window.cpp
  21. 6
      src/graphics/opengl/Window.h
  22. 6
      src/graphics/text/TextRasterizer.cpp
  23. 3
      src/main.cpp
  24. 5
      src/pnm.cpp

58
Makefile

@ -19,10 +19,9 @@ CXXFLAGS = -O3 -std=c++1y -DVERSION=\"$(GIT_VERSION)\" -DPLATFORM=\"$(PLATFORM @@ -19,10 +19,9 @@ CXXFLAGS = -O3 -std=c++1y -DVERSION=\"$(GIT_VERSION)\" -DPLATFORM=\"$(PLATFORM
CFLAGS = -O3 -std=c11 -DVERSION=\"$(GIT_VERSION)\"
WARNINGS =
ifneq ("$(wildcard /usr/local/lib/libglfw3.a)","")
CWARN =
LIBS = -lglfw3 -framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo -lGLEW -lfreetype -lharfbuzz -lmbedtls -lmbedcrypto -lmbedx509 \
else
LIBS = -lglfw3 -framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo -lGLEW -lfreetype -lharfbuzz -lmbedtls -lmbedcrypto -lmbedx509
LIBS = -lglfw.3 -framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo -lGLEW -lfreetype -lharfbuzz -lmbedtls -lmbedcrypto -lmbedx509
endif
LDFLAGS = -O3 -L/usr/local/lib
INCPATH = -I /usr/local/include -I /usr/local/include/freetype2 -I /usr/local/include/harfbuzz
@ -36,50 +35,19 @@ SRCDIR = src @@ -36,50 +35,19 @@ SRCDIR = src
OBJDIR = gen
DEPDIR = d
PREFIX = /usr/local
RESPREFIX = /usr/local/share
SOURCES = $(subst ./,,$(shell find src -name \*.cpp))
OBJECTS = $(subst $(SRCDIR),$(OBJDIR),$(SOURCES:.cpp=.o))
RES = res/
all: $(SOURCES) netrunner
netrunner: $(OBJECTS) $(OBJDIR)/tlsf.o
$(LINK) $(LDFLAGS) -o $@ $^ $(LIBS)
# make make 3.81 happy (since it can't parse any echo parameters)
ifeq ($(UNAME), Darwin)
shaders:
ifneq ($(shell cat src/graphics/opengl/shaders/FontShader.vert src/graphics/opengl/shaders/FontShader.frag src/graphics/opengl/shaders/TextureShader.vert src/graphics/opengl/shaders/TextureShader.frag | $(MD5SUM)), $(shell cat src/graphics/opengl/shaders/gen/hashsum))
@mkdir -p "src/graphics/opengl/shaders/gen"
@echo "#ifndef FONTSHADER_H\n#define FONTSHADER_H\n\nconst char *fontVertexShaderSource =\n" > src/graphics/opengl/shaders/gen/FontShader.h;
@cat src/graphics/opengl/shaders/FontShader.vert | awk '{if ($$0!="}") {print "\t\""$$0"\\n\""} else {print "\t\""$$0"\";\n"}}' >> src/graphics/opengl/shaders/gen/FontShader.h;
@echo "const char *fontFragmentShaderSource =\n" >> src/graphics/opengl/shaders/gen/FontShader.h;
@cat src/graphics/opengl/shaders/FontShader.frag | awk '{if ($$0!="}") {print "\t\""$$0"\\n\""} else {print "\t\""$$0"\";\n"}}' >> src/graphics/opengl/shaders/gen/FontShader.h;
@echo "#endif\n" >> src/graphics/opengl/shaders/gen/FontShader.h;
@echo "#ifndef TEXTURESHADER_H\n#define TEXTURESHADER_H\n\nconst char *textureVertexShaderSource =\n" > src/graphics/opengl/shaders/gen/TextureShader.h;
@cat src/graphics/opengl/shaders/TextureShader.vert | awk '{if ($$0!="}") {print "\t\""$$0"\\n\""} else {print "\t\""$$0"\";\n"}}' >> src/graphics/opengl/shaders/gen/TextureShader.h;
@echo "const char *textureFragmentShaderSource =\n" >> src/graphics/opengl/shaders/gen/TextureShader.h;
@cat src/graphics/opengl/shaders/TextureShader.frag | awk '{if ($$0!="}") {print "\t\""$$0"\\n\""} else {print "\t\""$$0"\";\n"}}' >> src/graphics/opengl/shaders/gen/TextureShader.h;
@echo "#endif\n" >> src/graphics/opengl/shaders/gen/TextureShader.h;
@cat src/graphics/opengl/shaders/FontShader.vert src/graphics/opengl/shaders/FontShader.frag src/graphics/opengl/shaders/TextureShader.vert src/graphics/opengl/shaders/TextureShader.frag | $(MD5SUM) > src/graphics/opengl/shaders/gen/hashsum;
endif
else
shaders:
ifneq ($(shell cat src/graphics/opengl/shaders/FontShader.vert src/graphics/opengl/shaders/FontShader.frag src/graphics/opengl/shaders/TextureShader.vert src/graphics/opengl/shaders/TextureShader.frag | $(MD5SUM)), $(shell cat src/graphics/opengl/shaders/gen/hashsum))
@mkdir -p "src/graphics/opengl/shaders/gen"
@cat src/graphics/opengl/shaders/FontShader.vert src/graphics/opengl/shaders/FontShader.frag src/graphics/opengl/shaders/TextureShader.vert src/graphics/opengl/shaders/TextureShader.frag | $(MD5SUM) > src/graphics/opengl/shaders/gen/hashsum;
@/bin/echo -ne "#ifndef FONTSHADER_H\n#define FONTSHADER_H\n\nconst char *fontVertexShaderSource =\n" > src/graphics/opengl/shaders/gen/FontShader.h;
@cat src/graphics/opengl/shaders/FontShader.vert | awk '{if ($$0!="}") {print "\t\""$$0"\\n\""} else {print "\t\""$$0"\";\n"}}' >> src/graphics/opengl/shaders/gen/FontShader.h;
@/bin/echo -ne "const char *fontFragmentShaderSource =\n" >> src/graphics/opengl/shaders/gen/FontShader.h;
@cat src/graphics/opengl/shaders/FontShader.frag | awk '{if ($$0!="}") {print "\t\""$$0"\\n\""} else {print "\t\""$$0"\";\n"}}' >> src/graphics/opengl/shaders/gen/FontShader.h;
@/bin/echo -ne "#endif\n" >> src/graphics/opengl/shaders/gen/FontShader.h;
@/bin/echo -ne "#ifndef TEXTURESHADER_H\n#define TEXTURESHADER_H\n\nconst char *textureVertexShaderSource =\n" > src/graphics/opengl/shaders/gen/TextureShader.h;
@cat src/graphics/opengl/shaders/TextureShader.vert | awk '{if ($$0!="}") {print "\t\""$$0"\\n\""} else {print "\t\""$$0"\";\n"}}' >> src/graphics/opengl/shaders/gen/TextureShader.h;
@/bin/echo -ne "const char *textureFragmentShaderSource =\n" >> src/graphics/opengl/shaders/gen/TextureShader.h;
@cat src/graphics/opengl/shaders/TextureShader.frag | awk '{if ($$0!="}") {print "\t\""$$0"\\n\""} else {print "\t\""$$0"\";\n"}}' >> src/graphics/opengl/shaders/gen/TextureShader.h;
@/bin/echo -ne "#endif\n" >> src/graphics/opengl/shaders/gen/TextureShader.h;
endif
endif
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp | shaders
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
@mkdir -p $(@D)
@mkdir -p $(subst gen,d,$(@D))
$(CXX) -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td $(CXXFLAGS) $(INCPATH) $(WARNINGS) -c -o $@ $<
@ -98,6 +66,18 @@ ntr-run-tests: test-url.o $(OBJDIR)/URL.o $(OBJDIR)/StringUtils.o @@ -98,6 +66,18 @@ ntr-run-tests: test-url.o $(OBJDIR)/URL.o $(OBJDIR)/StringUtils.o
$(LINK) $(LDFLAGS) -o $@ test-url.o $(OBJDIR)/URL.o $(OBJDIR)/StringUtils.o $(LIBS)
clean:
-@rm -rf src/graphics/opengl/shaders/gen $(OBJDIR) $(EXECUTABLE) 2>/dev/null || true
-@rm -rf $(OBJDIR) $(EXECUTABLE) 2>/dev/null || true
.PHONY: install
install: netrunner
mkdir -p $(DESTDIR)$(PREFIX)/bin
cp $< $(DESTDIR)$(PREFIX)/bin/netrunner
mkdir -p $(DESTDIR)$(RESPREFIX)/netrunner
cp -R $(RES) $(DESTDIR)$(RESPREFIX)/netrunner/
.PHONY: uninstall
uninstall:
rm -f $(DESTDIR)$(PREFIX)/bin/netrunner
rm -rf $(DESTDIR)$(RESPREFIX)/netrunner
include $(addsuffix .d,$(subst $(SRCDIR),$(DEPDIR),$(basename $(SOURCES))))

0
DejaVuSerif-Bold.ttf → res/DejaVuSerif-Bold.ttf

0
DejaVuSerif.ttf → res/DejaVuSerif.ttf

0
anime.pnm → res/anime.pnm

0
src/graphics/opengl/shaders/FontShader.frag → res/shaders/FontShader.frag

0
src/graphics/opengl/shaders/FontShader.vert → res/shaders/FontShader.vert

0
src/graphics/opengl/shaders/TextureShader.frag → res/shaders/TextureShader.frag

0
src/graphics/opengl/shaders/TextureShader.vert → res/shaders/TextureShader.vert

35
src/environment/Environment.cpp

@ -0,0 +1,35 @@ @@ -0,0 +1,35 @@
#include "Environment.h"
#include <vector>
#include "Path.h"
#include "../Log.h"
std::string Environment::resourceDir = "";
void Environment::init() {
if (!resourceDir.empty())
return; // already initialized O_o
std::vector<std::string> paths = {
"res"
};
#ifndef _WIN32
// linux or osx
paths.push_back("/usr/share/netrunner/resources");
#else
paths.push_back(""); //TODO: place it somewhere for windows
#endif
for (std::string &path : paths) {
if (Path::directoryExists(path)) {
resourceDir = path;
logInfo() << "Found resource dir at " << path << "!" << std::endl;
break;
}
}
}
std::string Environment::getResourceDir() {
return resourceDir;
}

16
src/environment/Environment.h

@ -0,0 +1,16 @@ @@ -0,0 +1,16 @@
#ifndef ENVIRONMENT_H
#define ENVIRONMENT_H
#include <string>
class Environment {
public:
static void init();
static std::string getResourceDir();
static void setResourceDir();
private:
static std::string resourceDir;
};
#endif

21
src/environment/Path.cpp

@ -0,0 +1,21 @@ @@ -0,0 +1,21 @@
#include "Path.h"
#include <sys/stat.h>
bool Path::directoryExists(const std::string &path) {
struct stat sb;
if (stat(path.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) {
return true;
}
return false;
}
std::string Path::fromUnixPath(const std::string &path) {
std::string returnPath(path);
#ifdef _WIN32
for (int i = 0; i < path.size(); i++)
if (path.at(i) == '/') path.at(i) = '\\';
#endif
return returnPath;
}

13
src/environment/Path.h

@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
#ifndef PATH_H
#define PATH_H
#include <string>
class Path {
public:
static bool directoryExists(const std::string &path);
static std::string fromUnixPath(const std::string &path);
};
#endif

19
src/graphics/components/DocumentComponent.cpp

@ -5,6 +5,8 @@ @@ -5,6 +5,8 @@
#include "InputComponent.h"
#include <ctime>
#include "../opengl/Shader.h"
void deleteComponent(std::shared_ptr<Component> &component);
void deleteNode(std::shared_ptr<Node> node);
@ -273,21 +275,28 @@ void DocumentComponent::render() { @@ -273,21 +275,28 @@ void DocumentComponent::render() {
// because window resets it
//if (transformMatrixDirty) {
//const std::clock_t begin = clock();
GLint transformLocation = glGetUniformLocation(win->fontProgram, "transform");
Shader *fontShader = window->shaderLoader.getShader(VertexShader("FontShader.vert"),
FragmentShader("FontShader.frag"));
GLint transformLocation = fontShader->uniform("transform");
glUniformMatrix4fv(transformLocation, 1, GL_FALSE, transformMatrix);
//const std::clock_t end = clock();
//std::cout << "Updated font matrix in: " << std::fixed << ((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl;
GLint transformLocation2 = glGetUniformLocation(win->textureProgram, "transform");
Shader *textureShader = window->shaderLoader.getShader(VertexShader("TextureShader.vert"),
FragmentShader("TextureShader.frag"));
GLint transformLocation2 = textureShader->uniform("transform");
glUniformMatrix4fv(transformLocation2, 1, GL_FALSE, transformMatrix);
transformMatrixDirty = false;
//}
//std::cout << "DocumentComponent::render - renderDirty" << std::endl;
glUseProgram(win->textureProgram);
textureShader->bind();
renderBoxComponents(rootComponent);
glUseProgram(win->fontProgram);
textureShader->release();
fontShader->bind();
renderComponents(rootComponent);
fontShader->release();
}
// create this component and all it's children

12
src/graphics/components/InputComponent.cpp

@ -3,6 +3,8 @@ @@ -3,6 +3,8 @@
#include "../text/TextRasterizerCache.h"
#include "../../scheduler.h"
#include "../opengl/Shader.h"
extern TextRasterizerCache *rasterizerCache;
extern std::unique_ptr<Scheduler> scheduler;
@ -159,9 +161,11 @@ void InputComponent::render() { @@ -159,9 +161,11 @@ void InputComponent::render() {
// can actuall delete vertices here
if (userInputText) {
// make sure we're using win's transformMatrix
glUseProgram(window->fontProgram);
Shader *fontShader = window->shaderLoader.getShader(VertexShader("FontShader.vert"),
FragmentShader("FontShader.frag"));
fontShader->bind();
if (!boundToPage) {
GLint transformLocation = glGetUniformLocation(window->fontProgram, "transform");
GLint transformLocation = fontShader->uniform("transform");
glErr=glGetError();
if(glErr != GL_NO_ERROR) {
std::cout << "InputComponent::render - glGetUniformLocation not ok: " << glErr << std::endl;
@ -175,7 +179,9 @@ void InputComponent::render() { @@ -175,7 +179,9 @@ void InputComponent::render() {
}
//std::cout << "rendering some text" << std::endl;
userInputText->render();
glUseProgram(window->textureProgram);
Shader *textureShader = window->shaderLoader.getShader(VertexShader("TextureShader.vert"),
FragmentShader("TextureShader.frag"));
textureShader->bind();
}
if (focused) {
//std::cout << "Rendering cursor" << std::endl;

17
src/graphics/components/MultiComponent.cpp

@ -6,6 +6,8 @@ @@ -6,6 +6,8 @@
#include "AnimeComponent.h"
#include "InputComponent.h"
#include "../opengl/Shader.h"
MultiComponent::MultiComponent(const float rawX, const float rawY, const float rawWidth, const float rawHeight, const int passedWindowWidth, const int passedWindowHeight) {
// take our space (for parent picking)
@ -215,14 +217,21 @@ void MultiComponent::resize(const int passedWindowWidth, const int passedWindowH @@ -215,14 +217,21 @@ void MultiComponent::resize(const int passedWindowWidth, const int passedWindowH
void MultiComponent::render() {
//std::cout << "MultiComponent::render" << std::endl;
glUseProgram(win->fontProgram);
Shader *fontShader = window->shaderLoader.getShader(VertexShader("FontShader.vert"),
FragmentShader("FontShader.frag"));
fontShader->bind();
renderDocumentComponents(rootComponent);
glUseProgram(win->textureProgram);
fontShader->release();
Shader *textureShader = window->shaderLoader.getShader(VertexShader("TextureShader.vert"),
FragmentShader("TextureShader.frag"));
textureShader->bind();
renderBoxComponents(rootComponent);
textureShader->release();
// if we flip, we can't put tab labels on top of the tab
glUseProgram(win->fontProgram);
fontShader->bind();
if (!boundToPage) {
GLint transformLocation = glGetUniformLocation(win->fontProgram, "transform");
GLint transformLocation = fontShader->uniform("transform");
GLenum glErr=glGetError();
if(glErr != GL_NO_ERROR) {
std::cout << "MultiComponent::render - glGetUniformLocation not ok: " << glErr << std::endl;

37
src/graphics/opengl/Shader.cpp

@ -0,0 +1,37 @@ @@ -0,0 +1,37 @@
#include "Shader.h"
void Shader::bind() const {
glUseProgram(handle);
}
void Shader::release() const {
glUseProgram(0);
}
int Shader::uniform(const std::string &name) {
// Is it already in the map?
auto it = locationMap.find(name);
if (it != locationMap.end()) {
return it->second;
}
// It isn't, get the uniform location and store it
GLint location = glGetUniformLocation(handle, name.c_str());
locationMap.insert(std::make_pair(name, location));
return location;
}
int Shader::attribute(const std::string &name) {
// Is it already in the map?
auto it = locationMap.find(name);
if (it != locationMap.end()) {
return it->second;
}
// It isn't, get the attribute location and store it
GLint location = glGetAttribLocation(handle, name.c_str());
locationMap.insert(std::make_pair(name, location));
return location;
}

37
src/graphics/opengl/Shader.h

@ -0,0 +1,37 @@ @@ -0,0 +1,37 @@
#ifndef SHADER_H
#define SHADER_H
#include <GL/glew.h>
#include <string>
#include <map>
struct VertexShader {
VertexShader(std::string n) : name(n) { }
std::string name;
};
struct FragmentShader {
FragmentShader(std::string n) : name(n) { }
std::string name;
};
class Shader {
public:
enum Type {
Vertex,
Fragment
};
Shader(unsigned int h) : handle(h) { }
void bind() const;
void release() const;
unsigned int handle;
int uniform(const std::string &name);
int attribute(const std::string &name);
private:
std::map<std::string, int> locationMap;
};
#endif

152
src/graphics/opengl/ShaderLoader.cpp

@ -0,0 +1,152 @@ @@ -0,0 +1,152 @@
#include "ShaderLoader.h"
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include "Shader.h"
#include "../../Log.h"
#include "../../environment/Environment.h"
#include "../../environment/Path.h"
constexpr GLint maxLogSize = 1024 * 1024 * 2; //2 MiB
GLuint ShaderLoader::loadShader(const std::string &shaderName, Shader::Type type) {
GLenum glType = getGlShaderType(type);
if (!glType) {
logError() << "Unknown shader type: " << type << std::endl;
return 0;
}
GLuint shader = glCreateShader(glType);
std::string path = Path::fromUnixPath(Environment::getResourceDir() + "/shaders/" + shaderName);
std::ifstream file(path, std::ifstream::ate);
if (!file.is_open()) {
logError() << "Could not open shader " << shaderName << std::endl;
return 0;
}
int fileSize = file.tellg();
file.seekg(std::ifstream::beg);
char *buffer = (char*) malloc(fileSize + 1);
file.read(buffer, fileSize);
buffer[fileSize] = 0;
glShaderSource(shader, 1, &buffer, NULL);
glCompileShader(shader);
free(buffer);
if(checkShader(shader, shaderName, type))
return 0;
return shader;
}
GLuint ShaderLoader::createProgram(const std::string &shader1Name, Shader::Type shader1Type,
const std::string &shader2Name, Shader::Type shader2Type) {
GLuint shader1 = loadShader(shader1Name, shader1Type);
GLuint shader2 = loadShader(shader2Name, shader2Type);
if (!shader1 || !shader2)
return 0;
GLuint program = glCreateProgram();
glAttachShader(program, shader1);
glAttachShader(program, shader2);
glLinkProgram(program);
if (checkProgram(program, GL_LINK_STATUS, shader1Name))
return 0;
/* glValidateProgram(program);
if (checkProgram(program, GL_VALIDATE_STATUS, shader1Name))
return 0; */
return program;
}
Shader *ShaderLoader::getShader(VertexShader vertexShader, FragmentShader fragmentShader) {
// concatenate strings to create unique key for the shader
std::string key = vertexShader.name + fragmentShader.name;
// has the shader been cached?
auto it = shaderCache.find(key);
if (it != shaderCache.end())
return it->second;
// Ok. It isn't. Let's create the shader Q.Q
GLuint program = createProgram(vertexShader.name, Shader::Type::Vertex,
fragmentShader.name, Shader::Type::Fragment);
if (!program) //reee it failed
return NULL;
Shader *shader = new Shader(program);
shaderCache.insert(std::make_pair(key, shader));
return shader;
}
int ShaderLoader::checkShader(GLuint shader, const std::string &shaderName, Shader::Type type) {
GLint success = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (success == GL_TRUE) {
logDebug() << "Shader " << shaderName << " compiled successfully!" << std::endl;
} else {
GLint logSize = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logSize);
if (logSize > maxLogSize) {
logError() << "Shader " << shaderName << " failed to compile but \
the log is too long to show." << std::endl;
return 1;
}
char log[logSize];
glGetShaderInfoLog(shader, logSize, &logSize, &log[0]);
logError() << "Shader " << shaderName << " failed to compile. GL log output: "
<< log << std::endl;
glDeleteShader(shader);
return 1;
}
return 0;
}
int ShaderLoader::checkProgram(GLuint program, GLenum pname, const std::string &shaderName) {
GLint success = 0;
glGetProgramiv(program, pname, &success);
if (success == GL_TRUE) {
logDebug() << "Program with shader1 " << shaderName << " " << getProgramStatusString(pname)
<< std::endl;
} else {
GLint logSize = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logSize);
if (logSize > maxLogSize) {
logError() << "Program with shader1 " << shaderName << " failed to be "
<< getProgramStatusString(pname) << " but the log is too long to show" << std::endl;
return 1;
}
char log[logSize];
glGetProgramInfoLog(program, logSize, &logSize, &log[0]);
logError() << "Program with shader1 " << shaderName << " failed to be "
<< getProgramStatusString(pname) << ". GL log output: " << log << std::endl;
glDeleteProgram(program);
return 1;
}
return 0;
}
constexpr const char *ShaderLoader::getProgramStatusString(GLenum pname) {
switch (pname) {
case GL_LINK_STATUS:
return "linked";
case GL_VALIDATE_STATUS:
return "validated";
}
return "";
}
constexpr GLenum ShaderLoader::getGlShaderType(const Shader::Type type) {
switch (type) {
case Shader::Vertex:
return GL_VERTEX_SHADER;
case Shader::Fragment:
return GL_FRAGMENT_SHADER;
}
return 0;
}

34
src/graphics/opengl/ShaderLoader.h

@ -0,0 +1,34 @@ @@ -0,0 +1,34 @@
#ifndef SHADERLOADER_H
#define SHADERLOADER_H
#include <GL/glew.h>
#include <string>
#include <map>
#include "Shader.h"
class ShaderLoader {
public:
using ShaderCache = std::map<std::string, Shader*>;
// Get a shader from the shader cache and load it from disk it isn't there
Shader *getShader(VertexShader vertexShader, FragmentShader fragmentShader);
// Load a shader from disk and return the its handle
GLuint loadShader(const std::string &shader, Shader::Type shaderType);
GLuint createProgram(const std::string &shader1Name, Shader::Type shader1Type,
const std::string &shader2Name, Shader::Type shader2Type);
private:
ShaderCache shaderCache;
int checkProgram(GLuint program, GLenum programName,
const std::string &shaderName);
int checkShader(GLuint shader, const std::string &shaderName, Shader::Type shaderType);
constexpr GLenum getGlShaderType(const Shader::Type type);
constexpr const char *getProgramStatusString(GLenum pname);
};
#endif

92
src/graphics/opengl/Window.cpp

@ -1,7 +1,5 @@ @@ -1,7 +1,5 @@
#include "../../URL.h"
#include "Window.h"
#include "shaders/gen/FontShader.h"
#include "shaders/gen/TextureShader.h"
#include "../../html/TagNode.h"
#include "../../html/TextNode.h"
@ -17,19 +15,13 @@ @@ -17,19 +15,13 @@
#include "../components/TextComponent.h"
#include "../components/TabbedComponent.h"
#include "../components/InputComponent.h"
#include "../opengl/Shader.h"
#include <cmath>
#include <ctime>
#include <iostream>
Window::~Window() {
if (fontProgram) {
glDeleteProgram(fontProgram);
}
if (textureProgram) {
glDeleteProgram(textureProgram);
}
glfwTerminate();
}
@ -638,26 +630,6 @@ bool Window::initGL() { @@ -638,26 +630,6 @@ bool Window::initGL() {
std::cout << "window::initGL - blend, clear, texParam - not ok: " << glErr << std::endl;
}
const GLuint fontVertexShader = compileShader(GL_VERTEX_SHADER, fontVertexShaderSource);
const GLuint fontFragmentShader = compileShader(GL_FRAGMENT_SHADER, fontFragmentShaderSource);
fontProgram = compileProgram(fontVertexShader, fontFragmentShader);
glDeleteShader(fontVertexShader);
glDeleteShader(fontFragmentShader);
glErr=glGetError();
if(glErr != GL_NO_ERROR) {
std::cout << "window::initGL - font program - not ok: " << glErr << std::endl;
}
const GLuint textureVertexShader = compileShader(GL_VERTEX_SHADER, textureVertexShaderSource);
const GLuint textureFragmentShader = compileShader(GL_FRAGMENT_SHADER, textureFragmentShaderSource);
textureProgram = compileProgram(textureVertexShader, textureFragmentShader);
glDeleteShader(textureVertexShader);
glDeleteShader(textureFragmentShader);
glErr=glGetError();
if(glErr != GL_NO_ERROR) {
std::cout << "window::initGL - texture program - not ok: " << glErr << std::endl;
}
//std::cout << "OpenGL is set up" << std::endl;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
@ -665,49 +637,6 @@ bool Window::initGL() { @@ -665,49 +637,6 @@ bool Window::initGL() {
return true;
}
GLuint Window::compileShader(const GLenum shaderType, const char *shaderSource) const {
const GLuint shader = glCreateShader(shaderType);
glShaderSource(shader, 1, &shaderSource, nullptr);
glCompileShader(shader);
GLenum glErr=glGetError();
if(glErr != GL_NO_ERROR) {
std::cout << "window::compileShader - compileShader - not ok: " << glErr << std::endl;
}
GLint success;
GLchar infoLog[1024];
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(shader, 1024, nullptr, infoLog);
std::cout << "Could not compile shader\n" << infoLog << std::endl;
}
return shader;
}
GLuint Window::compileProgram(const GLuint vertexShader, const GLuint fragmentShader) const {
const GLuint program = glCreateProgram();
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glLinkProgram(program);
GLenum glErr=glGetError();
if(glErr != GL_NO_ERROR) {
std::cout << "window::compileProgram - glLinkProgram - not ok: " << glErr << std::endl;
}
GLint success;
GLchar infoLog[1024];
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(program, 1024, nullptr, infoLog);
std::cout << "Could not compile program\n" << infoLog << std::endl;
}
return program;
}
void Window::onResize(int passedWidth, int passedHeight) {
this->windowWidth = passedWidth;
this->windowHeight = passedHeight;
@ -770,8 +699,9 @@ void Window::render() { @@ -770,8 +699,9 @@ void Window::render() {
std::cout << "window::render - box render start - not ok: " << glErr << std::endl;
}
glUseProgram(textureProgram);
Shader *textureShader = shaderLoader.getShader(VertexShader("TextureShader.vert"),
FragmentShader("TextureShader.frag"));
textureShader->bind();
glErr=glGetError();
if(glErr != GL_NO_ERROR) {
std::cout << "window::render - glUseProgram - not ok: " << glErr << std::endl;
@ -783,7 +713,7 @@ void Window::render() { @@ -783,7 +713,7 @@ void Window::render() {
renderComponentType("anime", rootComponent);
renderComponentType("tab", rootComponent);
glUseProgram(textureProgram); // reset after doc
textureShader->bind();
//GLint transformLocation = glGetUniformLocation(textureProgram, "transform");
//glUniformMatrix4fv(transformLocation, 1, GL_FALSE, transformMatrix);
@ -791,13 +721,15 @@ void Window::render() { @@ -791,13 +721,15 @@ void Window::render() {
renderComponentType("input", rootComponent);
// it's quick but done on scroll
glUseProgram(fontProgram);
Shader *fontShader = shaderLoader.getShader(VertexShader("FontShader.vert"),
FragmentShader("FontShader.frag"));
fontShader->bind();
// reset both, since components can change this
// god we may have to reset this after each component render...
// maybe we don't need too
if (transformMatrixDirty) {
//const std::clock_t begin = clock();
GLint transformLocation = glGetUniformLocation(fontProgram, "transform");
GLint transformLocation = fontShader->uniform("transform");
glErr=glGetError();
if(glErr != GL_NO_ERROR) {
std::cout << "window::render - glGetUniformLocation - not ok: " << glErr << std::endl;
@ -822,14 +754,14 @@ void Window::render() { @@ -822,14 +754,14 @@ void Window::render() {
// update 2nd buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(textureProgram);
textureShader->bind();
renderComponentType("anime", rootComponent);
renderComponentType("tab", rootComponent);
glUseProgram(textureProgram);
textureShader->bind();
renderComponentType("box", rootComponent);
renderComponentType("input", rootComponent);
// it's quick but done on scroll
glUseProgram(fontProgram);
fontShader->bind();
renderComponentType("text", rootComponent);
glfwSwapBuffers(window);

6
src/graphics/opengl/Window.h

@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
#include "../components/ComponentBuilder.h"
#include "../components/Component.h"
#include "ShaderLoader.h"
#include "../../html/Node.h"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
@ -15,15 +16,11 @@ @@ -15,15 +16,11 @@
class Window {
private:
public:
GLuint fontProgram = 0;
GLuint textureProgram = 0;
~Window();
bool init();
bool initGLFW();
bool initGLEW() const;
bool initGL();
GLuint compileShader(const GLenum shaderType, const char *shaderSource) const;
GLuint compileProgram(const GLuint vertexShader, const GLuint fragmentShader) const;
void render();
void setDOM(const std::shared_ptr<Node> rootNode);
@ -72,6 +69,7 @@ public: @@ -72,6 +69,7 @@ public:
GLFWcursor* cursorArrow;
GLFWcursor* cursorIbeam;
URL currentURL;
ShaderLoader shaderLoader;
};
bool setWindowContent(URL const& url);

6
src/graphics/text/TextRasterizer.cpp

@ -1,14 +1,18 @@ @@ -1,14 +1,18 @@
#include "TextRasterizer.h"
#include <string>
#include <cmath>
#include <limits>
#include <iostream>
#include "../../environment/Environment.h"
TextRasterizer::TextRasterizer(const std::string &fontPath, const int size, const unsigned int resolution, const bool bold) {
fontSize = size;
FT_Init_FreeType(&lib);
face = std::make_unique<FT_Face>();
if (FT_New_Face(lib, bold ? (fontPath.substr(0, fontPath.length() - 4) + "-Bold.ttf").c_str() : fontPath.c_str(), 0, face.get())) {
std::string path = Environment::getResourceDir() + "/" + fontPath;
if (FT_New_Face(lib, bold ? (path.substr(0, fontPath.length() - 4) + "-Bold.ttf").c_str() : path.c_str(), 0, face.get())) {
std::cout << "Could not open font" << std::endl;
return;
}

3
src/main.cpp

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
#include "CommandLineParams.h"
#include "graphics/opengl/Window.h"
#include "environment/Environment.h"
#include "html/HTMLParser.h"
#include "Log.h"
#include "URL.h"
@ -95,6 +96,7 @@ int main(int argc, char *argv[]) { @@ -95,6 +96,7 @@ int main(int argc, char *argv[]) {
# endif
std::cout << "/g/ntr - NetRunner build " << __DATE__ << ": rev-" << VERSION << " for " << PLATFORM << std::endl;
Environment::init();
// we need to set up OGL before we can setDOM (because component can't be constructed (currently) without OGL)
// but should be after CommandLineParams incase we need to change some type of window config
window->windowWidth = 1024;
@ -103,6 +105,7 @@ int main(int argc, char *argv[]) { @@ -103,6 +105,7 @@ int main(int argc, char *argv[]) {
if (!window->window) {
return 1;
}
//std::cout << "argc " << argc << std::endl;
if (argc > 1) {
initCLParams(argc, argv);

5
src/pnm.cpp

@ -1,8 +1,11 @@ @@ -1,8 +1,11 @@
#include "pnm.h"
#include <string>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include "environment/Environment.h"
// pnm
// The PNM format is just an abstraction of the PBM, PGM, and PPM formats. I.e. the name "PNM" refers collectively to PBM, PGM, and PPM.
// pam has the advanced header
@ -10,7 +13,7 @@ @@ -10,7 +13,7 @@
RGBAPNMObject * readPPM(const char* fileName) {
RGBAPNMObject *data=new RGBAPNMObject;
// open the file to read just the header reading
FILE* fr = fopen(fileName, "r");
FILE* fr = fopen(std::string(Environment::getResourceDir() + "/" + fileName).c_str(), "r");
if (!fr) {
std::cout << "Can't open " << fileName << std::endl;
return nullptr;

Loading…
Cancel
Save