2021-04-19 21:28:14 +03:00
|
|
|
/*******************************************************************************************
|
|
|
|
*
|
|
|
|
* raylib [shaders] example - OpenGL point particle system
|
|
|
|
*
|
2022-07-20 02:28:37 +03:00
|
|
|
* Example originally created with raylib 3.8, last time updated with raylib 2.5
|
2021-04-19 21:28:14 +03:00
|
|
|
*
|
2022-07-20 02:28:37 +03:00
|
|
|
* Example contributed by Stephan Soller (@arkanis) and reviewed by Ramon Santamaria (@raysan5)
|
2021-04-19 21:28:14 +03:00
|
|
|
*
|
2022-07-20 02:28:37 +03:00
|
|
|
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
|
|
|
* BSD-like license that allows static linking with closed source software
|
|
|
|
*
|
2024-01-02 22:58:12 +03:00
|
|
|
* Copyright (c) 2021-2024 Stephan Soller (@arkanis) and Ramon Santamaria (@raysan5)
|
2021-04-19 21:28:14 +03:00
|
|
|
*
|
|
|
|
********************************************************************************************
|
|
|
|
*
|
|
|
|
* Mixes raylib and plain OpenGL code to draw a GL_POINTS based particle system. The
|
|
|
|
* primary point is to demonstrate raylib and OpenGL interop.
|
2021-04-22 19:55:24 +03:00
|
|
|
*
|
2021-04-19 21:28:14 +03:00
|
|
|
* rlgl batched draw operations internally so we have to flush the current batch before
|
|
|
|
* doing our own OpenGL work (rlDrawRenderBatchActive()).
|
2021-04-22 19:55:24 +03:00
|
|
|
*
|
2021-04-19 21:28:14 +03:00
|
|
|
* The example also demonstrates how to get the current model view projection matrix of
|
|
|
|
* raylib. That way raylib cameras and so on work as expected.
|
|
|
|
*
|
|
|
|
********************************************************************************************/
|
|
|
|
|
|
|
|
#include "raylib.h"
|
2021-10-13 20:29:29 +03:00
|
|
|
|
2024-02-26 12:29:21 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_DESKTOP_SDL)
|
2023-01-10 14:34:06 +03:00
|
|
|
#if defined(GRAPHICS_API_OPENGL_ES2)
|
|
|
|
#include "glad_gles2.h" // Required for: OpenGL functionality
|
|
|
|
#define glGenVertexArrays glGenVertexArraysOES
|
|
|
|
#define glBindVertexArray glBindVertexArrayOES
|
|
|
|
#define glDeleteVertexArrays glDeleteVertexArraysOES
|
|
|
|
#define GLSL_VERSION 100
|
|
|
|
#else
|
|
|
|
#if defined(__APPLE__)
|
2024-02-28 00:15:09 +03:00
|
|
|
#define GL_SILENCE_DEPRECATION // Silence Opengl API deprecation warnings
|
2023-01-10 14:34:06 +03:00
|
|
|
#include <OpenGL/gl3.h> // OpenGL 3 library for OSX
|
|
|
|
#include <OpenGL/gl3ext.h> // OpenGL 3 extensions library for OSX
|
|
|
|
#else
|
|
|
|
#include "glad.h" // Required for: OpenGL functionality
|
|
|
|
#endif
|
|
|
|
#define GLSL_VERSION 330
|
|
|
|
#endif
|
2023-09-07 18:42:28 +03:00
|
|
|
#else // PLATFORM_ANDROID, PLATFORM_WEB
|
2021-04-19 21:28:14 +03:00
|
|
|
#define GLSL_VERSION 100
|
|
|
|
#endif
|
|
|
|
|
2023-01-10 14:34:06 +03:00
|
|
|
#include "rlgl.h" // Required for: rlDrawRenderBatchActive(), rlGetMatrixModelview(), rlGetMatrixProjection()
|
|
|
|
#include "raymath.h" // Required for: MatrixMultiply(), MatrixToFloat()
|
|
|
|
|
2021-07-31 19:36:06 +03:00
|
|
|
#define MAX_PARTICLES 1000
|
|
|
|
|
|
|
|
// Particle type
|
|
|
|
typedef struct Particle {
|
|
|
|
float x;
|
|
|
|
float y;
|
|
|
|
float period;
|
|
|
|
} Particle;
|
|
|
|
|
2022-06-21 20:53:18 +03:00
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
// Program main entry point
|
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
int main(void)
|
2021-04-19 21:28:14 +03:00
|
|
|
{
|
|
|
|
// Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
const int screenWidth = 800;
|
|
|
|
const int screenHeight = 450;
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2021-04-19 21:28:14 +03:00
|
|
|
InitWindow(screenWidth, screenHeight, "raylib - point particles");
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2021-07-31 19:36:06 +03:00
|
|
|
Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/point_particle.vs", GLSL_VERSION),
|
|
|
|
TextFormat("resources/shaders/glsl%i/point_particle.fs", GLSL_VERSION));
|
|
|
|
|
2021-04-19 21:28:14 +03:00
|
|
|
int currentTimeLoc = GetShaderLocation(shader, "currentTime");
|
|
|
|
int colorLoc = GetShaderLocation(shader, "color");
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2021-04-19 21:28:14 +03:00
|
|
|
// Initialize the vertex buffer for the particles and assign each particle random values
|
2021-07-31 19:36:06 +03:00
|
|
|
Particle particles[MAX_PARTICLES] = { 0 };
|
|
|
|
|
|
|
|
for (int i = 0; i < MAX_PARTICLES; i++)
|
2021-04-19 21:28:14 +03:00
|
|
|
{
|
2021-07-31 19:36:06 +03:00
|
|
|
particles[i].x = (float)GetRandomValue(20, screenWidth - 20);
|
|
|
|
particles[i].y = (float)GetRandomValue(50, screenHeight - 20);
|
|
|
|
|
|
|
|
// Give each particle a slightly different period. But don't spread it to much.
|
|
|
|
// This way the particles line up every so often and you get a glimps of what is going on.
|
|
|
|
particles[i].period = (float)GetRandomValue(10, 30)/10.0f;
|
2021-04-19 21:28:14 +03:00
|
|
|
}
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2021-07-31 19:36:06 +03:00
|
|
|
// Create a plain OpenGL vertex buffer with the data and an vertex array object
|
|
|
|
// that feeds the data from the buffer into the vertexPosition shader attribute.
|
|
|
|
GLuint vao = 0;
|
|
|
|
GLuint vbo = 0;
|
2021-04-19 21:28:14 +03:00
|
|
|
glGenVertexArrays(1, &vao);
|
|
|
|
glBindVertexArray(vao);
|
|
|
|
glGenBuffers(1, &vbo);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
2021-07-31 19:36:06 +03:00
|
|
|
glBufferData(GL_ARRAY_BUFFER, MAX_PARTICLES*sizeof(Particle), particles, GL_STATIC_DRAW);
|
2021-04-19 21:28:14 +03:00
|
|
|
// Note: LoadShader() automatically fetches the attribute index of "vertexPosition" and saves it in shader.locs[SHADER_LOC_VERTEX_POSITION]
|
|
|
|
glVertexAttribPointer(shader.locs[SHADER_LOC_VERTEX_POSITION], 3, GL_FLOAT, GL_FALSE, 0, 0);
|
|
|
|
glEnableVertexAttribArray(0);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
glBindVertexArray(0);
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2021-04-19 21:28:14 +03:00
|
|
|
// Allows the vertex shader to set the point size of each particle individually
|
2023-01-10 14:34:06 +03:00
|
|
|
#ifndef GRAPHICS_API_OPENGL_ES2
|
2021-04-19 21:28:14 +03:00
|
|
|
glEnable(GL_PROGRAM_POINT_SIZE);
|
2023-01-10 14:34:06 +03:00
|
|
|
#endif
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2021-04-19 21:28:14 +03:00
|
|
|
SetTargetFPS(60);
|
|
|
|
//--------------------------------------------------------------------------------------
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2021-04-19 21:28:14 +03:00
|
|
|
// Main game loop
|
|
|
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
|
|
|
{
|
|
|
|
// Draw
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
BeginDrawing();
|
|
|
|
ClearBackground(WHITE);
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2021-04-19 21:28:14 +03:00
|
|
|
DrawRectangle(10, 10, 210, 30, MAROON);
|
2021-07-31 19:36:06 +03:00
|
|
|
DrawText(TextFormat("%zu particles in one vertex buffer", MAX_PARTICLES), 20, 20, 10, RAYWHITE);
|
|
|
|
|
|
|
|
rlDrawRenderBatchActive(); // Draw iternal buffers data (previous draw calls)
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2021-04-19 21:28:14 +03:00
|
|
|
// Switch to plain OpenGL
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
glUseProgram(shader.id);
|
2021-07-31 19:36:06 +03:00
|
|
|
|
2021-04-19 21:28:14 +03:00
|
|
|
glUniform1f(currentTimeLoc, GetTime());
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2021-04-19 21:28:14 +03:00
|
|
|
Vector4 color = ColorNormalize((Color){ 255, 0, 0, 128 });
|
2021-07-31 19:36:06 +03:00
|
|
|
glUniform4fv(colorLoc, 1, (float *)&color);
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2021-07-31 19:36:06 +03:00
|
|
|
// Get the current modelview and projection matrix so the particle system is displayed and transformed
|
2021-04-19 21:28:14 +03:00
|
|
|
Matrix modelViewProjection = MatrixMultiply(rlGetMatrixModelview(), rlGetMatrixProjection());
|
2021-07-31 19:36:06 +03:00
|
|
|
|
2021-04-19 21:28:14 +03:00
|
|
|
glUniformMatrix4fv(shader.locs[SHADER_LOC_MATRIX_MVP], 1, false, MatrixToFloat(modelViewProjection));
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2021-04-19 21:28:14 +03:00
|
|
|
glBindVertexArray(vao);
|
2021-07-31 19:36:06 +03:00
|
|
|
glDrawArrays(GL_POINTS, 0, MAX_PARTICLES);
|
2021-04-19 21:28:14 +03:00
|
|
|
glBindVertexArray(0);
|
2021-07-31 19:36:06 +03:00
|
|
|
|
2021-04-19 21:28:14 +03:00
|
|
|
glUseProgram(0);
|
|
|
|
//------------------------------------------------------------------------------
|
2021-07-31 19:36:06 +03:00
|
|
|
|
2021-04-19 21:28:14 +03:00
|
|
|
DrawFPS(screenWidth - 100, 10);
|
2021-07-31 19:36:06 +03:00
|
|
|
|
2021-04-19 21:28:14 +03:00
|
|
|
EndDrawing();
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
}
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2021-04-19 21:28:14 +03:00
|
|
|
// De-Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
glDeleteBuffers(1, &vbo);
|
|
|
|
glDeleteVertexArrays(1, &vao);
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2021-07-31 19:36:06 +03:00
|
|
|
UnloadShader(shader); // Unload shader
|
|
|
|
|
|
|
|
CloseWindow(); // Close window and OpenGL context
|
2021-04-19 21:28:14 +03:00
|
|
|
//--------------------------------------------------------------------------------------
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2021-04-19 21:28:14 +03:00
|
|
|
return 0;
|
2024-02-26 12:29:21 +03:00
|
|
|
}
|