Skip to content

Commit

Permalink
Refactor: shaders can be read from file
Browse files Browse the repository at this point in the history
  • Loading branch information
denizdiktas committed Jun 12, 2023
1 parent 609a13e commit 84aa351
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 95 deletions.
28 changes: 22 additions & 6 deletions Arrangement_on_surface_2/demo/earth/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ qt_standard_project_setup()

qt_add_executable(earth
main.cpp
mainwidget.cpp mainwidget.h
Camera.cpp Camera.h
Common_defs.h

Shader_program.cpp Shader_program.h
Sphere.cpp Sphere.h
mainwidget.h mainwidget.cpp
Camera.h Camera.cpp
Common_defs.h
Shader_program.h Shader_program.cpp
Sphere.h Sphere.cpp
Tools.h
)


Expand All @@ -43,6 +43,22 @@ target_link_libraries(earth PRIVATE
)


# Resources:
#set(shaders_resource_files
# "vertex_shader.glsl"
#)
#
#qt6_add_resources(earth "shaders"
# PREFIX
# "/"
# FILES
# ${shaders_resource_files}
#)

file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/shaders
DESTINATION ${CMAKE_CURRENT_BINARY_DIR})


install(TARGETS earth
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
Expand Down
8 changes: 8 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Shader_program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <iostream>

#include "Tools.h"


bool Shader_program::init()
{
Expand Down Expand Up @@ -48,6 +50,12 @@ void Shader_program::add_shader(const char* shader_code, GLenum shader_type)

glAttachShader(m_program, the_shader);
}
void Shader_program::add_shader_from_file(const char* shader_file,
GLenum shader_type)
{
auto src = read_file(shader_file);
add_shader(src.c_str(), shader_type);
}

bool Shader_program::link()
{
Expand Down
1 change: 1 addition & 0 deletions Arrangement_on_surface_2/demo/earth/Shader_program.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Shader_program : protected OpenGLFunctionsBase
bool init();

void add_shader(const char* shader_code, GLenum shader_type);
void add_shader_from_file(const char* shader_file, GLenum shader_type);

bool link();
bool validate();
Expand Down
25 changes: 25 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Tools.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

#ifndef TOOLS_H
#define TOOLS_H

#include <fstream>
#include <string>
#include <vector>


std::string read_file(const std::string& file_name)
{
std::ifstream ifs(file_name.c_str(), std::ios::in | std::ios::binary |
std::ios::ate);

std::ifstream::pos_type file_size = ifs.tellg();
ifs.seekg(0, std::ios::beg);

std::vector<char> bytes(file_size);
ifs.read(&bytes[0], file_size);

return std::string(&bytes[0], file_size);
}


#endif
96 changes: 7 additions & 89 deletions Arrangement_on_surface_2/demo/earth/mainwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,92 +10,13 @@
#include <string>


namespace {
// vertex shader
const char* vertex_shader_code = R"vs(
#version 330
layout (location = 0) in vec3 pos;
layout (location = 1) in vec3 normal;
//out vec4 vCol;
//out vec3 vpos;
flat out vec3 vNormal;
uniform mat4 MVP;
void main()
{
//vpos = pos;
vNormal = normal;
gl_Position = MVP * vec4(pos.xyz, 1);
//gl_Position = vec4(pos.xyz, 1);
}
)vs";


// GEOMETRY SHADER
// * I am using the geometry shader to compute the face-normals in the GPU on the fly
const char* geometry_shader_code = R"gs(
#version 330
in vec3 vpos[];
out vec4 vCol;
layout (triangles) in;
layout (triangle_strip, max_vertices = 3) out;
void main()
{
const vec3 lightDir = normalize(vec3(1,.5,.5));
// compute the normal for the current triangle
vec3 triNormal = normalize(cross(vpos[1]-vpos[0], vpos[2]-vpos[0]));
//float c = clamp(dot(lightDir,triNormal), 0, 1);
float c = abs(dot(lightDir,triNormal));
vCol = vec4(.2, .2,0,1) + vec4(c,c,0,1);
gl_Position = gl_in[0].gl_Position; EmitVertex();
gl_Position = gl_in[1].gl_Position; EmitVertex();
gl_Position = gl_in[2].gl_Position; EmitVertex();
EndPrimitive();
}
)gs";


// FRAGMENT SHADER
static const char* fragment_shader_code = R"fs(
#version 330
//in vec4 vCol;
flat in vec3 vNormal;
out vec4 color;
void main()
{
const vec3 lightDir = normalize(vec3(1,.5,.5));
//float c = clamp(dot(lightDir,triNormal), 0, 1);
vec3 n = normalize(vNormal);
float c = abs( dot(lightDir, n) );
color = vec4(.2, .2,0,1) + 0.8*vec4(c,c,0,1);
//color = vec4(1,1,0,1);
//color = vCol;
}
)fs";
}


