Added framebuffer to render screen to
This commit is contained in:
parent
78370a2ffd
commit
de3a51f47f
80
core/Screen.cpp
Normal file
80
core/Screen.cpp
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
#include "Screen.h"
|
||||||
|
|
||||||
|
const Vertex vertices[] = {
|
||||||
|
Vertex(-1.0f, -1.0f, 0.0f, 0.0f, 0.0f),
|
||||||
|
Vertex(-1.0f, 1.0f, 0.0f, 0.0f, 1.0f),
|
||||||
|
Vertex(1.0f, -1.0f, 0.0f, 1.0f, 0.0f),
|
||||||
|
|
||||||
|
Vertex(-1.0f, 1.0f, 0.0f, 0.0f, 1.0f),
|
||||||
|
Vertex(1.0f, -1.0f, 0.0f, 1.0f, 0.0f),
|
||||||
|
Vertex(1.0f, 1.0f, 0.0f, 1.0f, 1.0f)
|
||||||
|
};
|
||||||
|
|
||||||
|
Screen::Screen() {
|
||||||
|
// Setup quad
|
||||||
|
glGenVertexArrays(1, &quad);
|
||||||
|
glBindVertexArray(quad);
|
||||||
|
|
||||||
|
Id vbo;
|
||||||
|
glGenBuffers(1, &vbo);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
// Vertex positions
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) 0);
|
||||||
|
//Vertex UV
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
|
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) (3 * sizeof(float)));
|
||||||
|
|
||||||
|
glBindVertexArray(0);
|
||||||
|
|
||||||
|
// Setup frame buffers
|
||||||
|
glGenFramebuffers(1, &framebuffer);
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
|
||||||
|
|
||||||
|
tex.width = width;
|
||||||
|
tex.height = height;
|
||||||
|
glGenTextures(1, &tex.id);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, tex.id);
|
||||||
|
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_NEAREST);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
|
||||||
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex.id, 0);
|
||||||
|
|
||||||
|
// Setup render buffer
|
||||||
|
glGenRenderbuffers(1, &renderbuffer);
|
||||||
|
glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
|
||||||
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
|
||||||
|
glBindRenderbuffer(GL_RENDERBUFFER, 0);
|
||||||
|
|
||||||
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, renderbuffer);
|
||||||
|
|
||||||
|
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
||||||
|
std::cerr << "Error setting up screen framebuffer." << std::endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Screen::Enable() {
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
|
||||||
|
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Screen::Disable() {
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
|
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
glDisable(GL_DEPTH_TEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Screen::Draw() {
|
||||||
|
glBindVertexArray(quad);
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||||
|
glBindVertexArray(0);
|
||||||
|
}
|
28
core/Screen.h
Normal file
28
core/Screen.h
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#ifndef SCREEN_H
|
||||||
|
#define SCREEN_H
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include <GL/glew.h>
|
||||||
|
|
||||||
|
#include "types.h"
|
||||||
|
#include "Vertex.h"
|
||||||
|
#include "Material.h"
|
||||||
|
|
||||||
|
class Screen {
|
||||||
|
public:
|
||||||
|
Screen();
|
||||||
|
void Enable();
|
||||||
|
void Disable();
|
||||||
|
void Draw();
|
||||||
|
const int width = 640;
|
||||||
|
const int height = 480;
|
||||||
|
Texture tex;
|
||||||
|
private:
|
||||||
|
Id quad;
|
||||||
|
Id framebuffer;
|
||||||
|
Id renderbuffer;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* SCREEN_H */
|
5
core/Shaders/ScreenShader.cpp
Normal file
5
core/Shaders/ScreenShader.cpp
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
#include "ScreenShader.h"
|
||||||
|
#include "screen.vert.h"
|
||||||
|
#include "screen.frag.h"
|
||||||
|
|
||||||
|
ScreenShader::ScreenShader() : Shader(screen_vert, screen_frag) {}
|
6
core/Shaders/ScreenShader.h
Normal file
6
core/Shaders/ScreenShader.h
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#include "Shader.h"
|
||||||
|
|
||||||
|
class ScreenShader : public Shader {
|
||||||
|
public:
|
||||||
|
ScreenShader();
|
||||||
|
};
|
@ -9,6 +9,10 @@
|
|||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
#include "Shaders/FlatShader.h"
|
#include "Shaders/FlatShader.h"
|
||||||
|
#include "Shaders/ScreenShader.h"
|
||||||
|
|
||||||
|
#include "Screen.h"
|
||||||
|
|
||||||
#include "Ball.h"
|
#include "Ball.h"
|
||||||
#include "Camera.h"
|
#include "Camera.h"
|
||||||
#include "Input.h"
|
#include "Input.h"
|
||||||
@ -80,7 +84,6 @@ int main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
glViewport(0, 0, width, height);
|
glViewport(0, 0, width, height);
|
||||||
glEnable(GL_DEPTH_TEST);
|
|
||||||
|
|
||||||
root = Node::GetRoot();
|
root = Node::GetRoot();
|
||||||
|
|
||||||
@ -89,11 +92,12 @@ int main() {
|
|||||||
|
|
||||||
Camera defaultCamera;
|
Camera defaultCamera;
|
||||||
|
|
||||||
|
Screen screen;
|
||||||
|
ScreenShader *screenShader = new ScreenShader();
|
||||||
|
|
||||||
FlatShader *shader = new FlatShader();
|
FlatShader *shader = new FlatShader();
|
||||||
shader->Use();
|
|
||||||
|
|
||||||
Matrix projection = glm::perspective(glm::radians(45.0f), 800.0f/600.0f, 0.1f, 100.0f);
|
Matrix projection = glm::perspective(glm::radians(45.0f), 800.0f/600.0f, 0.1f, 100.0f);
|
||||||
shader->UpdateProjection(projection);
|
|
||||||
|
|
||||||
// TODO Allow multiple scripting languages
|
// TODO Allow multiple scripting languages
|
||||||
Lua *lua = new Lua();
|
Lua *lua = new Lua();
|
||||||
@ -103,11 +107,13 @@ int main() {
|
|||||||
double delta = 0.0;
|
double delta = 0.0;
|
||||||
|
|
||||||
while(!glfwWindowShouldClose(window)) {
|
while(!glfwWindowShouldClose(window)) {
|
||||||
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
|
// Start rendering to texture;
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
screen.Enable();
|
||||||
|
|
||||||
lua->Update(delta);
|
lua->Update(delta);
|
||||||
|
|
||||||
|
shader->Use();
|
||||||
|
shader->UpdateProjection(projection);
|
||||||
Matrix view(1.0f);
|
Matrix view(1.0f);
|
||||||
Camera *camera = Camera::GetCurrentCamera();
|
Camera *camera = Camera::GetCurrentCamera();
|
||||||
view = glm::rotate(view, -camera->transform.rotation.x, Vector3(1.0f, 0.0f, 0.0f));
|
view = glm::rotate(view, -camera->transform.rotation.x, Vector3(1.0f, 0.0f, 0.0f));
|
||||||
@ -119,6 +125,14 @@ int main() {
|
|||||||
// Render the scene tree
|
// Render the scene tree
|
||||||
render(root, shader, Matrix(1.0f));
|
render(root, shader, Matrix(1.0f));
|
||||||
|
|
||||||
|
// Stop rendering to texture
|
||||||
|
screen.Disable();
|
||||||
|
// // Render the screen
|
||||||
|
screenShader->Use();
|
||||||
|
screenShader->UpdateTex(true, screen.tex);
|
||||||
|
glViewport(0, 0, width, height);
|
||||||
|
screen.Draw();
|
||||||
|
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
|
||||||
|
16
roadmap.md
16
roadmap.md
@ -36,3 +36,19 @@ For now, what we've got is good
|
|||||||
### Better error messages
|
### Better error messages
|
||||||
Okay, now I want a message other than "main.lua not found!" if the lua file fucks up.
|
Okay, now I want a message other than "main.lua not found!" if the lua file fucks up.
|
||||||
Also, I want Lua errors to kill the program with an error message and a stack trace.
|
Also, I want Lua errors to kill the program with an error message and a stack trace.
|
||||||
|
|
||||||
|
### Added model importing
|
||||||
|
I did this without writing about it in the roadmap. I still want to add a way to get multiple meshes on the same model, but
|
||||||
|
I'll save skeletal animation until we have a working physics system
|
||||||
|
|
||||||
|
### Refactoring pass
|
||||||
|
I want to clean things up and see if some of the code can be combined into multiple files.
|
||||||
|
Also, I want a better user experience, so I might wind up removeing the %includes from the
|
||||||
|
swig files and customizing the interface by hand. I also want to add a whole mess of constants
|
||||||
|
so that I don't have to test for keypresses. Lastly, I want to test if a function exists before
|
||||||
|
calling it from the lua file, so stubs aren't necessary to prevent a crash
|
||||||
|
|
||||||
|
- [ ] Combine files
|
||||||
|
- [ ] Better interface
|
||||||
|
- [ ] Constants for keys and the like
|
||||||
|
- [ ] No crash on calling non-existant scripting hooks
|
||||||
|
@ -10,7 +10,9 @@ endmacro()
|
|||||||
|
|
||||||
add_shader(flat.vert)
|
add_shader(flat.vert)
|
||||||
add_shader(flat.frag)
|
add_shader(flat.frag)
|
||||||
|
add_shader(screen.vert)
|
||||||
|
add_shader(screen.frag)
|
||||||
|
|
||||||
add_custom_target(shader_headers
|
add_custom_target(shader_headers
|
||||||
DEPENDS flat.vert.h flat.frag.h
|
DEPENDS flat.vert.h flat.frag.h screen.vert.h screen.frag.h
|
||||||
COMMENT "Generated shaders headers.")
|
COMMENT "Generated shaders headers.")
|
||||||
|
18
shaders/screen.frag
Normal file
18
shaders/screen.frag
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#version 330 core
|
||||||
|
|
||||||
|
in vec2 UV;
|
||||||
|
|
||||||
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
struct Material {
|
||||||
|
vec3 color;
|
||||||
|
bool usesColor;
|
||||||
|
sampler2D tex;
|
||||||
|
bool usesTex;
|
||||||
|
};
|
||||||
|
|
||||||
|
uniform Material material;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
FragColor = texture(material.tex, UV);
|
||||||
|
}
|
11
shaders/screen.vert
Normal file
11
shaders/screen.vert
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#version 330 core
|
||||||
|
|
||||||
|
layout (location = 0) in vec3 pos;
|
||||||
|
layout (location = 1) in vec2 uv;
|
||||||
|
|
||||||
|
out vec2 UV;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
gl_Position = vec4(pos, 1.0);
|
||||||
|
UV = uv;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user