Skip to content
Snippets Groups Projects
Commit ba6438db authored by Dave's avatar Dave :bicyclist:
Browse files

Added point for light, wireframe toggle, maybe fixed lighting

parent 5fefb836
No related branches found
No related tags found
No related merge requests found
......@@ -20,11 +20,13 @@ private:
VertexArray *VAO;
VertexBuffer *VBO;
IndexBuffer *IBO;
bool wireframe;
void setupMesh();
public:
Mesh(std::vector<vec3f> vertices, std::vector<vec3f> normals, std::vector<unsigned int> indices);
void ToggleWireframe();
void Draw();
unsigned int GetVertexCount();
//~Mesh();
......@@ -43,6 +45,7 @@ public:
// draws the model, and thus all its meshes
void Draw(Shader &shader);
unsigned int GetVertexCount();
void ToggleWireframe();
//~Model();
private:
......
......@@ -12,26 +12,14 @@
class Point
{
private:
// clang-format off
float quadVertices[12] = {
1.0f, 1.0f, 0.0f,
-1.0f, -1.0f, 0.0f,
-1.0f, 1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
};
unsigned int quadIndices[6]{
0,1,2,
1,0,3
};
// clang-format on
//std::vector<vec3f> *circle;
VertexArray *Va;
VertexBuffer *Vb;
IndexBuffer *Ibo;
Shader *S;
glm::vec3 scale;
std::vector<float> sphere_vertices;
std::vector<float> sphere_normals;
std::vector<float> sphere_texcoords;
......@@ -39,8 +27,9 @@ private:
vec3f location = {0, 0, 0};
void CreateCircleArray(float radius, unsigned int rings, unsigned int sectors);
void CreateCircleArray(unsigned int rings, unsigned int sectors);
public:
Point(float size);
Point();
~Point();
void Draw(float x, float y, float z, glm::mat4 proj, glm::mat4 camera);
......
......@@ -22,6 +22,7 @@ public:
void SetValue(std::string uniformName, int value) const;
void SetValue(std::string uniformName, float value) const;
void SetValue(std::string uniformName, glm::mat4 transform) const;
void SetValue(std::string uniformName, glm::vec3 pos) const;
return_status GetState() const;
};
......
......@@ -26,6 +26,7 @@ private:
Framebuffer *f;
Point *light;
Point *p;
VertexArray *gridVa;
......@@ -37,13 +38,17 @@ private:
glm::mat4 cameraTransform;
glm::mat4 model;
glm::vec3 lightPos;
public:
Viewport(Camera *camera);
~Viewport();
void AttachModel(std::string modelFilepath);
void SetScale(float scale);
void SetLightPos(glm::vec3 pos);
void ResetTransformations();
void SetTranslation(float x, float y, float z);
void ToggleWireframe();
void SetSize(ImVec2 size);
ImVec2 GetSize();
void SetPos(ImVec2 pos);
......
......@@ -2,17 +2,34 @@
out vec4 outColor;
uniform vec3 cameraPos;
uniform vec3 lightPos;
in vec3 normal;
in vec3 pos;
void main(){
vec3 lightPos = vec3(10.0, 20.0, 10.0);
vec3 lightDir = normalize(lightPos - pos);
vec3 lightColor = vec3(1.0, 1.0, 1.0);
vec3 objectColor = vec3(1.0);
float diff = max(dot(normal, lightDir), 0.0);
vec3 diffuse = diff * lightColor;
vec3 result = (0.8 + diffuse) * objectColor;
vec3 objectColor = vec3(0.9);
// ambient
float ambientStrength = 0.2;
vec3 ambient = ambientStrength * lightColor;
// diffuse
vec3 norm = normalize(normal);
vec3 lightDir = normalize(lightPos - pos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor;
// specular
float specularStrength = 0.7;
vec3 viewDir = normalize(cameraPos - pos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
vec3 specular = specularStrength * spec * lightColor;
vec3 result = diffuse * objectColor;
//result = vec3(normal.x, normal.y, normal.z);
outColor = vec4(result, 1.0);
outColor = vec4(result, 1.0);
}
\ No newline at end of file
......@@ -3,13 +3,14 @@
layout(location = 0) in vec3 vertexPos;
layout(location = 1) in vec3 normalPos;
uniform mat4 transform;
out vec3 normal;
out vec3 pos;
uniform mat4 transform;
uniform mat4 model;
void main(){
gl_Position = transform * vec4(vertexPos/10, 1.0);
pos = vertexPos;
normal = normalPos;
pos = vec3(model * vec4(vertexPos, 1.0));
normal = mat3(transpose(inverse(model))) * normalPos;
gl_Position = transform * vec4(pos, 1.0);
}
......@@ -2,14 +2,13 @@
layout(location = 0) in vec3 vertexPos;
uniform mat4 projection;
uniform mat4 arcball;
uniform mat4 transform;
out vec3 normal;
out vec3 fragmentposition;
void main(){
gl_Position = projection * arcball * vec4(vertexPos, 1.0);
gl_Position = transform * vec4(vertexPos, 1.0);
fragmentposition = vertexPos;
normal = normalize(vertexPos);
}
......@@ -236,18 +236,29 @@ void App::RenderUI(unsigned int FBOtexture)
ImGui::Text("Vertex count: %i", count);
static float scale = 1.0f, x = 0.0f, y = 0.0f, z = 0.0f;
static glm::vec3 lightPos = {10.0f, 10.0f, 10.0f};
v->ResetTransformations();
v->SetTranslation(x, y, z);
v->SetScale(scale);
v->SetLightPos(lightPos);
ImGui::Text("Ctrl + click on the sliders for text input");
ImGui::Text("Grid unit: %fmm", scale);
ImGui::SliderFloat("Scale", &scale, 0.0f, 10.0f);
ImGui::SliderFloat("x", &x, -100.0f, 100.0f);
ImGui::SliderFloat("y", &y, -100.0f, 100.0f);
ImGui::SliderFloat("z", &z, -100.0f, 100.0f);
ImGui::SliderFloat("Model x", &x, -10.0f, 10.0f);
ImGui::SliderFloat("Model y", &y, -10.0f, 10.0f);
ImGui::SliderFloat("Model z", &z, -10.0f, 10.0f);
ImGui::Text("Change light position");
ImGui::SliderFloat("Light x", &lightPos.x, -50.0f, 50.0f);
ImGui::SliderFloat("Light y", &lightPos.y, -50.0f, 50.0f);
ImGui::SliderFloat("Light z", &lightPos.z, -50.0f, 50.0f);
if (v->GetModel() != nullptr)
{
if (ImGui::Button("Toggle wireframe mode"))
v->ToggleWireframe();
}
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::End();
......
......@@ -35,15 +35,17 @@ void Mesh::setupMesh()
void Mesh::Draw()
{
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
if (wireframe)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glCullFace(GL_BACK);
VAO->Bind();
IBO->Draw();
glCullFace(GL_FRONT);
// glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
if (wireframe)
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
Mesh::Mesh(std::vector<vec3f> vertices, std::vector<vec3f> normals, std::vector<unsigned int> indices)
Mesh::Mesh(std::vector<vec3f> vertices, std::vector<vec3f> normals, std::vector<unsigned int> indices) : wireframe(false)
{
this->vertices = vertices;
this->normals = normals;
......@@ -51,6 +53,13 @@ Mesh::Mesh(std::vector<vec3f> vertices, std::vector<vec3f> normals, std::vector<
setupMesh();
}
void Mesh::ToggleWireframe(){
if (wireframe)
wireframe = false;
else
wireframe = true;
}
unsigned int Mesh::GetVertexCount()
{
return indices.size();
......@@ -68,6 +77,12 @@ void Model::Draw(Shader &shader)
m_meshes[i].Draw();
}
void Model::ToggleWireframe()
{
for (unsigned int i = 0; i < m_meshes.size(); i++)
m_meshes[i].ToggleWireframe();
}
unsigned int Model::GetVertexCount()
{
unsigned int sum = 0;
......
......@@ -6,9 +6,24 @@
#include "classes/Point.h"
Point::Point()
Point::Point(): scale(0.05)
{
CreateCircleArray(0.01, 15, 15);
CreateCircleArray(15, 15);
Va = new VertexArray();
Vb = new VertexBuffer(sphere_vertices);
Ibo = new IndexBuffer(&sphere_indices.at(0), sphere_indices.size() * sizeof(unsigned int));
Vb->Bind();
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void *)0);
glEnableVertexAttribArray(0);
S = new Shader("../resources/shaders/circlevertex.glsl", "../resources/shaders/circlefragment.glsl");
S->Use();
}
Point::Point(float size)
{
scale = glm::vec3(size);
CreateCircleArray(15, 15);
Va = new VertexArray();
Vb = new VertexBuffer(sphere_vertices);
Ibo = new IndexBuffer(&sphere_indices.at(0), sphere_indices.size() * sizeof(unsigned int));
......@@ -29,15 +44,15 @@ void Point::Draw(GLfloat x, GLfloat y, GLfloat z, glm::mat4 proj, glm::mat4 came
{
glCullFace(GL_BACK);
camera = glm::translate(camera, glm::vec3(x, y, z));
camera = glm::scale(camera, glm::vec3(0.03));
S->SetValue("projection", proj);
S->SetValue("arcball", camera);
camera = glm::scale(camera, scale);
glm::mat4 transform = proj * camera;
S->SetValue("transform", transform);
Va->Bind();
Ibo->Draw();
glCullFace(GL_FRONT);
}
void Point::CreateCircleArray(float radius, unsigned int stacks, unsigned int slices)
void Point::CreateCircleArray(unsigned int stacks, unsigned int slices)
{
const float PI = 3.14f;
......
......@@ -166,6 +166,17 @@ void Shader::SetValue(std::string uniformName, glm::mat4 transform) const
glUniformMatrix4fv(glGetUniformLocation(this->m_shaderID, uniformName.c_str()), 1, GL_FALSE, glm::value_ptr(transform));
}
/**
* Sets a uniform with the specified name and value
* @param uniformName The name of the uniform
* @param value The value that needs to be assigned to the uniform
*/
void Shader::SetValue(std::string uniformName, glm::vec3 pos) const
{
this->Use();
glUniform3fv(glGetUniformLocation(this->m_shaderID, uniformName.c_str()), 1, glm::value_ptr(pos));
}
/**
* Selects the shader in the object
*/
......
......@@ -30,7 +30,7 @@ unsigned int quadIndices[] = {
};
// clang-format on
Viewport::Viewport(Camera *camera) : c(camera), m1(nullptr), viewportSize({1200, 800}), model(glm::mat4(1.0f))
Viewport::Viewport(Camera *camera) : c(camera), m1(nullptr), viewportSize({1200, 800}), model(glm::mat4(1.0f)), lightPos(glm::vec3(10.0f, 10.0f, 10.0f))
{
f = new Framebuffer();
......@@ -43,6 +43,7 @@ Viewport::Viewport(Camera *camera) : c(camera), m1(nullptr), viewportSize({1200,
gridShader = new Shader("../resources/shaders/gridVertex.glsl", "../resources/shaders/gridFragment.glsl");
gridShader->Use();
light = new Point(1.0f);
p = new Point();
modelshader = new Shader("../resources/shaders/basicVertex.glsl", "../resources/shaders/basicFragment.glsl");
......@@ -60,6 +61,10 @@ void Viewport::AttachModel(std::string modelFilepath)
}
}
void Viewport::SetLightPos(glm::vec3 pos){
lightPos = pos;
}
Viewport::~Viewport()
{
delete gridVa, gridVb, gridShader, f, m1;
......@@ -78,12 +83,13 @@ void Viewport::Render()
glm::vec4 center = c->get_rotation_point();
p->Draw(-center.x, -center.y, -center.z, projection, cameraTransform);
light->Draw(lightPos.x, lightPos.y, lightPos.z, projection, cameraTransform);
if (m1 != nullptr)
{
modelshader->SetValue("transform", glm::mat4(projection * cameraTransform * model));
//modelshader->SetValue("arcball", cameraTransform);
//modelshader->SetValue("model", model);
modelshader->SetValue("transform", glm::mat4(projection * cameraTransform));
modelshader->SetValue("model", glm::scale(model, glm::vec3(0.1f)));
modelshader->SetValue("cameraPos", c->eye());
modelshader->SetValue("lightPos", lightPos);
m1->Draw(*modelshader);
}
......@@ -94,6 +100,10 @@ void Viewport::Render()
gridIbo->Draw();
}
void Viewport::ToggleWireframe(){
m1->ToggleWireframe();
}
void Viewport::ResetTransformations(){
model = glm::mat4(1.0f);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment