CSEngine
Loading...
Searching...
No Matches
OGLMgr.cpp
1#include "../Util/Matrix.h"
2#include "OGLMgr.h"
3#include "../Util/GLProgramHandle.h"
4#include "CameraMgr.h"
5#include "EngineCore.h"
6#include "../Util/Render/SEnvironmentMgr.h"
7
8#ifdef __linux__
9
10#include <iostream>
11
12#endif
13
14using namespace CSE;
15
16OGLMgr::OGLMgr() : m_projectionRatio(-1) {
17}
18
19void OGLMgr::Init() {
20 setupEGLGraphics();
21}
22
23OGLMgr::~OGLMgr() {
24 releaseBuffers();
25}
26
27void OGLMgr::setBuffers() {
28 //렌더버퍼 생성
29 glGenRenderbuffers(1, &m_colorRenderbuffer);
30 glBindRenderbuffer(GL_RENDERBUFFER, m_colorRenderbuffer);
31
32 //깊이버퍼 생성 및 바인딩(옮김)
33 //glGenRenderbuffers(1, &m_depthRenderbuffer);
34 //glBindRenderbuffer(GL_RENDERBUFFER, m_depthRenderbuffer);
35 //glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, m_width, m_height);
36
37 //프레임버퍼 생성
38 glGenFramebuffers(1, &m_framebuffer);
39 glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
40 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
41 GL_COLOR_ATTACHMENT0,
42 GL_RENDERBUFFER,
43 m_colorRenderbuffer);
44 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
45 GL_DEPTH_ATTACHMENT,
46 GL_RENDERBUFFER,
47 m_depthRenderbuffer);
48
49 // 렌더버퍼 바인딩
50 glBindRenderbuffer(GL_RENDERBUFFER, m_colorRenderbuffer);
51
52 glEnable(GL_DEPTH_TEST);
53 glClearDepthf(1.0f);
54
55#ifndef PLATFORM_IOS
56 //프레임버퍼 바인딩
57 glBindFramebuffer(GL_FRAMEBUFFER, 0);
58#endif
59}
60
61void OGLMgr::setupEGLGraphics() {
62 setBuffers();
63 setProjectionRatio(0, 0);
64
65 glEnable(GL_DEPTH_TEST);
66 glEnable(GL_CULL_FACE);
67 glCullFace(GL_BACK);
68 glFrontFace(GL_CCW);
69 glDepthFunc(GL_LEQUAL);
70 glDepthMask(true);
71 glBindFramebuffer(GL_FRAMEBUFFER, 0);
72 //glDepthRangef(0.0,1.0);
73}
74
75void OGLMgr::setProjectionRatio(int width, int height) {
76 m_projectionRatio = static_cast<GLfloat>(width) / static_cast<GLfloat>(height);
77 CORE->GetCore(CameraMgr)->SetProjectionRatio(m_projectionRatio);
78}
79
80void OGLMgr::Render() const {
81 glClearColor(0.4f, 0.4f, 0.4f, 1.0f);
82 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
83
85 glBindBuffer(GL_ARRAY_BUFFER, 0);
86 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
87
88 // 유효하지 않은 값이라서 주석처리함.
89// glEnable(GL_TEXTURE_2D);
90}
91
92void OGLMgr::ResizeWindow(GLuint width, GLuint height) {
93 if (m_depthRenderbuffer) {
94 glDeleteRenderbuffers(1, &m_depthRenderbuffer);
95 m_depthRenderbuffer = 0;
96 }
97
98 // Create the depth buffer.
99 glGenRenderbuffers(1, &m_depthRenderbuffer);
100 glBindRenderbuffer(GL_RENDERBUFFER, m_depthRenderbuffer);
101 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
102
103 // Protect against a divide by zero
104 if (height == 0) {
105 height = 1;
106 }
107
108 // Setup our viewport.
109 glViewport(0, 0, static_cast<GLsizei>(width), static_cast<GLsizei>(height));
110
111 SEnvironmentMgr::SetWidth(width);
112 SEnvironmentMgr::SetHeight(height);
113
114 setProjectionRatio(width, height);
115}
116
117void OGLMgr::releaseBuffers() {
118 glDeleteRenderbuffers(1, &m_colorRenderbuffer);
119 m_colorRenderbuffer = 0;
120
121 if (m_depthRenderbuffer) {
122 glDeleteRenderbuffers(1, &m_depthRenderbuffer);
123 m_depthRenderbuffer = 0;
124 }
125
126 glDeleteFramebuffers(1, &m_framebuffer);
127 m_framebuffer = 0;
128}