mirror of https://github.com/CGAL/cgal
smooth sphere shading + minor style corrections
This commit is contained in:
parent
d4ee1bba11
commit
07124850f8
|
|
@ -15,14 +15,18 @@ namespace {
|
||||||
#version 330
|
#version 330
|
||||||
|
|
||||||
layout (location = 0) in vec3 pos;
|
layout (location = 0) in vec3 pos;
|
||||||
|
layout (location = 1) in vec3 normal;
|
||||||
|
|
||||||
//out vec4 vCol;
|
//out vec4 vCol;
|
||||||
out vec3 vpos;
|
//out vec3 vpos;
|
||||||
|
out vec3 vNormal;
|
||||||
|
|
||||||
uniform mat4 MVP;
|
uniform mat4 MVP;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
vpos = pos;
|
//vpos = pos;
|
||||||
|
vNormal = normal;
|
||||||
gl_Position = MVP * vec4(pos.xyz, 1);
|
gl_Position = MVP * vec4(pos.xyz, 1);
|
||||||
//gl_Position = vec4(pos.xyz, 1);
|
//gl_Position = vec4(pos.xyz, 1);
|
||||||
}
|
}
|
||||||
|
|
@ -62,13 +66,22 @@ void main()
|
||||||
static const char* fragment_shader_code = R"fs(
|
static const char* fragment_shader_code = R"fs(
|
||||||
#version 330
|
#version 330
|
||||||
|
|
||||||
in vec4 vCol;
|
//in vec4 vCol;
|
||||||
|
in vec3 vNormal;
|
||||||
|
|
||||||
out vec4 color;
|
out vec4 color;
|
||||||
|
|
||||||
void main()
|
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) + vec4(c,c,0,1);
|
||||||
|
|
||||||
//color = vec4(1,1,0,1);
|
//color = vec4(1,1,0,1);
|
||||||
color = vCol;
|
//color = vCol;
|
||||||
}
|
}
|
||||||
)fs";
|
)fs";
|
||||||
}
|
}
|
||||||
|
|
@ -135,9 +148,8 @@ void MainWidget::initializeGL()
|
||||||
|
|
||||||
glClearColor(0, 0, 0, 1);
|
glClearColor(0, 0, 0, 1);
|
||||||
|
|
||||||
//initGeometry();
|
init_geometry();
|
||||||
createSphere(20, 10, 3);
|
init_shader_program();
|
||||||
initShaderProgram();
|
|
||||||
|
|
||||||
// Enable depth buffer
|
// Enable depth buffer
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
|
@ -149,7 +161,7 @@ void MainWidget::initializeGL()
|
||||||
m_timer.start(12, this);
|
m_timer.start(12, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWidget::addShader(GLuint the_program, const char* shader_code,
|
void MainWidget::add_shader(GLuint the_program, const char* shader_code,
|
||||||
GLenum shader_type)
|
GLenum shader_type)
|
||||||
{
|
{
|
||||||
GLuint the_shader = glCreateShader(shader_type);
|
GLuint the_shader = glCreateShader(shader_type);
|
||||||
|
|
@ -181,7 +193,7 @@ void MainWidget::addShader(GLuint the_program, const char* shader_code,
|
||||||
|
|
||||||
glAttachShader(the_program, the_shader);
|
glAttachShader(the_program, the_shader);
|
||||||
}
|
}
|
||||||
void MainWidget::initShaderProgram()
|
void MainWidget::init_shader_program()
|
||||||
{
|
{
|
||||||
shader = glCreateProgram();
|
shader = glCreateProgram();
|
||||||
if (!shader)
|
if (!shader)
|
||||||
|
|
@ -190,9 +202,9 @@ void MainWidget::initShaderProgram()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
addShader(shader, vertex_shader_code, GL_VERTEX_SHADER);
|
add_shader(shader, vertex_shader_code, GL_VERTEX_SHADER);
|
||||||
addShader(shader, geometry_shader_code, GL_GEOMETRY_SHADER);
|
//add_shader(shader, geometry_shader_code, GL_GEOMETRY_SHADER);
|
||||||
addShader(shader, fragment_shader_code, GL_FRAGMENT_SHADER);
|
add_shader(shader, fragment_shader_code, GL_FRAGMENT_SHADER);
|
||||||
|
|
||||||
GLint result = 0;
|
GLint result = 0;
|
||||||
GLchar elog[1024] = { 0 };
|
GLchar elog[1024] = { 0 };
|
||||||
|
|
@ -221,41 +233,15 @@ void MainWidget::initShaderProgram()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void MainWidget::initGeometry()
|
void MainWidget::init_geometry()
|
||||||
{
|
{
|
||||||
const float c = 0.5;
|
int num_slices, num_stacks;
|
||||||
GLfloat vertices[] = {
|
num_slices = num_stacks = 64;
|
||||||
-c, -c, 0,
|
float r = 3;
|
||||||
c, -c, 0,
|
create_sphere(num_slices, num_stacks, r);
|
||||||
0, c, 0
|
|
||||||
};
|
|
||||||
|
|
||||||
GLuint indices[] = { 0,1,2 };
|
|
||||||
|
|
||||||
glGenVertexArrays(1, &m_vao);
|
|
||||||
glBindVertexArray(m_vao);
|
|
||||||
{
|
|
||||||
glGenBuffers(1, &m_ibo);
|
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
|
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
|
||||||
|
|
||||||
glGenBuffers(1, &m_vbo);
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
|
||||||
{
|
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
|
||||||
|
|
||||||
GLint index = 0;
|
|
||||||
glVertexAttribPointer(index, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
|
||||||
glEnableVertexAttribArray(index);
|
|
||||||
}
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
||||||
|
|
||||||
}
|
|
||||||
glBindVertexArray(0);
|
|
||||||
|
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
||||||
}
|
}
|
||||||
void MainWidget::createSphere(int num_slices, int num_stacks, float r)
|
|
||||||
|
void MainWidget::create_sphere(int num_slices, int num_stacks, float r)
|
||||||
{
|
{
|
||||||
num_stacks = std::max<int>(2, num_stacks);
|
num_stacks = std::max<int>(2, num_stacks);
|
||||||
std::vector<QVector3D> vertices, normals;
|
std::vector<QVector3D> vertices, normals;
|
||||||
|
|
@ -424,15 +410,15 @@ void MainWidget::createSphere(int num_slices, int num_stacks, float r)
|
||||||
|
|
||||||
void MainWidget::resizeGL(int w, int h)
|
void MainWidget::resizeGL(int w, int h)
|
||||||
{
|
{
|
||||||
// Calculate aspect ratio
|
// Calculate aspect ratio
|
||||||
qreal aspect = qreal(w) / qreal(h ? h : 1);
|
qreal aspect = qreal(w) / qreal(h ? h : 1);
|
||||||
|
|
||||||
// near and far plane locations and vertical field-of-view angle in degrees
|
// 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;
|
const qreal z_near = 1.0, z_far = 100.0, fov = 45.0;
|
||||||
|
|
||||||
// Reset projection
|
// Reset projection
|
||||||
m_projection.setToIdentity();
|
m_projection.setToIdentity();
|
||||||
m_projection.perspective(fov, aspect, z_near, z_far);
|
m_projection.perspective(fov, aspect, z_near, z_far);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWidget::paintGL()
|
void MainWidget::paintGL()
|
||||||
|
|
|
||||||
|
|
@ -36,24 +36,27 @@ protected:
|
||||||
void paintGL() override;
|
void paintGL() override;
|
||||||
|
|
||||||
|
|
||||||
GLuint m_vao, m_vbo, m_ibo, shader, m_num_indices;
|
void add_shader(GLuint the_program,
|
||||||
GLuint m_uniform_mvp; // uniform location for MVP-matrix in the shader
|
const char* shader_code,
|
||||||
void addShader(GLuint program, const char* shader_code, GLenum shader_type);
|
GLenum shader_type);
|
||||||
void initShaderProgram();
|
void init_shader_program();
|
||||||
|
|
||||||
void initGeometry();
|
void init_geometry();
|
||||||
void createSphere(int num_slices, int num_stacks, float r);
|
void create_sphere(int num_slices, int num_stacks, float r);
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QBasicTimer m_timer;
|
GLuint m_vao, m_vbo, m_ibo, shader, m_num_indices;
|
||||||
|
GLuint m_uniform_mvp; // uniform location for MVP-matrix in the shader
|
||||||
|
|
||||||
|
QBasicTimer m_timer;
|
||||||
|
|
||||||
QMatrix4x4 m_projection;
|
QMatrix4x4 m_projection;
|
||||||
|
|
||||||
QVector2D m_mouse_press_position;
|
QVector2D m_mouse_press_position;
|
||||||
QVector3D m_rotation_axis;
|
QVector3D m_rotation_axis;
|
||||||
qreal m_angular_speed = 0;
|
qreal m_angular_speed = 0;
|
||||||
QQuaternion m_rotation;
|
QQuaternion m_rotation;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // MAINWIDGET_H
|
#endif // MAINWIDGET_H
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue