Compare commits

...

2 Commits

Author SHA1 Message Date
fb7164ae5d Merge branch 'master' of https://gitea.deadfish.cz/tovjemam/karo 2024-03-05 20:55:46 +01:00
9b44c244cb kosticky 2024-03-05 20:54:43 +01:00
2 changed files with 192 additions and 12 deletions

203
karo.cpp
View File

@ -12,11 +12,21 @@
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
#include <physx/PxPhysics.h>
//#include <physx/PxFoundation.h>
#include <physx/PxConfig.h>
#include <physx/PxPhysicsAPI.h>
//#include <physx/PxFiltering.h>
using namespace physx;
struct Vertex { struct Vertex {
glm::vec3 pos; glm::vec3 pos;
glm::vec3 color; glm::vec3 color;
}; };
static const char* s_vs_src = R"GLSL( static const char* s_vs_src = R"GLSL(
#version 330 core #version 330 core
@ -46,7 +56,7 @@ void main() {
} }
)GLSL"; )GLSL";
void CheckShaderCompileErrors(GLuint shader) { static void CheckShaderCompileErrors(GLuint shader) {
GLint success; GLint success;
GLchar infoLog[512]; GLchar infoLog[512];
glGetShaderiv(shader, GL_COMPILE_STATUS, &success); glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
@ -57,7 +67,7 @@ void CheckShaderCompileErrors(GLuint shader) {
} }
} }
void CheckProgramLinkErrors(GLuint program) { static void CheckProgramLinkErrors(GLuint program) {
GLint success; GLint success;
GLchar infoLog[512]; GLchar infoLog[512];
glGetProgramiv(program, GL_LINK_STATUS, &success); glGetProgramiv(program, GL_LINK_STATUS, &success);
@ -68,6 +78,133 @@ void CheckProgramLinkErrors(GLuint program) {
} }
} }
static std::vector<Vertex> s_draw_lines;
static std::vector<Vertex> s_draw_points;
static PxDefaultErrorCallback gDefaultErrorCallback;
static PxDefaultAllocator gDefaultAllocatorCallback;
static PxFoundation* s_foundation;
static PxPhysics* s_physics;
static PxScene* s_scene;
static PxDefaultCpuDispatcher* s_cpu_dispatcher;
static PxMaterial* s_material;
static PxRigidDynamic* s_box1;
static void InitPhysics() {
s_foundation = PxCreateFoundation(PX_PHYSICS_VERSION, gDefaultAllocatorCallback, gDefaultErrorCallback);
if (!s_foundation)
exit(1);
bool recordMemoryAllocations = false;
s_physics = PxCreatePhysics(PX_PHYSICS_VERSION, *s_foundation, PxTolerancesScale(), recordMemoryAllocations, nullptr);
if (!s_physics)
exit(2);
PxSceneDesc sceneDesc(s_physics->getTolerancesScale());
sceneDesc.gravity = PxVec3(0.0f, -9.81f, 0.0f);
// create CPU dispatcher which mNbThreads worker threads
s_cpu_dispatcher = PxDefaultCpuDispatcherCreate(1);
if (!s_cpu_dispatcher)
exit(3);
sceneDesc.cpuDispatcher = s_cpu_dispatcher;
if (!sceneDesc.filterShader)
sceneDesc.filterShader = &PxDefaultSimulationFilterShader;
s_scene = s_physics->createScene(sceneDesc);
if (!s_scene)
exit(4);
auto m_scene = s_scene;
bool enable = true;
m_scene->setVisualizationParameter(PxVisualizationParameter::eSCALE, enable ? 1.0f : 0.0f);
//m_scene->setVisualizationParameter(PxVisualizationParameter::eACTOR_AXES, enable ? 2.0f : 0.0f);
////m_scene->setVisualizationParameter(PxVisualizationParameter::eCOLLISION_EDGES, enable ? 1.0f : 0.0f);
////m_scene->setVisualizationParameter(PxVisualizationParameter::eCOLLISION_COMPOUNDS, enable ? 1.0f : 0.0f);
//m_scene->setVisualizationParameter(PxVisualizationParameter::eCOLLISION_AABBS, enable ? 1.0f : 0.0f);
//m_scene->setVisualizationParameter(PxVisualizationParameter::eMBP_REGIONS, enable ? 1.0f : 0.0f);
m_scene->setVisualizationParameter(PxVisualizationParameter::eACTOR_AXES, enable ? 0.5f : 0.0f);
//m_scene->setVisualizationParameter(PxVisualizationParameter::eBODY_ANG_VELOCITY, enable ? 1.0f : 0.0f);
//m_scene->setVisualizationParameter(PxVisualizationParameter::eBODY_AXES, enable ? 1.0f : 0.0f);
m_scene->setVisualizationParameter(PxVisualizationParameter::eBODY_LIN_VELOCITY, enable ? 1.0f : 0.0f);
//m_scene->setVisualizationParameter(PxVisualizationParameter::eBODY_MASS_AXES, enable ? 1.0f : 0.0f);
//m_scene->setVisualizationParameter(PxVisualizationParameter::eCOLLISION_AABBS, enable ? 1.0f : 0.0f);
m_scene->setVisualizationParameter(PxVisualizationParameter::eCOLLISION_AXES, enable ? 1.0f : 0.0f);
//m_scene->setVisualizationParameter(PxVisualizationParameter::eCOLLISION_COMPOUNDS, enable ? 1.0f : 0.0f);
//m_scene->setVisualizationParameter(PxVisualizationParameter::eCOLLISION_DYNAMIC, enable ? 1.0f : 0.0f);
m_scene->setVisualizationParameter(PxVisualizationParameter::eCOLLISION_EDGES, enable ? 1.0f : 0.0f);
//m_scene->setVisualizationParameter(PxVisualizationParameter::eCOLLISION_FNORMALS, enable ? 1.0f : 0.0f);
m_scene->setVisualizationParameter(PxVisualizationParameter::eCOLLISION_SHAPES, enable ? 1.0f : 0.0f);
//m_scene->setVisualizationParameter(PxVisualizationParameter::eCOLLISION_STATIC, enable ? 1.0f : 0.0f);
m_scene->setVisualizationParameter(PxVisualizationParameter::eCONTACT_ERROR, enable ? 1.0f : 0.0f);
m_scene->setVisualizationParameter(PxVisualizationParameter::eCONTACT_FORCE, enable ? 1.0f : 0.0f);
m_scene->setVisualizationParameter(PxVisualizationParameter::eCONTACT_NORMAL, enable ? 1.0f : 0.0f);
m_scene->setVisualizationParameter(PxVisualizationParameter::eCONTACT_POINT, enable ? 1.0f : 0.0f);
//m_scene->setVisualizationParameter(PxVisualizationParameter::eCULL_BOX, enable ? 1.0f : 0.0f);
//m_scene->setVisualizationParameter(PxVisualizationParameter::eFORCE_DWORD, enable ? 1.0f : 0.0f);
//m_scene->setVisualizationParameter(PxVisualizationParameter::eJOINT_LIMITS, enable ? 1.0f : 0.0f);
//m_scene->setVisualizationParameter(PxVisualizationParameter::eJOINT_LOCAL_FRAMES, enable ? 1.0f : 0.0f);
m_scene->setVisualizationParameter(PxVisualizationParameter::eWORLD_AXES, enable ? 1.0f : 0.0f);
auto floor_material = s_physics->createMaterial(1.0f, 1.0f, 0.0f);
s_material = s_physics->createMaterial(1.0f, 1.0f, 0.6f);
auto floor = PxCreatePlane(*s_physics, PxPlane(PxVec3(0.f, 1.f, 0.f), 0.f), *floor_material);
s_scene->addActor(*floor);
s_box1 = PxCreateDynamic(*s_physics, PxTransform(PxVec3(0.f, 5.0f, 0.f)), PxBoxGeometry(0.5f, 0.5f, 0.5f), *s_material, 1.f);
s_scene->addActor(*s_box1);
}
static void PhysicsFrame() {
s_scene->simulate(1.0f / 60.0f);
s_scene->fetchResults(true);
}
static glm::vec4 U32ColorToVec4(uint32_t rgba_value) {
float red = static_cast<float>((rgba_value >> 24) & 0xFF) / 255.0f;
float green = static_cast<float>((rgba_value >> 16) & 0xFF) / 255.0f;
float blue = static_cast<float>((rgba_value >> 8) & 0xFF) / 255.0f;
float alpha = static_cast<float>(rgba_value & 0xFF) / 255.0f;
return glm::vec4(red, green, blue, alpha);
}
static void DrawPhysxDebug() {
const PxRenderBuffer& rb = s_scene->getRenderBuffer();
for(PxU32 i=0; i < rb.getNbPoints(); i++)
{
const PxDebugPoint& point = rb.getPoints()[i];
// render the point
s_draw_points.push_back({ glm::vec3(point.pos.x, point.pos.y, point.pos.z), U32ColorToVec4(point.color) });
}
for(PxU32 i=0; i < rb.getNbLines(); i++)
{
const PxDebugLine& line = rb.getLines()[i];
// render the line
s_draw_lines.push_back({ glm::vec3(line.pos0.x, line.pos0.y, line.pos0.z), U32ColorToVec4(line.color0) });
s_draw_lines.push_back({ glm::vec3(line.pos1.x, line.pos1.y, line.pos1.z), U32ColorToVec4(line.color1) });
}
}
static void ShutdownPhysics() {
s_physics->release();
s_foundation->release();
}
static int s_count = 0;
int main() { int main() {
if (!glfwInit()) { if (!glfwInit()) {
@ -150,8 +287,40 @@ int main() {
glfwShowWindow(window); glfwShowWindow(window);
//list of lines InitPhysics();
std::vector<Vertex> vertices;
glfwSetKeyCallback(window, [](GLFWwindow* window, int key, int scancode, int action, int mods) {
if (action != GLFW_PRESS)
return;
switch (key) {
case GLFW_KEY_ESCAPE:
glfwSetWindowShouldClose(window, true);
break;
case GLFW_KEY_SPACE:
s_box1->addForce(PxVec3(0.0f, 100.0f, 0.0f));
break;
case GLFW_KEY_E:
for (int i = 0; i < 100; i++) {
auto newbox = PxCreateDynamic(*s_physics, PxTransform(PxVec3(0.f, 5.0f * i, 0.f)), PxBoxGeometry(0.5f, 0.5f, 0.5f), *s_material, 1.f);
s_scene->addActor(*newbox);
s_count++;
}
break;
case GLFW_KEY_W:
printf("pocet: %d\n", s_count);
break;
}
});
glfwSwapInterval(1);
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
glfwPollEvents(); glfwPollEvents();
@ -161,8 +330,8 @@ int main() {
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
glm::mat4 proj = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 100.0f); glm::mat4 proj = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 1000.0f);
glm::mat4 view = glm::lookAt(glm::vec3(1.0f, 1.0f, 1.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0, 1.0, 0.0)); glm::mat4 view = glm::lookAt(glm::vec3(1.0f, 10.0f, 20.0f) * 10.0f, glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0, 1.0, 0.0));
glm::mat4 mvp = proj * view; glm::mat4 mvp = proj * view;
@ -171,20 +340,30 @@ int main() {
//glClearColor(0.2f, 0.3f, 0.3f, 1.0f); //glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
// test data //// test data
vertices.push_back({ glm::vec3(-1, 0, 0), glm::vec3(1.0f, 0.0f, 0.0f) }); //s_draw_verts.push_back({ glm::vec3(-1, 0, 0), glm::vec3(1.0f, 0.0f, 0.0f) });
vertices.push_back({ glm::vec3(1, 0, 0), glm::vec3(0.0f, 1.0f, 0.0f) }); //s_draw_verts.push_back({ glm::vec3(1, 0, 0), glm::vec3(0.0f, 1.0f, 0.0f) });
PhysicsFrame();
DrawPhysxDebug();
glBindVertexArray(vao); glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STREAM_DRAW);
glDrawArrays(GL_LINES, 0, vertices.size()); glBufferData(GL_ARRAY_BUFFER, s_draw_lines.size() * sizeof(Vertex), s_draw_lines.data(), GL_STREAM_DRAW);
glDrawArrays(GL_LINES, 0, s_draw_lines.size());
vertices.clear(); //glBufferData(GL_ARRAY_BUFFER, s_draw_points.size() * sizeof(Vertex), s_draw_points.data(), GL_STREAM_DRAW);
//glDrawArrays(GL_POINTS, 0, s_draw_points.size());
s_draw_lines.clear();
s_draw_points.clear();
glfwSwapBuffers(window); glfwSwapBuffers(window);
} }
ShutdownPhysics();
} }
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu // Run program: Ctrl + F5 or Debug > Start Without Debugging menu

View File

@ -76,6 +76,7 @@
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>physx</AdditionalIncludeDirectories>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>