-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from 3DevApps/opengl-support
Opengl support
- Loading branch information
Showing
7 changed files
with
234 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
#include "Renderer.h" | ||
#include "Utils.h" | ||
|
||
Renderer::Renderer(Window& window) | ||
: window_(window) | ||
, width_(window.getWidth()) | ||
, height_(window.getHeight()) { | ||
|
||
CheckedGLCall(glGenTextures(1, &texId_)); | ||
CheckedGLCall(glGenFramebuffers(1, &fboId_)); | ||
} | ||
|
||
void Renderer::renderFrame(const uint8_t *frame) { | ||
CheckedGLCall(glBindTexture(GL_TEXTURE_2D, texId_)); | ||
CheckedGLCall(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width_, height_, 0, GL_RGB, GL_UNSIGNED_BYTE, frame)); | ||
|
||
CheckedGLCall(glBindFramebuffer(GL_READ_FRAMEBUFFER, fboId_)); | ||
CheckedGLCall(glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texId_, 0)); | ||
|
||
CheckedGLCall(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0)); | ||
CheckedGLCall(glBlitFramebuffer(0, 0, width_, height_, 0, 0, width_, height_, GL_COLOR_BUFFER_BIT, GL_NEAREST)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#pragma once | ||
|
||
#include <memory> | ||
#include <ctime> | ||
#include <vector> | ||
#include "Window.h" | ||
#include <GL/glew.h> | ||
#include <GLFW/glfw3.h> | ||
|
||
class Renderer { | ||
public: | ||
Renderer(Window& window); | ||
~Renderer() = default; | ||
void renderFrame(const uint8_t *frame); | ||
private: | ||
Window& window_; | ||
std::uint32_t width_; | ||
std::uint32_t height_; | ||
GLuint fboId_; | ||
GLuint texId_; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
#pragma once | ||
#include <GL/glew.h> | ||
#include <GLFW/glfw3.h> | ||
#include <iostream> | ||
#include <string> | ||
|
||
#define CheckedGLCall(x) do { printOpenGLErrors(">>BEFORE<< "#x, __FILE__, __LINE__); (x); printOpenGLErrors(#x, __FILE__, __LINE__); } while (0) | ||
#define CheckedGLResult(x) (x); printOpenGLErrors(#x, __FILE__, __LINE__); | ||
#define CheckExistingErrors(x) printOpenGLErrors(">>BEFORE<< "#x, __FILE__, __LINE__); | ||
|
||
void printOpenGLErrors(char const * const function, char const * const file, int const line) { | ||
bool succeeded = true; | ||
GLenum error = glGetError(); | ||
if (error != GL_NO_ERROR) { | ||
char const *errorString = (char const *) gluErrorString(error); | ||
throw std::runtime_error( | ||
"OpenGL Error in " + std::string(file) + | ||
" at line" + std::to_string(line) + | ||
" calling function " + std::string(function) + | ||
": " + std::string(errorString)); | ||
} | ||
} | ||
|
||
void printShaderInfoLog(GLint const shader) { | ||
int infoLogLength = 0; | ||
int charsWritten = 0; | ||
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength); | ||
if (infoLogLength > 0) { | ||
GLchar * infoLog = new GLchar[infoLogLength]; | ||
glGetShaderInfoLog(shader, infoLogLength, &charsWritten, infoLog); | ||
std::cout << "Shader Info Log:" << std::endl << infoLog << std::endl; | ||
delete [] infoLog; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#include <GL/glew.h> | ||
#include <GLFW/glfw3.h> | ||
#include "Window.h" | ||
#include <stdexcept> | ||
#include <cstdio> | ||
|
||
const std::unordered_map<MouseButton, int> mouseButtonToGlfwButton = { | ||
{ MouseButton::Left, GLFW_MOUSE_BUTTON_LEFT }, | ||
{ MouseButton::Right, GLFW_MOUSE_BUTTON_RIGHT }, | ||
{ MouseButton::Middle, GLFW_MOUSE_BUTTON_MIDDLE }, | ||
}; | ||
|
||
Window::Window(std::uint32_t width, std::uint32_t height, const std::string& title) | ||
: window_(nullptr, glfwDestroyWindow) | ||
, width_(width) | ||
, height_(height) { | ||
|
||
if (!glfwInit()) { | ||
throw std::runtime_error("glfwInit() failed"); | ||
} | ||
// glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); | ||
window_.reset(glfwCreateWindow(width, height, title.c_str(), nullptr, nullptr)); | ||
if (!window_.get()) { | ||
throw std::runtime_error("Failed to create GLFW window!"); | ||
} | ||
|
||
glfwMakeContextCurrent(window_.get()); | ||
glfwSetWindowUserPointer(window_.get(), this); | ||
glfwSetScrollCallback(window_.get(), scrollCallback); | ||
|
||
GLenum glew_status = glewInit(); | ||
|
||
// GLEW_ERROR_NO_GLX_DISPLAY occurs when running the program on a WSL system. | ||
// This error is not critical and can be ignored. | ||
if (glew_status != GLEW_OK && glew_status != GLEW_ERROR_NO_GLX_DISPLAY) { | ||
fprintf(stderr, "Error: %s\n", glewGetErrorString(glew_status)); | ||
throw std::runtime_error("Failed to init GLEW"); | ||
} | ||
} | ||
|
||
Window::~Window() { | ||
glfwTerminate(); | ||
} | ||
|
||
std::uint32_t Window::getWidth() const { | ||
return width_; | ||
} | ||
|
||
std::uint32_t Window::getHeight() const { | ||
return height_; | ||
} | ||
|
||
void Window::pollEvents() { | ||
glfwPollEvents(); | ||
} | ||
|
||
bool Window::shouldClose() const { | ||
return glfwWindowShouldClose(window_.get()); | ||
} | ||
|
||
bool Window::getMouseButton(MouseButton button) const { | ||
auto b = mouseButtonToGlfwButton.find(button); | ||
return glfwGetMouseButton(window_.get(), b->second) == GLFW_PRESS; | ||
} | ||
|
||
void Window::getMousePos(int& x, int& y) const { | ||
double xpos, ypos; | ||
glfwGetCursorPos(window_.get(), &xpos, &ypos); | ||
x = static_cast<int>(xpos); | ||
y = static_cast<int>(ypos); | ||
} | ||
|
||
void Window::setMousePos(int x, int y) const { | ||
glfwSetCursorPos(window_.get(), (double)x, (double)y); | ||
} | ||
|
||
void Window::swapBuffers() { | ||
glfwSwapBuffers(window_.get()); | ||
} | ||
|
||
void Window::scrollCallback(GLFWwindow* window, double xoffset, double yoffset) { | ||
Window* this_window = (Window*)glfwGetWindowUserPointer(window); | ||
for (auto callback : this_window->scroll_callbacks_) | ||
{ | ||
callback(yoffset); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#pragma once | ||
|
||
#include <GL/glew.h> | ||
#include <GLFW/glfw3.h> | ||
#include <cstdint> | ||
#include <memory> | ||
#include <vector> | ||
#include <functional> | ||
#include <string> | ||
|
||
enum class MouseButton { | ||
Left, | ||
Right, | ||
Middle, | ||
}; | ||
|
||
|
||
class Window { | ||
public: | ||
Window(Window const&) = delete; | ||
Window& operator=(Window const&) = delete; | ||
|
||
Window(std::uint32_t width, std::uint32_t height, const std::string& title); | ||
~Window(); | ||
|
||
void getMousePos(int& x, int& y) const; | ||
void setMousePos(int x, int y) const; | ||
void pollEvents(); | ||
bool shouldClose() const; | ||
void swapBuffers(); | ||
void addScrollCallback(std::function<void(float)> callback) { scroll_callbacks_.push_back(callback); } | ||
std::uint32_t getWidth() const; | ||
std::uint32_t getHeight() const; | ||
bool getMouseButton(MouseButton button) const; | ||
|
||
private: | ||
static void scrollCallback(GLFWwindow* window, double xoffset, double yoffset); | ||
std::unique_ptr<GLFWwindow, void(*)(GLFWwindow*)> window_; | ||
std::vector<std::function<void(float)>> scroll_callbacks_; | ||
std::uint32_t width_ = 0u; | ||
std::uint32_t height_ = 0u; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters