-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'gsoc2023-aos_sphere_demo-denizdiktas' of github.com:CGA…
…L/cgal-public-dev into gsoc2023-aos_sphere_demo-denizdiktas
- Loading branch information
Showing
15 changed files
with
670 additions
and
427 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
|
||
#include "Camera.h" | ||
|
||
|
||
Camera::Camera() : | ||
m_ux(1, 0, 0), | ||
m_uy(0, 1, 0), | ||
m_uz(0, 0, 1) | ||
{ | ||
} | ||
|
||
void Camera::perspective(float fov, float aspect, float z_near, float z_far) | ||
{ | ||
m_projection.setToIdentity(); | ||
m_projection.perspective(fov, aspect, z_near, z_far); | ||
} | ||
|
||
|
||
QMatrix4x4 Camera::get_view_matrix() const | ||
{ | ||
QMatrix4x4 view; | ||
const QVector3D center = m_pos - m_uz; | ||
view.lookAt(m_pos, center, m_uy); | ||
return view; | ||
} | ||
|
||
|
||
void Camera::rotate(float theta_around_x, float theta_around_y) | ||
{ | ||
// rotate the camera around its x-axis | ||
QMatrix4x4 rot; | ||
rot.rotate(theta_around_x, m_ux); | ||
m_pos = m_pos * rot; | ||
m_uy = m_uy * rot; | ||
m_uz = m_uz * rot; | ||
|
||
// rotate the camera around its y-axis | ||
rot.setToIdentity(); | ||
rot.rotate(theta_around_y, m_uy); | ||
m_pos = m_pos * rot; | ||
m_ux = m_ux * rot; | ||
m_uz = m_uz * rot; | ||
} | ||
|
||
void Camera::move_forward(float distance) | ||
{ | ||
// recall that in OpenGL camera model, camera's z-axis points always | ||
// out of the screen (towards the user). | ||
m_pos -= distance * m_uz; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
#ifndef CAMERA_H | ||
#define CAMERA_H | ||
|
||
|
||
#include <qvector3d.h> | ||
#include <qmatrix4x4.h> | ||
|
||
|
||
class Camera | ||
{ | ||
public: | ||
|
||
Camera(); | ||
|
||
void set_pos(const QVector3D& pos) { m_pos = pos; } | ||
void set_pos(float x, float y, float z) { m_pos = QVector3D(x,y,z); } | ||
|
||
void perspective(float fov, float aspect_ratio, float z_near, float z_far); | ||
|
||
QMatrix4x4 get_view_matrix() const; | ||
QMatrix4x4 get_projection_matrix() const { return m_projection; } | ||
|
||
// rotate the camera around its own axes | ||
void rotate(float theta_around_x, float theta_around_y); | ||
|
||
// move the camera forward around its own z-axis | ||
void move_forward(float distance); | ||
|
||
private: | ||
QVector3D m_pos; | ||
QVector3D m_ux; | ||
QVector3D m_uy; | ||
QVector3D m_uz; | ||
|
||
QMatrix4x4 m_projection; | ||
}; | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
#ifndef COMMON_DEFS_H | ||
#define COMMON_DEFS_H | ||
|
||
#include <qopenglfunctions_3_3_core.h> | ||
//#include <qopenglfunctions_4_5_core.h> | ||
|
||
|
||
using OpenGLFunctionsBase = QOpenGLFunctions_3_3_Core; | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
|
||
#include "Shader_program.h" | ||
|
||
#include <iostream> | ||
|
||
#include "Tools.h" | ||
|
||
|
||
bool Shader_program::init() | ||
{ | ||
initializeOpenGLFunctions(); | ||
|
||
m_program = glCreateProgram(); | ||
if (!m_program) | ||
{ | ||
std::cout << "error creating shader program!\n"; | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
void Shader_program::add_shader(const char* shader_code, GLenum shader_type) | ||
{ | ||
GLuint the_shader = glCreateShader(shader_type); | ||
|
||
const GLchar* the_code[] = { shader_code }; | ||
GLint code_length[] = { strlen(shader_code) }; | ||
|
||
glShaderSource(the_shader, 1, the_code, code_length); | ||
glCompileShader(the_shader); | ||
|
||
|
||
GLint result = 0; | ||
GLchar elog[1024] = { 0 }; | ||
glGetShaderiv(the_shader, GL_COMPILE_STATUS, &result); | ||
if (!result) | ||
{ | ||
std::string shader_type_name; | ||
switch (shader_type) | ||
{ | ||
case GL_VERTEX_SHADER: shader_type_name = "VERTEX"; break; | ||
case GL_GEOMETRY_SHADER: shader_type_name = "GEOMETRY"; break; | ||
case GL_FRAGMENT_SHADER: shader_type_name = "FRAGMENT"; break; | ||
} | ||
glGetShaderInfoLog(the_shader, sizeof(elog), NULL, elog); | ||
std::cout << "! error compiling the " << shader_type_name << | ||
" shader:\n" << elog << std::endl; | ||
return; | ||
} | ||
|
||
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() | ||
{ | ||
GLint result = 0; | ||
GLchar elog[1024] = { 0 }; | ||
|
||
glLinkProgram(m_program); | ||
glGetProgramiv(m_program, GL_LINK_STATUS, &result); | ||
if (!result) | ||
{ | ||
glGetProgramInfoLog(m_program, sizeof(elog), NULL, elog); | ||
std::cout << "! error linking program:\n" << elog << std::endl; | ||
return false; | ||
} | ||
return true; | ||
} | ||
bool Shader_program::validate() | ||
{ | ||
GLint result = 0; | ||
GLchar elog[1024] = { 0 }; | ||
|
||
glValidateProgram(m_program); | ||
glGetProgramiv(m_program, GL_VALIDATE_STATUS, &result); | ||
if (!result) | ||
{ | ||
glGetProgramInfoLog(m_program, sizeof(elog), NULL, elog); | ||
std::cout << "! error validating program:\n" << elog << std::endl; | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
GLint Shader_program::get_uniform_location(const GLchar* name) | ||
{ | ||
const auto uniform_loc = glGetUniformLocation(m_program, name); | ||
return uniform_loc; | ||
} | ||
|
||
void Shader_program::use() | ||
{ | ||
glUseProgram(m_program); | ||
} | ||
void Shader_program::unuse() | ||
{ | ||
glUseProgram(0); | ||
} | ||
|
||
void Shader_program::set_uniform(GLint uniform_loc, const QMatrix4x4& m) | ||
{ | ||
glUniformMatrix4fv(uniform_loc, 1, GL_FALSE, m.data()); | ||
} | ||
void Shader_program::set_uniform(const GLchar* name, const QMatrix4x4& m) | ||
{ | ||
const auto uniform_loc = get_uniform_location(name); | ||
set_uniform(uniform_loc, m); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
#ifndef SHADER_PROGRAM_H | ||
#define SHADER_PROGRAM_H | ||
|
||
#include <qmatrix4x4.h> | ||
#include "Common_defs.h" | ||
|
||
|
||
class Shader_program : protected OpenGLFunctionsBase | ||
{ | ||
public: | ||
|
||
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(); | ||
|
||
GLint get_uniform_location(const GLchar* name); | ||
|
||
void use(); | ||
void unuse(); | ||
|
||
void set_uniform(GLint uniform_loc, const QMatrix4x4& m); | ||
void set_uniform(const GLchar* name, const QMatrix4x4& m); | ||
|
||
//private: | ||
GLuint m_program; | ||
}; | ||
|
||
|
||
#endif |
Oops, something went wrong.