MainWidget::~MainWidget()
{
// Make sure the context is current when deleting the texture and the buffers.
makeCurrent();
doneCurrent();
}


void MainWidget::mousePressEvent(QMouseEvent *e)
{
m_mouse_pressed = true;
Expand Down Expand Up @@ -145,7 +66,6 @@ void MainWidget::initializeGL()
m_timer.start(12, this);
}


void MainWidget::init_camera()
{
m_camera.set_pos(0, 0, 10);
Expand All @@ -161,26 +81,24 @@ void MainWidget::init_shader_program()
{
m_shader_program.init();

m_shader_program.add_shader(vertex_shader_code, GL_VERTEX_SHADER);
//m_program.add_shader(geometry_shader_code, GL_GEOMETRY_SHADER);
m_shader_program.add_shader(fragment_shader_code, GL_FRAGMENT_SHADER);
const char* vs = "shaders/vertex.glsl";
const char* gs = "shaders/geometry.glsl";
const char* fs = "shaders/fragment.glsl";
m_shader_program.add_shader_from_file(vs, GL_VERTEX_SHADER);
//m_shader_program.add_shader_from_file(gs, GL_GEOMETRY_SHADER);
m_shader_program.add_shader_from_file(fs, GL_FRAGMENT_SHADER);

m_shader_program.link();
m_shader_program.validate();
}




void MainWidget::resizeGL(int w, int h)
{
// Calculate aspect ratio
// Reset projection
qreal aspect = qreal(w) / qreal(h ? h : 1);

// near and far plane locations and vertical field-of-view angle in degrees
const qreal z_near = 1.0, z_far = 100.0, fov = 45.0;

// Reset projection
m_camera.perspective(fov, aspect, z_near, z_far);
}

Expand Down
20 changes: 20 additions & 0 deletions Arrangement_on_surface_2/demo/earth/shaders/fragment.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

#version 330

//in vec4 vCol;
flat in vec3 vNormal;

out vec4 color;

void main()
{
const vec3 lightDir = normalize(vec3(1,.5,.5));

//float c = clamp(dot(lightDir,triNormal), 0, 1);
vec3 n = normalize(vNormal);
float c = abs( dot(lightDir, n) );
color = vec4(.2, .2,0,1) + 0.8*vec4(c,c,0,1);

//color = vec4(1,1,0,1);
//color = vCol;
}
24 changes: 24 additions & 0 deletions Arrangement_on_surface_2/demo/earth/shaders/geometry.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

#version 330

in vec3 vpos[];
out vec4 vCol;

layout (triangles) in;
layout (triangle_strip, max_vertices = 3) out;

void main()
{
const vec3 lightDir = normalize(vec3(1,.5,.5));

// compute the normal for the current triangle
vec3 triNormal = normalize(cross(vpos[1]-vpos[0], vpos[2]-vpos[0]));
//float c = clamp(dot(lightDir,triNormal), 0, 1);
float c = abs(dot(lightDir,triNormal));
vCol = vec4(.2, .2,0,1) + vec4(c,c,0,1);

gl_Position = gl_in[0].gl_Position; EmitVertex();
gl_Position = gl_in[1].gl_Position; EmitVertex();
gl_Position = gl_in[2].gl_Position; EmitVertex();
EndPrimitive();
}
19 changes: 19 additions & 0 deletions Arrangement_on_surface_2/demo/earth/shaders/vertex.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

#version 330

layout (location = 0) in vec3 pos;
layout (location = 1) in vec3 normal;

//out vec4 vCol;
//out vec3 vpos;
flat out vec3 vNormal;

uniform mat4 MVP;

void main()
{
//vpos = pos;
vNormal = normal;
gl_Position = MVP * vec4(pos.xyz, 1);
//gl_Position = vec4(pos.xyz, 1);
}

0 comments on commit 84aa351

Please sign in to comment.