2d drawing test
This commit is contained in:
parent
fdc3948f8c
commit
24f569ebc1
@ -15,6 +15,12 @@ class OpenGLWidget(QOpenGLWidget):
|
|||||||
self.yRot = 0
|
self.yRot = 0
|
||||||
self.zoom = -10.0
|
self.zoom = -10.0
|
||||||
self.sketch = []
|
self.sketch = []
|
||||||
|
self.gl_width = self.width() / 1000
|
||||||
|
self.gl_height = self.height() / 1000
|
||||||
|
|
||||||
|
print(self.gl_height)
|
||||||
|
print(self.gl_width)
|
||||||
|
|
||||||
def map_value_to_range(self, value, value_min: int = 0, value_max: int= 1920, range_min=-1, range_max=1):
|
def map_value_to_range(self, value, value_min: int = 0, value_max: int= 1920, range_min=-1, range_max=1):
|
||||||
"""
|
"""
|
||||||
Maps a value from one range to another.
|
Maps a value from one range to another.
|
||||||
@ -56,6 +62,8 @@ class OpenGLWidget(QOpenGLWidget):
|
|||||||
glMatrixMode(GL_PROJECTION)
|
glMatrixMode(GL_PROJECTION)
|
||||||
glLoadIdentity()
|
glLoadIdentity()
|
||||||
aspect = width / float(height)
|
aspect = width / float(height)
|
||||||
|
self.gl_width = self.width() / 1000
|
||||||
|
self.gl_height = self.height() / 1000
|
||||||
|
|
||||||
gluPerspective(45.0, aspect, 1.0, 100.0)
|
gluPerspective(45.0, aspect, 1.0, 100.0)
|
||||||
glMatrixMode(GL_MODELVIEW)
|
glMatrixMode(GL_MODELVIEW)
|
||||||
@ -82,16 +90,16 @@ class OpenGLWidget(QOpenGLWidget):
|
|||||||
# Draw vertical lines
|
# Draw vertical lines
|
||||||
glBegin(GL_LINES)
|
glBegin(GL_LINES)
|
||||||
for x in range(0, self.width(), 20):
|
for x in range(0, self.width(), 20):
|
||||||
x_ndc = self.map_value_to_range(x, 0, value_max=self.width(), range_min=-2, range_max=2)
|
x_ndc = self.map_value_to_range(x, 0, value_max=self.width(), range_min=-self.gl_width, range_max=self.gl_width)
|
||||||
glVertex2f(x_ndc, -1) # Start from y = -1
|
glVertex2f(x_ndc, -self.gl_height) # Start from y = -1
|
||||||
glVertex2f(x_ndc, 1) # End at y = 1
|
glVertex2f(x_ndc, self.gl_height) # End at y = 1
|
||||||
|
|
||||||
# Draw horizontal lines
|
# Draw horizontal lines
|
||||||
|
|
||||||
for y in range(0, self.height(), 20):
|
for y in range(0, self.height(), 20):
|
||||||
y_ndc = self.map_value_to_range(y, 0, value_max=self.height(),range_min=-1, range_max=1)
|
y_ndc = self.map_value_to_range(y, 0, value_max=self.height(), range_min=-self.gl_height, range_max=self.gl_height)
|
||||||
glVertex2f(-2, y_ndc) # Start from x = -1
|
glVertex2f(-self.gl_width, y_ndc) # Start from x = -1
|
||||||
glVertex2f(2, y_ndc) # End at x = 1
|
glVertex2f(self.gl_width, y_ndc) # End at x = 1
|
||||||
glEnd()
|
glEnd()
|
||||||
|
|
||||||
|
|
||||||
@ -116,8 +124,6 @@ class OpenGLWidget(QOpenGLWidget):
|
|||||||
glVertex2f(x2, y2)
|
glVertex2f(x2, y2)
|
||||||
glEnd()
|
glEnd()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def draw_lines_between_points(self, points):
|
def draw_lines_between_points(self, points):
|
||||||
glBegin(GL_LINES)
|
glBegin(GL_LINES)
|
||||||
glColor3f(1.0, 1.0, 1.0) # Set line color to white
|
glColor3f(1.0, 1.0, 1.0) # Set line color to white
|
||||||
@ -148,10 +154,10 @@ class OpenGLWidget(QOpenGLWidget):
|
|||||||
mouse_pos = event.pos()
|
mouse_pos = event.pos()
|
||||||
|
|
||||||
# Map mouse position to normalized device coordinates
|
# Map mouse position to normalized device coordinates
|
||||||
normalized_x = self.map_value_to_range(mouse_pos.x(), 0, value_max=self.width(),range_min=-2, range_max=2)
|
normalized_x = self.map_value_to_range(mouse_pos.x(), 0, value_max=self.width(),range_min=-self.gl_width, range_max=self.gl_width)
|
||||||
normalized_y = self.map_value_to_range(mouse_pos.y(), 0, value_max=self.height(),range_min=-1, range_max=1)
|
normalized_y = self.map_value_to_range(mouse_pos.y(), 0, value_max=self.height(),range_min=-self.gl_height, range_max=self.gl_height)
|
||||||
|
|
||||||
self.startPos = [normalized_x, -normalized_y]
|
self.startPos = [normalized_x * -self.zoom, -normalized_y * -self.zoom]
|
||||||
|
|
||||||
# Now you have the mouse position in normalized coordinates
|
# Now you have the mouse position in normalized coordinates
|
||||||
print("Right mouse button pressed - Mouse position (normalized):", normalized_x, normalized_y)
|
print("Right mouse button pressed - Mouse position (normalized):", normalized_x, normalized_y)
|
||||||
@ -160,10 +166,10 @@ class OpenGLWidget(QOpenGLWidget):
|
|||||||
mouse_pos = event.pos()
|
mouse_pos = event.pos()
|
||||||
|
|
||||||
# Map mouse position to normalized device coordinates
|
# Map mouse position to normalized device coordinates
|
||||||
normalized_x = self.map_value_to_range(mouse_pos.x(), 0, value_max=self.width(), range_min=-2, range_max=2)
|
normalized_x = self.map_value_to_range(mouse_pos.x(), 0, value_max=self.width(), range_min=-self.gl_width, range_max=self.gl_width)
|
||||||
normalized_y = self.map_value_to_range(mouse_pos.y(), 0, value_max=self.height(), range_min=-1, range_max=1)
|
normalized_y = self.map_value_to_range(mouse_pos.y(), 0, value_max=self.height(), range_min=-self.gl_height, range_max=self.gl_height)
|
||||||
|
|
||||||
self.endPos = [normalized_x, -normalized_y]
|
self.endPos = [normalized_x * -self.zoom, -normalized_y * -self.zoom]
|
||||||
self.add_to_sketch(self.startPos)
|
self.add_to_sketch(self.startPos)
|
||||||
self.add_to_sketch(self.endPos)
|
self.add_to_sketch(self.endPos)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user