Created
November 13, 2024 01:19
-
-
Save 8Observer8/96f061b23f4b874e91e2c0a4042f127e to your computer and use it in GitHub Desktop.
Line drawer in 3D to draw Bullet Physics colliders and rays using SDL3 and OpenGL for Desktop and WebAssembly
This file contains hidden or 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
#include <cmath> | |
#include "line_drawer.h" | |
#include "math_helper.h" | |
#include <iostream> | |
LineDrawer::LineDrawer(GLuint program, const glm::mat4 &projViewMatrix) | |
: m_program(program) | |
, m_projViewMatrix(projViewMatrix) | |
{ | |
glUseProgram(m_program); | |
m_aPositionLocation = glGetAttribLocation(m_program, "aPosition"); | |
m_uColorLocation = glGetUniformLocation(m_program, "uColor"); | |
m_uMvpMatrixLocation = glGetUniformLocation(m_program, "uMvpMatrix"); | |
// Create a cube | |
// v6----- v5 | |
// /| /| | |
// v1------v0| | |
// | | | | | |
// | |v7---|-|v4 | |
// |/ |/ | |
// v2------v3 | |
float vertPositions[] = { | |
0.5f, 0.5f, 0.5f, // v0 | |
-0.5f, 0.5f, 0.5f, // v1 | |
-0.5f, -0.5f, 0.5f, // v2 | |
0.5f, -0.5f, 0.5f, // v3 | |
0.5f, -0.5f, -0.5f, // v4 | |
0.5f, 0.5f, -0.5f, // v5 | |
-0.5f, 0.5f, -0.5f, // v6 | |
-0.5f, -0.5f, -0.5f // v7 | |
}; | |
glGenBuffers(1, &m_vertPosBuffer); | |
glBindBuffer(GL_ARRAY_BUFFER, m_vertPosBuffer); | |
int amount = sizeof(vertPositions) / sizeof(vertPositions[0]); | |
glBufferData(GL_ARRAY_BUFFER, amount * sizeof(GLfloat), | |
vertPositions, GL_STATIC_DRAW); | |
int indices[] = { | |
0, 1, 2, 0, 2, 3, // front | |
0, 3, 4, 0, 4, 5, // right | |
0, 5, 6, 0, 6, 1, // up | |
1, 6, 7, 1, 7, 2, // left | |
7, 4, 3, 7, 3, 2, // down | |
4, 7, 6, 4, 6, 5 // back | |
}; | |
glGenBuffers(1, &m_indexBuffer); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffer); | |
amount = sizeof(indices) / sizeof(indices[0]); | |
glBufferData(GL_ELEMENT_ARRAY_BUFFER, amount * sizeof(int), | |
indices, GL_STATIC_DRAW); | |
m_amountOfVertices = amount; | |
} | |
void LineDrawer::setProjViewMatrix(const glm::mat4 &projViewMatrix) | |
{ | |
m_projViewMatrix = projViewMatrix; | |
} | |
void LineDrawer::bind() | |
{ | |
// Bind the vertex position buffer | |
glUseProgram(m_program); | |
glBindBuffer(GL_ARRAY_BUFFER, m_vertPosBuffer); | |
glVertexAttribPointer(m_aPositionLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); | |
glEnableVertexAttribArray(m_aPositionLocation); | |
// Bind the index buffer | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffer); | |
} | |
void LineDrawer::draw(const glm::vec3 &from, const glm::vec3 &to, | |
const glm::vec3 &color, float thickness) | |
{ | |
bind(); | |
glm::vec3 v = to - from; | |
glm::vec3 center = to - v / 2.f; | |
float length = glm::length(v); | |
// Normalize | |
glm::vec3 norm = glm::normalize(v); | |
// Calculate a rotation | |
glm::quat q = MathHelper::rotationTo(glm::vec3(1.f, 0.f, 0.f), norm); | |
// Translate | |
m_modelMatrix = glm::translate(glm::mat4(1.f), center); | |
// Rotate | |
glm::mat4 rotationMatrix = glm::mat4_cast(q); | |
m_modelMatrix = m_modelMatrix * rotationMatrix; | |
// Scale | |
m_modelMatrix = glm::scale(m_modelMatrix, glm::vec3(length, thickness, thickness)); | |
m_mvpMatrix = m_projViewMatrix * m_modelMatrix; | |
glUniform3fv(m_uColorLocation, 1, glm::value_ptr(color)); | |
glUniformMatrix4fv(m_uMvpMatrixLocation, 1, false, glm::value_ptr(m_mvpMatrix)); | |
glDrawElements(GL_TRIANGLES, m_amountOfVertices, GL_UNSIGNED_INT, 0); | |
} |
This file contains hidden or 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
#ifndef LINE_DRAWER_H | |
#define LINE_DRAWER_H | |
#ifdef __EMSCRIPTEN__ | |
#include <emscripten/html5.h> | |
#include <SDL3/SDL_opengles2.h> | |
#else | |
#include <glad/glad.h> | |
#endif // __EMSCRIPTEN__ | |
#define GLM_FORCE_PURE | |
#include <glm/glm.hpp> | |
#include <glm/gtc/matrix_transform.hpp> | |
#include <glm/gtc/type_ptr.hpp> | |
#define GLM_ENABLE_EXPERIMENTAL | |
#include <glm/gtx/transform.hpp> | |
class LineDrawer | |
{ | |
public: | |
LineDrawer(GLuint program, const glm::mat4 &projViewMatrix); | |
void draw(const glm::vec3 &from, const glm::vec3 &to, | |
const glm::vec3 &color, float thickness = 0.1f); | |
void setProjViewMatrix(const glm::mat4 &projViewMatrix); | |
private: | |
void bind(); | |
private: | |
GLuint m_program; | |
GLuint m_vertPosBuffer; | |
GLuint m_indexBuffer; | |
GLint m_aPositionLocation; | |
GLint m_uMvpMatrixLocation; | |
GLint m_uColorLocation; | |
glm::mat4 m_projViewMatrix; | |
glm::mat4 m_modelMatrix; | |
glm::mat4 m_mvpMatrix; | |
int m_amountOfVertices; | |
}; | |
#endif // LINE_DRAWER_H |
Author
8Observer8
commented
Nov 13, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment