Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Rabbid76 committed Aug 24, 2024
2 parents 294cc83 + 3cd9d9e commit b2dbd6b
Show file tree
Hide file tree
Showing 21 changed files with 839 additions and 433 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"cmake.configureOnOpen": true,
"cmake.sourceDirectory": "${workspaceFolder}/example/cpp"
"cmake.sourceDirectory": "${workspaceFolder}/example/cpp",
"cmake.debugConfig": {
"cwd": "${workspaceFolder}"
}
}
12 changes: 6 additions & 6 deletions documentation/model_transformation.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
<!-- TOC -->

- [Transformation of the model](#transformation-of-the-model)
- [Rotation matrix](#rotation-matrix)
- [Euler angles](#euler-angles)
- [Quaternion](#quaternion)
- [Rotate by quaternion](#rotate-by-quaternion)
- [Matrix calculus](#matrix-calculus)
- [Rotation matrix](#rotation-matrix)
- [Euler angles](#euler-angles)
- [Quaternion](#quaternion)
- [Rotate by quaternion](#rotate-by-quaternion)
- [Matrix calculus](#matrix-calculus)

<!-- /TOC -->

Expand Down Expand Up @@ -125,7 +125,7 @@ q = (c, ux*s, uy*s, uz*s)

## Matrix calculus

Matrix calculus, wikipedia [https://en.wikipedia.org/wiki/Matrix_calculus]
[Matrix calculus, wikipedia](https://en.wikipedia.org/wiki/Matrix_calculus)
> In mathematics, matrix calculus is a specialized notation for doing multivariable calculus, especially over spaces of matrices. It collects the various partial derivatives of a single function with respect to many variables, and/or of a multivariate function with respect to a single variable, into vectors and matrices that can be treated as single entities. This greatly simplifies operations such as finding the maximum or minimum of a multivariate function and solving systems of differential equations.
<br/><hr/>
Expand Down
8 changes: 5 additions & 3 deletions example/cpp/_util/include/gl/gl_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
// OpenGL
#include <gl/gl_glew.h>

#ifdef __APPLE__
#define OpenGL_program_introspection_old
#endif

using TShaderInfo = std::tuple< std::string, int >;
using TVec2 = std::array< float, 2 >;
Expand All @@ -26,9 +29,6 @@ using TVec4 = std::array< float, 4 >;
using TMat44 = std::array< TVec4, 4 >;
using TSize = std::array< int, 2 >;




namespace OpenGL
{
GLuint CreateProgram(const std::string& vsh, const std::string& fsh);
Expand Down Expand Up @@ -336,12 +336,14 @@ namespace OpenGL
// Get transform feedvback varyings
void GetTransformFeedbackVaryings(void)
{
#if not defined(OpenGL_program_introspection_old)
GetInterfaceResources(GL_TRANSFORM_FEEDBACK_VARYING, _transformFeedbackVaryings, TResourceKind::NON);

if (_verbose == false)
return;
for (auto& resource : _transformFeedbackVaryings)
std::cout << "transform feedback varyings " << resource.second << ": " << resource.first << std::endl;
#endif
}

// Get fragmetn outputs
Expand Down
11 changes: 3 additions & 8 deletions example/cpp/opengl/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# for mingw, the order of the libraries seems to matter: glew before opengl32
set(GLUT_LINK_LIBRARIES ${GLEW_LIB} ${DEFAULT_LIB} ${GLUT_LIB} ${FREETYPE_LIB})
if(NOT MAKE_CONFIG STREQUAL "MacXCode")
set(GLFW_LINK_LIBRARIES ${GLEW_LIB} ${DEFAULT_LIB} ${GLFW_LIB} ${FREETYPE_LIB})
else()
set(GLFW_LINK_LIBRARIES ${GLEW_LIB} ${DEFAULT_LIB} ${GLFW_LIB})
endif()
set(SDL_LINK_LIBRARIES ${GLEW_LIB} ${DEFAULT_LIB} ${SDL_LIB} ${FREETYPE_LIB})
set(SFML_LINK_LIBRARIES ${GLEW_LIB} ${DEFAULT_LIB} ${SFML_LIB} ${FREETYPE_LIB})

Expand Down Expand Up @@ -56,15 +52,14 @@ endif()

# GLFW
glfw_exe(example_glfw_1 example_glfw_1.cpp)
glfw_exe(example_glfw_2_perpspcetive example_glfw_2_perpspcetive.cpp)

if(NOT MAKE_CONFIG STREQUAL "MacXCode")
glfw_exe(example_glfw_text_freetype example_glfw_text_freetype.cpp)
glfw_exe(example_glfw_fps example_glfw_fps.cpp)
glfw_exe(example_glfw_2_perpspcetive example_glfw_2_perpspcetive.cpp)
glfw_exe(example_glm_animation_1 example_glm_animation_1.cpp)
glfw_exe(example_barycentric_cordinates_print example_barycentric_cordinates_print.cpp)
glfw_exe(example_mesh_1_instancing example_mesh_1_instancing.cpp)
glfw_exe(example_mesh_1 example_mesh_1.cpp)

if(NOT MAKE_CONFIG STREQUAL "MacXCode")
glfw_exe(example_mesh_2_tetrahedron example_mesh_2_tetrahedron.cpp)
glfw_exe(example_mesh_3_a_cube example_mesh_3_a_cube.cpp)
glfw_exe(example_mesh_3_b_cube example_mesh_3_b_cube.cpp)
Expand Down
30 changes: 17 additions & 13 deletions example/cpp/opengl/example_barycentric_cordinates_print.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include <stdafx.h>

#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GLFW/glfw3.h>

#include <glm/glm.hpp>
Expand Down Expand Up @@ -100,15 +98,17 @@ class Window
public:

Window( std::array< int, 2 > size )
: _vpSize( size )
{
if ( glfwInit() == 0 )
throw std::runtime_error( "error initializing glfw" );

//glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
//glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
//glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

#ifdef __APPLE__
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
glewExperimental = true;
#endif
Init( size[0], size[1] );

std::string ver = (const char*)glGetString( GL_VERSION );
Expand Down Expand Up @@ -140,6 +140,7 @@ class Window
_monitor = glfwGetPrimaryMonitor();

// get window size and posiiton and invalidate viewport size
glfwGetFramebufferSize(_wnd, &_vpSize[0], &_vpSize[1]);
glfwGetWindowSize(_wnd, &_wndSize[0], &_wndSize[1]);
glfwGetWindowPos(_wnd, &_wndPos[0], &_wndPos[1]);
_updateViewport = true;
Expand Down Expand Up @@ -217,7 +218,7 @@ class Window
{
if ( button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS )
{
vec3 b_coord = BarycentricCoordinates( _cursorPos[0], _cursorPos[1], (double)_vpSize[0], (double)_vpSize[1] );
vec3 b_coord = BarycentricCoordinates( _cursorPos[0], _cursorPos[1], (double)_wndSize[0], (double)_wndSize[1] );
std::cout << std::fixed << std::setprecision( 3 ) << "barycentric : (" << b_coord[0] << ", " << b_coord[1] << ", " << b_coord[2] << ")" << std::endl;
}

Expand Down Expand Up @@ -264,9 +265,10 @@ class Window
glfwGetFramebufferSize( _wnd, &_vpSize[0], &_vpSize[1] );
glViewport( 0, 0, _vpSize[0], _vpSize[1] );
_updateViewport = false;
glfwGetWindowSize(_wnd, &_wndSize[0], &_wndSize[1]);
}

vec3 b_coord = BarycentricCoordinates( _cursorPos[0], _cursorPos[1], (double)_vpSize[0], (double)_vpSize[1] );
vec3 b_coord = BarycentricCoordinates( _cursorPos[0], _cursorPos[1], (double)_wndSize[0], (double)_wndSize[1] );
glClearColor(b_coord[0] < 0.0f ? 1.0f : 0.0f, b_coord[1] < 0.0f ? 1.0f : 0.0f, b_coord[2] < 0.0f ? 1.0f : 0.0f, 0.0f);

Render();
Expand Down Expand Up @@ -309,13 +311,14 @@ class Window
};

static const std::string shader_vert = R"(
#version 460
#version 410 core
layout (location=0) in vec3 in_pos;
layout (location=1) in vec4 in_col;
// layout locations are distributed incrementally to the members, starting with the specified location
layout (location=0) out TVertexData
//layout (location=0)
out TVertexData
{
vec4 color;
} out_data;
Expand All @@ -328,10 +331,11 @@ void main ()
)";

static const std::string shader_frag = R"(
#version 460
#version 410 core
// layout locations are distributed incrementally to the members, starting with the specified location
layout (location=0) in TVertexData
//layout (location=0)
in TVertexData
{
vec4 color;
} in_data;
Expand Down
3 changes: 1 addition & 2 deletions example/cpp/opengl/example_glfw_2_perpspcetive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ int main(void)
//glfwSwapInterval( 2 );

std::cout << "Current path is " << std::filesystem::current_path() << '\n';
//std::string path = "C:/source/rabbid76workbench/test/texture/boomerang.png";
std::string path = "../../resource/texture/ObjectSheet.png";
std::string path = "./resource/texture/ObjectSheet.png";
int cx, cy, ch;
stbi_uc *img = stbi_load( path.c_str(), &cx, &cy, &ch, 4 );
if ( img == nullptr )
Expand Down
50 changes: 20 additions & 30 deletions example/cpp/opengl/example_glfw_text_freetype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@
#include <stdexcept>
#include <chrono>


// GLM [https://glm.g-truc.net/0.9.9/api/index.html]
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>


// OpenGL
#include <gl/gl_glew.h>


// GLFW [https://www.glfw.org/]
#include <GLFW/glfw3.h>


// project includes
#include <gl/gl_debug.h>
#include <gl/gl_shader.h>
Expand All @@ -33,13 +29,12 @@
#define M_PI 3.14159265358979323846
#endif

const char* fontfilename = "../../../../../../resource/font/FreeSans.ttf";
//const char* fontfilename = "C:/source/graphics-snippets/resource/font/FreeSans.ttf";
const char* fontfilename = "./resource/font/FreeSans.ttf";

// shader

std::string sh_vert = R"(
#version 460
#version 410 core
layout (location = 0) in vec4 in_attr;
Expand All @@ -51,16 +46,16 @@ uniform mat4 model;
void main()
{
vUV = in_attr.zw;
gl_Position = projection * model * vec4(in_attr.xy, 0.0, 1.0);
gl_Position = projection * model * vec4(in_attr.xy, 0.0, 1.0);
}
)";

std::string sh_frag = R"(
#version 460
#version 410 core
in vec2 vUV;
layout (binding=0) uniform sampler2D u_texture;
uniform sampler2D u_texture;
uniform vec3 textColor;
Expand All @@ -74,10 +69,6 @@ void main()
}
)";


// Properties
const GLuint WIDTH = 800, HEIGHT = 600;

/// Holds all state information relevant to a character as loaded using FreeType
struct Character {
GLuint TextureID; // ID handle of the glyph texture
Expand All @@ -97,26 +88,28 @@ int main()
{
// Init GLFW
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
#ifdef __APPLE__
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
glewExperimental = true;
#endif
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr); // Windowed
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr); // Windowed
glfwMakeContextCurrent(window);

// Initialize GLEW to setup the OpenGL Function pointers
glewExperimental = GL_TRUE;
glewInit();

// Define the viewport dimensions
glViewport(0, 0, WIDTH, HEIGHT);

// Set OpenGL options
//glCullFace( GL_BACK );
//glFrontFace( GL_CW );
//glEnable(GL_CULL_FACE);
int vpSize[2];
glfwGetFramebufferSize(window, &vpSize[0], &vpSize[1]);
float dpiScale = static_cast<GLfloat>(vpSize[0]) / 800.0f;

// Define the viewport dimensions
glViewport(0, 0, vpSize[0], vpSize[1]);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Expand All @@ -128,10 +121,7 @@ int main()
{ sh_frag, GL_FRAGMENT_SHADER }
});

//glm::mat4 projection = glm::ortho(0.0f, static_cast<GLfloat>(WIDTH), 0.0f, static_cast<GLfloat>(HEIGHT));

glm::mat4 projection = glm::ortho(0.0f, static_cast<GLfloat>(WIDTH), static_cast<GLfloat>(HEIGHT), 0.0f);

glm::mat4 projection = glm::ortho(0.0f, static_cast<GLfloat>(vpSize[0]), static_cast<GLfloat>(vpSize[1]), 0.0f);

shader.Use();
GLint project_loc = glGetUniformLocation(shader.Prog(), "projection");
Expand Down Expand Up @@ -219,8 +209,8 @@ int main()
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

RenderText1(shader, "This is sample text", 25.0f, 50.0f, 1.0f, glm::vec3(0.5, 0.8f, 0.2f));
RenderText2(shader, "(C) LearnOpenGL.com", 35.0f, 200.0f, 0.8f, glm::vec3(0.3, 0.7f, 0.9f), glm::vec2(1.0f, -0.25f));
RenderText1(shader, "This is sample text", dpiScale * 25.0f, dpiScale * 50.0f, dpiScale, glm::vec3(0.5, 0.8f, 0.2f));
RenderText2(shader, "(C) LearnOpenGL.com", dpiScale * 35.0f, dpiScale * 200.0f, dpiScale * 0.8f, glm::vec3(0.3, 0.7f, 0.9f), glm::vec2(1.0f, -0.25f));

// Swap the buffers
glfwSwapBuffers(window);
Expand Down
13 changes: 9 additions & 4 deletions example/cpp/opengl/example_glm_animation_1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

// OpenGL
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glu.h>

// glad
//#include <glad/glad.h>
Expand Down Expand Up @@ -50,7 +48,7 @@
#endif

std::string sh_vert = R"(
#version 400
#version 410 core
layout (location = 0) in vec3 inPos;
layout (location = 1) in vec4 inColor;
Expand All @@ -73,7 +71,7 @@ void main()
)";

std::string sh_frag = R"(
#version 400
#version 410 core
in vec3 vertPos;
in vec4 vertCol;
Expand All @@ -95,6 +93,13 @@ int main(void)
if ( glfwInit() == 0 )
throw std::runtime_error( "error initializing glfw" );

#ifdef __APPLE__
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
glewExperimental = true;
#endif
GLFWwindow *wnd = glfwCreateWindow( 800, 600, "OGL window", nullptr, nullptr );
if ( wnd == nullptr )
{
Expand Down
Loading

0 comments on commit b2dbd6b

Please sign in to comment.