Skip to content

Commit

Permalink
Merge branch 'gsoc2023-aos_sphere_demo-denizdiktas' of github.com:CGA…
Browse files Browse the repository at this point in the history
…L/cgal-public-dev into gsoc2023-aos_sphere_demo-denizdiktas
  • Loading branch information
efifogel committed Jun 13, 2023
2 parents 7ffda35 + 2ee3832 commit e4840f5
Show file tree
Hide file tree
Showing 15 changed files with 670 additions and 427 deletions.
28 changes: 26 additions & 2 deletions Arrangement_on_surface_2/demo/earth/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,23 @@ qt_standard_project_setup()

qt_add_executable(earth
main.cpp
mainwidget.cpp mainwidget.h
mainwidget.h mainwidget.cpp
Camera.h Camera.cpp
Common_defs.h
Shader_program.h Shader_program.cpp
Sphere.h Sphere.cpp
Tools.h
)


set_target_properties(earth PROPERTIES
WIN32_EXECUTABLE TRUE
MACOSX_BUNDLE TRUE
)

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SUBSYSTEM:CONSOLE")
if (MSVC)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SUBSYSTEM:CONSOLE")
endif (MSVC)

target_link_libraries(earth PRIVATE
Qt6::Core
Expand All @@ -35,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
50 changes: 50 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Camera.cpp
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;
}
40 changes: 40 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Camera.h
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
12 changes: 12 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Common_defs.h
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
114 changes: 114 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Shader_program.cpp
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);
}
34 changes: 34 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Shader_program.h
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
Loading

0 comments on commit e4840f5

Please sign in to comment.