commit 6b097fa6a03cc953b563e0875d36c10987d646f7 Author: Valentin Verdier Date: Fri Apr 17 18:11:57 2020 +0200 Commit initial diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8b1c88b --- /dev/null +++ b/Makefile @@ -0,0 +1,40 @@ +.PHONY:build bin clean +export includedir=$(CURDIR)/include/ +export builddir=$(CURDIR)/obj/ + +export CXX=g++ + +export CXXFLAGS= -fPIC -DGLEW_STATIC -I $(includedir) + +libs= -lGL -lGLU -lGLEW -lSDL -lSDL_image + +obj_files=obj/debug.o obj/render.o obj/OGL_ext.o obj/matrix.o \ +obj/vector.o obj/ressource.o obj/shader.o obj/mesh.o obj/program.o obj/object.o \ +obj/scene.o obj/texture.o obj/light.o obj/material.o obj/buffer.o obj/camera.o \ +obj/event.o obj/plane.o obj/box.o + +all:bin + +build: + -mkdir obj/ + make -C src/ build + +bin:build + -mkdir bin/ + $(CXX) -shared $(obj_files) -Wl,-soname,libXRA3D.so.1 -o libXRA3D.so $(libs) + mv libXRA3D.so bin/ + +install: + cp bin/libXRA3D.so /usr/local/lib/ + -mkdir /usr/local/include/XRA3D + cp $(includedir)* /usr/local/include/XRA3D/ + ldconfig + +uninstall: + -rm -f /usr/local/lib/libXRA3D.so* + -rm -rf /usr/local/include/XRA3D + ldconfig + +clean: + -rm -rf obj/ + -rm -rf bin/ diff --git a/include/OGL_ext.h b/include/OGL_ext.h new file mode 100644 index 0000000..aa4de5c --- /dev/null +++ b/include/OGL_ext.h @@ -0,0 +1,8 @@ +#pragma once +#ifndef OGL_EXT +#define OGL_EXT + +void Xr_InitExtSupport(); +void Xr_CheckExt(char*); + +#endif diff --git a/include/box.h b/include/box.h new file mode 100644 index 0000000..c791df5 --- /dev/null +++ b/include/box.h @@ -0,0 +1,58 @@ +#pragma once +#ifndef BOX +#define BOX + +#include "mesh.h" +#include "vector.h" +#include "plane.h" + +class Xr_AABB +{ + public: + Xr_AABB(); + ~Xr_AABB(); + + void setPos(float, float, float); + void genBox(Xr_Mesh*); + + Xr_Vector3D getPos() const; + Xr_Vector3D* const getPoints() const; + + private: + Xr_Vector3D A_pos; + Xr_Vector3D A_points[8]; +}; + +#define FRUSTUM_PLANES_NUMBER 5 + +#define TOP 0 +#define BOTTOM 1 +#define RIGHT 2 +#define LEFT 3 +#define NEAR 4 + +#define MAX_DISTANCE_TEST 4 + +class Xr_FrustumBox +{ + public: + Xr_FrustumBox(); + ~Xr_FrustumBox(); + + void setBoxSize(float, float, float, float); + void genBox(Xr_Vector3D const&, Xr_Vector3D const&, Xr_Vector3D const&, Xr_Vector3D const&, Xr_Vector3D const&, Xr_Vector3D const&); + + bool testPoint(float, float, float); + bool testAABB(Xr_AABB*); + + private: + float F_far; + float F_near; + float F_heightFar; + float F_heightNear; + float F_widthFar; + float F_widthNear; + Xr_Plane F_frustumPlanes[5]; +}; + +#endif diff --git a/include/buffer.h b/include/buffer.h new file mode 100644 index 0000000..c9278ed --- /dev/null +++ b/include/buffer.h @@ -0,0 +1,51 @@ +#pragma once +#ifndef BUFFER +#define BUFFER + +#include +#include + +#include "mesh.h" + +struct meshDesc +{ + meshDesc() : mesh(0), vertexOffset(0), vertexSize(0), texCoordOffset(0), texCoordSize(0), normalOffset(0), normalSize(0), vbo(0) + { + } + + Xr_Mesh *mesh; + unsigned int vertexOffset; + unsigned int vertexSize; + unsigned int texCoordOffset; + unsigned int texCoordSize; + unsigned int normalOffset; + unsigned int normalSize; + GLuint vbo; +}; + +class Xr_BufferManager +{ + public: + Xr_BufferManager(); + ~Xr_BufferManager(); + + static Xr_BufferManager* getInstance(); + static bool instanced(); + static void quitInstance(); + + bool load(Xr_Mesh*); + void unload(Xr_Mesh*); + void unloadAll(); + + void fetchVertex(Xr_Mesh*); + void fetchTexCoord(Xr_Mesh*); + void fetchNormal(Xr_Mesh*); + void fetchNull(); + + private: + + static Xr_BufferManager* B_instance; + std::vector B_meshList; +}; + +#endif diff --git a/include/camera.h b/include/camera.h new file mode 100644 index 0000000..4f42ac5 --- /dev/null +++ b/include/camera.h @@ -0,0 +1,56 @@ +#pragma once +#ifndef CAMERA +#define CAMERA + +#include "matrix.h" +#include "vector.h" +#include "box.h" + +class Xr_Camera +{ + public: + Xr_Camera(); + ~Xr_Camera(); + + void configModelview(Xr_MATRIX_4X4*); + void computeProjection(); + + void setPos(float, float, float); + void relPos(float, float, float); + void setAngle(float, float); + void relAngle(float, float); + void setFov(float); + void relFov(float); + void setFar(float); + void setNear(float); + + void moveForward(float); + void moveBackward(float); + void moveRight(float); + void moveLeft(float); + void moveUpDown(float); + + Xr_Vector3D getPos() const; + float getFov() const; + float* const getAngle() const; + Xr_MATRIX_4X4* const getProjection() const; + Xr_FrustumBox* const getFrustumBox() const; + + private: + Xr_MATRIX_4X4 *C_projection; + Xr_FrustumBox *C_box; + Xr_Vector3D C_eye; + Xr_Vector3D C_pos; + float C_fov; + float C_far; + float C_near; + float C_angle[2]; + + Xr_Vector3D C_dir; + Xr_Vector3D C_right; + Xr_Vector3D C_left; + Xr_Vector3D C_up; + Xr_Vector3D C_down; +}; + +#endif diff --git a/include/debug.h b/include/debug.h new file mode 100644 index 0000000..bfaa483 --- /dev/null +++ b/include/debug.h @@ -0,0 +1,10 @@ +#pragma once +#ifndef DEBUG +#define DEBUG + +#include + +void Xr_Log(std::string); +void Xr_Crash(); + +#endif diff --git a/include/devices.h b/include/devices.h new file mode 100644 index 0000000..02ab39f --- /dev/null +++ b/include/devices.h @@ -0,0 +1,142 @@ +#pragma once +#ifndef DEVICES +#define DEVICES + +#include + +#define XR_KEY_ESCAPE 27 +#define XR_KEY_SPACE 32 + +#define XR_KEY_a 97 +#define XR_KEY_b 98 +#define XR_KEY_c 99 +#define XR_KEY_d 100 +#define XR_KEY_e 101 +#define XR_KEY_f 102 +#define XR_KEY_g 103 +#define XR_KEY_h 104 +#define XR_KEY_i 105 +#define XR_KEY_j 106 +#define XR_KEY_k 107 +#define XR_KEY_l 108 +#define XR_KEY_m 109 +#define XR_KEY_n 110 +#define XR_KEY_o 111 +#define XR_KEY_p 112 +#define XR_KEY_q 113 +#define XR_KEY_r 114 +#define XR_KEY_s 115 +#define XR_KEY_t 116 +#define XR_KEY_u 117 +#define XR_KEY_v 118 +#define XR_KEY_w 119 +#define XR_KEY_x 120 +#define XR_KEY_y 121 +#define XR_KEY_z 122 + +#define XR_KEY_UP 273 +#define XR_KEY_DOWN 274 +#define XR_KEY_RIGHT 275 +#define XR_KEY_LEFT 276 + +#define XR_KEY_KP0 256 +#define XR_KEY_KP1 257 +#define XR_KEY_KP2 258 +#define XR_KEY_KP3 259 +#define XR_KEY_KP4 260 +#define XR_KEY_KP5 261 +#define XR_KEY_KP6 262 +#define XR_KEY_KP7 263 +#define XR_KEY_KP8 264 +#define XR_KEY_KP9 265 + +#define XR_KEY_F1 282 +#define XR_KEY_F2 283 +#define XR_KEY_F3 284 +#define XR_KEY_F4 285 +#define XR_KEY_F5 286 +#define XR_KEY_F6 287 +#define XR_KEY_F7 288 +#define XR_KEY_F8 289 +#define XR_KEY_F9 290 +#define XR_KEY_F10 291 +#define XR_KEY_F11 292 +#define XR_KEY_F12 293 + +#define XR_KEY_RSHIFT 303 +#define XR_KEY_LSHIFT 304 +#define XR_KEY_RCTRL 305 +#define XR_KEY_LCTRL 306 +#define XR_KEY_RALT 307 +#define XR_KEY_LALT 308 + +#define XR_MOUSE_BUTTON_LEFT 1 +#define XR_MOUSE_BUTTON_MIDDLE 2 +#define XR_MOUSE_BUTTON_RIGHT 3 +#define XR_MOUSE_WHEELUP 4 +#define XR_MOUSE_WHEELDOWN 5 + +struct Xr_MouseDesc +{ + Xr_MouseDesc() : button(new bool[8]), posX(0), posY(0), relX(0), relY(0), oldPosX(0), oldPosY(0) + { + int i; + for(i = 0; i < 8; i++) + { + button[i] = false; + } + } + + ~Xr_MouseDesc() + { + delete[] button; + } + + bool *button; + int posX; + int posY; + int relX; + int relY; + int oldPosX; + int oldPosY; +}; + +struct Xr_KeyboardDesc +{ + Xr_KeyboardDesc() : key(new bool[SDLK_LAST]) + { + int i; + for(i = 0; i < SDLK_LAST; i++) + { + key[i] = false; + } + } + + ~Xr_KeyboardDesc() + { + delete[] key; + } + + bool *key; +}; + +struct Xr_DevicesDesc +{ + Xr_DevicesDesc() : mouse(new Xr_MouseDesc), keyboard(new Xr_KeyboardDesc), windowHeight(0), windowWidth(0) + { + } + + ~Xr_DevicesDesc() + { + delete mouse; + delete keyboard; + } + + Xr_MouseDesc *mouse; + Xr_KeyboardDesc *keyboard; + + int windowHeight; + int windowWidth; +}; + +#endif diff --git a/include/event.h b/include/event.h new file mode 100644 index 0000000..9d9752d --- /dev/null +++ b/include/event.h @@ -0,0 +1,47 @@ +#pragma once +#ifndef EVENT +#define EVENT + +#include + +#include "devices.h" + +class Xr_EventProfil +{ + public: + Xr_EventProfil(); + ~Xr_EventProfil(); + + virtual void execute(Xr_DevicesDesc*); +}; + +class Xr_EventManager +{ + public: + Xr_EventManager(); + ~Xr_EventManager(); + + static Xr_EventManager* getInstance(); + static bool instaced(); + static void quitInstance(); + + void setProfil(Xr_EventProfil*); + void updateDeviceState(); + void executeProfil(); + bool quitEvent(); + void genQuitEvent(); + + void enableMouseCapture(); + void disableMouseCapture(); + bool mouseCaptured(); + + private: + static Xr_EventManager* E_instance; + Xr_EventProfil *E_eventProfil; + Xr_DevicesDesc *E_devices; + SDL_Event *E_event; + bool E_mouseCapture; + bool E_quit; +}; + +#endif diff --git a/include/light.h b/include/light.h new file mode 100644 index 0000000..4cd4088 --- /dev/null +++ b/include/light.h @@ -0,0 +1,42 @@ +#pragma once +#ifndef LIGHT +#define LIGHT + +#include + +#include "matrix.h" + +#define DEPTH_MAP_SIZE 1024 + +class Xr_Light +{ + public: + Xr_Light(); + Xr_Light(float posX, float poxY, float posZ, float eyeX, float eyeY, float eyeZ, float intensity, float colorR, float colorG, float colorB, float fov, bool shadow); + ~Xr_Light(); + + void configure(float posX, float posY, float posZ, float eyeX, float eyeY, float eyeZ, float intensity, float colorR, float colorG, float colorB, float fov, bool shadow); + + float* const getColorRGB() const; + float getIntensity() const; + float* const getPos() const; + float* const getEye() const; + float getFov() const; + Xr_MATRIX_4X4* const getLightProjectionMatrix() const; + Xr_MATRIX_4X4* const getLightModelviewMatrix() const; + GLuint getLightDepthMap() const; + bool hasShadow() const; + + private: + float L_colorRGB[3]; + float L_intensity; + float L_pos[3]; + float L_eye[3]; + float L_fov; + bool L_shadow; + Xr_MATRIX_4X4 *L_projection; + Xr_MATRIX_4X4 *L_modelview; + GLuint L_depthMap; +}; + +#endif diff --git a/include/material.h b/include/material.h new file mode 100644 index 0000000..93682bc --- /dev/null +++ b/include/material.h @@ -0,0 +1,45 @@ +#pragma once +#ifndef MATERIAL +#define MATERIAL + +#include "texture.h" + +class Xr_Material +{ + public: + Xr_Material(); + Xr_Material(bool, bool, float, float, float, Xr_Texture*); + ~Xr_Material(); + + void enableShadow(); + void disableShadow(); + void enableShaderless(); + void disableShaderless(); + + void setAmbient(float); + void setDiffuse(float); + void setSpecular(float); + + void attachTexture(Xr_Texture*); + void detachTexture(); + + float getAmbient() const; + float getDiffuse() const; + float getSpecular() const; + Xr_Texture* const getTexture() const; + + /* à ajouter -> support multi-texturage */ + + private: + bool M_shadow; + bool M_shaderless; + + float M_ambient; + float M_diffuse; + float M_specular; + Xr_Texture *M_texture; + + /* à ajouter -> support multi-texturage */ +}; + +#endif diff --git a/include/matrix.h b/include/matrix.h new file mode 100644 index 0000000..65db214 --- /dev/null +++ b/include/matrix.h @@ -0,0 +1,30 @@ +#pragma once +#ifndef MATRIX +#define MATRIX + +class Xr_MATRIX_4X4 +{ + public: + Xr_MATRIX_4X4(); + Xr_MATRIX_4X4(float*); + Xr_MATRIX_4X4(Xr_MATRIX_4X4 const&); + ~Xr_MATRIX_4X4(); + + /* Valeur de retour en lecture seul */ + float* const getValues() const; + + void loadIdentity(); + void loadProjection(float, float, float, float); + void lookAt(float, float, float, float, float, float, float, float, float); + void translate(float, float, float); + void rotate(float, float, float, float); + void scale(float, float, float); + + Xr_MATRIX_4X4& operator=(Xr_MATRIX_4X4 const& matrix); + Xr_MATRIX_4X4 operator*(Xr_MATRIX_4X4 const& matrix); + + private: + float *M_values; +}; + +#endif diff --git a/include/mesh.h b/include/mesh.h new file mode 100644 index 0000000..6a2d949 --- /dev/null +++ b/include/mesh.h @@ -0,0 +1,31 @@ +#pragma once +#ifndef MESH +#define MESH + +#include "ressource.h" + +class Xr_Mesh : public Xr_Ressource +{ + public: + Xr_Mesh(); + Xr_Mesh(char*); + Xr_Mesh(Xr_Mesh const&); + virtual ~Xr_Mesh(); + + float* const getVertex() const; + float* const getTexCoord() const; + float* const getNormal() const; + int getFaceNumber() const; + + void load(); + + + private: + float *M_vertex; + float *M_texCoord; + float *M_normal; + + int M_faceNumber; +}; + +#endif diff --git a/include/object.h b/include/object.h new file mode 100644 index 0000000..43fb3e0 --- /dev/null +++ b/include/object.h @@ -0,0 +1,41 @@ +#pragma once +#ifndef OBJECT +#define OBJECT + +#include + +#include "mesh.h" +#include "material.h" +#include "box.h" + +class Xr_Object +{ + public: + Xr_Object(); + ~Xr_Object(); + + bool isReady() const; + float* const getPos() const; + Xr_Material* const getMaterial() const; + Xr_AABB* const getBox() const; + + void attachMesh(Xr_Mesh*); + void detachMesh(); + void loadToServer(); + void unloadToServer(); + void attachMaterial(Xr_Material*); + void detachMaterial(); + void setPos(float, float, float); + void draw(); + + private: + Xr_Mesh *O_mesh; + Xr_AABB *O_box; + + Xr_Material *O_material; + + float O_pos[3]; + bool O_isReady; +}; + +#endif diff --git a/include/plane.h b/include/plane.h new file mode 100644 index 0000000..77acebe --- /dev/null +++ b/include/plane.h @@ -0,0 +1,28 @@ +#pragma once +#ifndef PLAN +#define PLAN + +#include "vector.h" + +class Xr_Plane +{ + public: + Xr_Plane(); + Xr_Plane(Xr_Vector3D const&, Xr_Vector3D const&, Xr_Vector3D const&); + Xr_Plane(Xr_Vector3D const&, Xr_Vector3D const&); + ~Xr_Plane(); + + void setPoints(Xr_Vector3D const&, Xr_Vector3D const&, Xr_Vector3D const&); + void setNormalPoint(Xr_Vector3D const&, Xr_Vector3D const&); + + float getDistanceToPoint(Xr_Vector3D const&); + void flipNormal(); + Xr_Vector3D getNormal(); + + private: + Xr_Vector3D P_normal; + Xr_Vector3D P_point; + float P_d; +}; + +#endif diff --git a/include/program.h b/include/program.h new file mode 100644 index 0000000..405796d --- /dev/null +++ b/include/program.h @@ -0,0 +1,29 @@ +#pragma once +#ifndef PROGRAM +#define PROGRAM + +#include + +#include "shader.h" + +class Xr_Program +{ + public: + Xr_Program(); + ~Xr_Program(); + + bool isLink() const; + GLuint getProgramId() const; + + void attachShader(Xr_Shader*); + void detachShader(Xr_Shader*); + void link(); + + private: + GLuint P_id; + Xr_Shader *P_shader1; + Xr_Shader *P_shader2; + bool P_isLink; +}; + +#endif diff --git a/include/render.h b/include/render.h new file mode 100644 index 0000000..5c05f0b --- /dev/null +++ b/include/render.h @@ -0,0 +1,45 @@ +#pragma once +#ifndef RENDER +#define RENDER + +#include "scene.h" + +class Xr_Renderer +{ + public: + Xr_Renderer(); + ~Xr_Renderer(); + + static Xr_Renderer* getInstance(); + static bool instanced(); + static void quitInstance(); + + void init(unsigned int, unsigned int, bool, unsigned int); + void mainLoop(); + + void setVideo(unsigned int, unsigned int, bool); + void setFrameRateLimite(unsigned int); + void setImageSize(unsigned int, unsigned int); + + void setScene(Xr_Scene*); + + unsigned int getWindowWidth() const; + unsigned int getWindowHeight() const; + unsigned int getImageWidth() const; + unsigned int getImageHeight() const; + unsigned int getFrameRate() const; + + private: + static Xr_Renderer* R_instance; + bool isInit; + unsigned int R_windowWidth; + unsigned int R_windowHeight; + unsigned int R_imageWidth; + unsigned int R_imageHeight; + bool R_fullscreen; + unsigned int R_frameRateLimite; + unsigned int R_frameRate; + Xr_Scene *R_scene; +}; + +#endif diff --git a/include/ressource.h b/include/ressource.h new file mode 100644 index 0000000..09c9034 --- /dev/null +++ b/include/ressource.h @@ -0,0 +1,54 @@ +#pragma once +#ifndef RESSOURCE +#define RESSOURCE + +#include + +enum ressourceType +{ + xr_mesh_type, + xr_shader_type, + xr_texture_type +}; + +class Xr_Ressource +{ + public: + Xr_Ressource(); + Xr_Ressource(char*, ressourceType); + Xr_Ressource(ressourceType); + virtual ~Xr_Ressource(); + + char* const getName() const; + bool isLoaded() const; + ressourceType getType() const; + + + protected: + char *R_name; + bool R_isLoaded; + ressourceType R_type; +}; + +class Xr_RessourceManager +{ + public: + Xr_RessourceManager(); + ~Xr_RessourceManager(); + + static bool instanced(); + static Xr_RessourceManager* getInstance(); + static void quitInstance(); + + bool load(ressourceType, char*); + void unload(ressourceType, char*); + void unloadAll(); + + Xr_Ressource* get(ressourceType, char*); + + private: + static Xr_RessourceManager *R_instance; + std::vector R_ressourcesList; +}; + +#endif diff --git a/include/scene.h b/include/scene.h new file mode 100644 index 0000000..801869c --- /dev/null +++ b/include/scene.h @@ -0,0 +1,38 @@ +#pragma once +#ifndef SCENE +#define SCENE + +#include + +#include "program.h" +#include "object.h" +#include "matrix.h" +#include "light.h" +#include "camera.h" + + +class Xr_Scene +{ + public: + Xr_Scene(); + ~Xr_Scene(); + + void addObject(Xr_Object*); + void deleteObject(Xr_Object*); + void addLight(Xr_Light*); + void deleteLight(Xr_Light*); + void attachCamera(Xr_Camera*); + void detachCamera(); + void draw(); + + private: + std::vector S_objectList; + std::vector S_lightList; + Xr_MATRIX_4X4 *modelview; + Xr_Camera *S_camera; + + Xr_Program *basicProg; + Xr_Program *render1Prog; +}; + +#endif diff --git a/include/shader.h b/include/shader.h new file mode 100644 index 0000000..7d2b64b --- /dev/null +++ b/include/shader.h @@ -0,0 +1,34 @@ +#pragma once +#ifndef SHADER +#define SHADER + +#include + +#include "ressource.h" + + +class Xr_Shader : public Xr_Ressource +{ + public: + Xr_Shader(); + Xr_Shader(char*); + virtual ~Xr_Shader(); + + GLuint getShaderId() const; + GLenum getShaderType() const; + bool isCompile() const; + char* const getSrc() const; + int getSrcLength() const; + + void load(); + void compile(); + + private: + GLuint S_id; + GLenum S_type; + char *S_src; + bool S_isCompile; + int S_srcLength; +}; + +#endif diff --git a/include/texture.h b/include/texture.h new file mode 100644 index 0000000..e856671 --- /dev/null +++ b/include/texture.h @@ -0,0 +1,24 @@ +#pragma once +#ifndef TEXTURE +#define TEXTURE + +#include + +#include "ressource.h" + +class Xr_Texture : public Xr_Ressource +{ + public: + Xr_Texture(); + Xr_Texture(char*); + virtual ~Xr_Texture(); + + GLuint getTextureId() const; + + void load(); + + private: + GLuint T_id; +}; + +#endif diff --git a/include/vector.h b/include/vector.h new file mode 100644 index 0000000..1b323ce --- /dev/null +++ b/include/vector.h @@ -0,0 +1,37 @@ +#pragma once +#ifndef VECTOR +#define VECTOR + +class Xr_Vector3D +{ + public: + Xr_Vector3D(); + Xr_Vector3D(float, float, float); + ~Xr_Vector3D(); + + void setCoords(float, float, float); + void normalize(); + static float dot(Xr_Vector3D const&, Xr_Vector3D const&); + + float getX() const; + float getY() const; + float getZ() const; + + Xr_Vector3D& operator=(Xr_Vector3D const&); + Xr_Vector3D& operator+=(Xr_Vector3D const&); + Xr_Vector3D& operator-=(Xr_Vector3D const&); + Xr_Vector3D operator+(Xr_Vector3D const&) const; + Xr_Vector3D operator-(Xr_Vector3D const&) const; + Xr_Vector3D operator-() const; + Xr_Vector3D operator*(Xr_Vector3D const&) const; + Xr_Vector3D operator*(float) const; + float operator[](unsigned int) const; + + private: + float xCoord; + float yCoord; + float zCoord; + +}; + +#endif diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..ab33514 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,12 @@ +.PHONY:%.o build +objects=debug.o render.o OGL_ext.o matrix.o vector.o ressource.o shader.o \ +mesh.o program.o object.o scene.o texture.o light.o material.o buffer.o camera.o \ +event.o plane.o box.o + +build:$(objects) + for object in $(objects) ; do \ + mv $$object $(builddir)$$object; \ + done + +%.o:%.cpp + $(CXX) $(CXXFLAGS) -c $^ diff --git a/src/OGL_ext.cpp b/src/OGL_ext.cpp new file mode 100644 index 0000000..0ebee37 --- /dev/null +++ b/src/OGL_ext.cpp @@ -0,0 +1,28 @@ +#include + +#include "OGL_ext.h" + +#include "debug.h" + +void Xr_InitExtSupport() +{ + GLenum returnCode; + + returnCode = glewInit(); + if(returnCode != GLEW_OK) + { + Xr_Log("X-Ray3D ne peut fonctionner sans les extentions OpenGL"); + Xr_Crash(); + } + Xr_Log("Les extentions OpenGL sont disponibles"); +} + +void Xr_CheckExt(char *extName) +{ + if(!glewIsSupported(extName)) + { + Xr_Log(std::string(extName) + " : Extention OpenGL non supportée par votre pilote graphique"); + Xr_Crash(); + } + Xr_Log("L'extention < " + std::string(extName) + " > est supportée"); +} diff --git a/src/box.cpp b/src/box.cpp new file mode 100644 index 0000000..e7915de --- /dev/null +++ b/src/box.cpp @@ -0,0 +1,217 @@ +#include + +#include "box.h" + +#include "debug.h" +#include + +/* class Xr_AABB */ + +Xr_AABB::Xr_AABB() +{ +} + +Xr_AABB::~Xr_AABB() +{ +} + +void Xr_AABB::setPos(float x, float y, float z) +{ + A_pos.setCoords(x, y, z); +} + +void Xr_AABB::genBox(Xr_Mesh *mesh) +{ + int i; + float Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0; + float *vertex; + + if(mesh) + { + if(mesh->getVertex()) + { + vertex = mesh->getVertex(); + for(i = 0; i < mesh->getFaceNumber() * 3 * 3; i += 3) + { + if(vertex[i] > Xmax) + { + Xmax = vertex[i]; + } + else if(vertex[i] < Xmin) + { + Xmin = vertex[i]; + } + + if(vertex[i + 1] > Ymax) + { + Ymax = vertex[i + 1]; + } + else if(vertex[i + 1] < Ymin) + { + Ymin = vertex[i + 1]; + } + + if(vertex[i + 2] > Zmax) + { + Zmax = vertex[i + 2]; + } + else if(vertex[i + 2] < Zmin) + { + Zmin = vertex[i + 2]; + } + } + + A_points[0].setCoords(Xmax, Ymax, Zmax); + A_points[1].setCoords(Xmax, Ymax, Zmin); + A_points[2].setCoords(Xmax, Ymin, Zmin); + A_points[3].setCoords(Xmax, Ymin, Zmax); + A_points[4].setCoords(Xmin, Ymax, Zmax); + A_points[5].setCoords(Xmin, Ymax, Zmin); + A_points[6].setCoords(Xmin, Ymin, Zmin); + A_points[7].setCoords(Xmin, Ymin, Zmax); + } + else + { + Xr_Log("Xr_AABB : Le mesh ne contient aucun vertices"); + } + } + else + { + Xr_Log("Xr_AABB : Le mesh est invalide"); + } +} + +Xr_Vector3D Xr_AABB::getPos() const +{ + return A_pos; +} + +Xr_Vector3D* const Xr_AABB::getPoints() const +{ + return (Xr_Vector3D* const) A_points; +} + + +/* class Xr_FrustumBox */ + +Xr_FrustumBox::Xr_FrustumBox() +{ +} + +Xr_FrustumBox::~Xr_FrustumBox() +{ +} + +void Xr_FrustumBox::setBoxSize(float near, float far, float fov, float ratio) +{ + F_far = far; + F_near = near; + + float radFov = M_PI * (fov / (float) 180); + + F_heightFar = 2 * tan(radFov / 2) * F_far; + F_widthFar = F_heightFar * ratio; + + F_heightNear = 2 * tan(radFov / 2) * F_near; + F_widthNear = F_heightNear * ratio; +} + +void Xr_FrustumBox::genBox(Xr_Vector3D const& camPos, Xr_Vector3D const& dir, Xr_Vector3D const& right, Xr_Vector3D const& left, Xr_Vector3D const& up, Xr_Vector3D const& down) +{ + /* + * box's points + * + * far plane + * + * a +-------------+ b + * | | + * | | + * | | + * c +-------------+ d + * + * near plane + * + * e +-------------+ h + * | | + * | | + * | | + * f +-------------+ g + * + */ + + Xr_Vector3D a, b, c, d, e, f, g, h; + + a = camPos + (dir * F_far) + (up * (F_heightFar / 2)) + (left * (F_widthFar / 2)); + b = camPos + (dir * F_far) + (up * (F_heightFar / 2)) + (right * (F_widthFar / 2)); + c = camPos + (dir * F_far) + (down * (F_heightFar / 2)) + (left * (F_widthFar / 2)); + d = camPos + (dir * F_far) + (down * (F_heightFar / 2)) + (right * (F_widthFar / 2)); + + e = camPos + (dir * F_near) + (up * (F_heightNear / 2)) + (left * (F_widthNear / 2)); + f = camPos + (dir * F_near) + (down * (F_heightNear / 2)) + (left * (F_widthNear / 2)); + g = camPos + (dir * F_near) + (down * (F_heightNear / 2)) + (right * (F_widthNear / 2)); + h = camPos + (dir * F_near) + (up * (F_heightNear / 2)) + (right * (F_widthNear / 2)); + + /*std::cout << "v " << a[0] << " " << a[1] << " " << a[2] << std::endl; + std::cout << "v " << b[0] << " " << b[1] << " " << b[2] << std::endl; + std::cout << "v " << c[0] << " " << c[1] << " " << c[2] << std::endl; + std::cout << "v " << d[0] << " " << d[1] << " " << d[2] << std::endl; + + std::cout << "v " << e[0] << " " << e[1] << " " << e[2] << std::endl; + std::cout << "v " << f[0] << " " << f[1] << " " << f[2] << std::endl; + std::cout << "v " << g[0] << " " << g[1] << " " << g[2] << std::endl; + std::cout << "v " << h[0] << " " << h[1] << " " << h[2] << std::endl; + + std::cout << "f 1 2 3 4\nf 2 8 7 4\nf 1 5 6 3\nf 1 2 8 5\nf 3 4 7 6\n" << std::endl;*/ + + F_frustumPlanes[RIGHT].setPoints(g, h, d); + F_frustumPlanes[LEFT].setPoints(e, f, c); + F_frustumPlanes[TOP].setPoints(h, e, a); + F_frustumPlanes[BOTTOM].setPoints(f, g, d); + F_frustumPlanes[NEAR].setPoints(e, h, g); +} + +bool Xr_FrustumBox::testPoint(float x, float y, float z) +{ + Xr_Vector3D point(x, y, z); + + int i; + + for(i = 0; i < FRUSTUM_PLANES_NUMBER; i++) + { + if(F_frustumPlanes[i].getDistanceToPoint(point) < -MAX_DISTANCE_TEST) + { + return false; + } + } + + return true; +} + +bool Xr_FrustumBox::testAABB(Xr_AABB *box) +{ + Xr_Vector3D *points = box->getPoints(); + + float maxDistance, distance; + + int i, j; + for(i = 0; i < FRUSTUM_PLANES_NUMBER; i++) + { + maxDistance = F_frustumPlanes[i].getDistanceToPoint(points[0]); + + for(j = 1; j < 8; j++) + { + distance = F_frustumPlanes[i].getDistanceToPoint(points[j]); + + if(distance > maxDistance) + { + maxDistance = distance; + } + } + + if(maxDistance < -MAX_DISTANCE_TEST) + { + return false; + } + } + return true; +} diff --git a/src/buffer.cpp b/src/buffer.cpp new file mode 100644 index 0000000..5ddc7b2 --- /dev/null +++ b/src/buffer.cpp @@ -0,0 +1,194 @@ +#include +#include + +#include "buffer.h" + +#include "debug.h" + +Xr_BufferManager* Xr_BufferManager::B_instance = 0; + +Xr_BufferManager::Xr_BufferManager() +{ +} + +Xr_BufferManager::~Xr_BufferManager() +{ + unloadAll(); +} + +Xr_BufferManager* Xr_BufferManager::getInstance() +{ + if(B_instance) + { + return B_instance; + } + else + { + B_instance = new Xr_BufferManager; + return B_instance; + } +} + +bool Xr_BufferManager::instanced() +{ + if(B_instance) + { + return true; + } + else + { + return false; + } +} + +void Xr_BufferManager::quitInstance() +{ + if(B_instance) + { + delete B_instance; + } +} + +bool Xr_BufferManager::load(Xr_Mesh *mesh) +{ + int i; + + if(mesh) + { + for(i = 0; i < B_meshList.size(); i++) + { + if(B_meshList[i].mesh == mesh) + { + return true; + } + } + + Xr_Log("Xr_BufferManager : Chargement des données géométriques du mesh < " + std::string(mesh->getName()) + " >"); + if(mesh->getFaceNumber() == 0) + { + Xr_Log("Xr_BufferManager : Le mesh ne contient aucune face"); + return false; + } + + if(mesh->getVertex()) + { + B_meshList.push_back(meshDesc()); + B_meshList.back().mesh = mesh; + B_meshList.back().vertexSize = ((mesh->getFaceNumber() * 3) * 3) * sizeof(float); + B_meshList.back().vertexOffset = 0; + } + else + { + Xr_Log("Xr_BufferManager : Le mesh ne contient aucun vertice"); + B_meshList.erase(B_meshList.end()); + return false; + } + + if(mesh->getTexCoord()) + { + B_meshList.back().texCoordSize = ((mesh->getFaceNumber() * 3) * 2) * sizeof(float); + B_meshList.back().texCoordOffset = B_meshList.back().vertexSize; + } + + if(mesh->getNormal()) + { + B_meshList.back().normalSize = ((mesh->getFaceNumber() * 3) * 3) * sizeof(float); + B_meshList.back().normalOffset = B_meshList.back().vertexSize + B_meshList.back().texCoordSize; + } + + glGenBuffers(1, &(B_meshList.back().vbo)); + glBindBuffer(GL_ARRAY_BUFFER, B_meshList.back().vbo); + glBufferData(GL_ARRAY_BUFFER, (GLsizei) B_meshList.back().vertexSize + B_meshList.back().texCoordSize + B_meshList.back().normalSize, 0, GL_STREAM_DRAW); + glBufferSubData(GL_ARRAY_BUFFER, (GLint) B_meshList.back().vertexOffset, (GLsizei) B_meshList.back().vertexSize, mesh->getVertex()); + glBufferSubData(GL_ARRAY_BUFFER, (GLint) B_meshList.back().texCoordOffset, (GLsizei) B_meshList.back().texCoordSize, mesh->getTexCoord()); + glBufferSubData(GL_ARRAY_BUFFER, (GLint) B_meshList.back().normalOffset, (GLsizei) B_meshList.back().normalSize, mesh->getNormal()); + + return true; + } + else + { + Xr_Log("Xr_BufferManager : Le mesh est invalide"); + } +} + +void Xr_BufferManager::unload(Xr_Mesh *mesh) +{ + int i; + + for(i = 0; i < B_meshList.size(); i++) + { + if(B_meshList[i].mesh == mesh) + { + Xr_Log("Xr_BufferManager : Suppression des données géométriques du mesh < " + std::string(mesh->getName()) + " >"); + glDeleteBuffers(1, &(B_meshList[i].vbo)); + B_meshList.erase(B_meshList.begin() + i); + return; + } + } +} + + +void Xr_BufferManager::unloadAll() +{ + int i; + + for(i = 0; i < B_meshList.size(); i++) + { + Xr_Log("Xr_BufferManager : Suppression des données géométriques du mesh < " + std::string(B_meshList[i].mesh->getName()) + " >"); + glDeleteBuffers(1, &(B_meshList[i].vbo)); + } + B_meshList.clear(); +} + +void Xr_BufferManager::fetchVertex(Xr_Mesh *mesh) +{ + int i; + for(i = 0; i < B_meshList.size(); i++) + { + if(B_meshList[i].mesh == mesh) + { + glBindBuffer(GL_ARRAY_BUFFER, B_meshList[i].vbo); + glVertexPointer(3, GL_FLOAT, 0, (float*) B_meshList[i].vertexOffset); + glEnableClientState(GL_VERTEX_ARRAY); + return; + } + } +} + +void Xr_BufferManager::fetchTexCoord(Xr_Mesh *mesh) +{ + int i; + for(i = 0; i < B_meshList.size(); i++) + { + if(B_meshList[i].mesh == mesh) + { + glBindBuffer(GL_ARRAY_BUFFER, B_meshList[i].vbo); + glTexCoordPointer(2, GL_FLOAT, 0,(float*) B_meshList[i].texCoordOffset); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + return; + } + } +} + +void Xr_BufferManager::fetchNormal(Xr_Mesh *mesh) +{ + int i; + for(i = 0; i < B_meshList.size(); i++) + { + if(B_meshList[i].mesh == mesh) + { + glBindBuffer(GL_ARRAY_BUFFER, B_meshList[i].vbo); + glNormalPointer(GL_FLOAT, 0,(float*) B_meshList[i].normalOffset); + glEnableClientState(GL_NORMAL_ARRAY); + return; + } + } +} + +void Xr_BufferManager::fetchNull() +{ + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glDisableClientState(GL_NORMAL_ARRAY); + glBindBuffer(GL_ARRAY_BUFFER, 0); +} diff --git a/src/camera.cpp b/src/camera.cpp new file mode 100644 index 0000000..2ec9726 --- /dev/null +++ b/src/camera.cpp @@ -0,0 +1,188 @@ +#include +#include + +#include "camera.h" + +#include "render.h" + +/* class Xr_Camera */ + +Xr_Camera::Xr_Camera() : C_projection(new Xr_MATRIX_4X4), C_box(new Xr_FrustumBox), C_fov(70), C_far(1000), C_near(0.001), C_angle({0}) +{ + computeProjection(); + C_dir.setCoords(1, 0, 0); + C_right.setCoords(0, 0, -1); + C_left = -C_right; + C_up.setCoords(0, 1, 0); + C_down = -C_up; +} + +Xr_Camera::~Xr_Camera() +{ + delete C_projection; + delete C_box; +} + +void Xr_Camera::configModelview(Xr_MATRIX_4X4 *modelview) +{ + float aX = M_PI * (C_angle[0] / (float) 180); + float aY = M_PI * (C_angle[1] / (float) 180); + + C_eye.setCoords(C_near * (cos(aY) * cos(aX)), C_near * sin(aY), C_near * (cos(aY) * sin(aX))); + C_eye += C_pos; + + C_dir = C_pos - C_eye; + C_right.setCoords(-C_dir.getZ(), 0, C_dir.getX()); + C_left = -C_right; + C_up = C_right * C_dir; + C_down = -C_up; + + C_dir.normalize(); + C_right.normalize(); + C_left.normalize(); + C_up.normalize(); + C_down.normalize(); + + C_box->genBox(C_pos, C_dir, C_right, C_left, C_up, C_down); + modelview->lookAt(C_eye[0], C_eye[1], C_eye[2], C_pos[0], C_pos[1], C_pos[2], 0, 1, 0); +} + +void Xr_Camera::computeProjection() +{ + float ratio = (float) Xr_Renderer::getInstance()->getImageWidth() / Xr_Renderer::getInstance()->getWindowHeight(); + C_projection->loadIdentity(); + C_projection->loadProjection(C_fov, ratio, C_near, C_far); + + C_box->setBoxSize(C_near, C_far, C_fov, ratio); +} + +void Xr_Camera::setPos(float x, float y, float z) +{ + C_pos.setCoords(x, y, z); +} + +void Xr_Camera::relPos(float x, float y, float z) +{ + C_pos += Xr_Vector3D(x, y, z); +} + +void Xr_Camera::setAngle(float x, float y) +{ + C_angle[0] = x; + + if(y > 89) + { + C_angle[1] = 89; + } + else if(y < -89) + { + C_angle[1] = -89; + } +} + +void Xr_Camera::relAngle(float x, float y) +{ + C_angle[0] += x; + C_angle[1] += y; + + if(C_angle[1] > 89) + { + C_angle[1] = 89; + } + else if(C_angle[1] < -89) + { + C_angle[1] = -89; + } +} + +void Xr_Camera::setFov(float fov) +{ + C_fov = fov; + + if(C_fov > 180) + { + C_fov = 180; + } + if(C_fov < 1) + { + C_fov = 1; + } + + computeProjection(); +} + +void Xr_Camera::relFov(float fov) +{ + C_fov += fov; + + if(C_fov > 180) + { + C_fov = 180; + } + if(C_fov < 1) + { + C_fov = 1; + } + + computeProjection(); +} + +void Xr_Camera::setFar(float far) +{ + C_far = far; +} + +void Xr_Camera::setNear(float near) +{ + C_near = near; +} + +void Xr_Camera::moveForward(float n) +{ + C_pos += C_dir * n; +} + +void Xr_Camera::moveBackward(float n) +{ + C_pos -= C_dir * n; +} + +void Xr_Camera::moveRight(float n) +{ + C_pos += C_right * n; +} + +void Xr_Camera::moveLeft(float n) +{ + C_pos += C_left * n; +} + +void Xr_Camera::moveUpDown(float n) +{ + relPos(0, n, 0); +} + +Xr_Vector3D Xr_Camera::getPos() const +{ + return C_pos; +} + +float Xr_Camera::getFov() const +{ + return C_fov; +} + +float* const Xr_Camera::getAngle() const +{ + return (float* const) C_angle; +} + +Xr_MATRIX_4X4* const Xr_Camera::getProjection() const +{ + return (Xr_MATRIX_4X4* const) C_projection; +} + +Xr_FrustumBox* const Xr_Camera::getFrustumBox() const +{ + return (Xr_FrustumBox* const) C_box; +} diff --git a/src/debug.cpp b/src/debug.cpp new file mode 100644 index 0000000..d6a1193 --- /dev/null +++ b/src/debug.cpp @@ -0,0 +1,22 @@ +#include +#include +#include + +#include "debug.h" + +#include "ressource.h" + +void Xr_Log(std::string log) +{ + std::cout << log << std::endl; +} + +void Xr_Crash() +{ + std::cout << "X-Ray3D ne peut plus fonctionner correctement" << std::endl; + if(Xr_RessourceManager::instanced()) + { + Xr_RessourceManager::quitInstance(); + } + exit(1); +} diff --git a/src/event.cpp b/src/event.cpp new file mode 100644 index 0000000..77cafb6 --- /dev/null +++ b/src/event.cpp @@ -0,0 +1,182 @@ +#include "event.h" + +#include "debug.h" +#include "render.h" + +/* class Xr_EventProfil */ + +Xr_EventProfil::Xr_EventProfil() +{ +} + +Xr_EventProfil::~Xr_EventProfil() +{ +} + +void Xr_EventProfil::execute(Xr_DevicesDesc *devices) +{ +} + +/* class Xr_EventManager */ + +Xr_EventManager* Xr_EventManager::E_instance = 0; + +Xr_EventManager::Xr_EventManager() : E_eventProfil(0), E_devices(new Xr_DevicesDesc), E_event(new SDL_Event), E_mouseCapture(false), E_quit(false) +{ +} + +Xr_EventManager::~Xr_EventManager() +{ + delete E_devices; + delete E_event; +} + +Xr_EventManager* Xr_EventManager::getInstance() +{ + if(E_instance) + { + return E_instance; + } + else + { + E_instance = new Xr_EventManager; + return E_instance; + } +} + +bool Xr_EventManager::instaced() +{ + if(E_instance) + { + return true; + } + else + { + return false; + } +} + +void Xr_EventManager::quitInstance() +{ + if(E_instance) + { + delete E_instance; + } +} + +void Xr_EventManager::setProfil(Xr_EventProfil *profil) +{ + if(profil) + { + E_eventProfil = profil; + } + else + { + Xr_Log("Xr_EventManager : Le profil est invalide"); + } +} + +void Xr_EventManager::updateDeviceState() +{ + E_devices->mouse->relX = 0; + E_devices->mouse->relY = 0; + + E_devices->windowHeight = 0; + E_devices->windowWidth = 0; + + while(SDL_PollEvent(E_event)) + { + switch(E_event->type) + { + case SDL_VIDEORESIZE: + E_devices->windowHeight = E_event->resize.h; + E_devices->windowWidth = E_event->resize.w; + + case SDL_KEYDOWN: + E_devices->keyboard->key[E_event->key.keysym.sym] = true; + break; + + case SDL_KEYUP: + E_devices->keyboard->key[E_event->key.keysym.sym] = false; + break; + + case SDL_MOUSEMOTION: + E_devices->mouse->oldPosX = E_devices->mouse->posX; + E_devices->mouse->oldPosY = E_devices->mouse->posY; + + E_devices->mouse->posX = E_event->motion.x; + E_devices->mouse->posY = E_event->motion.y; + + E_devices->mouse->relX = E_devices->mouse->posX - E_devices->mouse->oldPosX; + E_devices->mouse->relY = E_devices->mouse->posY - E_devices->mouse->oldPosY; + if(E_mouseCapture) + { + if(E_devices->mouse->posX >= Xr_Renderer::getInstance()->getWindowWidth() - 30 || E_devices->mouse->posX <= 30 || E_devices->mouse->posY >= Xr_Renderer::getInstance()->getWindowHeight() - 20 || E_devices->mouse->posY <= 20) + { + SDL_WarpMouse((Xr_Renderer::getInstance()->getWindowWidth() / 2), (Xr_Renderer::getInstance()->getWindowHeight() / 2)); + E_devices->mouse->posX = (Xr_Renderer::getInstance()->getWindowWidth() / 2); + E_devices->mouse->posY = (Xr_Renderer::getInstance()->getWindowHeight() / 2); + } + } + break; + + case SDL_MOUSEBUTTONDOWN: + E_devices->mouse->button[E_event->button.button] = true; + break; + + case SDL_MOUSEBUTTONUP: + E_devices->mouse->button[E_event->button.button] = false; + break; + + case SDL_QUIT: + E_quit = true; + break; + + default: + break; + } + } +} + +void Xr_EventManager::executeProfil() +{ + if(E_eventProfil) + { + E_eventProfil->execute(E_devices); + } +} + +bool Xr_EventManager::quitEvent() +{ + return E_quit; +} + +void Xr_EventManager::genQuitEvent() +{ + E_quit = true; +} + +void Xr_EventManager::enableMouseCapture() +{ + if(!E_mouseCapture) + { + SDL_WM_GrabInput(SDL_GRAB_ON); + SDL_ShowCursor(SDL_DISABLE); + E_mouseCapture = true; + } +} + +void Xr_EventManager::disableMouseCapture() +{ + if(E_mouseCapture) + { + SDL_WM_GrabInput(SDL_GRAB_OFF); + SDL_ShowCursor(SDL_ENABLE); + E_mouseCapture = false; + } +} + +bool Xr_EventManager::mouseCaptured() +{ + return E_mouseCapture; +} diff --git a/src/light.cpp b/src/light.cpp new file mode 100644 index 0000000..1a46fb4 --- /dev/null +++ b/src/light.cpp @@ -0,0 +1,102 @@ +#include "light.h" + +Xr_Light::Xr_Light() : L_colorRGB({0}), L_intensity(0), L_pos({0}), L_eye({0}), L_fov(0), L_shadow(false), L_projection(new Xr_MATRIX_4X4), L_modelview(new Xr_MATRIX_4X4), L_depthMap(0) +{ + L_projection->loadIdentity(); + + glGenTextures(1, &L_depthMap); + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, L_depthMap); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY); + glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, DEPTH_MAP_SIZE, DEPTH_MAP_SIZE, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0); +} + +Xr_Light::Xr_Light(float posX, float posY, float posZ, float eyeX, float eyeY, float eyeZ, float intensity, float colorR, float colorG, float colorB, float fov, bool shadow) + : L_colorRGB({colorR, colorG, colorB}), L_intensity(intensity), L_pos({posX, posY, posZ}), L_eye({eyeX, eyeY, eyeZ}), L_fov(fov), L_shadow(shadow), L_projection(new Xr_MATRIX_4X4), L_modelview(new Xr_MATRIX_4X4), L_depthMap(0) +{ + L_projection->loadIdentity(); + L_modelview->loadIdentity(); + configure(posX, posY, posZ, eyeX, eyeY, eyeZ, intensity, colorR, colorG, colorB, fov, shadow); + + glGenTextures(1, &L_depthMap); + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, L_depthMap); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY); + glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, DEPTH_MAP_SIZE, DEPTH_MAP_SIZE, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0); +} + +Xr_Light::~Xr_Light() +{ + delete L_projection; + delete L_modelview; + glDeleteTextures(1, &L_depthMap); +} + +void Xr_Light::configure(float posX, float posY, float posZ, float eyeX, float eyeY, float eyeZ, float intensity, float colorR, float colorG, float colorB, float fov, bool shadow) +{ + L_projection->loadProjection(fov, 1, 1, 1000); + L_modelview->lookAt(eyeX, eyeY, eyeZ, posX, posY, posZ, 0, 1, 0); + + L_intensity = intensity; + + L_colorRGB[0] = colorR; + L_colorRGB[1] = colorG; + L_colorRGB[2] = colorB; + + L_shadow = shadow; +} + +float* const Xr_Light::getColorRGB() const +{ + return (float* const) L_colorRGB; +} + +float Xr_Light::getIntensity() const +{ + return L_intensity; +} + +float* const Xr_Light::getPos() const +{ + return (float* const) L_pos; +} + +float* const Xr_Light::getEye() const +{ + return (float* const) L_eye; +} + +float Xr_Light::getFov() const +{ + return L_fov; +} + +Xr_MATRIX_4X4* const Xr_Light::getLightProjectionMatrix() const +{ + return (Xr_MATRIX_4X4* const) L_projection; +} + +Xr_MATRIX_4X4* const Xr_Light::getLightModelviewMatrix() const +{ + return (Xr_MATRIX_4X4* const) L_modelview; +} + +GLuint Xr_Light::getLightDepthMap() const +{ + return L_depthMap; +} + +bool Xr_Light::hasShadow() const +{ + return L_shadow; +} diff --git a/src/material.cpp b/src/material.cpp new file mode 100644 index 0000000..c2219bc --- /dev/null +++ b/src/material.cpp @@ -0,0 +1,117 @@ +#include "material.h" + +#include "debug.h" + +Xr_Material::Xr_Material() : M_shadow(false), M_shaderless(true), M_ambient(1), M_diffuse(0), M_specular(0), M_texture(0) +{ +} + +Xr_Material::Xr_Material(bool shadow, bool shaderless, float ambient, float diffuse, float specular, Xr_Texture *texture) + : M_shadow(shadow), M_shaderless(shaderless), M_ambient(ambient), M_diffuse(diffuse), M_specular(specular) +{ + if(texture) + { + if(texture->isLoaded()) + { + M_texture = texture; + } + else + { + Xr_Log("Xr_Material : La texture n'est pas chargée"); + } + } + else + { + Xr_Log("Xr_Material : Aucune texture de sera utilisée"); + } +} + +Xr_Material::~Xr_Material() +{ +} + +void Xr_Material::enableShadow() +{ + M_shadow = true; +} + +void Xr_Material::disableShadow() +{ + M_shadow = false; +} + +void Xr_Material::enableShaderless() +{ + M_shaderless = true; +} + +void Xr_Material::disableShaderless() +{ + M_shaderless = false; +} + +void Xr_Material::setAmbient(float ambient) +{ + M_ambient = ambient; +} + +void Xr_Material::setDiffuse(float diffuse) +{ + M_diffuse = diffuse; +} + +void Xr_Material::setSpecular(float specular) +{ + M_specular = specular; +} + +void Xr_Material::attachTexture(Xr_Texture *texture) +{ + if(M_texture == 0) + { + if(texture) + { + if(texture->isLoaded()) + { + M_texture = texture; + } + else + { + Xr_Log("Xr_Material : La texture n'est pas chargée"); + } + } + else + { + Xr_Log("Xr_Material : La texture n'est pas valide"); + } + } + else + { + Xr_Log("Xr_Material : Une texture est déjà attachée au materiau"); + } +} + +void Xr_Material::detachTexture() +{ + M_texture = 0; +} + +float Xr_Material::getAmbient() const +{ + return M_ambient; +} + +float Xr_Material::getDiffuse() const +{ + return M_diffuse; +} + +float Xr_Material::getSpecular() const +{ + return M_specular; +} + +Xr_Texture* const Xr_Material::getTexture() const +{ + return (Xr_Texture* const) M_texture; +} diff --git a/src/matrix.cpp b/src/matrix.cpp new file mode 100644 index 0000000..15e3ccc --- /dev/null +++ b/src/matrix.cpp @@ -0,0 +1,247 @@ +#include + +#include "matrix.h" + +#include "vector.h" + +Xr_MATRIX_4X4::Xr_MATRIX_4X4() : M_values(0) +{ + int i; + M_values = new float[16]; + + for(i = 0; i < 16; i++) + { + M_values[i] = 0; + } +} + +Xr_MATRIX_4X4::Xr_MATRIX_4X4(float *values) : M_values(0) +{ + int i; + M_values = new float[16]; + + for(i = 0; i < 16; i++) + { + M_values[i] = values[i]; + } +} + +Xr_MATRIX_4X4::Xr_MATRIX_4X4(Xr_MATRIX_4X4 const& matrix) : M_values(0) +{ + int i; + float *values = matrix.getValues(); + M_values = new float[16]; + + for(i = 0; i < 16; i++) + { + M_values[i] = values[i]; + } +} + +Xr_MATRIX_4X4::~Xr_MATRIX_4X4() +{ + delete[] M_values; +} + +float* const Xr_MATRIX_4X4::getValues() const +{ + return (float* const) M_values; +} + +void Xr_MATRIX_4X4::loadIdentity() +{ + int i; + for(i = 0; i < 16; i++) + { + M_values[i] = 0; + } + + M_values[0] = 1; + M_values[5] = 1; + M_values[10] = 1; + M_values[15] = 1; +} + +void Xr_MATRIX_4X4::loadProjection(float fov, float aspect, float zNear, float zFar) +{ + int i; + float *values = new float[16]; + + for(i = 0; i < 16; i++) + { + values[i] = 0; + } + + float f = (1 / tan((fov / 2) * M_PI / 180)); + + values[0] = (f / aspect); + values[5] = f; + values[10] = (zFar + zNear) / (zNear - zFar); + values[11] = (2 * zFar * zNear) / (zNear - zFar); + values[14] = -1; + + Xr_MATRIX_4X4 projection(values); + delete[] values; + + *this = *this * projection; +} + +void Xr_MATRIX_4X4::lookAt(float eyeX, float eyeY, float eyeZ, float centerX, float centerY, float centerZ, float upX, float upY, float upZ) +{ + int i; + float *values = new float[16]; + + for(i = 0; i < 16; i++) + { + values[i] = 0; + } + + Xr_Vector3D axe(upX, upY, upZ); + Xr_Vector3D look(centerX - eyeX, centerY - eyeY, centerZ - eyeZ); + Xr_Vector3D normal = look * axe; + Xr_Vector3D axeRecalc = normal * look; + + normal.normalize(); + axeRecalc.normalize(); + look.normalize(); + + values[0] = normal[0]; + values[1] = normal[1]; + values[2] = normal[2]; + values[4] = axeRecalc[0]; + values[5] = axeRecalc[1]; + values[6] = axeRecalc[2]; + values[8] = -look[0]; + values[9] = -look[1]; + values[10] = -look[2]; + values[15] = 1; + + Xr_MATRIX_4X4 lookAt(values); + delete[] values; + + *this = *this * lookAt; + translate(-eyeX, -eyeY, -eyeZ); +} + +void Xr_MATRIX_4X4::translate(float x, float y, float z) +{ + int i; + float *values = new float[16]; + + for(i = 0; i < 16; i++) + { + values[i] = 0; + } + + values[0] = 1; + values[5] = 1; + values[10] = 1; + values[15] = 1; + + values[3] = x; + values[7] = y; + values[11] = z; + + Xr_MATRIX_4X4 translate(values); + delete[] values; + + *this = *this * translate; +} + +void Xr_MATRIX_4X4::rotate(float angle, float x, float y, float z) +{ + int i; + float *values = new float[16]; + + for(i = 0; i < 16; i++) + { + values[i] = 0; + } + + float c = cos(angle); + float s = sin(angle); + Xr_Vector3D vec(x, y, z); + + vec.normalize(); + + values[0] = (vec[0] * vec[0]) * (1 - c) + c; + values[1] = (vec[0] * vec[1]) * (1 - c) - (vec[2] * s); + values[2] = (vec[0] * vec[2]) * (1 - c) + (vec[1] * s); + + values[4] = (vec[1] * vec[0]) * (1 - c) + (vec[2] * s); + values[5] = (vec[1] * vec[1]) * (1 - c) + c; + values[6] = (vec[1] * vec[2]) * (1 - c) - (vec[0] * s); + + values[8] = (vec[0] * vec[2]) * (1 - c) - (vec[1] * s); + values[9] = (vec[1] * vec[2]) * (1 - c) + (vec[0] * s); + values[10] = (vec[2] * vec[2]) * (1 - c) + c; + + values[15] = 1; + + Xr_MATRIX_4X4 rotate(values); + delete[] values; + + *this = *this * rotate; +} + +void Xr_MATRIX_4X4::scale(float x, float y, float z) +{ + int i; + float *values = new float[16]; + + for(i = 0; i < 16; i++) + { + values[i] = 0; + } + + values[0] = x; + values[5] = y; + values[10] = z; + values[15] = 1; + + Xr_MATRIX_4X4 scale(values); + delete[] values; + + *this = *this * scale; +} + +Xr_MATRIX_4X4& Xr_MATRIX_4X4::operator=(Xr_MATRIX_4X4 const& matrix) +{ + int i; + float *values = matrix.getValues(); + + for(i = 0; i < 16; i++) + { + M_values[i] = values[i]; + } + + return *this; +} + +Xr_MATRIX_4X4 Xr_MATRIX_4X4::operator*(Xr_MATRIX_4X4 const& matrix) +{ + int i, j, k; + float *result = new float[16]; + + for(i = 0; i < 16; i++) + { + result[i] = 0; + } + + float *values = matrix.getValues(); + + for(k = 0; k < 4; k++) + { + for(j = 0; j < 4; j++) + { + for(i = 0; i < 4; i++) + { + result[(j * 4) + k] += M_values[(j * 4) + i] * values[(4 * i) + k]; + } + } + } + + Xr_MATRIX_4X4 res(result); + delete[] result; + return res; +} diff --git a/src/mesh.cpp b/src/mesh.cpp new file mode 100644 index 0000000..d049b0e --- /dev/null +++ b/src/mesh.cpp @@ -0,0 +1,213 @@ +#include +#include +#include + +#include "mesh.h" + +#include "debug.h" + + +Xr_Mesh::Xr_Mesh() : Xr_Ressource(xr_mesh_type), M_vertex(0), M_texCoord(0), M_normal(0), M_faceNumber(0) +{ +} + +Xr_Mesh::Xr_Mesh(char *name) : Xr_Ressource(name, xr_mesh_type), M_vertex(0), M_texCoord(0), M_normal(0), M_faceNumber(0) +{ + load(); +} + +Xr_Mesh::Xr_Mesh(Xr_Mesh const &mesh) : Xr_Ressource(mesh.getName(), xr_mesh_type), M_vertex(0), M_texCoord(0), +M_normal(0), M_faceNumber(mesh.getFaceNumber()) +{ + int i; + float* const vertex = mesh.getVertex(); + float* const texCoord = mesh.getTexCoord(); + float* const normal = mesh.getNormal(); + + if(vertex != 0) + { + M_vertex = new float[mesh.getFaceNumber() * 3 * 3]; + for(i = 0; i < M_faceNumber * 3 * 3; i++) + { + M_vertex[i] = vertex[i]; + } + } + + if(texCoord != 0) + { + M_texCoord = new float[mesh.getFaceNumber() * 3 * 2]; + for(i = 0; i < M_faceNumber * 3 * 2; i++) + { + M_texCoord[i] = texCoord[i]; + } + } + + if(normal != 0) + { + M_normal = new float[mesh.getFaceNumber() * 3 * 3]; + for(i = 0; i < M_faceNumber * 3 * 3; i++) + { + M_normal[i] = normal[i]; + } + } +} + +Xr_Mesh::~Xr_Mesh() +{ + delete[] M_vertex; + delete[] M_texCoord; + delete[] M_normal; +} + +void Xr_Mesh::load() +{ + if(R_name) + { + std::ifstream flux(R_name); + if(flux) + { + flux.seekg(0, std::ios::beg); + + std::string word; + float coord; + int index, i; + char c; + std::vector vertex; + std::vector texCoord; + std::vector normal; + + std::vector iVertex; + std::vector iTexCoord; + std::vector iNormal; + + // Recherche et récupération des données + while(!flux.eof()) + { + flux >> word; + + //Coordonnées de vertice + if(word == "v") + { + flux >> coord; vertex.push_back(coord); + flux >> coord; vertex.push_back(coord); + flux >> coord; vertex.push_back(coord); + } + //Coordonnées de texture (UV map) + else if(word == "vt") + { + flux >> coord; texCoord.push_back(coord); + flux >> coord; texCoord.push_back(coord); + } + //Coordonnées de normal + else if(word == "vn") + { + flux >> coord; normal.push_back(coord); + flux >> coord; normal.push_back(coord); + flux >> coord; normal.push_back(coord); + } + //indices + else if(word == "f") + { + M_faceNumber++; + for(i = 0; i < 3; i++) + { + //indice de coordonnées de vertice + flux >> index; iVertex.push_back(index); + //flux.seekg(-1, std::ios::cur); + + flux.get(c); + if(c == ' ') + { + continue; + } + + else if(c == '/') + { + flux.get(c); + if(c == '/') //si on trouve "//" -> pas d'indice de coordonnées de texture + { + //indice de coordonnées de normal + flux >> index; iNormal.push_back(index); + continue; + }flux.unget(); + + //si on trouve uniquement "/" -> on récupère l'indice de coordonnées de texture + flux >> index; iTexCoord.push_back(index); + } + + //si on trouve un espace on passe au indices suivant + flux.get(c); + if(c == ' ') + { + continue; + } + // si on trouve encore "/" on récupère l'indice de coordonnées de normal + else if(c == '/') + { + flux >> index; iNormal.push_back(index); + } + //si on est en fin de ligne on relance l'étape de recherche + else if(c == '\n') + { + break; + } + } + } + } + + //Construction des coordonnées + if(iVertex.size() != 0) + { + M_vertex = new float[iVertex.size() * 3]; + for(i = 0; i < (int) iVertex.size() * 3; i += 3) + { + M_vertex[i] = vertex[(iVertex[i / 3] - 1) * 3]; + M_vertex[i + 1] = vertex[(iVertex[i / 3] - 1) * 3 + 1]; + M_vertex[i + 2] = vertex[(iVertex[i / 3] - 1) * 3 + 2]; + } + } + + if(iTexCoord.size() != 0) + { + M_texCoord = new float[iTexCoord.size() * 2]; + for(i = 0; i < (int) iTexCoord.size() * 2; i += 2) + { + M_texCoord[i] = texCoord[(iTexCoord[i / 2] - 1) * 2]; + M_texCoord[i + 1] = texCoord[(iTexCoord[i / 2] - 1) * 2 + 1]; + } + } + + if(iNormal.size() != 0) + { + M_normal = new float[iNormal.size() * 3]; + for(i = 0; i < (int) iNormal.size() * 3; i += 3) + { + M_normal[i] = normal[(iNormal[i / 3] - 1) * 3]; + M_normal[i + 1] = normal[(iNormal[i / 3] - 1) * 3 + 1]; + M_normal[i + 2] = normal[(iNormal[i / 3] - 1) * 3 + 2]; + } + } + R_isLoaded = true; + } + } +} + +float* const Xr_Mesh::getVertex() const +{ + return (float* const) M_vertex; +} + +float* const Xr_Mesh::getTexCoord() const +{ + return (float* const) M_texCoord; +} + +float* const Xr_Mesh::getNormal() const +{ + return (float* const) M_normal; +} + +int Xr_Mesh::getFaceNumber() const +{ + return M_faceNumber; +} diff --git a/src/object.cpp b/src/object.cpp new file mode 100644 index 0000000..bbaa0ea --- /dev/null +++ b/src/object.cpp @@ -0,0 +1,156 @@ +#include + +#include "object.h" + +#include "debug.h" +#include "buffer.h" + +Xr_Object::Xr_Object() : O_mesh(0), O_box(0), O_material(0), O_pos({0}), O_isReady(false) +{ +} + +Xr_Object::~Xr_Object() +{ + if(O_isReady) + { + Xr_BufferManager::getInstance()->unload(O_mesh); + } + + if(O_box) + { + delete O_box; + } +} + +void Xr_Object::attachMesh(Xr_Mesh *mesh) +{ + if(O_mesh == 0) + { + if(mesh) + { + if(mesh->isLoaded() != true) + { + Xr_Log("Xr_Object : Le mesh n'est pas chargé"); + return; + } + O_mesh = mesh; + + O_box = new Xr_AABB; + O_box->genBox(O_mesh); + O_box->setPos(O_pos[0], O_pos[1], O_pos[2]); + } + else + { + Xr_Log("Xr_Object : Le mesh est invalide"); + } + } + else + { + Xr_Log("Xr_Object : Un mesh est déjà attaché à l'objet"); + } +} + +void Xr_Object::attachMaterial(Xr_Material *material) +{ + if(O_material == 0) + { + if(material) + { + O_material = material; + } + } + else + { + Xr_Log("Xr_Object : Un materiau est déjà attaché à l'objet"); + } +} + +void Xr_Object::detachMesh() +{ + if(O_isReady) + { + Xr_BufferManager::getInstance()->unload(O_mesh); + O_isReady = false; + } + delete O_box; + O_box = 0; + O_mesh = 0; +} + +void Xr_Object::detachMaterial() +{ + O_material = 0; +} + +void Xr_Object::loadToServer() +{ + if(Xr_BufferManager::getInstance()->load(O_mesh) && O_box) + { + O_isReady = true; + } +} + +void Xr_Object::unloadToServer() +{ + Xr_BufferManager::getInstance()->unload(O_mesh); + O_isReady = false; +} + +void Xr_Object::draw() +{ + if(O_isReady) + { + Xr_BufferManager::getInstance()->fetchVertex(O_mesh); + + if(O_material) + { + if(O_material->getTexture()) + { + glActiveTexture(GL_TEXTURE2); + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, O_material->getTexture()->getTextureId()); + glActiveTexture(GL_TEXTURE0); + + Xr_BufferManager::getInstance()->fetchTexCoord(O_mesh); + } + } + + Xr_BufferManager::getInstance()->fetchNormal(O_mesh); + + glDrawArrays(GL_TRIANGLES, 0, O_mesh->getFaceNumber() * 3); + + Xr_BufferManager::getInstance()->fetchNull(); + } +} + +void Xr_Object::setPos(float x, float y, float z) +{ + O_pos[0] = x; + O_pos[1] = y; + O_pos[2] = z; + + if(O_box) + { + O_box->setPos(O_pos[0], O_pos[1], O_pos[2]); + } +} + +bool Xr_Object::isReady() const +{ + return O_isReady; +} + +float* const Xr_Object::getPos() const +{ + return (float* const) O_pos; +} + +Xr_Material* const Xr_Object::getMaterial() const +{ + return (Xr_Material* const) O_material; +} + +Xr_AABB* const Xr_Object::getBox() const +{ + return (Xr_AABB* const) O_box; +} diff --git a/src/plane.cpp b/src/plane.cpp new file mode 100644 index 0000000..e6bf297 --- /dev/null +++ b/src/plane.cpp @@ -0,0 +1,52 @@ +#include "plane.h" + +Xr_Plane::Xr_Plane() +{ +} + +Xr_Plane::Xr_Plane(Xr_Vector3D const& p1, Xr_Vector3D const& p2, Xr_Vector3D const& p3) +{ + setPoints(p1, p2, p3); +} + +Xr_Plane::Xr_Plane(Xr_Vector3D const& normal, Xr_Vector3D const& point) +{ + setNormalPoint(normal, point); +} + +Xr_Plane::~Xr_Plane() +{ +} + +void Xr_Plane::setPoints(Xr_Vector3D const& p1, Xr_Vector3D const& p2, Xr_Vector3D const& p3) +{ + P_normal = (p3 - p2) * (p1 - p2); + P_normal.normalize(); + + P_point = p2; + P_d = -(Xr_Vector3D::dot(P_normal, P_point)); +} + +void Xr_Plane::setNormalPoint(Xr_Vector3D const& normal, Xr_Vector3D const& point) +{ + P_normal = normal; + P_normal.normalize(); + P_point = point; + P_d = -(Xr_Vector3D::dot(P_normal, P_point)); +} + +float Xr_Plane::getDistanceToPoint(Xr_Vector3D const& point) +{ + float distance = (P_d + Xr_Vector3D::dot(P_normal, point)); + return distance; +} + +void Xr_Plane::flipNormal() +{ + P_normal = -P_normal; +} + +Xr_Vector3D Xr_Plane::getNormal() +{ + return P_normal; +} diff --git a/src/program.cpp b/src/program.cpp new file mode 100644 index 0000000..34e19d0 --- /dev/null +++ b/src/program.cpp @@ -0,0 +1,112 @@ +#include + +#include "program.h" + +#include "debug.h" + +Xr_Program::Xr_Program() : P_id(0), P_shader1(0), P_shader2(0), P_isLink(false) +{ +} + +Xr_Program::~Xr_Program() +{ + glDeleteProgram(P_id); +} + +void Xr_Program::attachShader(Xr_Shader *shader) +{ + if(shader) + { + if(shader->isCompile()) + { + if(P_shader1 == 0) + { + P_shader1 = shader; + } + else if(P_shader2 == 0) + { + P_shader2 = shader; + } + else + { + Xr_Log("Xr_Program : Des shaders sont déjà attaché au program"); + return; + } + } + else + { + Xr_Log("Xr_Program : Le shader n'est pas compilé"); + return; + } + } + else + { + Xr_Log("Xr_Program : Le shader est invalide"); + } +} + +void Xr_Program::detachShader(Xr_Shader *shader) +{ + if(P_shader1 == shader) + { + P_shader1 = 0; + } + else if(P_shader2 == shader) + { + P_shader2 = 0; + } +} + +void Xr_Program::link() +{ + GLint status = GL_TRUE; + + if(P_shader1 && P_shader2) + { + P_id = glCreateProgram(); + if(P_id == 0) + { + Xr_Log("Xr_Program : Impossible de créer un program de shaders"); + P_isLink = false; + return; + } + + glAttachShader(P_id, P_shader1->getShaderId()); + glAttachShader(P_id, P_shader2->getShaderId()); + glLinkProgram(P_id); + + glGetProgramiv(P_id, GL_LINK_STATUS, &status); + + if(status != GL_TRUE) + { + GLint logSize; + glGetProgramiv(P_id, GL_INFO_LOG_LENGTH, &logSize); + + char *log = new char[logSize + 1]; + + glGetProgramInfoLog(P_id, logSize, &logSize, log); + log[logSize] = 0; + + Xr_Log("Xr_Program : Le linkage des shaders < " + std::string(P_shader1->getName()) + " > et < " + std::string(P_shader2->getName()) + " > a échoué\n[OPENGL_GLSL] : " + std::string(log)); + delete[] log; + P_isLink = false; + return; + } + Xr_Log("Xr_Program : Les shaders < " + std::string(P_shader1->getName()) + " > et < " + std::string(P_shader2->getName()) + " ont été linkés"); + P_isLink = true; + } + else + { + Xr_Log("Xr_Program : Le program ne contient pas deux shaders"); + } +} + +bool Xr_Program::isLink() const +{ + return P_isLink; +} + +GLuint Xr_Program::getProgramId() const +{ + return P_id; +} diff --git a/src/render.cpp b/src/render.cpp new file mode 100644 index 0000000..ba28482 --- /dev/null +++ b/src/render.cpp @@ -0,0 +1,204 @@ +#include +#include + +#include "render.h" + +#include "OGL_ext.h" +#include "buffer.h" +#include "ressource.h" +#include "event.h" +#include "debug.h" + +Xr_Renderer* Xr_Renderer::R_instance = 0; + +Xr_Renderer::Xr_Renderer() : isInit(false), R_scene(0) +{ + +} + +Xr_Renderer::~Xr_Renderer() +{ + Xr_EventManager::quitInstance(); + Xr_BufferManager::quitInstance(); + Xr_RessourceManager::quitInstance(); + + if(isInit) + { + SDL_Quit(); + } +} + +Xr_Renderer* Xr_Renderer::getInstance() +{ + if(R_instance) + { + return R_instance; + } + else + { + R_instance = new Xr_Renderer; + return R_instance; + } +} + +bool Xr_Renderer::instanced() +{ + if(R_instance) + { + return true; + } + else + { + return false; + } +} + +void Xr_Renderer::quitInstance() +{ + if(R_instance) + { + delete R_instance; + R_instance = 0; + } +} + +void Xr_Renderer::init(unsigned int width, unsigned int height, bool fullscreen, unsigned int framerate) +{ + R_fullscreen = fullscreen; + R_frameRateLimite = framerate; + R_windowWidth = width; + R_imageWidth = width; + R_windowHeight = height; + R_imageHeight = height; + + SDL_Init(SDL_INIT_VIDEO); + + if(fullscreen) + { + SDL_SetVideoMode(width, height, 32, SDL_OPENGL | SDL_DOUBLEBUF | SDL_FULLSCREEN); + } + else + { + SDL_SetVideoMode(width, height, 32, SDL_OPENGL | SDL_DOUBLEBUF /*| SDL_RESIZABLE*/); + } + + SDL_WM_SetCaption("X-Ray3D Render Window", NULL); + + glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); + + Xr_InitExtSupport(); + Xr_CheckExt("GL_ARB_vertex_buffer_object"); + Xr_CheckExt("GL_ARB_shading_language_100"); + Xr_CheckExt("GL_ARB_shader_objects"); + Xr_CheckExt("GL_ARB_vertex_shader"); + Xr_CheckExt("GL_ARB_fragment_shader"); + + isInit = true; +} + +void Xr_Renderer::mainLoop() +{ + if(isInit) + { + if(R_scene) + { + Uint32 startTime, endTime, elapsedTime; + while(!(Xr_EventManager::getInstance()->quitEvent())) + { + startTime = SDL_GetTicks(); + + Xr_EventManager::getInstance()->updateDeviceState(); + Xr_EventManager::getInstance()->executeProfil(); + + R_scene->draw(); + + endTime = SDL_GetTicks(); + elapsedTime = endTime - startTime; + + if(elapsedTime < (1000 / R_frameRateLimite)) + { + SDL_Delay((1000 / R_frameRateLimite) - elapsedTime); + R_frameRate = R_frameRateLimite; + } + else + { + R_frameRate = 1000 / elapsedTime; + } + } + } + else + { + Xr_Log("Xr_Renderer : Pas de scène"); + } + } + else + { + Xr_Log("Xr_Renderer : Le renderer n'est pas initialisé"); + } +} + +void Xr_Renderer::setVideo(unsigned int width, unsigned int height, bool fullscreen) +{ + R_fullscreen = fullscreen; + R_windowWidth = width; + R_windowHeight = height; + + if(fullscreen) + { + SDL_SetVideoMode(width, height, 32, SDL_OPENGL | SDL_DOUBLEBUF | SDL_FULLSCREEN); + } + else + { + SDL_SetVideoMode(width, height, 32, SDL_OPENGL | SDL_DOUBLEBUF /*| SDL_RESIZABLE*/); + } + SDL_Delay(1000); +} + +void Xr_Renderer::setFrameRateLimite(unsigned int framerate) +{ + R_frameRateLimite = framerate; +} + +void Xr_Renderer::setImageSize(unsigned int width, unsigned int height) +{ + R_imageWidth = width; + R_imageHeight = height; +} + +void Xr_Renderer::setScene(Xr_Scene *scene) +{ + if(scene) + { + R_scene = scene; + } + else + { + Xr_Log("Xr_Renderer : La scène est invalide"); + } +} + +unsigned int Xr_Renderer::getWindowWidth() const +{ + return R_windowWidth; +} + +unsigned int Xr_Renderer::getWindowHeight() const +{ + return R_windowHeight; +} + +unsigned int Xr_Renderer::getImageWidth() const +{ + return R_imageWidth; +} + +unsigned int Xr_Renderer::getImageHeight() const +{ + return R_imageHeight; +} + +unsigned int Xr_Renderer::getFrameRate() const +{ + return R_frameRate; +} diff --git a/src/ressource.cpp b/src/ressource.cpp new file mode 100644 index 0000000..f37f8e6 --- /dev/null +++ b/src/ressource.cpp @@ -0,0 +1,197 @@ +#include + +#include "ressource.h" + +#include "mesh.h" +#include "texture.h" +#include "shader.h" +#include "debug.h" + +/* class Xr_Ressource */ + +Xr_Ressource::Xr_Ressource() : R_name(0), R_isLoaded(false) +{ +} + +Xr_Ressource::Xr_Ressource(char *name, ressourceType type) : R_name(name), R_isLoaded(false), R_type(type) +{ + +} + +Xr_Ressource::Xr_Ressource(ressourceType type) : R_name(0), R_isLoaded(false), R_type(type) +{ +} + +Xr_Ressource::~Xr_Ressource() +{ +} + +char* const Xr_Ressource::getName() const +{ + return (char* const) R_name; +} + +bool Xr_Ressource::isLoaded() const +{ + return R_isLoaded; +} + +ressourceType Xr_Ressource::getType() const +{ + return R_type; +} + +/* class Xr_RessourceManager */ + +Xr_RessourceManager* Xr_RessourceManager::R_instance = 0; + +Xr_RessourceManager::Xr_RessourceManager() +{ +} + +Xr_RessourceManager::~Xr_RessourceManager() +{ + unloadAll(); +} + +bool Xr_RessourceManager::instanced() +{ + if(R_instance) + { + return true; + } + else + { + return false; + } +} + +Xr_RessourceManager* Xr_RessourceManager::getInstance() +{ + if(R_instance) + { + return R_instance; + } + else + { + R_instance = new Xr_RessourceManager; + return R_instance; + } +} + +void Xr_RessourceManager::quitInstance() +{ + if(R_instance) + { + delete R_instance; + } +} + +Xr_Ressource* Xr_RessourceManager::get(ressourceType type, char *name) +{ + int i; + std::string Rname((const char*) name); + + for(i = 0; i < R_ressourcesList.size(); i++) + { + if(std::string((const char*) R_ressourcesList[i]->getName()) == Rname && type == R_ressourcesList[i]->getType()) + { + return R_ressourcesList[i]; + } + } + + Xr_Ressource *ressource = 0; + + if(type == xr_mesh_type) + { + ressource = new Xr_Mesh(name); + + if(ressource->isLoaded()) + { + Xr_Log("Xr_RessourceManager : Le mesh < " + Rname + " > est chargé"); + R_ressourcesList.push_back(ressource); + return ressource; + } + else + { + Xr_Log("Xr_RessourceManager : Le chargement du mesh < " + Rname + " > a échoué"); + delete ressource; + return 0; + } + } + else if(type == xr_texture_type) + { + ressource = new Xr_Texture(name); + + if(ressource->isLoaded()) + { + Xr_Log("Xr_RessourceManager : La texture < " + Rname + " > est chargée"); + R_ressourcesList.push_back(ressource); + return ressource; + } + else + { + Xr_Log("Xr_RessourceManager : Le chargement de la texture < " + Rname + " > a échoué"); + delete ressource; + return 0; + } + } + else if(type == xr_shader_type) + { + ressource = new Xr_Shader(name); + + if(ressource->isLoaded() && ((Xr_Shader*) ressource)->isCompile()) + { + Xr_Log("Xr_RessourceManager : Le shader < " + Rname + " > est chargé et compilé"); + R_ressourcesList.push_back(ressource); + return ressource; + } + else + { + Xr_Log("Xr_RessourceManager : Le chargement du shader < " + Rname + " > a échoué"); + delete ressource; + return 0; + } + } +} + +bool Xr_RessourceManager::load(ressourceType type, char *name) +{ + if(get(type, name)) + { + return true; + } + else + { + return false; + } +} + +void Xr_RessourceManager::unload(ressourceType type, char *name) +{ + int i; + std::string Rname((const char*) name); + + for(i = 0; i < R_ressourcesList.size(); i++) + { + if(std::string((const char*) R_ressourcesList[i]->getName()) == Rname && type == R_ressourcesList[i]->getType()) + { + Xr_Log("Xr_RessourceManager : Suppression de < " + Rname + " >"); + delete R_ressourcesList[i]; + R_ressourcesList.erase(R_ressourcesList.begin() + i); + return; + } + } +} + +void Xr_RessourceManager::unloadAll() +{ + int i; + for(i = 0; i < R_ressourcesList.size(); i++) + { + Xr_Log("Xr_RessourceManager : Suppression de < " + std::string((const char*) R_ressourcesList[i]->getName()) + " >"); + delete R_ressourcesList[i]; + } + + R_ressourcesList.clear(); +} diff --git a/src/scene.cpp b/src/scene.cpp new file mode 100644 index 0000000..a4f746d --- /dev/null +++ b/src/scene.cpp @@ -0,0 +1,224 @@ +#include +#include + +#include "scene.h" + +#include "debug.h" +#include "ressource.h" +#include "render.h" + +Xr_Scene::Xr_Scene() : modelview(new Xr_MATRIX_4X4), S_camera(0), basicProg(new Xr_Program), render1Prog(new Xr_Program) +{ + basicProg->attachShader((Xr_Shader*) Xr_RessourceManager::getInstance()->get(xr_shader_type, "shader/basic.vert")); + basicProg->attachShader((Xr_Shader*) Xr_RessourceManager::getInstance()->get(xr_shader_type, "shader/basic.frag")); + basicProg->link(); + + render1Prog->attachShader((Xr_Shader*) Xr_RessourceManager::getInstance()->get(xr_shader_type, "shader/render1.vert")); + render1Prog->attachShader((Xr_Shader*) Xr_RessourceManager::getInstance()->get(xr_shader_type, "shader/render1.frag")); + render1Prog->link(); +} + +Xr_Scene::~Xr_Scene() +{ + delete modelview; + delete basicProg; + delete render1Prog; +} + +void Xr_Scene::addObject(Xr_Object *object) +{ + if(object) + { + if(object->isReady()) + { + S_objectList.push_back(object); + Xr_Log("Xr_Scene : Un objet a été ajouté dans la scène"); + } + else + { + Xr_Log("Xr_Scene : Impossible d'ajouter l'objet dans la scène"); + return; + } + } + else + { + Xr_Log("Xr_Scene : L'objet est invalide"); + } +} + +void Xr_Scene::deleteObject(Xr_Object *object) +{ + int i; + for(i = 0; i < S_objectList.size(); i++) + { + if(S_objectList[i] == object) + { + S_objectList[i] = 0; + return; + } + } +} + +void Xr_Scene::addLight(Xr_Light *light) +{ + int i; + if(light) + { + S_lightList.push_back(light); + Xr_Log("Xr_Scene : Une source de lumière a été ajoutée dans la scène"); + } + else + { + Xr_Log("Xr_Scene : La source de lumière est invalide"); + } +} + +void Xr_Scene::deleteLight(Xr_Light *light) +{ + int i; + for(i = 0; i < S_lightList.size(); i++) + { + if(S_lightList[i] == light) + { + S_lightList[i] = 0; + return; + } + } +} + +void Xr_Scene::attachCamera(Xr_Camera *camera) +{ + if(S_camera == 0) + { + if(camera) + { + Xr_Log("Xr_Scene : Une caméra a été ajoutée dans la scène"); + S_camera = camera; + } + else + { + Xr_Log("Xr_Scene : La caméra est invalide"); + } + } + else + { + Xr_Log("Xr_Scene : Une caméra est déjà attachée à la scène"); + } + +} + +void Xr_Scene::detachCamera() +{ + S_camera = 0; +} + +void Xr_Scene::draw() +{ + int i, j; + GLuint fb; + bool lighting = false; + + if(!S_camera) + { + return; + } + + glViewport(0, 0, Xr_Renderer::getInstance()->getImageWidth(), Xr_Renderer::getInstance()->getImageHeight()); + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glClearColor(0.4, 0.4, 1, 0); + + glCullFace(GL_BACK); + + // ------------------ preparation des lumières ---------------------------- + /*glViewport(0, 0, DEPTH_MAP_SIZE, DEPTH_MAP_SIZE); + glUseProgram(basicProg->getProgramId()); + for(i = 0; i < 15; i++) + { + if(S_light[i]) + { + bool lighting = true; + glGenFramebuffers(1, &fb); + glBindFramebuffer(GL_FRAMEBUFFER, fb); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, S_light[i]->getLightDepthMap(), 0); + + glActiveTexture(GL_TEXTURE1); + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, S_light[i]->getLightDepthMap()); + glActiveTexture(GL_TEXTURE0); + + glBindFramebuffer(GL_FRAMEBUFFER, fb); + + for(j = 0; j < 100; j++) + { + if(S_object[j]) + { + S_light[j]->getLightModelviewMatrix()->translate(S_object[j]->getPos()[0], S_object[j]->getPos()[1], S_object[j]->getPos()[2]); + S_light[j]->getLightModelviewMatrix()->rotate(b, 0, 1, 0); + + glUniformMatrix4fv(glGetUniformLocation(basicProg->getProgramId(), "projection"), 1, GL_TRUE, S_light[i]->getLightProjectionMatrix()->getValues()); + glUniformMatrix4fv(glGetUniformLocation(basicProg->getProgramId(), "modelview"), 1, GL_TRUE, S_light[i]->getLightModelviewMatrix()->getValues()); + S_object[i]->draw(); + } + } + glDeleteFramebuffers(1, &fb); + } + } + + glUseProgram(0); + glBindFramebuffer(GL_FRAMEBUFFER, 0);*/ + + // ------------------------------------------------------------------ + + // dessin du point de vue de la camera + + for(i = 0; i < S_objectList.size(); i++) + { + if(S_objectList[i]) + { + modelview->loadIdentity(); + S_camera->configModelview(modelview); + if(!(S_camera->getFrustumBox()->testAABB(S_objectList[i]->getBox())) || !S_objectList[i]->isReady()) + { + //Xr_Log("Culling !"); + continue; + } + + modelview->translate(S_objectList[i]->getPos()[0], S_objectList[i]->getPos()[1], S_objectList[i]->getPos()[2]); + //modelview->rotate(b, 0, 1, 0); + + if(!lighting && !(S_objectList[i]->getMaterial())) + { + glUseProgram(basicProg->getProgramId()); + glUniformMatrix4fv(glGetUniformLocation(basicProg->getProgramId(), "projection"), 1, GL_TRUE, S_camera->getProjection()->getValues()); + glUniformMatrix4fv(glGetUniformLocation(basicProg->getProgramId(), "modelview"), 1, GL_TRUE, modelview->getValues()); + S_objectList[i]->draw(); + glUseProgram(0); + } + else if(!lighting && S_objectList[i]->getMaterial()) + { + glUseProgram(render1Prog->getProgramId()); + glUniformMatrix4fv(glGetUniformLocation(render1Prog->getProgramId(), "projection"), 1, GL_TRUE, S_camera->getProjection()->getValues()); + glUniformMatrix4fv(glGetUniformLocation(render1Prog->getProgramId(), "modelview"), 1, GL_TRUE, modelview->getValues()); + glUniform1i(glGetUniformLocation(render1Prog->getProgramId(), "texture"), 2); + S_objectList[i]->draw(); + glUseProgram(0); + } + else + { + continue; + } + /*glUseProgram(render1->getProgramId()); + glUniformMatrix4fv(glGetUniformLocation(render1->getProgramId(), "projection"), 1, GL_TRUE, projection->getValues()); + glUniformMatrix4fv(glGetUniformLocation(render1->getProgramId(), "modelview"), 1, GL_TRUE, modelview->getValues()); + + glUniform1i(glGetUniformLocation(render1->getProgramId(), "lightDepthMap"), GL_TEXTURE1); + glUniform1f(glGetUniformLocation(render1->getProgramId(), "ambient"), S_object[i]->getMaterial()->getAmbient()); + + S_objectList[i]->draw(); + + glUseProgram(0);*/ + } + } + SDL_GL_SwapBuffers(); +} diff --git a/src/shader.cpp b/src/shader.cpp new file mode 100644 index 0000000..a0fe0d2 --- /dev/null +++ b/src/shader.cpp @@ -0,0 +1,127 @@ +#include +#include + +#include "shader.h" + +#include "debug.h" + + +Xr_Shader::Xr_Shader() : Xr_Ressource(xr_shader_type), S_id(0), S_src(0), S_isCompile(false), S_srcLength(0) +{ +} + +Xr_Shader::Xr_Shader(char *name) : Xr_Ressource(name, xr_shader_type), S_id(0), S_src(0), S_isCompile(false), S_srcLength(0) +{ + load(); + compile(); +} + +Xr_Shader::~Xr_Shader() +{ + glDeleteShader(S_id); + if(S_src) + { + delete[] S_src; + } +} + +void Xr_Shader::load() +{ + if(R_name) + { + if(std::string(R_name).rfind(".vert") != std::string::npos) + { + S_type = GL_VERTEX_SHADER; + } + else if(std::string(R_name).rfind(".frag") != std::string::npos) + { + S_type = GL_FRAGMENT_SHADER; + } + else + { + return; + } + + std::ifstream flux; + flux.open(R_name, std::ios::binary); + + if(flux) + { + //taille du fichier + flux.seekg(0, std::ios::end); + S_srcLength = flux.tellg(); + flux.seekg(0, std::ios::beg); + + S_src = new char[S_srcLength + 1]; + + flux.read(S_src, S_srcLength); + S_src[S_srcLength] = 0; + + flux.close(); + + S_id = glCreateShader(S_type); + + if(S_id == 0) + { + Xr_Log("Xr_Shader : Erreur lors de la création du shader"); + return; + } + + glShaderSource(S_id, 1, (const GLchar**) &S_src, NULL); + R_isLoaded = true; + } + } +} + +void Xr_Shader::compile() +{ + if(R_isLoaded) + { + GLint status = GL_TRUE; + + glCompileShader(S_id); + glGetShaderiv(S_id, GL_COMPILE_STATUS, &status); + + if(status != GL_TRUE) + { + GLint logSize; + glGetShaderiv(S_id, GL_INFO_LOG_LENGTH, &logSize); + + char *log = new char[logSize + 1]; + + glGetShaderInfoLog(S_id, logSize, &logSize, log); + log[logSize] = 0; + + Xr_Log("Xr_Shader : La compilation du shader < " + std::string(R_name) + " > a échouée\n[OPENGL_GLSL] : " + std::string(log)); + delete[] log; + S_isCompile = false; + return; + } + S_isCompile = true; + } +} + +GLuint Xr_Shader::getShaderId() const +{ + return S_id; +} + +GLenum Xr_Shader::getShaderType() const +{ + return S_type; +} + +int Xr_Shader::getSrcLength() const +{ + return S_srcLength; +} + +bool Xr_Shader::isCompile() const +{ + return S_isCompile; +} + +char* const Xr_Shader::getSrc() const +{ + return (char* const) S_src; +} diff --git a/src/texture.cpp b/src/texture.cpp new file mode 100644 index 0000000..f23cbe5 --- /dev/null +++ b/src/texture.cpp @@ -0,0 +1,77 @@ +#include +#include +#include + +#include "texture.h" + +#include "debug.h" + +Xr_Texture::Xr_Texture() : Xr_Ressource(xr_texture_type), T_id(0) +{ +} + +Xr_Texture::Xr_Texture(char *name) : Xr_Ressource(name, xr_texture_type), T_id(0) +{ + load(); +} + +Xr_Texture::~Xr_Texture() +{ + glDeleteTextures(1, &T_id); +} + +void Xr_Texture::load() +{ + if(R_name) + { + SDL_Surface *image = NULL; + SDL_Surface *final = NULL; + Uint32 rmask, gmask, bmask, amask; + + //chargement de l'image avec SDL + image = IMG_Load(R_name); + if(image == NULL) + { + return; + } + + //model de format pour OPENGL + rmask = 0x000000ff; + gmask = 0x0000ff00; + bmask = 0x00ff0000; + amask = 0xff000000; + + SDL_PixelFormat format = *(image->format); + format.BitsPerPixel = 32; + format.BytesPerPixel = 4; + format.Rmask = rmask; + format.Gmask = gmask; + format.Bmask = bmask; + format.Amask = amask; + + //convertion vers le format définit + final = SDL_ConvertSurface(image,&format,SDL_SWSURFACE); + + glGenTextures(1, &T_id); + glActiveTexture(GL_TEXTURE0); + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, T_id); + glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + //envoie des pixels de l'image traiter à OPENGL + glTexImage2D(GL_TEXTURE_2D, 0, 4, final->w, final->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, final->pixels); + + + glBindTexture(GL_TEXTURE_2D, 0); + SDL_FreeSurface(image); + SDL_FreeSurface(final); + + R_isLoaded = true; + } +} + +GLuint Xr_Texture::getTextureId() const +{ + return T_id; +} diff --git a/src/vector.cpp b/src/vector.cpp new file mode 100644 index 0000000..f3c3010 --- /dev/null +++ b/src/vector.cpp @@ -0,0 +1,151 @@ +#include + +#include "vector.h" + +Xr_Vector3D::Xr_Vector3D() : xCoord(0), yCoord(0), zCoord(0) +{ +} + +Xr_Vector3D::Xr_Vector3D(float x, float y, float z) : xCoord(x), yCoord(y), zCoord(z) +{ +} + +Xr_Vector3D::~Xr_Vector3D() +{ +} + +void Xr_Vector3D::setCoords(float x, float y, float z) +{ + xCoord = x; + yCoord = y; + zCoord = z; +} + +void Xr_Vector3D::normalize() +{ + float length = sqrt((xCoord * xCoord) + (yCoord * yCoord) + (zCoord * zCoord)); + + if(length != 0) + { + xCoord /= length; + yCoord /= length; + zCoord /= length; + } +} + +float Xr_Vector3D::dot(Xr_Vector3D const& vector1, Xr_Vector3D const& vector2) +{ + float dot = (vector1[0] * vector2[0]) + (vector1[1] * vector2[1]) + (vector1[2] * vector2[2]); + + return dot; +} + +float Xr_Vector3D::getX() const +{ + return xCoord; +} + +float Xr_Vector3D::getY() const +{ + return yCoord; +} + +float Xr_Vector3D::getZ() const +{ + return zCoord; +} + +Xr_Vector3D& Xr_Vector3D::operator=(Xr_Vector3D const& vector) +{ + xCoord = vector.getX(); + yCoord = vector.getY(); + zCoord = vector.getZ(); + + return *this; +} + +Xr_Vector3D& Xr_Vector3D::operator+=(Xr_Vector3D const& vector) +{ + xCoord += vector.getX(); + yCoord += vector.getY(); + zCoord += vector.getZ(); + + return *this; +} + +Xr_Vector3D& Xr_Vector3D::operator-=(Xr_Vector3D const& vector) +{ + xCoord -= vector.getX(); + yCoord -= vector.getY(); + zCoord -= vector.getZ(); + + return *this; +} + +Xr_Vector3D Xr_Vector3D::operator+(Xr_Vector3D const& vector) const +{ + float x = xCoord + vector.getX(); + float y = yCoord + vector.getY(); + float z = zCoord + vector.getZ(); + + Xr_Vector3D vec(x, y, z); + return vec; +} + +Xr_Vector3D Xr_Vector3D::operator-(Xr_Vector3D const& vector) const +{ + float x = xCoord - vector.getX(); + float y = yCoord - vector.getY(); + float z = zCoord - vector.getZ(); + + Xr_Vector3D vec(x, y, z); + return vec; +} + +Xr_Vector3D Xr_Vector3D::operator-() const +{ + float x = -xCoord; + float y = -yCoord; + float z = -zCoord; + + Xr_Vector3D vec(x, y, z); + return vec; +} + +Xr_Vector3D Xr_Vector3D::operator*(Xr_Vector3D const& vector) const +{ + float x = (yCoord * vector.getZ()) - (zCoord * vector.getY()); + float y = (zCoord * vector.getX()) - (xCoord * vector.getZ()); + float z = (xCoord * vector.getY()) - (yCoord * vector.getX()); + + Xr_Vector3D vec(x, y, z); + return vec; +} + +Xr_Vector3D Xr_Vector3D::operator*(float n) const +{ + float x = xCoord * n; + float y = yCoord * n; + float z = zCoord * n; + + Xr_Vector3D vec(x, y, z); + return vec; +} + +float Xr_Vector3D::operator[](unsigned int n) const +{ + switch(n) + { + case 0: + return xCoord; + + case 1: + return yCoord; + + case 2: + return zCoord; + + default: + return 0; + } +} diff --git a/test/event.cpp b/test/event.cpp new file mode 100644 index 0000000..204793d --- /dev/null +++ b/test/event.cpp @@ -0,0 +1,84 @@ +#include "event.h" +#include +#include +#include + +myEventProfil::myEventProfil() : m_camera(0) +{ +} + +myEventProfil::~myEventProfil() +{ + +} + +void myEventProfil::input(Xr_Camera *camera) +{ + if(camera) + { + m_camera = camera; + } +} + +void myEventProfil::execute(Xr_DevicesDesc *devices) +{ + if(m_camera) + { + /*if(devices->windowHeight && devices->windowWidth) + { + Xr_Renderer::getInstance()->setVideo(devices->windowWidth, devices->windowHeight, false); + Xr_Renderer::getInstance()->setImageSize(devices->windowWidth, devices->windowHeight); + std::cout << devices->windowWidth << "x" << devices->windowHeight << std::endl; + }*/ + + if(Xr_EventManager::getInstance()->mouseCaptured()) + { + m_camera->relAngle(devices->mouse->relX * 20 * ((float) 1 / Xr_Renderer::getInstance()->getFrameRate()), devices->mouse->relY * 20 * ((float) 1 / Xr_Renderer::getInstance()->getFrameRate())); + } + + if(devices->mouse->button[XR_MOUSE_BUTTON_LEFT]) + { + Xr_EventManager::getInstance()->enableMouseCapture(); + } + + if(devices->keyboard->key[XR_KEY_LCTRL]) + { + Xr_EventManager::getInstance()->disableMouseCapture(); + } + + if(devices->keyboard->key[XR_KEY_ESCAPE]) + { + Xr_EventManager::getInstance()->genQuitEvent(); + } + + if(devices->keyboard->key[XR_KEY_LSHIFT]) + { + m_camera->moveUpDown(-4 * ((float) 1 / Xr_Renderer::getInstance()->getFrameRate())); + } + + if(devices->keyboard->key[XR_KEY_SPACE]) + { + m_camera->moveUpDown(4 * ((float) 1 / Xr_Renderer::getInstance()->getFrameRate())); + } + + if(devices->keyboard->key[XR_KEY_z]) + { + m_camera->moveForward(4 * ((float) 1 / Xr_Renderer::getInstance()->getFrameRate())); + } + + if(devices->keyboard->key[XR_KEY_s]) + { + m_camera->moveBackward(4 * ((float) 1 / Xr_Renderer::getInstance()->getFrameRate())); + } + + if(devices->keyboard->key[XR_KEY_q]) + { + m_camera->moveLeft(4 * ((float) 1 / Xr_Renderer::getInstance()->getFrameRate())); + } + + if(devices->keyboard->key[XR_KEY_d]) + { + m_camera->moveRight(4 * ((float) 1 / Xr_Renderer::getInstance()->getFrameRate())); + } + } +} diff --git a/test/event.h b/test/event.h new file mode 100644 index 0000000..346c51c --- /dev/null +++ b/test/event.h @@ -0,0 +1,16 @@ +#include +#include + +class myEventProfil : public Xr_EventProfil +{ + public: + myEventProfil(); + ~myEventProfil(); + + void input(Xr_Camera*); + virtual void execute(Xr_DevicesDesc*); + + private: + Xr_Camera *m_camera; +}; + diff --git a/test/main.cpp b/test/main.cpp new file mode 100644 index 0000000..131e6cc --- /dev/null +++ b/test/main.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "event.h" + +int main(int argc, char *argv[]) +{ + Xr_Renderer::getInstance()->init(1280, 640, false, 60); + + Xr_Material *material1 = new Xr_Material; + Xr_Material *material2 = new Xr_Material; + + material1->attachTexture((Xr_Texture*) Xr_RessourceManager::getInstance()->get(xr_texture_type, "tex1.jpg")); + material2->attachTexture((Xr_Texture*) Xr_RessourceManager::getInstance()->get(xr_texture_type, "tex1.jpg")); + + Xr_Camera *camera = new Xr_Camera; + camera->setPos(0, 2, 5); + camera->setAngle(90, 0); + + myEventProfil *profil = new myEventProfil; + profil->input(camera); + Xr_EventManager::getInstance()->setProfil((Xr_EventProfil*) profil); + + Xr_Scene *scene = new Xr_Scene; + scene->attachCamera(camera); + + //Xr_Object* obj_list[200]; + + /*for(int i = 0; i < 100; i++) { + obj_list[i] = new Xr_Object; + obj_list[i]->attachMesh((Xr_Mesh*) Xr_RessourceManager::getInstance()->get(xr_mesh_type, "duck.obj")); + obj_list[i]->attachMaterial(material1); + obj_list[i]->loadToServer(); + obj_list[i]->setPos(i * 2, 0, 0); + scene->addObject(obj_list[i]); + }*/ + + /*for(int i = 100; i < 200; i++) { + obj_list[i] = new Xr_Object; + obj_list[i]->attachMesh((Xr_Mesh*) Xr_RessourceManager::getInstance()->get(xr_mesh_type, "plan.obj")); + obj_list[i]->attachMaterial(material2); + obj_list[i]->loadToServer(); + obj_list[i]->setPos(0, -3, (i - 100) * 5); + scene->addObject(obj_list[i]); + }*/ + + Xr_Object *object1 = new Xr_Object; + object1->attachMesh((Xr_Mesh*) Xr_RessourceManager::getInstance()->get(xr_mesh_type, "car.obj")); + object1->attachMaterial(material1); + object1->loadToServer(); + + /*Xr_Object *object2 = new Xr_Object; + object2->attachMesh((Xr_Mesh*) Xr_RessourceManager::getInstance()->get(xr_mesh_type, "duck.obj")); + object2->attachMaterial(material2); + object2->loadToServer(); + object2->setPos(0, -3, 0);*/ + + scene->addObject(object1); + //scene->addObject(object2); + + Xr_Renderer::getInstance()->setScene(scene); + Xr_Renderer::getInstance()->mainLoop(); + + delete scene; + delete camera; + /*for(int i = 0; i < 200; i++) { + delete obj_list[i]; + }*/ + delete object1; + //delete object2; + delete material1; + delete material2; + + Xr_Renderer::quitInstance(); + + return 0; +} diff --git a/test/shader/.svn/all-wcprops b/test/shader/.svn/all-wcprops new file mode 100644 index 0000000..9aa535c --- /dev/null +++ b/test/shader/.svn/all-wcprops @@ -0,0 +1,17 @@ +K 25 +svn:wc:ra_dav:version-url +V 36 +/svn/x-ray3d/!svn/ver/2/trunk/shader +END +test2.frag +K 25 +svn:wc:ra_dav:version-url +V 47 +/svn/x-ray3d/!svn/ver/2/trunk/shader/test2.frag +END +test.vert +K 25 +svn:wc:ra_dav:version-url +V 46 +/svn/x-ray3d/!svn/ver/2/trunk/shader/test.vert +END diff --git a/test/shader/.svn/entries b/test/shader/.svn/entries new file mode 100644 index 0000000..05a3331 --- /dev/null +++ b/test/shader/.svn/entries @@ -0,0 +1,96 @@ +10 + +dir +2 +https://subversion.assembla.com/svn/x-ray3d/trunk/shader +https://subversion.assembla.com/svn/x-ray3d + + + +2012-02-08T12:28:31.659376Z +2 +ralf03 + + + + + + + + + + + + + + +877da02c-69c5-4f9d-8e5e-0e81075059a3 + +test2.frag +file + + + + +2012-02-08T12:31:45.000000Z +0ade7cf890704bb0ceea74db6dfd2ac7 +2012-02-08T12:28:31.659376Z +2 +ralf03 + + + + + + + + + + + + + + + + + + + + + +96 + +test.vert +file + + + + +2012-02-08T12:31:45.000000Z +5290f61bf5df181b98675f08b063ea4c +2012-02-08T12:28:31.659376Z +2 +ralf03 + + + + + + + + + + + + + + + + + + + + + +155 + diff --git a/test/shader/.svn/text-base/test.vert.svn-base b/test/shader/.svn/text-base/test.vert.svn-base new file mode 100644 index 0000000..521108a --- /dev/null +++ b/test/shader/.svn/text-base/test.vert.svn-base @@ -0,0 +1,9 @@ +uniform mat4 modelview; +uniform mat4 projection; + +void main() +{ + gl_Position = projection * modelview * gl_Vertex; + gl_TexCoord[0] = gl_MultiTexCoord0; +} + diff --git a/test/shader/.svn/text-base/test2.frag.svn-base b/test/shader/.svn/text-base/test2.frag.svn-base new file mode 100644 index 0000000..f86c57b --- /dev/null +++ b/test/shader/.svn/text-base/test2.frag.svn-base @@ -0,0 +1,6 @@ +uniform sampler2D texture; + +void main() +{ + gl_FragColor = texture2D(texture, gl_TexCoord[0]); +} diff --git a/test/shader/basic.frag b/test/shader/basic.frag new file mode 100644 index 0000000..8bd9162 --- /dev/null +++ b/test/shader/basic.frag @@ -0,0 +1,4 @@ +void main() +{ + gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +} diff --git a/test/shader/basic.vert b/test/shader/basic.vert new file mode 100644 index 0000000..b6a0661 --- /dev/null +++ b/test/shader/basic.vert @@ -0,0 +1,9 @@ +uniform mat4 modelview; +uniform mat4 projection; + + +void main() +{ + gl_Position = projection * modelview * gl_Vertex; +} + diff --git a/test/shader/render1.frag b/test/shader/render1.frag new file mode 100644 index 0000000..b17b221 --- /dev/null +++ b/test/shader/render1.frag @@ -0,0 +1,8 @@ +uniform sampler2D texture; +varying float lum; + +void main() +{ + //gl_FragColor = texture2D(texture, vec2(gl_TexCoord[0])) * lum; + gl_FragColor = vec4(lum, lum, lum, 0); +} diff --git a/test/shader/render1.vert b/test/shader/render1.vert new file mode 100644 index 0000000..79f20a8 --- /dev/null +++ b/test/shader/render1.vert @@ -0,0 +1,17 @@ +uniform mat4 projection; +uniform mat4 modelview; +varying float lum; + +void main() +{ + vec3 light = normalize(vec3(-1.0, -1.0, 0.0)); + vec3 norm = normalize(gl_Normal); + lum = -dot(light, gl_Normal) + 0.20; + if(lum <= 0.20) { + lum = 0.20; + } + lum *= 0.80; + + gl_Position = projection * modelview * gl_Vertex; + //gl_TexCoord[0] = gl_MultiTexCoord0; +} diff --git a/test/shader/render2.frag b/test/shader/render2.frag new file mode 100644 index 0000000..5491ad5 --- /dev/null +++ b/test/shader/render2.frag @@ -0,0 +1,6 @@ +uniform sampler2D lightView; + +void main() +{ + +} diff --git a/test/shader/render2.vert b/test/shader/render2.vert new file mode 100644 index 0000000..6166bf5 --- /dev/null +++ b/test/shader/render2.vert @@ -0,0 +1,15 @@ +uniform mat4 modelview; +uniform mat4 projection; + +uniform mat4 lightProjection; +uniform mat4 lightModelview; + +varying vec4 lightCoord; + +void main() +{ + gl_Position = projection * modelview * gl_Vertex; + + lightCoord = lightProjection * lightModelview * gl_Vertex; +} +