Compare commits
7 Commits
Prerelease
...
Prerelease
Author | SHA1 | Date | |
---|---|---|---|
ca7abb3044 | |||
14c45ab0f1 | |||
1ca5e5a694 | |||
25fc3f8698 | |||
a836fc7b32 | |||
e6dcc9d61e | |||
6dff2f97c1 |
1001
include/JGL/JGL.h
1001
include/JGL/JGL.h
File diff suppressed because it is too large
Load Diff
9
include/JGL/ShapeCache.h
Normal file
9
include/JGL/ShapeCache.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
#include <JGL/types/VRamList.h>
|
||||
#include <array>
|
||||
|
||||
namespace JGL::ShapeCache {
|
||||
inline VRamList* cube_vertex_data = nullptr;
|
||||
inline VRamList* cube_index_data = nullptr;
|
||||
void Init();
|
||||
}
|
@@ -20,15 +20,15 @@ private:
|
||||
bool spin_lock = false;
|
||||
void load(const GLfloat* data, const long& size);
|
||||
void load(const GLuint* data, const long& size);
|
||||
void SetData(void* data, const long& length);
|
||||
void UpdateData(void* data, const long& offset, const long& length);
|
||||
void SetData(void* data, const long& count);
|
||||
void UpdateData(void* data, const long& offset, const long& count);
|
||||
void Erase();
|
||||
public:
|
||||
VRamList(const GLuint* data, const long& length);
|
||||
VRamList(const GLfloat* data, const long& length);
|
||||
VRamList(const Vector2* data, const long& length);
|
||||
VRamList(const Vector3* data, const long& length);
|
||||
VRamList(const Vector4* data, const long& length);
|
||||
VRamList(const GLuint* data, const long& count);
|
||||
VRamList(const GLfloat* data, const long& count);
|
||||
VRamList(const Vector2* data, const long& count);
|
||||
VRamList(const Vector3* data, const long& count);
|
||||
VRamList(const Vector4* data, const long& count);
|
||||
|
||||
~VRamList();
|
||||
/** Copying around the VBO data to a new VBO like this is slow.
|
||||
@@ -46,25 +46,23 @@ public:
|
||||
[[nodiscard]] std::vector<GLfloat> GetDataF() const;
|
||||
[[nodiscard]] std::vector<GLuint> GetDataUI() const;
|
||||
[[nodiscard]] bool IsFloatArray() const;
|
||||
/** Replace the data of an existing VBO in it's entirety. Must be same type.
|
||||
* "length" refers to the number of elements in data. Not the number of bytes. */
|
||||
void SetData(const GLfloat* data, const long& length);
|
||||
void SetData(const Vector2* data, const long& length);
|
||||
void SetData(const Vector3* data, const long& length);
|
||||
void SetData(const Vector4* data, const long& length);
|
||||
/** Replace the data of an existing VBO in it's entirety. Must be same type. */
|
||||
void SetData(const GLfloat* data, const long& count);
|
||||
void SetData(const Vector2* data, const long& count);
|
||||
void SetData(const Vector3* data, const long& count);
|
||||
void SetData(const Vector4* data, const long& count);
|
||||
|
||||
void SetData(const GLuint* data, const long& length);
|
||||
void SetData(const Vector2i* data, const long& length);
|
||||
void SetData(const GLuint* data, const long& count);
|
||||
void SetData(const Vector2i* data, const long& count);
|
||||
|
||||
/** Update only a portion of the data in a VBO. Must be same type.
|
||||
* "length" refers to the number of elements in data. Not the number of bytes.
|
||||
* "offset" refers the number of Typename T into the buffer the data you want to change is.
|
||||
* For ex, offset 0 and length of 1 overwrites the first value. Offset 1 the second etc */
|
||||
void UpdateData(const GLfloat* data, const long& offset, const long& length);
|
||||
void UpdateData(const Vector2* data, const long& offset, const long& length);
|
||||
void UpdateData(const Vector3* data, const long& offset, const long& length);
|
||||
void UpdateData(const Vector4* data, const long& offset, const long& length);
|
||||
void UpdateData(const GLfloat* data, const long& offset, const long& count);
|
||||
void UpdateData(const Vector2* data, const long& offset, const long& count);
|
||||
void UpdateData(const Vector3* data, const long& offset, const long& count);
|
||||
void UpdateData(const Vector4* data, const long& offset, const long& count);
|
||||
|
||||
void UpdateData(const GLuint* data, const long& offset, const long& length);
|
||||
void UpdateData(const Vector2i* data, const long& offset, const long& length);
|
||||
void UpdateData(const GLuint* data, const long& offset, const long& count);
|
||||
void UpdateData(const Vector2i* data, const long& offset, const long& count);
|
||||
};
|
34
main.cpp
34
main.cpp
@@ -4,6 +4,7 @@
|
||||
#include <chrono>
|
||||
#include <J3ML/LinearAlgebra/Vector2.hpp>
|
||||
#include <JGL/logger/logger.h>
|
||||
#include <J3ML/Geometry/AABB.hpp>
|
||||
|
||||
using J3ML::LinearAlgebra::Vector2;
|
||||
using namespace JGL;
|
||||
@@ -151,7 +152,7 @@ public:
|
||||
//J3D::ChangeFOV(fov);
|
||||
|
||||
sprite_radians += 0.005;
|
||||
textAngle.y += (5.f * delta_time);
|
||||
textAngle.y += (1);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
@@ -159,17 +160,22 @@ public:
|
||||
camera->render();
|
||||
// All 3D elements of the scene and JGL elements *must* be rendered before the 2D stuff
|
||||
/* if rendering to screen space directly. */
|
||||
|
||||
// If a 3D object has transparency. The things you'd like to see through it must be drawn before.
|
||||
J3D::Begin();
|
||||
J3D::DrawLine(Colors::Red, {-0.33,-0.125,1}, {-1,-0.125,1});
|
||||
J3D::DrawLine(Colors::Red, {-0.33,-0.125,1}, {-0.33,0.25,1});
|
||||
J3D::DrawString(Colors::Red, "JGL Sample Text", {-0.33, -0.1, 1.0f}, 1.f, 32, FreeSans, textAngle * 100, true);
|
||||
J3D::DrawString(Colors::Red, "JGL Sample Text", {-0.33, -0.1, 1.0f}, 1.f, 32, FreeSans, textAngle, true);
|
||||
//J3D::WireframeSphere(Colors::Green, {0,0,0.5f}, 0.25f, 1, 128, 128);
|
||||
Sphere sphere[5] = {{{0,0, 0.5f}, 0.2125}, {{0,0, 0.5f}, 0.025},{{0,0, 0.5f}, 0.05},{{0,0, 0.5f}, 0.075},{{0,0, 0.5f}, 0.1}};
|
||||
J3D::BatchWireframeRevoSphere(Colors::Green, sphere, 1, 1, 16, 16, true);
|
||||
//J3D::FillAABB(Colors::Whites::AliceBlue, {0,0,0.5f}, {0.1f, 0.05f, 0.1f});
|
||||
//J3D::WireframeAABB(Colors::Gray, {0,0,0.5f}, {0.11f, 0.06f, 0.11f});
|
||||
Sphere sphere = {{0,0, 0.5f}, 0.2125};
|
||||
J3D::BatchWireframeRevoSphere(Colors::Green, &sphere, 1, 1, 16, 16, true);
|
||||
J3D::FillAABB(Colors::Whites::AliceBlue, {0,0,0.5f}, {0.1f, 0.1f, 0.1f});
|
||||
J3D::WireframeAABB(Colors::Gray, {0,0,0.5f}, {0.11f, 0.06f, 0.11f});
|
||||
|
||||
J3D::WireframeOBB(Colors::Red, {0, 0, 1.5f}, {0.40f, 0.10f, 0.10f}, {0,textAngle.y, 0});
|
||||
AABB boxes[1] = {{Vector3(-0.2125, -0.2125,0.28750), Vector3(0.2125,0.2125,0.7125)}};
|
||||
J3D::BatchWireframeAABB(Colors::Yellow, boxes, 1, 1);
|
||||
//J3D::WireframeOBB(Colors::Red, {0, 0, 1.5f}, {0.40f, 0.10f, 0.10f}, {0,textAngle.y, 0});
|
||||
//J3D::FillSphere({0,255,0,120}, sphere);
|
||||
|
||||
//J3D::DrawCubicBezierCurve(Colors::Blue, {0,0,0.3}, {0,0,0.5}, {0.2,0,0.3}, {0.2, 0.3, 0.1}, 30);
|
||||
|
||||
@@ -221,7 +227,8 @@ public:
|
||||
//Draw the Render Target that we just drew all that stuff onto.
|
||||
|
||||
J2D::Begin();
|
||||
J2D::DrawSprite(j2d_render_target, {0, 0}, 0, {0.5, 0.5}, {1,1}, Colors::White);
|
||||
J2D::DrawPartialRenderTarget(j2d_render_target, {0, 0}, {0,0}, {512, 512});
|
||||
//J2D::DrawSprite(j2d_render_target, {0, 0}, 0, {0.5, 0.5}, {1,1}, Colors::White);
|
||||
J2D::DrawSprite(image2_render_target, {300, 500}, 0, {0.5, 0.5}, {1,1}, Colors::White);
|
||||
J2D::End();
|
||||
|
||||
@@ -241,6 +248,17 @@ public:
|
||||
if (isKeyDown(Keys::LeftShift))
|
||||
camera->position.y -= 1.f * elapsed;
|
||||
|
||||
/* This is wrong of course. Just for testing purposes.
|
||||
if (isKeyDown(Keys::W))
|
||||
camera->position.z += 1.f * elapsed;
|
||||
if (isKeyDown(Keys::S))
|
||||
camera->position.z -= 1.f * elapsed;
|
||||
if (isKeyDown(Keys::A))
|
||||
camera->position.x += 1.f * elapsed;
|
||||
if (isKeyDown(Keys::D))
|
||||
camera->position.x -= 1.f * elapsed;
|
||||
*/
|
||||
|
||||
auto mouse = GetMouseCoordinates();
|
||||
a.Update(mouse);
|
||||
b.Update(mouse);
|
||||
|
398
src/JGL.cpp
398
src/JGL.cpp
@@ -11,6 +11,7 @@
|
||||
#include <J3ML/Geometry/OBB.hpp>
|
||||
#include <JGL/types/VRamList.h>
|
||||
#include <JGL/types/Font.h>
|
||||
#include <JGL/ShapeCache.h>
|
||||
|
||||
JGL::RenderTarget* render_target = nullptr;
|
||||
GLfloat oldColor[4] = {0, 0, 0, 1};
|
||||
@@ -46,6 +47,7 @@ namespace JGL {
|
||||
}
|
||||
|
||||
InitTextEngine();
|
||||
ShapeCache::Init();
|
||||
wS = window_size;
|
||||
j3d_fov = fovY;
|
||||
j3d_far_plane = far_plane;
|
||||
@@ -390,6 +392,41 @@ namespace JGL {
|
||||
if (rt.TextureCreatedByRenderTarget())
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
}
|
||||
|
||||
void J2D::DrawPartialRenderTarget(const JGL::RenderTarget & rt, const Vector2& position, const Vector2& sub_texture_position, const Vector2& sub_texture_size,
|
||||
float rad_rotation, const Vector2& origin, const Vector2& scale, const Color4& color, JGL::Direction inversion) {
|
||||
|
||||
//Correct for the render-target being upside-down.
|
||||
Direction d{};
|
||||
if (inversion == Direction::None && !(rt.GetJGLTexture()->GetFlags() & INVERT_Y))
|
||||
d = Direction::Vertical;
|
||||
|
||||
else if (inversion == Direction::Horizontal) {
|
||||
d = Direction::Horizontal;
|
||||
if (!(rt.GetJGLTexture()->GetFlags() & INVERT_Y))
|
||||
d = Direction::Horizontal | Direction::Vertical;
|
||||
}
|
||||
else if (inversion& Direction::Horizontal && inversion& Direction::Vertical) {
|
||||
d = Direction::Horizontal;
|
||||
if (!(rt.GetJGLTexture()->GetFlags() & INVERT_Y))
|
||||
d = Direction::Horizontal | Direction::Vertical;
|
||||
}
|
||||
|
||||
//Change the blending mode such that the alpha doesn't get multiplied again.
|
||||
if (rt.TextureCreatedByRenderTarget())
|
||||
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
J2D::DrawPartialSprite(*rt.GetJGLTexture(), position, sub_texture_position, sub_texture_size, rad_rotation, origin, scale, color, d);
|
||||
|
||||
if (rt.TextureCreatedByRenderTarget())
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
}
|
||||
|
||||
void J2D::DrawPartialRenderTarget(const JGL::RenderTarget* rt, const Vector2& position,const Vector2& sub_texture_position,const Vector2& sub_texture_size, float rad_rotation,
|
||||
const Vector2& origin, const Vector2& scale, const Color4& color, Direction inversion) {
|
||||
DrawPartialRenderTarget(*rt, position, sub_texture_position, sub_texture_size, rad_rotation, origin, scale, color, inversion);
|
||||
}
|
||||
|
||||
void J2D::DrawSprite(const Texture& texture, const Vector2& pos, float rad_rotation, const Vector2& origin,
|
||||
const Vector2& scale, const Color4& color, Direction inversion) {
|
||||
if (!inJ2D)
|
||||
@@ -462,7 +499,7 @@ namespace JGL {
|
||||
color, inversion);
|
||||
}
|
||||
|
||||
void J2D::DrawPartialSprite(const Texture& texture, const Vector2 &position, const Vector2& sub_texture_position,
|
||||
void J2D::DrawPartialSprite(const Texture& texture, const Vector2& position, const Vector2& sub_texture_position,
|
||||
const Vector2& sub_texture_size, float rad_rotation, const Vector2& origin,
|
||||
const Vector2& scale, const Color4& color, Direction inversion) {
|
||||
if (!inJ2D)
|
||||
@@ -731,7 +768,7 @@ namespace JGL {
|
||||
}
|
||||
|
||||
//TODO render all in once pass with GL_LINE_LOOP instead of separate lines.
|
||||
void J2D::DrawCubicBezierCurve(const Color4 &color, const Vector2& controlA, const Vector2& pointA, const Vector2& pointB, const Vector2& controlB,
|
||||
void J2D::DrawCubicBezierCurve(const Color4& color, const Vector2& controlA, const Vector2& pointA, const Vector2& pointB, const Vector2& controlB,
|
||||
int subdivisions, float thickness) {
|
||||
|
||||
|
||||
@@ -763,7 +800,7 @@ namespace JGL {
|
||||
glColor4fv(baseColor);
|
||||
}
|
||||
|
||||
void J2D::DrawGradientLine(const Color4 &color_a, const Color4 &color_b, float x, float y, float w, float h,
|
||||
void J2D::DrawGradientLine(const Color4& color_a, const Color4& color_b, float x, float y, float w, float h,
|
||||
float thickness) {
|
||||
DrawGradientLine(color_a, color_b, {x, y}, {w, h}, thickness);
|
||||
}
|
||||
@@ -801,7 +838,7 @@ namespace JGL {
|
||||
glColor4fv(baseColor);
|
||||
}
|
||||
|
||||
void J2D::OutlineRoundedRect(const Color4 &color, const Vector2 &pos, const Vector2 &size, float radius, float thickness)
|
||||
void J2D::OutlineRoundedRect(const Color4& color, const Vector2& pos, const Vector2& size, float radius, float thickness)
|
||||
{
|
||||
// A rounded rectangle of size 2a x 2b with rounding radius r is given by
|
||||
// f(x; a, r) + f(y; b, r) = 1
|
||||
@@ -865,7 +902,7 @@ namespace JGL {
|
||||
|
||||
}
|
||||
|
||||
void J2D::FillChamferRect(const Color4 &color, const Vector2 &pos, const Vector2 &size, float radius) {
|
||||
void J2D::FillChamferRect(const Color4& color, const Vector2& pos, const Vector2& size, float radius) {
|
||||
FillRoundedRect(color, pos, size, radius, 4);
|
||||
}
|
||||
|
||||
@@ -900,14 +937,12 @@ namespace JGL {
|
||||
FillTriangle(color, {triA, triB, triC});
|
||||
}
|
||||
|
||||
void
|
||||
J2D::FillGradientTriangle(const Color4 &a_color, const Color4 &b_color, const Color4 &c_color, const Vector2 &tri_a,
|
||||
const Vector2 &tri_b, const Vector2 &tri_c) {
|
||||
void J2D::FillGradientTriangle(const Color4& a_color, const Color4& b_color, const Color4& c_color, const Vector2& tri_a,
|
||||
const Vector2& tri_b, const Vector2& tri_c) {
|
||||
FillGradientTriangle(a_color, b_color, c_color, {tri_a, tri_b, tri_c});
|
||||
}
|
||||
|
||||
void
|
||||
J2D::OutlineEllipse(const Color4 &color, const Vector2 &position, float radius_x, float radius_y, float thickness,
|
||||
void J2D::OutlineEllipse(const Color4& color, const Vector2& position, float radius_x, float radius_y, float thickness,
|
||||
int subdivisions) {
|
||||
if (!inJ2D)
|
||||
Logger::Error("Drawing J2D element before J2D begin.");
|
||||
@@ -934,8 +969,7 @@ namespace JGL {
|
||||
glColor4fv(baseColor);
|
||||
}
|
||||
|
||||
void
|
||||
J2D::FillEllipse(const Color4 &color, const Vector2 &position, float radius_x, float radius_y, int subdivisions) {
|
||||
void J2D::FillEllipse(const Color4& color, const Vector2& position, float radius_x, float radius_y, int subdivisions) {
|
||||
if (!inJ2D)
|
||||
Logger::Error("Drawing J2D element before J2D begin.");
|
||||
|
||||
@@ -997,10 +1031,11 @@ namespace JGL {
|
||||
glGetFloatv(GL_CURRENT_COLOR, oldColor);
|
||||
glColor4fv(baseColor);
|
||||
|
||||
wasDepthTestEnabled = false;
|
||||
if (glIsEnabled(GL_DEPTH_TEST))
|
||||
wasDepthTestEnabled = true,
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
if (!glIsEnabled(GL_DEPTH_TEST))
|
||||
wasDepthTestEnabled = false,
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
else
|
||||
wasDepthTestEnabled = true;
|
||||
|
||||
wasVertexArraysEnabled = false;
|
||||
if (!glIsEnabled(GL_VERTEX_ARRAY))
|
||||
@@ -1034,8 +1069,8 @@ namespace JGL {
|
||||
}
|
||||
|
||||
void J3D::End() {
|
||||
if (wasDepthTestEnabled)
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
if (!wasDepthTestEnabled)
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
|
||||
if (!wasVertexArraysEnabled)
|
||||
glDisableClientState(GL_VERTEX_ARRAY);
|
||||
@@ -1070,16 +1105,16 @@ namespace JGL {
|
||||
glColor4fv(baseColor);
|
||||
}
|
||||
|
||||
void J3D::WireframeSphere(const Color4& color, const Vector3& position, float radius, float thickness, uint sectors, uint stacks) {
|
||||
void J3D::WireframeSphere(const Color4& color, const Vector3& position, float radius, float thickness, unsigned int sectors, unsigned int stacks) {
|
||||
Sphere sphere = {position, radius};
|
||||
BatchWireframeSphere(color, &sphere, 1, thickness, sectors, stacks);
|
||||
BatchWireframeSphere(color,& sphere, 1, thickness, sectors, stacks);
|
||||
}
|
||||
|
||||
void J3D::WireframeSphere(const Color4& color, const Sphere& sphere, float thickness, uint sectors, uint stacks) {
|
||||
BatchWireframeSphere(color, &sphere, 1, thickness, sectors, stacks);
|
||||
void J3D::WireframeSphere(const Color4& color, const Sphere& sphere, float thickness, unsigned int sectors, unsigned int stacks) {
|
||||
BatchWireframeSphere(color,& sphere, 1, thickness, sectors, stacks);
|
||||
}
|
||||
|
||||
void J3D::BatchWireframeSphere(const Color4& color, const Sphere* spheres, const size_t& sphere_count, float thickness, uint sectors, uint stacks) {
|
||||
void J3D::BatchWireframeSphere(const Color4& color, const Sphere* spheres, const size_t& sphere_count, float thickness, unsigned int sectors, unsigned int stacks) {
|
||||
if (!inJ3D)
|
||||
Logger::Error("Drawing J3D element before J3D begin.");
|
||||
|
||||
@@ -1089,7 +1124,7 @@ namespace JGL {
|
||||
|
||||
int index = 0;
|
||||
for (int i = 0; i <= sectors; i++) {
|
||||
float lat = M_PI * (-0.5 + (float) i / sectors);
|
||||
float lat = J3ML::Math::Pi * (-0.5 + (float) i / sectors);
|
||||
float z = J3ML::Math::Sin(lat);
|
||||
float zr = J3ML::Math::Cos(lat);
|
||||
|
||||
@@ -1121,23 +1156,23 @@ namespace JGL {
|
||||
glColor4fv(baseColor);
|
||||
}
|
||||
|
||||
void J3D::WireframeRevoSphere(const Color4& color, const Vector3& position, float radius, float thickness, uint sectors, uint revolutions, bool draw_stacks) {
|
||||
void J3D::WireframeRevoSphere(const Color4& color, const Vector3& position, float radius, float thickness, unsigned int sectors, unsigned int revolutions, bool draw_stacks) {
|
||||
Sphere sphere = {position, radius};
|
||||
BatchWireframeRevoSphere(color, &sphere, 1, thickness, sectors, revolutions, draw_stacks);
|
||||
BatchWireframeRevoSphere(color,& sphere, 1, thickness, sectors, revolutions, draw_stacks);
|
||||
}
|
||||
|
||||
void J3D::WireframeRevoSphere(const Color4& color, const Sphere& sphere, float thickness, uint sectors, uint revolutions, bool draw_stacks) {
|
||||
BatchWireframeRevoSphere(color, &sphere, 1, thickness, sectors, revolutions, draw_stacks);
|
||||
void J3D::WireframeRevoSphere(const Color4& color, const Sphere& sphere, float thickness, unsigned int sectors, unsigned int revolutions, bool draw_stacks) {
|
||||
BatchWireframeRevoSphere(color,& sphere, 1, thickness, sectors, revolutions, draw_stacks);
|
||||
}
|
||||
|
||||
void J3D::BatchWireframeRevoSphere(const Color4& color, const Sphere* spheres, const size_t& sphere_count, float thickness, uint sectors, uint revolutions, bool draw_stacks) {
|
||||
void J3D::BatchWireframeRevoSphere(const Color4& color, const Sphere* spheres, const size_t& sphere_count, float thickness, unsigned int sectors, unsigned int revolutions, bool draw_stacks) {
|
||||
float r = 1;
|
||||
std::vector<Vector3> vertices;
|
||||
vertices.reserve((sectors + 1) * (revolutions + 1));
|
||||
|
||||
std::vector<Vector3> cross_section(sectors + 1);
|
||||
for (int i = 0; i <= sectors; i++) {
|
||||
float lat = M_PI * (-0.5 + (float)i / sectors);
|
||||
float lat = J3ML::Math::Pi * (-0.5 + (float)i / sectors);
|
||||
float z = J3ML::Math::Sin(lat);
|
||||
float zr = J3ML::Math::Cos(lat);
|
||||
cross_section[i] = Vector3(0, zr * r, z * r);
|
||||
@@ -1145,7 +1180,7 @@ namespace JGL {
|
||||
|
||||
// Revolve
|
||||
for (int j = 0; j <= revolutions; j++) {
|
||||
float lng = 2 * M_PI * (float)j / revolutions;
|
||||
float lng = 2 * J3ML::Math::Pi * (float)j / revolutions;
|
||||
float cosLng = J3ML::Math::Cos(lng);
|
||||
float sinLng = J3ML::Math::Sin(lng);
|
||||
|
||||
@@ -1160,19 +1195,23 @@ namespace JGL {
|
||||
|
||||
glLineWidth(thickness);
|
||||
glColor4ubv(color.ptr());
|
||||
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), vertices.data());
|
||||
|
||||
VRamList vertex_data(vertices.data(), vertices.size());
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_data.GetHandle());
|
||||
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), nullptr);
|
||||
|
||||
// Render each sphere in the batch at their given position and radius.
|
||||
for(size_t i = 0; i < sphere_count; i++) {
|
||||
glPushMatrix();
|
||||
glTranslatef(spheres[i].Position.x, spheres[i].Position.y, spheres[i].Position.z);
|
||||
glScalef(spheres[i].Radius, spheres[i].Radius, spheres[i].Radius);
|
||||
glDrawArrays(GL_LINE_LOOP, 0, vertices.size());
|
||||
if (draw_stacks)
|
||||
glRotatef(90, 0, 1, 0),
|
||||
glDrawArrays(GL_LINE_LOOP, 0, vertices.size());
|
||||
glTranslatef(spheres[i].Position.x, spheres[i].Position.y, spheres[i].Position.z);
|
||||
glScalef(spheres[i].Radius, spheres[i].Radius, spheres[i].Radius);
|
||||
glDrawArrays(GL_LINE_LOOP, 0, vertex_data.GetLength());
|
||||
if (draw_stacks)
|
||||
glRotatef(90, 0, 1, 0),
|
||||
glDrawArrays(GL_LINE_LOOP, 0, vertex_data.GetLength());
|
||||
glPopMatrix();
|
||||
}
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glColor4fv(baseColor);
|
||||
}
|
||||
|
||||
@@ -1250,8 +1289,7 @@ namespace JGL {
|
||||
}
|
||||
}
|
||||
|
||||
void J3D::WireframeIcosahedron(const Color4 &color, const Vector3 &position, float radius, float thickness) {
|
||||
|
||||
void J3D::WireframeIcosahedron(const Color4& color, const Vector3& position, float radius, float thickness) {
|
||||
if (!inJ3D)
|
||||
Logger::Error("Drawing J3D element before J3D begin.");
|
||||
|
||||
@@ -1296,220 +1334,170 @@ namespace JGL {
|
||||
glColor4fv(baseColor);
|
||||
}
|
||||
|
||||
void J3D::WireframeAABB(const Color4& color, const Vector3& pos, const Vector3& radii, float thickness) {
|
||||
|
||||
if (!inJ3D)
|
||||
Logger::Error("Drawing J3D element before J3D begin.");
|
||||
|
||||
|
||||
// Top of cube
|
||||
Vector3 top_right_top = pos + Vector3(radii.x, radii.y, -radii.z);
|
||||
Vector3 top_left_top = pos + Vector3(-radii.x, radii.y, -radii.z);
|
||||
Vector3 bottom_left_top = pos - radii;
|
||||
Vector3 bottom_right_top = pos + Vector3(radii.x, radii.y, radii.z);
|
||||
|
||||
// Bottom of cube
|
||||
Vector3 top_right_bottom = pos + Vector3(radii.x, -radii.y, radii.z);
|
||||
Vector3 top_left_bottom = pos + Vector3(-radii.x, -radii.y, radii.z);
|
||||
Vector3 bottom_left_bottom = pos - radii;
|
||||
Vector3 bottom_right_bottom = pos + Vector3(radii.x, -radii.y, -radii.z);
|
||||
|
||||
// front of cube
|
||||
Vector3 top_right_front = pos + radii;
|
||||
Vector3 top_left_front = pos + Vector3(-radii.x, radii.y, radii.z);
|
||||
Vector3 bottom_left_front = pos + Vector3(-radii.x, -radii.y, radii.z);
|
||||
Vector3 bottom_right_front = pos + Vector3(radii.x, -radii.y, radii.z);
|
||||
|
||||
// back of cube
|
||||
Vector3 top_right_back = pos + Vector3(radii.x, -radii.y, -radii.z);
|
||||
Vector3 top_left_back = pos - radii;
|
||||
Vector3 bottom_left_back = pos + Vector3(-radii.x, radii.y, -radii.z);
|
||||
Vector3 bottom_right_back = pos + Vector3(radii.x, radii.y, -radii.z);
|
||||
|
||||
// left of cube
|
||||
Vector3 top_right_left = pos + Vector3(-radii.x, radii.y, radii.z);
|
||||
Vector3 top_left_left = pos + Vector3(-radii.x, radii.y, -radii.z);
|
||||
Vector3 bottom_left_left = pos - radii;
|
||||
Vector3 bottom_right_left = pos + Vector3(-radii.x, -radii.y, radii.z);
|
||||
|
||||
// right of cube
|
||||
Vector3 top_right_right = pos + Vector3(radii.x, radii.y, -radii.z);
|
||||
Vector3 top_left_right = pos + radii;
|
||||
Vector3 bottom_left_right = pos + Vector3(radii.x, -radii.y, radii.z);
|
||||
Vector3 bottom_right_right = pos + Vector3(radii.x, -radii.y, -radii.z);
|
||||
|
||||
std::vector<Vector3> vertices
|
||||
{
|
||||
top_right_top, top_left_top, bottom_left_top, bottom_right_top,
|
||||
top_right_bottom, top_left_bottom, bottom_left_bottom, bottom_right_bottom,
|
||||
top_right_front, top_left_front, bottom_left_front, bottom_right_front,
|
||||
top_right_back, top_left_back, bottom_left_back, bottom_right_back,
|
||||
top_right_left, top_left_left, bottom_left_left, bottom_right_left,
|
||||
top_right_right, top_left_right, bottom_left_right, bottom_right_right
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
void J3D::BatchWireframeAABB(const Color4& color, const AABB* boxes, const size_t& box_count, float thickness) {
|
||||
glColor4ubv(color.ptr());
|
||||
glLineWidth(thickness);
|
||||
glColor4ubv(color.ptr());
|
||||
//glBindBuffer(GL_ARRAY_BUFFER, vertices.size());
|
||||
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), vertices.data());
|
||||
glDrawArrays(GL_LINE_LOOP, 0, vertices.size());
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, ShapeCache::cube_vertex_data->GetHandle());
|
||||
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), nullptr);
|
||||
|
||||
for (size_t i = 0; i < box_count; i++) {
|
||||
Vector3 delta = (boxes[i].maxPoint - boxes[i].minPoint) / 2;
|
||||
Vector3 center = boxes[i].Centroid();
|
||||
glPushMatrix();
|
||||
glTranslatef(center.x, center.y, center.z);
|
||||
glScalef(delta.x, delta.y, delta.z);
|
||||
glDrawArrays(GL_LINES, 0, ShapeCache::cube_vertex_data->GetLength());
|
||||
glPopMatrix();
|
||||
}
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glColor4fv(baseColor);
|
||||
}
|
||||
|
||||
void J3D::FillAABB(const Color4 &color, const Vector3 &pos, const Vector3 &radii) {
|
||||
void J3D::WireframeAABB(const Color4& color, const Vector3& pos, const Vector3& radii, float thickness) {
|
||||
AABB aabb = {Vector3(pos.x - radii.x, pos.y - radii.y, pos.z - radii.z), Vector3(pos.x + radii.x, pos.y + radii.y, pos.z + radii.z)};
|
||||
BatchWireframeAABB(color, &aabb, 1, thickness);
|
||||
}
|
||||
|
||||
void J3D::BatchFillAABB(const Color4& color, const AABB* boxes, const size_t& box_count) {
|
||||
if (!inJ3D)
|
||||
Logger::Error("Drawing J3D element before J3D begin.");
|
||||
|
||||
// Top of cube
|
||||
Vector3 top_right_top = pos + Vector3(radii.x, radii.y, -radii.z);
|
||||
Vector3 bottom_right_top = pos + Vector3(radii.x, radii.y, radii.z);
|
||||
Vector3 bottom_left_top = pos + Vector3(-radii.x, radii.y, radii.z);
|
||||
Vector3 top_left_top = pos + Vector3(-radii.x, radii.y, -radii.z);
|
||||
|
||||
// Bottom of cube
|
||||
Vector3 top_right_bottom = pos + Vector3(radii.x, -radii.y, radii.z);
|
||||
Vector3 bottom_right_bottom = pos + Vector3(radii.x, -radii.y, -radii.z);
|
||||
Vector3 bottom_left_bottom = pos + Vector3(-radii.x, -radii.y, -radii.z);
|
||||
Vector3 top_left_bottom = pos + Vector3(-radii.x, -radii.y, radii.z);
|
||||
|
||||
// front of cube
|
||||
Vector3 top_right_front = pos + Vector3(radii.x, radii.y, radii.z);
|
||||
Vector3 bottom_right_front = pos + Vector3(radii.x, -radii.y, radii.z);
|
||||
Vector3 bottom_left_front = pos + Vector3(-radii.x, -radii.y, radii.z);
|
||||
Vector3 top_left_front = pos + Vector3(-radii.x, radii.y, radii.z);
|
||||
|
||||
// back of cube
|
||||
Vector3 top_right_back = pos + Vector3(radii.x, radii.y, -radii.z);
|
||||
Vector3 bottom_right_back = pos + Vector3(radii.x, -radii.y, -radii.z);
|
||||
Vector3 bottom_left_back = pos + Vector3(-radii.x, -radii.y, -radii.z);
|
||||
Vector3 top_left_back = pos + Vector3(-radii.x, radii.y, -radii.z);
|
||||
|
||||
// left of cube
|
||||
Vector3 top_right_left = pos + Vector3(-radii.x, radii.y, radii.z);
|
||||
Vector3 bottom_right_left = pos + Vector3(-radii.x, -radii.y, radii.z);
|
||||
Vector3 bottom_left_left = pos + Vector3(-radii.x, -radii.y, -radii.z);
|
||||
Vector3 top_left_left = pos + Vector3(-radii.x, radii.y, -radii.z);
|
||||
|
||||
// right of cube
|
||||
Vector3 top_right_right = pos + Vector3(radii.x, radii.y, -radii.z);
|
||||
Vector3 bottom_right_right = pos + Vector3(radii.x, -radii.y, -radii.z);
|
||||
Vector3 bottom_left_right = pos + Vector3(radii.x, -radii.y, radii.z);
|
||||
Vector3 top_left_right = pos + Vector3(radii.x, radii.y, radii.z);
|
||||
|
||||
std::vector<Vector3> vertices
|
||||
{
|
||||
top_right_top, bottom_right_top, bottom_left_top, top_left_top,
|
||||
top_right_bottom, bottom_right_bottom, bottom_left_bottom, top_left_bottom,
|
||||
top_right_front, bottom_right_front, bottom_left_front, top_left_front,
|
||||
top_right_back, bottom_right_back, bottom_left_back, top_left_back,
|
||||
top_right_left, bottom_right_left, bottom_left_left, top_left_left,
|
||||
top_right_right, bottom_right_right, bottom_left_right, top_left_right
|
||||
};
|
||||
|
||||
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
glColor4ubv(color.ptr());
|
||||
//glBindBuffer(GL_ARRAY_BUFFER, vertices.size());
|
||||
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), vertices.data());
|
||||
glDrawArrays(GL_QUADS, 0, vertices.size());
|
||||
glColor4fv(baseColor);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, ShapeCache::cube_vertex_data->GetHandle());
|
||||
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), nullptr);
|
||||
|
||||
for (size_t i = 0; i < box_count; i++) {
|
||||
Vector3 delta = (boxes[i].maxPoint - boxes[i].minPoint) / 2;
|
||||
Vector3 center = boxes[i].Centroid();
|
||||
glPushMatrix();
|
||||
glTranslatef(center.x, center.y, center.z);
|
||||
glScalef(delta.x, delta.y, delta.z);
|
||||
glDrawArrays(GL_QUAD_STRIP, 0, ShapeCache::cube_vertex_data->GetLength());
|
||||
glPopMatrix();
|
||||
}
|
||||
glColor4fv(oldColor);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
void J3D::FillAABB(const Color4& color, const Vector3& pos, const Vector3& radii) {
|
||||
AABB box = {pos - radii, pos + radii};
|
||||
BatchFillAABB(color, &box, 1);
|
||||
}
|
||||
|
||||
void J3D::FillAABB(const Color4 &color, const AABB &aabb) {
|
||||
FillAABB(color, aabb.Centroid(), aabb.Size());
|
||||
void J3D::FillAABB(const Color4& color, const AABB& aabb) {
|
||||
BatchFillAABB(color, &aabb, 1);
|
||||
}
|
||||
|
||||
void J3D::FillSphere(const Color4 &color, const Vector3& position, float radius, uint sectors, uint stacks) {
|
||||
void J3D::FillSphere(const Color4& color, const Sphere& sphere, unsigned int sectors, unsigned int stacks) {
|
||||
BatchFillSphere(color, &sphere, 1, sectors, stacks);
|
||||
}
|
||||
|
||||
void J3D::BatchFillSphere(const Color4& color, const Sphere* spheres, const size_t& sphere_count, unsigned int sectors, unsigned int stacks) {
|
||||
if (!inJ3D)
|
||||
Logger::Error("Drawing J3D element before J3D begin.");
|
||||
|
||||
float r = 1;
|
||||
std::vector<Vector3> vertices((sectors + 1) * (stacks + 1));
|
||||
std::vector<unsigned int> indices; indices.reserve(sectors * stacks * 6);
|
||||
|
||||
glColor4ubv(color.ptr());
|
||||
|
||||
unsigned int lats = sectors;
|
||||
unsigned int longs = stacks;
|
||||
|
||||
float r = radius;
|
||||
std::vector<Vector3> vertices((lats + 1) * (longs + 1));
|
||||
|
||||
float two_pi = 2 * J3ML::Math::Pi;
|
||||
int index = 0;
|
||||
for (int i = 0; i <= lats; i++) {
|
||||
float lat = M_PI * (-0.5 + (float) i / lats);
|
||||
for (int i = 0; i <= sectors; i++) {
|
||||
float lat = J3ML::Math::Pi * (-0.5 + (float) i / sectors);
|
||||
float z = J3ML::Math::Sin(lat);
|
||||
float zr = J3ML::Math::Cos(lat);
|
||||
|
||||
for (int j = 0; j <= longs; j++) {
|
||||
float lng = 2 * J3ML::Math::Pi * (float) (j - 1) / longs;
|
||||
for (int j = 0; j <= stacks; j++) {
|
||||
float lng = two_pi * (float) (j - 1) / stacks;
|
||||
float x = J3ML::Math::Cos(lng);
|
||||
float y = J3ML::Math::Sin(lng);
|
||||
|
||||
float pos_x = r * x * zr;
|
||||
float pos_y = r * y * zr;
|
||||
float pos_z = r * z;
|
||||
|
||||
pos_x += position.x;
|
||||
pos_y += position.y;
|
||||
pos_z += position.z;
|
||||
|
||||
vertices[index++] = Vector3(pos_x, pos_y, pos_z);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < sectors; i++) {
|
||||
for (int j = 0; j < stacks; j++) {
|
||||
int first_index = i * (stacks + 1) + j;
|
||||
int second_index = first_index + stacks + 1;
|
||||
|
||||
indices.push_back(first_index);
|
||||
indices.push_back(second_index);
|
||||
indices.push_back(first_index + 1);
|
||||
|
||||
indices.push_back(second_index);
|
||||
indices.push_back(second_index + 1);
|
||||
indices.push_back(first_index + 1);
|
||||
}
|
||||
}
|
||||
|
||||
VRamList vertex_data(vertices.data(), vertices.size());
|
||||
VRamList index_data(indices.data(), indices.size());
|
||||
|
||||
glColor4ubv(color.ptr());
|
||||
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), vertices.data());
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size()); // TODO: Make it render correctingly!
|
||||
glColor4fv(baseColor);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_data.GetHandle());
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_data.GetHandle());
|
||||
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), nullptr);
|
||||
|
||||
for (size_t i = 0; i < sphere_count; i++) {
|
||||
const Vector3 position = spheres[i].Position;
|
||||
glPushMatrix();
|
||||
glTranslatef(position.x, position.y, position.z);
|
||||
glScalef(spheres[i].Radius, spheres[i].Radius, spheres[i].Radius);
|
||||
glDrawElements(GL_TRIANGLES, index_data.GetLength(), GL_UNSIGNED_INT, nullptr);
|
||||
glPopMatrix();
|
||||
}
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
glColor4fv(oldColor);
|
||||
}
|
||||
|
||||
void J3D::WireframeAABB(const Color4 &color, const AABB &aabb, float thickness) {
|
||||
WireframeAABB(color, aabb.Centroid(), aabb.Size(), thickness);
|
||||
void J3D::FillSphere(const Color4& color, const Vector3& position, float radius, unsigned int sectors, unsigned int stacks) {
|
||||
Sphere sphere = {position, radius};
|
||||
BatchFillSphere(color, &sphere, 1, sectors, stacks);
|
||||
}
|
||||
|
||||
// Why is this here?
|
||||
float ComputeBinomial(int n, int k)
|
||||
{
|
||||
float value = 1.f;
|
||||
|
||||
for (int i = 1; i <= k; i++)
|
||||
value = value * ((n + 1 - i) / i);
|
||||
|
||||
if (n == k)
|
||||
value = 1;
|
||||
|
||||
return value;
|
||||
void J3D::WireframeAABB(const Color4& color, const AABB& aabb, float thickness) {
|
||||
BatchWireframeAABB(color, &aabb, 1, thickness);
|
||||
}
|
||||
|
||||
void J3D::WireframeOBB(const Color4& color, const J3ML::Geometry::OBB& obb, float thickness) {
|
||||
// TODO Make it work the same as AABB batching. I couldn't get rotation to do anything more than twitch in place :(.
|
||||
void J3D::BatchWireframeOBB(const Color4& color, const OBB* boxes, const size_t& box_count, float thickness) {
|
||||
std::array<Vector3, 8> corner_points;
|
||||
obb.GetCornerPoints(corner_points.data());
|
||||
|
||||
std::array<GLuint, 24> indices =
|
||||
{
|
||||
0, 1, 1, 2, 2, 3, 3, 0,
|
||||
4, 5, 5, 6, 6, 7, 7, 4,
|
||||
0, 4, 1, 5, 2, 6, 3, 7
|
||||
0, 1, 1, 2, 2, 3, 3, 0,
|
||||
4, 5, 5, 6, 6, 7, 7, 4,
|
||||
0, 4, 1, 5, 2, 6, 3, 7
|
||||
};
|
||||
|
||||
glPushMatrix();
|
||||
glTranslatef(obb.pos.x, obb.pos.y, obb.pos.z);
|
||||
|
||||
glLineWidth(thickness);
|
||||
glColor4ubv(color.ptr());
|
||||
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), corner_points.data());
|
||||
glDrawElements(GL_LINES, indices.size(), GL_UNSIGNED_INT, indices.data());
|
||||
glColor4fv(baseColor);
|
||||
glPopMatrix();
|
||||
|
||||
for (size_t i = 0; i < box_count; i++) {
|
||||
boxes[i].GetCornerPoints(corner_points.data());
|
||||
glPushMatrix();
|
||||
glTranslatef(boxes[i].pos.x, boxes[i].pos.y, boxes[i].pos.z);
|
||||
glDrawElements(GL_LINES, indices.size(), GL_UNSIGNED_INT, indices.data());
|
||||
glPopMatrix();
|
||||
}
|
||||
glColor4fv(oldColor);
|
||||
}
|
||||
|
||||
void J3D::WireframeOBB(const Color4& color, const J3ML::LinearAlgebra::Vector3& position, const J3ML::LinearAlgebra::Vector3& radii,
|
||||
const J3ML::LinearAlgebra::EulerAngle& orientation, float thickness) {
|
||||
Matrix3x3 rotation = orientation.ToQuaternion().ToMatrix3x3();
|
||||
WireframeOBB(color, OBB(position, radii, rotation * Vector3::UnitX, rotation * Vector3::UnitY, rotation * Vector3::UnitZ), thickness);
|
||||
void J3D::WireframeOBB(const Color4& color, const OBB& obb, float thickness) {
|
||||
BatchWireframeOBB(color, &obb, 1, thickness);
|
||||
}
|
||||
|
||||
/*
|
||||
void J3D::WireframeOBB(const Color4& color, const Vector3& position, const Vector3& radii, const Matrix3x3& orientation, float thickness) {
|
||||
WireframeOBB(color, OBB(position, radii, orientation. * Vector3::UnitX, rotation * Vector3::UnitY, rotation * Vector3::UnitZ), thickness);
|
||||
}
|
||||
*/
|
||||
|
||||
void J3D::DrawCubicBezierCurve(const Color4& color, const Vector3& controlA, const Vector3& pointA,
|
||||
const Vector3& pointB, const Vector3& controlB, int subdivisions, float thickness) {
|
||||
Vector3 last = controlA;
|
||||
|
38
src/ShapeCache.cpp
Normal file
38
src/ShapeCache.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <JGL/ShapeCache.h>
|
||||
|
||||
void JGL::ShapeCache::Init() {
|
||||
if (!cube_vertex_data) {
|
||||
std::array<Vector3, 24> vertices {
|
||||
Vector3(-1, 1, -1), Vector3(1, 1, -1),
|
||||
Vector3(1, 1, -1), Vector3(1, 1, 1),
|
||||
Vector3(1, 1, 1), Vector3(-1, 1, 1),
|
||||
Vector3(-1, 1, 1), Vector3(-1, 1, -1),
|
||||
Vector3(-1, -1, -1), Vector3(1, -1, -1),
|
||||
Vector3(1, -1, -1), Vector3(1, -1, 1),
|
||||
Vector3(1, -1, 1), Vector3(-1, -1, 1),
|
||||
Vector3(-1, -1, 1), Vector3(-1, -1, -1),
|
||||
Vector3(-1, -1, -1), Vector3(-1, 1, -1),
|
||||
Vector3(1, -1, -1), Vector3(1, 1, -1),
|
||||
Vector3(1, -1, 1), Vector3(1, 1, 1),
|
||||
Vector3(-1, -1, 1), Vector3(-1, 1, 1)
|
||||
};
|
||||
cube_vertex_data = new VRamList(vertices.data(), vertices.size());
|
||||
}
|
||||
|
||||
if (!cube_index_data) {
|
||||
std::array<GLuint, 36> indices {
|
||||
0, 1, 3, 0, 3, 5,
|
||||
|
||||
8, 9, 11, 8, 11, 13,
|
||||
|
||||
5, 3, 11, 5, 11, 13,
|
||||
|
||||
0, 1, 9, 0, 9, 8,
|
||||
|
||||
0, 5, 13, 0, 13, 8,
|
||||
|
||||
1, 3, 11, 1, 11, 9
|
||||
};
|
||||
cube_index_data = new VRamList(indices.data(), indices.size());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user