{"id":24532661,"url":"https://github.com/hieki-chan/opengl-sample","last_synced_at":"2025-03-15T21:16:29.249Z","repository":{"id":272002061,"uuid":"915237738","full_name":"hieki-chan/openGL-sample","owner":"hieki-chan","description":"OpenGL project example using freeglut and glew for learning in the university","archived":false,"fork":false,"pushed_at":"2025-02-22T07:03:20.000Z","size":139513,"stargazers_count":1,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-22T08:19:07.805Z","etag":null,"topics":["glew","glut","opengl","render-engine"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hieki-chan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-11T10:25:56.000Z","updated_at":"2025-02-22T07:03:23.000Z","dependencies_parsed_at":"2025-01-11T11:35:18.435Z","dependency_job_id":"4ff57424-0557-479d-8c2d-b50e96ce64f9","html_url":"https://github.com/hieki-chan/openGL-sample","commit_stats":null,"previous_names":["hieki-chan/opengl-sample"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hieki-chan%2FopenGL-sample","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hieki-chan%2FopenGL-sample/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hieki-chan%2FopenGL-sample/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hieki-chan%2FopenGL-sample/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hieki-chan","download_url":"https://codeload.github.com/hieki-chan/openGL-sample/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243790998,"owners_count":20348385,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["glew","glut","opengl","render-engine"],"created_at":"2025-01-22T10:17:53.307Z","updated_at":"2025-03-15T21:16:29.235Z","avatar_url":"https://github.com/hieki-chan.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# OpenGL Project Example\n\n\u003cp align=\"center\"\u003e\n\t\u003cimg src=\"resources/sphere.jpeg\" alt=\"Sphere Image\"\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\t\u003cp\u003eExample scene:\u003c/p\u003e\n\t\u003cimg src=\"resources/scene.png\" alt=\"Sphere Image\"\u003e\n\u003c/p\u003e\n\nbest way to lean OpenGL : ```https://learnopengl.com```\n\n\n## CÁCH TẠO OBJECT MỚI TỪ KHỐI CÓ SẴN\n\nCách tạo mới 1 object được dựng từ các khối có sẵn\n\nCần tạo 3 files sau:\n- ```object.h``` : header để dùng trong main, thêm vào thư mục ```include/objects```\n- ```object.cpp``` : file cpp để code object, thêm vào thư mục ```objects```\n\n```object.h``` để định nghĩa object này\n\n```C++\n#pragma once\n\n// include opengl utils\n#include \"core/openGL.h\"\n\n// include file cube để dùng cube, ngoài ra include thêm các object khác nếu có\n#include \"cube.h\"\n\n// định nghĩa hàm draw object\nvoid drawObject(vec3 position, vec3 rotation, vec3 scale);\n```\n\n```object.cpp``` để vẽ object\n\nví dụ vẽ 1 khối cube\n\n```C++\n// include file object.h\n#include \"objects/object.h\"\n\n// ví dụ draw cube\nvoid drawObject(vec3 position, vec3 rotation, vec3 scale)\n{\n\t// gọi hàm draw cube trong cube,h\n\tdrawCube(vec3(0, 0, 0), vec3(0, 0, 0), vec3(1, 1, 1), color(1, 1, 1, 1));\n}\n```\n\n\nTrong hàm ```display()``` ở ```main```, gọi hàm ```drawObject()``` trên để vẽ \n\n```C++\n//include file object đó\n#include \"objects/object.h\" \n\n//ví dụ hàm display\nvoid display()\n{\n\tglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);\n\n\tglClearColor(0.2f, 0.2f, 0.2f, 1.0f);\n\n\tintroScreen();  //ui\n\n\tdrawAxes();     // truc xyz toa do (envỉonment)\n\n\n\tdrawObject(vec3(), vec3(), vec3(1, 1, 1));  //goi ham draw object do do day\n\n\n\tglEnable(GL_DEPTH_TEST);\n\n\tglutSwapBuffers();\n}\n```\n\nTrong hàm draw, để dùng mô hình phân cấp thì dựa vào hàm ```cubeTransform``` và ```cubeTransformMatrix```\nđể xác định matrix ở cấp hiện tại\n\n```C++\nvoid drawShelf(vec3 position, vec3 rotation, vec3 scale)\n{\n\t//ap dung pos, rot, scale cho cube, tra ve global model matrix\n\tmat4 globalTransformMatrix = cubeTransform(position, rotation, scale);\n\n\tdrawCube(vec3(), vec3(), vec3(1, 1, 1), color(1, 1, 1, 1));\n\n\n\t//mo hinh phan cap, goi ham nay de thay doi transform cho nhung cube sau\n\t//tra ve current model matrix\n\tmat4 currentModelMatrix = cubeTransformMatrix(globalTransformMatrix * Angel::Scale(2) * Angel::RotateY(100));\n\n\tdrawCube(vec3(1, 1, 1), vec3(), vec3(.2, .2, 3), color(1, 1, 1, 1));\n\n\t//cube sau la con cua cube truoc (3cap) nen lay current model matrix\n\tmat4 childModelMatrix = cubeTransformMatrix(currentModelMatrix * Angel::RotateY(50));\n\n\tdrawCube(vec3(1, 0, 1), vec3(), vec3(1, 1, 3), color(1, 1, 1, 1));\n\n\t//tro ve cap 2\n\t//cubeTransformMatrix(currentModelMatrix);\n\t// tro ve cap 1\n\t//cubeTransformMatrix(globalTransformMatrix);\n\n\t//reset cube neu ko se bi loi khi draw nhieu objects khac\n\tcubeReset();\n}\n```\n\nĐể thêm input cho hàm trên, ta thêm biến position và hàm input\n\n```C++\n//dung cho input\nvec3 cube_position;\n\n\nvoid drawShelf(vec3 position, vec3 rotation, vec3 scale)\n{\n\t//ap dung pos, rot, scale cho cube, tra ve global model matrix\n\tposition += cube_position;\t//cong them cube input\n\n\t//đoạn code dưới y như hàm ở trên\n\tmat4 globalTransformMatrix = cubeTransform(position, rotation, scale);\n\tdrawCube(vec3(), vec3(), vec3(1, 1, 1), color(1, 1, 1, 1));\n\n\tmat4 currentModelMatrix = cubeTransformMatrix(globalTransformMatrix * Angel::Scale(2) * Angel::RotateY(100));\n\tdrawCube(vec3(1, 1, 1), vec3(), vec3(.2, .2, 3), color(1, 1, 1, 1));\n\n\tmat4 childModelMatrix = cubeTransformMatrix(currentModelMatrix * Angel::RotateY(50));\n\tdrawCube(vec3(1, 0, 1), vec3(), vec3(1, 1, 3), color(1, 1, 1, 1));\n\n\tcubeReset();\n}\n\n//nhớ phải gọi hàm này trong hàm input =)\nvoid shelfKeyboard(unsigned char key, int mouseX, int mouseY)\n{\n\tswitch (key)\n\t{\n\tcase 'a':\n\t\tcube_position.x -= 0.1f;\n\t\tbreak;\n\tdefault:\n\t\tbreak;\n\t}\n}\n```\n\n\n## CÁCH TẠO MỘT OBJECT MỚI\n\nĐể tạo object mới thì cần xác định \n\nxem ví dụ file ```cube.h ```, ``cube.cpp`` để làm\n\n```cube.h``` file header phải đặt trong folder ```include```\n[cube.h](https://github.com/hieki-chan/openGL-sample/blob/master/render-engine/include/objects/cube.h)\n\n``cube.cpp`` file header nên đặt trong folder ```objects```\n[cube.cpp](https://github.com/hieki-chan/openGL-sample/blob/master/render-engine/objects/cube/cube.cpp)\n\n\n\n# CORE\n\ncore in render engine\n\n## DEFAULT CUBE\n\n```C++\n// dùng cube: core/cube.h\ninclude \"objects/cube.h\"\n```\n\n```C++\n// vẽ 1 cube tại position với góc xoay rotation tỉ lệ scale và màu color\n// color là typedef vec4\nvoid drawCube(vec3 position, vec3 rotation, vec3 scale, color color);\n```\n\n## CAMERA\n\n#### Camera view and Projection\n\n```C++\n// camera định nghĩa trong header core/camera.h, triển khai trong core/camera.cpp\ninclude \"core/camera.h\"\n```\n\n```C++\n// chuyển sang tọa độ camera và dùng phép chiếu perspective \n// view và projection location từ shader program, gọi hàm này trước khi draw object\nvoid useCameraMatrix(GLuint view_location, GLuint projection_location);\n```\n\n#### Camera position\n\n```C++\n// thay đổi tọa độ camera (eye)\nvoid setCameraPos(const vec4\u0026 position);\n```\n\n#### Zoom in/out\n\n```C++\n// zoom camera, value \u003e 0 thì phóng to, value \u003c 0 thì thu nhỏ, value = 0 thì không đổi\nvoid zoomCamera(float value);\n```\n\n```C++\n// set tốc độ zoom\nvoid setZoomSpeed(float zoomSpeed);\n```\n\n#### Camera Motion\n\n```C++\n// chuyển động của camera xoay quanh gốc tọa độ khi di chuyển chuột\nvoid cameraMotion(int mouseX, int mouseY, int\u0026 lastMouseX, int\u0026 lastMouseY);\n```\n\n## INPUT\n\n\u003e Not supported\n\n## ENVIRONMENT\n\n```C++\n//init environment\ninitEnvironment();\n//gọi trong hàm display để vẽ trục xyz\ndrawAxes();\n```\n\n## LIGHTING\n\n#### Create a new light\n```C++\n// tao 1 light moi va them vao lighting list\nlight addLight(const vec3\u0026 position, const color3\u0026 color);\n```\n\n## UI\n\n#### Init UI\n```C++\n// gọi hàm mỗi khi thay đổi window width và window height\nvoid ui::screenChange(int width, int height);\n```\n\n#### Render Text\n```C++\n// vẽ text với tại tọa độ màn (x,y)\nvoid text2D(const char* text, int fontSize, int x, int y, color color = Angel::vec4(1, 1, 1, 1));\n```\n\n#### Render Button\n```C++\n// tạo button\nbutton button2D(const char* text, int fontSize, int sizeX, int sizeY, int x, int y, color buttonColor = Angel::vec4(0, 0, 1, 1), color textColor = Angel::vec4(1, 1, 1, 1));\n```\n\n#### Render text and button example\n\n```C++\nvoid onGUI()\n{\n\tui::text2D(\"Render Engine\", 10, 10, ui::window_height - 20, color(1, 1, 0, 1));\n\n\tbtnAxes = ui::button2D(\"Axes\", 14, 80, 40, ui::window_width - 90, ui::window_height - 50, color(1, 1, 0, 1), color(0, 0, 0, 1));\n\t// doSomething se duoc goi neu click trong ham onEvent()\n\tbtnAxes.callback = doSomethingOnClick;\n}\n\nvoid display()\n{\n\tglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);\n\tglClearColor(0.2f, 0.2f, 0.2f, 1.0f);\n\n\t// DRAW 3D objects  here\n\n\t// ON GUI\n\tonGUI();\n\n\n\tglEnable(GL_DEPTH_TEST);\n\tglutSwapBuffers();\n}\n\n// sau khi render button, gọi button trong hàm mouse func\nvoid mouse(int button, int state, int x, int y)\n{\n\tbtnAxes.onEvent(button, state, x, y);\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhieki-chan%2Fopengl-sample","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhieki-chan%2Fopengl-sample","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhieki-chan%2Fopengl-sample/lists"}