From b351b70ddf726130c85c45f1b9dbea2dfb5bbcd6 Mon Sep 17 00:00:00 2001 From: omigamedev Date: Wed, 18 Jan 2017 00:14:50 +0000 Subject: [PATCH] enable multi sampling in Windows, fix circle shape --- engine/app.cpp | 44 +++++++++++++++++++++++++------------ engine/app.hpp | 2 +- engine/main.cpp | 57 ++++++++++++++++++++++++++++++++++-------------- engine/shape.cpp | 22 +++++++++---------- engine/shape.hpp | 8 +++---- 5 files changed, 87 insertions(+), 46 deletions(-) diff --git a/engine/app.cpp b/engine/app.cpp index e772e6d..1bcfffe 100644 --- a/engine/app.cpp +++ b/engine/app.cpp @@ -55,8 +55,10 @@ void App::init() shader_color.create(shader_color_v, shader_color_f); shader_uv.create(shader_v, shader_uv_f); plane.create<5>(50, 50); - circle.create<10>(25, Circle::kUVMapping::Tube); - circle2.create<10>(25, 12, Circle::kUVMapping::Tube); + circle.create<10>(25); + circle2.create<10>(25, Circle::kUVMapping::Tube); + circle3.create<10>(25, 12, Circle::kUVMapping::Tube); + circle4.create<10>(25, 12, Circle::kUVMapping::Planar); rounded.create<3>(50, 50, 10); slice.create(50, 50, 10, .3f); if (!tex.load("data/uvs.jpg")) @@ -96,13 +98,14 @@ void App::update(float dt) glm::mat4 proj = glm::ortho(0.f, (float)width, (float)height, 0.f, -1.f, 1.f); - Shape* shapes[] = { &circle, &circle2, &plane, &rounded, &slice }; + Shape* shapes[] = { &circle, &circle2, &circle3, &circle4, &plane, &rounded, &slice }; //glClearColor(red, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT); - auto s = glm::scale(glm::vec3(2)); - for (int i = 0; i < 5; i++) + auto s = glm::scale(glm::vec3(1.5)); + int h = 100; + for (int i = 0; i < sizeof(shapes)/sizeof(Shape*); i++) { shader.use(); @@ -110,32 +113,45 @@ void App::update(float dt) glActiveTexture(GL_TEXTURE0); tex.bind(); shader.u_int("tex", 0); - shader.u_mat4("mvp", proj * glm::translate(glm::vec3{75 + 120 * 1, 75 + 120 * i, 0.f}) * s); + shader.u_mat4("mvp", proj * glm::translate(glm::vec3{75 + h * 1, 75 + h * i, 0.f}) * s); shapes[i]->draw_fill(); tex.unbind(); shader_color.use(); - shader_color.u_mat4("mvp", proj * glm::translate(glm::vec3{75 + 120 * 2, 75 + 120 * i, 0.f}) * s); + shader_color.u_mat4("mvp", proj * glm::translate(glm::vec3{75 + h * 2, 75 + h * i, 0.f}) * s); shader_color.u_vec4("col", {1, 1, 1, 1}); shapes[i]->draw_stroke(); - shader_color.u_mat4("mvp", proj * glm::translate(glm::vec3{75 + 120 * 0, 75 + 120 * i, 0.f}) * s); + shader_color.u_mat4("mvp", proj * glm::translate(glm::vec3{75 + h * 0, 75 + h * i, 0.f}) * s); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); shapes[i]->draw_fill(); shader_uv.use(); - shader_uv.u_mat4("mvp", proj * glm::translate(glm::vec3{75 + 120 * 3, 75 + 120 * i, 0.f}) * s); + shader_uv.u_mat4("mvp", proj * glm::translate(glm::vec3{75 + h * 3, 75 + h * i, 0.f}) * s); shader_uv.u_int("tex", 0); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); shapes[i]->draw_fill(); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - shader_uv.u_mat4("mvp", proj * glm::translate(glm::vec3{75 + 120 * 4, 75 + 120 * i, 0.f}) * s); + shader_uv.u_mat4("mvp", proj * glm::translate(glm::vec3{75 + h * 4, 75 + h * i, 0.f}) * s); shapes[i]->draw_fill(); tex.unbind(); } //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); -// shader_color.use(); -// shader_color.u_mat4("mvp", proj * glm::translate(glm::vec3{width/2, height/2, 0.f}) * glm::scale(glm::vec3(8))); -// shader_color.u_vec4("col", {1, 1, 1, 1}); -// slice.draw_fill(); + //shader_color.use(); + //shader_color.u_mat4("mvp", proj * glm::translate(glm::vec3{width/2, height/2, 0.f}) * glm::scale(glm::vec3(8))); + //shader_color.u_vec4("col", {1, 1, 1, 1}); + + //glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + //tex.bind(); + //shader.use(); + //shader.u_mat4("mvp", proj * glm::translate(glm::vec3{ width / 2, height / 2, 0.f }) * glm::scale(glm::vec3(8))); + //shader.u_int("tex", 0); + //circle3.draw_fill(); + + //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + //shader_color.use(); + //shader_color.u_mat4("mvp", proj * glm::translate(glm::vec3{width/2, height/2, 0.f}) * glm::scale(glm::vec3(8))); + //shader_color.u_vec4("col", {1, 1, 1, 1}); + //glLineWidth(2); + //circle3.draw_fill(); } diff --git a/engine/app.hpp b/engine/app.hpp index 8bbad96..f88370f 100644 --- a/engine/app.hpp +++ b/engine/app.hpp @@ -10,7 +10,7 @@ class App Shader shader_color; Shader shader_uv; Plane plane; - Circle circle, circle2; + Circle circle, circle2, circle3, circle4; Rounded rounded; Slice9 slice; Texture2D tex; diff --git a/engine/main.cpp b/engine/main.cpp index bb3e403..1805bf8 100644 --- a/engine/main.cpp +++ b/engine/main.cpp @@ -198,16 +198,16 @@ int main(int argc, const char * argv[]) #pragma comment (lib, "glew32.lib") LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp); -HINSTANCE hInst; -HWND hWnd; -HDC hDC; -HGLRC hRC; -char *className; -bool keys[256]; +HINSTANCE hInst; +HWND hWnd; +HDC hDC; +HGLRC hRC; +wchar_t* className; +bool keys[256]; int main() { - WNDCLASSA wc; + WNDCLASS wc; PIXELFORMATDESCRIPTOR pfd; App::I.create(); @@ -221,20 +221,19 @@ int main() // Create the main window hInst = GetModuleHandle(NULL); - className = "DrosophilaMain"; + className = L"EngineMain"; wc.hInstance = hInst; wc.lpfnWndProc = (WNDPROC)WndProc; wc.lpszClassName = className; wc.hbrBackground = (HBRUSH)COLOR_WINDOW; - RegisterClassA(&wc); + RegisterClass(&wc); AdjustWindowRect(&clientRect, WS_OVERLAPPEDWINDOW, false); - hWnd = CreateWindowA(wc.lpszClassName, "New Engine", WS_OVERLAPPEDWINDOW, + hWnd = CreateWindow(wc.lpszClassName, L"New Engine", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, clientRect.right - clientRect.left, clientRect.bottom - clientRect.top, 0, 0, hInst, 0); - ShowWindow(hWnd, SW_NORMAL); // Setup GL Rendering Context pfd.nSize = sizeof(pfd); @@ -262,17 +261,41 @@ int main() // If supported create a 3.1 context if (wglewIsSupported("WGL_ARB_create_context")) { - int attribs[] = + int contex_attribs[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 1, WGL_CONTEXT_FLAGS_ARB, 0, 0 }; - auto oldContext = hRC; - hRC = wglCreateContextAttribsARB(hDC, NULL, attribs); + int pixel_attribs[] = + { + WGL_DRAW_TO_WINDOW_ARB, GL_TRUE, + WGL_SUPPORT_OPENGL_ARB, GL_TRUE, + WGL_DOUBLE_BUFFER_ARB, GL_TRUE, + WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB, + WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB, + WGL_COLOR_BITS_ARB, 32, + WGL_DEPTH_BITS_ARB, 24, + WGL_STENCIL_BITS_ARB, 8, + WGL_SAMPLE_BUFFERS_ARB, 1, // Number of buffers (must be 1 at time of writing) + WGL_SAMPLES_ARB, 4, // Number of samples + 0 + }; + UINT numFormat; + wglMakeCurrent(NULL, NULL); - wglDeleteContext(oldContext); + wglDeleteContext(hRC); + DestroyWindow(hWnd); + + hWnd = CreateWindow(wc.lpszClassName, L"New Engine", WS_OVERLAPPEDWINDOW, + CW_USEDEFAULT, CW_USEDEFAULT, clientRect.right - clientRect.left, + clientRect.bottom - clientRect.top, 0, 0, hInst, 0); + + hDC = GetDC(hWnd); + wglChoosePixelFormatARB(hDC, pixel_attribs, nullptr, 1, &pxfmt, &numFormat); + SetPixelFormat(hDC, pxfmt, &pfd); + hRC = wglCreateContextAttribsARB(hDC, NULL, contex_attribs); wglMakeCurrent(hDC, hRC); } else @@ -283,6 +306,8 @@ int main() App::I.init(); + ShowWindow(hWnd, SW_NORMAL); + MSG msg; bool running = true; unsigned long t0 = GetTickCount(); @@ -315,7 +340,7 @@ int main() // Clean up DestroyWindow(hWnd); - UnregisterClassA(className, hInst); + UnregisterClass(className, hInst); } LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) diff --git a/engine/shape.cpp b/engine/shape.cpp index dab43b1..c89b210 100644 --- a/engine/shape.cpp +++ b/engine/shape.cpp @@ -125,7 +125,7 @@ void Circle::create_impl(float radius, int div, GLushort* idx, vertex_t* vertice void Circle::create_impl(float radius_out, float radius_in, int div, GLushort* idx, vertex_t* vertices, kUVMapping map) { - count[0] = div * 6; + count[0] = div * (radius_in == 0.f ? 3 : 6); count[1] = div * 4; ioff[0] = (GLvoid*)0; ioff[1] = (GLvoid*)(count[0] * sizeof(GLushort)); @@ -143,8 +143,8 @@ void Circle::create_impl(float radius_out, float radius_in, int div, GLushort* i } else { - vertices[i*2].uvs = { (float)i / div, 0.f }; - vertices[i*2+1].uvs = { (float)i / div, 1.f}; + vertices[i*2].uvs = { (float)i / div, 0.f }; // inner + vertices[i*2+1].uvs = { (float)i / div, 1.f};// outer } vertices[i*2].pos = glm::vec4(uv * radius_in, 0, 1); vertices[i*2+1].pos = glm::vec4(uv * radius_out, 0, 1); @@ -155,16 +155,16 @@ void Circle::create_impl(float radius_out, float radius_in, int div, GLushort* i if (radius_in != 0.f) { - *pidx++ = i * 2; // A - *pidx++ = ((i + 1) * 2 + 1) % (div * 2); // C - *pidx++ = ((i + 1) * 2) % (div * 2); // D + *pidx++ = i * 2; // A + *pidx++ = ((i+1)*2+1) % (div*2); // C + *pidx++ = ((i+1)*2) % (div*2); // D } - *pidx2++ = i*2; // A - *pidx2++ = ((i+1)*2) % (div*2); // D - - *pidx2++ = i*2+1; // B - *pidx2++ = ((i+1)*2+1) % (div*2);// C + *pidx2++ = i*2; // A + *pidx2++ = ((i+1)*2) % (div*2); // D + + *pidx2++ = i*2+1; // B + *pidx2++ = ((i+1)*2+1) % (div*2); // C } } diff --git a/engine/shape.hpp b/engine/shape.hpp index 77056c7..81bf544 100644 --- a/engine/shape.hpp +++ b/engine/shape.hpp @@ -37,15 +37,15 @@ public: { static GLushort idx[div*3 + div*2]; static vertex_t vertices[div+1]; - create_impl(radius, div, idx, vertices, kUVMapping::Planar); + create_impl(radius, div, idx, vertices); return create_buffers(idx, vertices, sizeof(idx), sizeof(vertices)); } template bool create(float radius, kUVMapping map) { - static GLushort idx[div*3 + div*2]; - static vertex_t vertices[div*2]; - create_impl(radius, 0.f, div, idx, vertices, map); + static GLushort idx[(div+1)*3 + (div+1)*4]; + static vertex_t vertices[(div+1)*2]; + create_impl(radius, 0.f, (div+1), idx, vertices, map); return create_buffers(idx, vertices, sizeof(idx), sizeof(vertices)); } template