From f17bc687a3e14343e9da0705563de81cd6bbd9cf Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 16 Aug 2016 23:23:01 +0100 Subject: [PATCH] "Cool" OpenGL Demo of a rectangle --- examples/main.odin | 60 +++++++++++++++++++++++++++++--------------- examples/opengl.odin | 46 +++++++++++++++++++++++++++++++++ examples/win32.odin | 9 ++++++- 3 files changed, 94 insertions(+), 21 deletions(-) create mode 100644 examples/opengl.odin diff --git a/examples/main.odin b/examples/main.odin index 0fb03d1db..36e4599c6 100644 --- a/examples/main.odin +++ b/examples/main.odin @@ -1,5 +1,6 @@ #load "basic.odin" #load "win32.odin" +#load "opengl.odin" win32_perf_count_freq := GetQueryPerformanceFrequency(); @@ -26,10 +27,10 @@ win32_print_last_error :: proc() { main :: proc() { instance := GetModuleHandleA(null); - class_name := "Odin-Language-Demo\x00"; - title := "Odin Language Demo\x00"; + class_name := "Odin-Language-Demo\x00"; + title := "Odin Language Demo\x00"; c_class_name := ^class_name[0]; - c_title := ^title[0]; + c_title := ^title[0]; wc := WNDCLASSEXA{ cbSize = size_of(WNDCLASSEXA) as u32, @@ -49,10 +50,14 @@ main :: proc() { return; } + WINDOW_WIDTH :: 854; + WINDOW_HEIGHT :: 480; + hwnd := CreateWindowExA(0, c_class_name, c_title, WS_VISIBLE | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, - CW_USEDEFAULT, CW_USEDEFAULT, 854, 480, + CW_USEDEFAULT, CW_USEDEFAULT, + WINDOW_WIDTH, WINDOW_HEIGHT, null, null, instance, null); @@ -82,30 +87,25 @@ main :: proc() { wglMakeCurrent(dc, opengl_context); attribs := [8]i32{ - 0x2091, // WGL_CONTEXT_MAJOR_VERSION_ARB - 2, // Major - 0x2092, // WGL_CONTEXT_MINOR_VERSION_ARB - 1, // Minor - 0x9126, // WGL_CONTEXT_PROFILE_MASK_ARB - 0x0002, // WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB + WGL_CONTEXT_MAJOR_VERSION_ARB, 2, + WGL_CONTEXT_MINOR_VERSION_ARB, 1, + WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, 0, // NOTE(bill): tells the proc that this is the end of attribs }; wgl_string := "wglCreateContextAttribsARB\x00"; - wglCreateContextAttribsARB := wglGetProcAddress(^wgl_string[0]) as wglCreateContextAttribsARBType; + c_wgl_string := ^wgl_string[0]; + wglCreateContextAttribsARB := wglGetProcAddress(c_wgl_string) as wglCreateContextAttribsARBType; rc := wglCreateContextAttribsARB(dc, 0, ^attribs[0]); wglMakeCurrent(dc, rc); SwapBuffers(dc); } start_time := time_now(); - running := false; + running := true; for running { curr_time := time_now(); dt := curr_time - start_time; - if dt > 2.0 { - running = false; - } msg: MSG; for { @@ -115,14 +115,34 @@ main :: proc() { } if msg.message == WM_QUIT { - return; - } else { - _ = TranslateMessage(^msg); - _ = DispatchMessageA(^msg); + running = false; + break; } + _ = TranslateMessage(^msg); + _ = DispatchMessageA(^msg); + } + + glClearColor(0.5, 0.7, 1.0, 1.0); + glClear(GL_COLOR_BUFFER_BIT); + // glOrtho(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT, -1, +1); + + { + glBegin(GL_TRIANGLES); + defer glEnd(); + + glColor3f(1, 0, 0); glVertex3f(+0.5, -0.5, 0); + glColor3f(0, 1, 0); glVertex3f(+0.5, +0.5, 0); + glColor3f(0, 0, 1); glVertex3f(-0.5, +0.5, 0); + + glColor3f(0, 0, 1); glVertex3f(-0.5, +0.5, 0); + glColor3f(1, 1, 0); glVertex3f(-0.5, -0.5, 0); + glColor3f(1, 0, 0); glVertex3f(+0.5, -0.5, 0); } SwapBuffers(dc); - sleep_ms(2); + { + ms := (16 - dt*1000) as i32; + if ms > 0 { sleep_ms(ms); } + } } } diff --git a/examples/opengl.odin b/examples/opengl.odin new file mode 100644 index 000000000..9002c1e61 --- /dev/null +++ b/examples/opengl.odin @@ -0,0 +1,46 @@ +GL_ZERO :: 0x0000; +GL_ONE :: 0x0001; +GL_TRIANGLES :: 0x0004; +GL_BLEND :: 0x0BE2; +GL_SRC_ALPHA :: 0x0302; +GL_ONE_MINUS_SRC_ALPHA :: 0x0303; +GL_TEXTURE_2D :: 0x0DE1; +GL_RGBA8 :: 0x8058; +GL_UNSIGNED_BYTE :: 0x1401; +GL_BGRA_EXT :: 0x80E1; +GL_TEXTURE_MAX_LEVEL :: 0x813D; +GL_RGBA :: 0x1908; + +GL_NEAREST :: 0x2600; +GL_LINEAR :: 0x2601; + +GL_DEPTH_BUFFER_BIT :: 0x00000100; +GL_STENCIL_BUFFER_BIT :: 0x00000400; +GL_COLOR_BUFFER_BIT :: 0x00004000; + +GL_TEXTURE_MAX_ANISOTROPY_EXT :: 0x84FE; + +GL_TEXTURE_MAG_FILTER :: 0x2800; +GL_TEXTURE_MIN_FILTER :: 0x2801; +GL_TEXTURE_WRAP_S :: 0x2802; +GL_TEXTURE_WRAP_T :: 0x2803; + +glClear :: proc(mask: u32) #foreign +glClearColor :: proc(r, g, b, a: f32) #foreign +glBegin :: proc(mode: i32) #foreign +glEnd :: proc() #foreign +glColor3f :: proc(r, g, b: f32) #foreign +glColor4f :: proc(r, g, b, a: f32) #foreign +glVertex3f :: proc(x, y, z: f32) #foreign +glTexCoord2f :: proc(u, v: f32) #foreign +glLoadIdentity :: proc() #foreign +glOrtho :: proc(left, right, bottom, top, near, far: f64) #foreign +glBlendFunc :: proc(sfactor, dfactor: i32) #foreign +glEnable :: proc(cap: i32) #foreign +glDisable :: proc(cap: i32) #foreign +glGenTextures :: proc(count: i32, result: ^u32) #foreign +glTexParameteri :: proc(target, pname, param: i32) #foreign +glTexParameterf :: proc(target: i32, pname: i32, param: f32) #foreign +glBindTexture :: proc(target: i32, texture: u32) #foreign +glTexImage2D :: proc(target, level, internal_format, width, height, border, format, _type: i32, pixels: rawptr) #foreign + diff --git a/examples/win32.odin b/examples/win32.odin index 95428ccb2..a4a7e1e4f 100644 --- a/examples/win32.odin +++ b/examples/win32.odin @@ -178,8 +178,15 @@ type PIXELFORMATDESCRIPTOR: struct { GetDC :: proc(h: HANDLE) -> HDC #foreign SetPixelFormat :: proc(hdc: HDC, pixel_format: i32, pfd: ^PIXELFORMATDESCRIPTOR ) -> BOOL #foreign ChoosePixelFormat :: proc(hdc: HDC, pfd: ^PIXELFORMATDESCRIPTOR) -> i32 #foreign +SwapBuffers :: proc(hdc: HDC) -> BOOL #foreign + + +WGL_CONTEXT_MAJOR_VERSION_ARB :: 0x2091; +WGL_CONTEXT_MINOR_VERSION_ARB :: 0x2092; +WGL_CONTEXT_PROFILE_MASK_ARB :: 0x9126; +WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB :: 0x0002; + wglCreateContext :: proc(hdc: HDC) -> HGLRC #foreign wglMakeCurrent :: proc(hdc: HDC, hglrc: HGLRC) -> BOOL #foreign wglGetProcAddress :: proc(c_str: ^u8) -> PROC #foreign wglDeleteContext :: proc(hglrc: HGLRC) -> BOOL #foreign -SwapBuffers :: proc(hdc: HDC) -> BOOL #foreign