mirror of
https://github.com/ApfelTeeSaft/vb01.git
synced 2026-08-26 19:33:45 +00:00
implemented line rendering
This commit is contained in:
@@ -0,0 +1,9 @@
|
|||||||
|
#version 330 core
|
||||||
|
|
||||||
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
uniform vec3 color;
|
||||||
|
|
||||||
|
void main(){
|
||||||
|
FragColor = vec4(color, 1);
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
#version 330 core
|
||||||
|
|
||||||
|
layout (location = 0) in vec3 aPos;
|
||||||
|
|
||||||
|
uniform mat4 proj, view;
|
||||||
|
|
||||||
|
void main(){
|
||||||
|
gl_Position = proj * view * vec4(aPos, 1);
|
||||||
|
}
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
#include "lineRenderer.h"
|
||||||
|
#include "root.h"
|
||||||
|
|
||||||
|
#include "glad.h"
|
||||||
|
|
||||||
|
#include <glm.hpp>
|
||||||
|
#include <ext.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace glm;
|
||||||
|
|
||||||
|
namespace vb01{
|
||||||
|
LineRenderer *lineRenderer = nullptr;
|
||||||
|
|
||||||
|
LineRenderer* LineRenderer::getSingleton(){
|
||||||
|
if(!lineRenderer)
|
||||||
|
lineRenderer = new LineRenderer();
|
||||||
|
|
||||||
|
return lineRenderer;
|
||||||
|
}
|
||||||
|
|
||||||
|
LineRenderer::LineRenderer(){
|
||||||
|
shader = new Shader(Root::getSingleton()->getLibPath() + "line3D");
|
||||||
|
}
|
||||||
|
|
||||||
|
void LineRenderer::drawLines(){
|
||||||
|
Root *root = Root::getSingleton();
|
||||||
|
Camera *cam = root->getCamera();
|
||||||
|
|
||||||
|
Vector3 dir = cam->getDirection(), up = cam->getUp();
|
||||||
|
Vector3 camPos = cam->getPosition();
|
||||||
|
mat4 view = lookAt(vec3(camPos.x, camPos.y, camPos.z), vec3(camPos.x + dir.x, camPos.y + dir.y, camPos.z + dir.z), vec3(up.x, up.y, up.z));
|
||||||
|
|
||||||
|
float fov = cam->getFov(), width = root->getWidth(), height = root->getHeight(), nearPlane = cam->getNearPlane(), farPlane = cam->getFarPlane();
|
||||||
|
mat4 proj = perspective(radians(fov), width / height, nearPlane, farPlane);
|
||||||
|
|
||||||
|
for(Line &l : lines){
|
||||||
|
shader->use();
|
||||||
|
shader->setVec3(l.color, "color");
|
||||||
|
shader->setMat4(proj, "proj");
|
||||||
|
shader->setMat4(view, "view");
|
||||||
|
|
||||||
|
glBegin(GL_LINES);
|
||||||
|
glVertex3f(l.start.x, l.start.y, l.start.z);
|
||||||
|
glVertex3f(l.end.x, l.end.y, l.end.z);
|
||||||
|
glEnd();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void LineRenderer::addLine(Vector3 start, Vector3 end, Vector3 color){
|
||||||
|
Line line;
|
||||||
|
line.id = nextId;
|
||||||
|
line.start = start;
|
||||||
|
line.end = end;
|
||||||
|
line.color = color;
|
||||||
|
lines.push_back(line);
|
||||||
|
|
||||||
|
nextId++;
|
||||||
|
}
|
||||||
|
|
||||||
|
LineRenderer::Line& LineRenderer::getLine(int i){
|
||||||
|
for(Line &l : lines)
|
||||||
|
if(l.id == i)
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
|
||||||
|
void LineRenderer::changeLineField(int id, Vector3 vec, VectorFieldType field){
|
||||||
|
Line &line = LineRenderer::getLine(id);
|
||||||
|
|
||||||
|
switch(field){
|
||||||
|
case VectorFieldType::START:
|
||||||
|
line.start = vec;
|
||||||
|
break;
|
||||||
|
case VectorFieldType::END:
|
||||||
|
line.end = vec;
|
||||||
|
break;
|
||||||
|
case VectorFieldType::COLOR:
|
||||||
|
line.color= vec;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void LineRenderer::removeLine(int id){
|
||||||
|
for(Line &l : lines)
|
||||||
|
if(l.id == id){
|
||||||
|
lines.erase(lines.begin() + id);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
#ifndef LINE_H
|
||||||
|
#define LINE_H
|
||||||
|
|
||||||
|
#include "vector.h"
|
||||||
|
#include "mesh.h"
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace vb01{
|
||||||
|
class Shader;
|
||||||
|
|
||||||
|
class LineRenderer{
|
||||||
|
public:
|
||||||
|
struct Line{int id; Vector3 start; Vector3 end; Vector3 color;};
|
||||||
|
enum VectorFieldType{START, END, COLOR};
|
||||||
|
|
||||||
|
static LineRenderer* getSingleton();
|
||||||
|
void drawLines();
|
||||||
|
void addLine(Vector3 start, Vector3 end, Vector3 color);
|
||||||
|
void changeLineField(int, Vector3, VectorFieldType);
|
||||||
|
void removeLine(int);
|
||||||
|
Line& getLine(int);
|
||||||
|
private:
|
||||||
|
LineRenderer();
|
||||||
|
|
||||||
|
int nextId = 0;
|
||||||
|
Shader *shader = nullptr;
|
||||||
|
std::vector<LineRenderer::Line> lines;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
#include "shader.h"
|
#include "shader.h"
|
||||||
#include "box.h"
|
#include "box.h"
|
||||||
#include "quad.h"
|
#include "quad.h"
|
||||||
|
#include "lineRenderer.h"
|
||||||
|
|
||||||
#include "glad.h"
|
#include "glad.h"
|
||||||
#include <glfw3.h>
|
#include <glfw3.h>
|
||||||
@@ -15,6 +16,7 @@ using namespace std;
|
|||||||
|
|
||||||
namespace vb01{
|
namespace vb01{
|
||||||
static Root *root = nullptr;
|
static Root *root = nullptr;
|
||||||
|
Shader *shader = nullptr;
|
||||||
|
|
||||||
Root* Root::getSingleton(){
|
Root* Root::getSingleton(){
|
||||||
if(!root)
|
if(!root)
|
||||||
@@ -55,6 +57,8 @@ namespace vb01{
|
|||||||
initMainFramebuffer(fragTexture, brightTexture);
|
initMainFramebuffer(fragTexture, brightTexture);
|
||||||
initBloomFramebuffer();
|
initBloomFramebuffer();
|
||||||
initGuiPlane(fragTexture, brightTexture);
|
initGuiPlane(fragTexture, brightTexture);
|
||||||
|
|
||||||
|
shader = new Shader(Root::getSingleton()->getLibPath() + "line3D");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Root::initWindow(string name){
|
void Root::initWindow(string name){
|
||||||
@@ -148,6 +152,8 @@ namespace vb01{
|
|||||||
|
|
||||||
rootNode->update();
|
rootNode->update();
|
||||||
|
|
||||||
|
LineRenderer::getSingleton()->drawLines();
|
||||||
|
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user