379 lines
12 KiB
C++
379 lines
12 KiB
C++
// karo.cpp : This file contains the 'main' function. Program execution begins and ends there.
|
|
//
|
|
|
|
//#define GLEW_STATIC
|
|
#include <GL/glew.h>
|
|
#include <GLFW/glfw3.h>
|
|
|
|
#include <iostream>
|
|
#include <vector>
|
|
|
|
#include <glm/glm.hpp>
|
|
#include <glm/gtc/matrix_transform.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 {
|
|
glm::vec3 pos;
|
|
glm::vec3 color;
|
|
};
|
|
|
|
|
|
static const char* s_vs_src = R"GLSL(
|
|
#version 330 core
|
|
|
|
layout (location = 0) in vec3 a_pos;
|
|
layout (location = 1) in vec3 a_color;
|
|
|
|
out vec3 u_color;
|
|
|
|
uniform mat4 u_mvp;
|
|
|
|
void main() {
|
|
gl_Position = u_mvp * vec4(a_pos, 1.0);
|
|
u_color = a_color;
|
|
}
|
|
|
|
)GLSL";
|
|
|
|
static const char* s_fs_src = R"GLSL(
|
|
#version 330 core
|
|
|
|
in vec3 u_color;
|
|
|
|
out vec4 o_color;
|
|
|
|
void main() {
|
|
o_color = vec4(u_color, 1.0);
|
|
}
|
|
)GLSL";
|
|
|
|
static void CheckShaderCompileErrors(GLuint shader) {
|
|
GLint success;
|
|
GLchar infoLog[512];
|
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
|
if (!success) {
|
|
glGetShaderInfoLog(shader, 512, NULL, infoLog);
|
|
std::cerr << "Shader compilation failed: " << infoLog << std::endl;
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
static void CheckProgramLinkErrors(GLuint program) {
|
|
GLint success;
|
|
GLchar infoLog[512];
|
|
glGetProgramiv(program, GL_LINK_STATUS, &success);
|
|
if (!success) {
|
|
glGetProgramInfoLog(program, 512, NULL, infoLog);
|
|
std::cerr << "Program link failed: " << infoLog << std::endl;
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
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() {
|
|
if (!glfwInit()) {
|
|
std::cout << "Failed to initialize GLFW\n";
|
|
return 1;
|
|
}
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
|
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
|
|
glfwWindowHint(GLFW_MAXIMIZED, GL_TRUE);
|
|
glfwWindowHint(GLFW_DEPTH_BITS, 0);
|
|
//glfwWindowHint(GLFW_DEPTH_BITS, 0);
|
|
glfwWindowHint(GLFW_STENCIL_BITS, 0);
|
|
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
|
|
|
|
GLFWwindow* window = glfwCreateWindow(800, 600, "Karo", NULL, NULL);
|
|
if (!window) {
|
|
std::cerr << "Failed to create window\n";
|
|
glfwTerminate();
|
|
return 1;
|
|
}
|
|
|
|
glfwMakeContextCurrent(window);
|
|
|
|
if (glewInit() != GLEW_OK) {
|
|
std::cerr << "Failed to initialize GLEW\n";
|
|
glfwTerminate();
|
|
return 1;
|
|
}
|
|
|
|
std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;
|
|
|
|
GLuint shader;
|
|
shader = glCreateProgram();
|
|
|
|
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
|
|
glShaderSource(vs, 1, &s_vs_src, NULL);
|
|
glCompileShader(vs);
|
|
CheckShaderCompileErrors(vs);
|
|
|
|
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
|
|
glShaderSource(fs, 1, &s_fs_src, NULL);
|
|
glCompileShader(fs);
|
|
CheckShaderCompileErrors(fs);
|
|
|
|
glAttachShader(shader, vs);
|
|
glAttachShader(shader, fs);
|
|
glLinkProgram(shader);
|
|
CheckProgramLinkErrors(shader);
|
|
|
|
glDeleteShader(vs);
|
|
glDeleteShader(fs);
|
|
|
|
glUseProgram(shader);
|
|
|
|
GLuint u_mvp = glGetUniformLocation(shader, "u_mvp");
|
|
|
|
GLuint vao;
|
|
glGenVertexArrays(1, &vao);
|
|
glBindVertexArray(vao);
|
|
|
|
GLuint vbo;
|
|
glGenBuffers(1, &vbo);
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
|
|
|
//setup for stream draw
|
|
glBufferData(GL_ARRAY_BUFFER, 0, NULL, GL_STREAM_DRAW);
|
|
|
|
glEnableVertexAttribArray(0);
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, pos));
|
|
glEnableVertexAttribArray(1);
|
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, color));
|
|
|
|
glBindVertexArray(0);
|
|
|
|
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
|
|
|
glfwShowWindow(window);
|
|
|
|
InitPhysics();
|
|
|
|
|
|
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)) {
|
|
glfwPollEvents();
|
|
|
|
int width, height;
|
|
glfwGetFramebufferSize(window, &width, &height);
|
|
|
|
glViewport(0, 0, width, height);
|
|
|
|
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, 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;
|
|
|
|
glUniformMatrix4fv(u_mvp, 1, GL_FALSE, glm::value_ptr(mvp));
|
|
|
|
//glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
//// test data
|
|
//s_draw_verts.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(0.0f, 1.0f, 0.0f) });
|
|
|
|
PhysicsFrame();
|
|
|
|
DrawPhysxDebug();
|
|
|
|
glBindVertexArray(vao);
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
|
|
|
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());
|
|
|
|
//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);
|
|
}
|
|
|
|
ShutdownPhysics();
|
|
}
|
|
|
|
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
|
|
// Debug program: F5 or Debug > Start Debugging menu
|
|
|
|
// Tips for Getting Started:
|
|
// 1. Use the Solution Explorer window to add/manage files
|
|
// 2. Use the Team Explorer window to connect to source control
|
|
// 3. Use the Output window to see build output and other messages
|
|
// 4. Use the Error List window to view errors
|
|
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
|
|
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
|