1
0
mirror of https://github.com/XProger/OpenLara.git synced 2025-08-18 02:41:29 +02:00

add debug renderer, camera & main shader

This commit is contained in:
XProger
2016-08-23 01:35:09 +03:00
parent 4970d99587
commit d90a650d3e
3 changed files with 203 additions and 0 deletions

46
src/camera.h Normal file
View File

@@ -0,0 +1,46 @@
#ifndef H_CAMERA
#define H_CAMERA
#include "core.h"
struct Camera {
float fov, znear, zfar;
vec3 pos, angle;
void update() {
vec3 dir = vec3(sinf(angle.y - PI) * cosf(-angle.x), -sinf(-angle.x), cosf(angle.y - PI) * cosf(-angle.x));
vec3 v = vec3(0);
if (Input::down[ikW]) v = v + dir;
if (Input::down[ikS]) v = v - dir;
if (Input::down[ikD]) v = v + dir.cross(vec3(0, 1, 0));
if (Input::down[ikA]) v = v - dir.cross(vec3(0, 1, 0));
pos = pos + v.normal() * (Core::deltaTime * 2048.0f);
if (Input::down[ikMouseL]) {
vec2 delta = Input::mouse.pos - Input::mouse.start.L;
angle.x -= delta.y * 0.01f;
angle.y -= delta.x * 0.01f;
angle.x = min(max(angle.x, -PI * 0.5f + EPS), PI * 0.5f - EPS);
Input::mouse.start.L = Input::mouse.pos;
}
}
void setup() {
Core::mView.identity();
Core::mView.rotateZ(-angle.z);
Core::mView.rotateX(-angle.x);
Core::mView.rotateY(-angle.y);
Core::mView.translate(vec3(-pos.x, -pos.y, -pos.z));
Core::mView.scale(vec3(-1, -1, 1));
Core::mProj = mat4(fov, (float)Core::width / (float)Core::height, znear, zfar);
Core::mViewProj = Core::mProj * Core::mView;
Core::viewPos = Core::mView.inverse().getPos();
}
};
#endif

113
src/debug.h Normal file
View File

@@ -0,0 +1,113 @@
#ifndef H_DEBUG
#define H_DEBUG
#include "core.h"
namespace Debug {
namespace Draw {
void begin() {
glMatrixMode(GL_PROJECTION);
glLoadMatrixf((GLfloat*)&Core::mProj);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLoadMatrixf((GLfloat*)&Core::mView);
glLineWidth(3);
glPointSize(32);
glUseProgram(0);
}
void end() {
//
}
void box(const vec3 &min, const vec3 &max) {
glBegin(GL_LINES);
glVertex3f(min.x, min.y, min.z);
glVertex3f(max.x, min.y, min.z);
glVertex3f(min.x, max.y, min.z);
glVertex3f(max.x, max.y, min.z);
glVertex3f(min.x, min.y, max.z);
glVertex3f(max.x, min.y, max.z);
glVertex3f(min.x, max.y, max.z);
glVertex3f(max.x, max.y, max.z);
glVertex3f(min.x, min.y, min.z);
glVertex3f(min.x, min.y, max.z);
glVertex3f(min.x, max.y, min.z);
glVertex3f(min.x, max.y, max.z);
glVertex3f(max.x, min.y, min.z);
glVertex3f(max.x, min.y, max.z);
glVertex3f(max.x, max.y, min.z);
glVertex3f(max.x, max.y, max.z);
glVertex3f(min.x, min.y, min.z);
glVertex3f(min.x, max.y, min.z);
glVertex3f(max.x, min.y, min.z);
glVertex3f(max.x, max.y, min.z);
glVertex3f(min.x, min.y, min.z);
glVertex3f(min.x, max.y, min.z);
glVertex3f(max.x, min.y, max.z);
glVertex3f(max.x, max.y, max.z);
glVertex3f(min.x, min.y, max.z);
glVertex3f(min.x, max.y, max.z);
glEnd();
}
void sphere(const vec3 &center, const float radius, const vec4 &color) {
const float k = PI * 2.0f / 18.0f;
glColor4fv((GLfloat*)&color);
for (int j = 0; j < 3; j++) {
glBegin(GL_LINE_STRIP);
for (int i = 0; i < 19; i++) {
vec3 p = vec3(sinf(i * k), cosf(i * k), 0.0f) * radius;
glVertex3f(p[j] + center.x, p[(j + 1) % 3] + center.y, p[(j + 2) % 3] + center.z);
}
glEnd();
}
}
void mesh(vec3 *vertices, Index *indices, int iCount) {
glBegin(GL_LINES);
for (int i = 0; i < iCount; i += 3) {
vec3 &a = vertices[indices[i + 0]];
vec3 &b = vertices[indices[i + 1]];
vec3 &c = vertices[indices[i + 2]];
glVertex3fv((GLfloat*)&a);
glVertex3fv((GLfloat*)&b);
glVertex3fv((GLfloat*)&b);
glVertex3fv((GLfloat*)&c);
glVertex3fv((GLfloat*)&c);
glVertex3fv((GLfloat*)&a);
}
glEnd();
}
void axes(float size) {
glBegin(GL_LINES);
glColor3f(1, 0, 0); glVertex3f(0, 0, 0); glVertex3f(size, 0, 0);
glColor3f(0, 1, 0); glVertex3f(0, 0, 0); glVertex3f( 0, size, 0);
glColor3f(0, 0, 1); glVertex3f(0, 0, 0); glVertex3f( 0, 0, size);
glEnd();
}
void point(const vec3 &p, const vec4 &color) {
glColor4fv((GLfloat*)&color);
glBegin(GL_POINTS);
glVertex3fv((GLfloat*)&p);
glEnd();
}
}
}
#endif

44
src/shader.glsl Normal file
View File

@@ -0,0 +1,44 @@
R"====(
varying vec3 vLightVec;
varying vec2 vTexCoord;
varying vec4 vNormal;
varying vec4 vColor;
#ifdef VERTEX
uniform mat4 uViewProj;
uniform mat4 uModel;
uniform vec3 uLightPos;
attribute vec3 aCoord;
attribute vec2 aTexCoord;
attribute vec4 aNormal;
attribute vec4 aColor;
void main() {
vec4 coord = uModel * vec4(aCoord, 1.0);
vLightVec = uLightPos - coord.xyz;
vTexCoord = aTexCoord;
vNormal = uModel * aNormal;
vColor = aColor;
gl_Position = uViewProj * coord;
}
#else
uniform sampler2D sDiffuse;
uniform vec4 uColor;
uniform vec3 uAmbient;
uniform vec4 uLightColor;
void main() {
vec4 color = texture2D(sDiffuse, vTexCoord) * vColor * uColor;
// #ifdef LIGHTING
color.xyz = pow(color.xyz, vec3(2.2));
float lum = dot(normalize(vNormal.xyz), normalize(vLightVec));
float att = max(0.0, 1.0 - dot(vLightVec, vLightVec) / uLightColor.w);
vec3 light = uLightColor.xyz * max(vNormal.w, lum * att) + uAmbient;
color.xyz *= light;
color.xyz = pow(color.xyz, vec3(1.0/2.2));
// #endif
gl_FragColor = color;
}
#endif
)===="