ändern ohne das Seitenverhältnis zu beeinflussen.
So weit so gut
Aber leider stimmen die Maus Koordinaten nicht mehr
Wie kann ich die errechnen?
Der Cursor sollte genau in die Mitte des Kreuzes zeigen:
Rot ist der Viewport.
Beispiel Code (Problem in oglUpdateView() -> \window\mouse_factor\x & y):
Code: Alles auswählen
EnableExplicit
Structure VECTOR_STRUCT
x.f
y.f
EndStructure
Structure SCREEN_STRUCT
hdc.i
hrc.i
width.i
height.i
view_width.i
view_height.i
aspect_ratio.f
aspect_x.i
aspect_y.i
EndStructure
Structure WINDOW_STRUCT
id.i
hwnd.i
width.i
height.i
mouse.VECTOR_STRUCT
mouse_factor.VECTOR_STRUCT
EndStructure
Structure OPENGL_STRUCT
window.WINDOW_STRUCT
screen.SCREEN_STRUCT
EndStructure
Global ogl.OPENGL_STRUCT
Procedure.i oglUpdateView()
With ogl
\screen\aspect_ratio = \screen\view_width / \screen\view_height
\screen\height = \window\width / \screen\aspect_ratio
\screen\width = \window\width
If \screen\height > \window\height
\screen\height = \window\height
\screen\width = \window\height * \screen\aspect_ratio
EndIf
\screen\aspect_x = (\window\width - \screen\width) >> 1
\screen\aspect_y = (\window\height - \screen\height) >> 1
\window\mouse_factor\x = (\screen\view_width / \window\width);<------ WAS HIER TUN?
\window\mouse_factor\y = (\screen\view_height / \window\height);<---- ???????????????
glClear_(#GL_COLOR_BUFFER_BIT)
glViewport_(\screen\aspect_x,\screen\aspect_y,\screen\width,\screen\height)
glMatrixMode_(#GL_PROJECTION)
glLoadIdentity_()
glOrtho_(0,\screen\view_width,\screen\view_height,0,0,1)
glMatrixMode_(#GL_MODELVIEW)
glLoadIdentity_()
ProcedureReturn #Null
EndWith
EndProcedure
Procedure.i oglCreate(Window.i,ViewWidth.i,ViewHeight.i)
Protected pfd.PIXELFORMATDESCRIPTOR
Protected pix.i
With ogl
\window\id = Window
\window\hwnd = WindowID(Window)
\screen\hdc = GetDC_(\window\hwnd)
If \screen\hdc
pfd\nSize = SizeOf(PIXELFORMATDESCRIPTOR)
pfd\nVersion = 1
pfd\dwFlags = #PFD_SUPPORT_OPENGL|#PFD_DOUBLEBUFFER|#PFD_DRAW_TO_WINDOW
pfd\iLayerType = #PFD_MAIN_PLANE
pfd\iPixelType = #PFD_TYPE_RGBA
pfd\cColorBits = 24
pfd\cDepthBits = 8
pfd\cAlphaBits = 8
pix = ChoosePixelFormat_(\screen\hdc,@pfd)
If pix
If SetPixelFormat_(\screen\hdc,pix,@pfd)
\screen\hrc = wglCreateContext_(\screen\hdc)
If \screen\hrc
If wglMakeCurrent_(\screen\hdc,\screen\hrc)
\window\width = WindowWidth(Window)
\window\height = WindowHeight(Window)
\screen\view_width = ViewWidth
\screen\view_height = ViewHeight
ProcedureReturn #True
EndIf
wglDeleteContext_(\screen\hrc)
EndIf
EndIf
EndIf
ReleaseDC_(\window\hwnd,\screen\hdc)
EndIf
ProcedureReturn #False
EndWith
EndProcedure
Procedure.i oglTranslateMouse(*Mouse.VECTOR_STRUCT)
With ogl
\window\mouse\x = WindowMouseX(\window\id)
\window\mouse\y = WindowMouseY(\window\id)
If \window\mouse\x = -1 And \window\mouse\y = -1
ProcedureReturn #False
Else
*Mouse\x = \window\mouse\x * \window\mouse_factor\x
*Mouse\y = \window\mouse\y * \window\mouse_factor\y
ProcedureReturn #True
EndIf
EndWith
EndProcedure
Procedure.i oglRenderBegin()
glClear_(#GL_COLOR_BUFFER_BIT)
glLoadIdentity_()
ProcedureReturn #Null
EndProcedure
Procedure.i oglBox(X.f,Y.f,Width.f,Height.f,Filled.i = #False,R.f = 1,G.f = 1,B.f = 1)
glLineWidth_(2)
If Filled
glBegin_(#GL_POLYGON)
Else
glBegin_(#GL_LINE_LOOP)
EndIf
glColor3f_(R,G,B)
glVertex2f_(X,Y)
glVertex2f_(X + Width,Y)
glVertex2f_(X + Width,Y + Height)
glVertex2f_(X,Y + Height)
glEnd_()
ProcedureReturn #Null
EndProcedure
Procedure.i oglRenderEnd()
With ogl
wglSwapBuffers_(\screen\hdc)
Sleep_(1);LAZY BAD SLEEP!
ProcedureReturn #Null
EndWith
EndProcedure
Procedure.i oglDestroy()
With ogl
If \screen\hrc
wglMakeCurrent_(#Null,#Null)
wglDeleteContext_(\screen\hrc)
EndIf
If \screen\hdc
ReleaseDC_(\window\hwnd,\screen\hdc)
EndIf
ClearStructure(@ogl,OPENGL_STRUCT)
ProcedureReturn #Null
EndWith
EndProcedure
Procedure.i DemoResize()
Protected window.i
With ogl
window = EventWindow()
\window\width = WindowWidth(window)
\window\height = WindowHeight(window)
oglUpdateView()
ProcedureReturn #Null
EndWith
EndProcedure
Procedure.i Demo(Width.i,Height.i)
Protected window.i
Protected window_flag.i
Protected window_msg.i
Protected mouse.VECTOR_STRUCT
window_flag|#PB_Window_SystemMenu|#PB_Window_ScreenCentered
window_flag|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_SizeGadget
window = OpenWindow(#PB_Any,#Null,#Null,Width,Height,"Demo",window_flag)
If IsWindow(window)
If oglCreate(window,320,200)
oglUpdateView()
WindowBounds(window,Width,Height,#PB_Ignore,#PB_Ignore)
BindEvent(#PB_Event_SizeWindow,@DemoResize())
Repeat
Repeat
window_msg = WindowEvent()
Select window_msg
Case #PB_Event_CloseWindow
Break 2
EndSelect
Until window_msg = #Null
oglTranslateMouse(@mouse)
oglRenderBegin()
oglBox(0,0,320,200,#True,0.2,0,0)
oglBox(mouse\x - 4,mouse\y - 1,7,1)
oglBox(mouse\x - 1,mouse\y - 4,1,7)
oglRenderEnd()
ForEver
oglDestroy()
EndIf
CloseWindow(window)
EndIf
EndProcedure
Demo(1080,720)
End