日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python画平面直角坐标系_Python之OpenGL笔记(20):画平面直角坐标系

發布時間:2024/4/11 python 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python画平面直角坐标系_Python之OpenGL笔记(20):画平面直角坐标系 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、目的

1、畫平面直角坐標系;

二、程序運行結果

平面直角坐標系

三、numpy.hstack()函數

1、函數

函數原型:numpy.hstack(tup),其中tup是arrays序列,陣列必須具有相同的形狀,除了對應于軸的維度(默認情況下,第一個)。

等價于np.concatenate(tup,axis=1)

2、例1

>>> import numpy as np

>>> a=np.array((1,2,3))

>>> b=np.array((4,5,6))

>>> np.hstack((a,b))

array([1, 2, 3, 4, 5, 6])

3、例2

>>> a=np.array(([1],[2],[3]))

>>> b=np.array(([4],[5],[6]))

>>> np.hstack((a,b))

array([[1, 4],

[2, 5],

[3, 6]])

四、glDrawArrays 函數

1、函數原型:

GL_APICALL void GL_APIENTRY glDrawArrays (GLenum mode, GLint first, GLsizei count);

提供繪制功能。當采用頂點數組方式繪制圖形時,使用該函數。該函數根據頂點數組中的坐標數據和指定的模式,進行繪制。

相似功能的函數是 glDrawElements。

2、參數說明:

mode,繪制方式,OpenGL2.0以后提供以下參數:GL_POINTS、GL_LINES、GL_LINE_LOOP、GL_LINE_STRIP、GL_TRIANGLES、GL_TRIANGLE_STRIP、GL_TRIANGLE_FAN。

first,從數組緩存中的哪一位開始繪制,一般為0。

count,數組中頂點的數量。

五、源代碼

"""

dalong2020_1.py

Author: dalong10

Description: Draw a Rectangular Coordinates, learning OPENGL

"""

import glutils #Common OpenGL utilities,see glutils.py,見 之二

import sys, random, math

import OpenGL

from OpenGL.GL import *

from OpenGL.GL.shaders import *

import numpy

import numpy as np

import glfw

strVS = """

#version 330 core

layout(location = 0) in vec3 position;

void main(){

gl_Position = vec4(position.x, position.y, position.z, 1.0);

}

"""

strFS = """

#version 330 core

out vec4 color;

void main(){

color = vec4(0.0, 1.0, 0.0, 1.0);

}

"""

class FirstCircle:

def __init__(self, myvertices):

self.vertices = myvertices

# load shaders

self.program = glutils.loadShaders(strVS, strFS)

glUseProgram(self.program)

vertices = myvertices

# set up vertex array object (VAO)

self.vao = glGenVertexArrays(1)

glBindVertexArray(self.vao)

# set up VBOs

vertexData = numpy.array(vertices, numpy.float32)

self.vertexBuffer = glGenBuffers(1)

glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer)

glBufferData(GL_ARRAY_BUFFER, 4*len(vertexData), vertexData,

GL_STATIC_DRAW)

#enable arrays

self.vertIndex = 0

glEnableVertexAttribArray(self.vertIndex)

# set buffers

glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer)

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None)

# unbind VAO

glBindVertexArray(0)

def render(self, myFS):

# use shader

glUseProgram(self.program)

# bind VAO

glBindVertexArray(self.vao)

# draw

glDrawArrays(GL_LINES, 0, self.vertices.size )

# unbind VAO

glBindVertexArray(0)

if __name__ == '__main__':

import sys

import glfw

import OpenGL.GL as gl

def on_key(window, key, scancode, action, mods):

if key == glfw.KEY_ESCAPE and action == glfw.PRESS:

glfw.set_window_should_close(window,1)

# Initialize the library

if not glfw.init():

sys.exit()

# Create a windowed mode window and its OpenGL context

window = glfw.create_window(400, 400, "Rectangular Coordinates", None, None)

if not window:

glfw.terminate()

sys.exit()

# Make the window's context current

glfw.make_context_current(window)

# Install a key handler

glfw.set_key_callback(window, on_key)

t=0.9

c=numpy.array([-t,0,0, t,0,0 , 0,t,0 , 0, -t, 0] , numpy.float32) #X,Y軸

for i in range(19): #X軸畫線

x=float(t*(-9.0+i)/10)

c_i=numpy.array([x,0,0,x,0.02,0], numpy.float32)

c=np.hstack((c,c_i ))

c_i=numpy.array([t,0,0,t-0.02,0.02,0 , t,0,0,t-0.02,-0.02,0 ], numpy.float32)

c=np.hstack((c,c_i ))

for i in range(19): #Y軸畫線

y=float(t*(-9.0+i)/10)

c_i=numpy.array([0,y,0,0.02,y,0], numpy.float32)

c=np.hstack((c,c_i ))

c_i=numpy.array([0,t,0,0.02,t-0.02,0 ,0, t,0,-0.02,t-0.02,0 ], numpy.float32)

c=np.hstack((c,c_i ))

# Loop until the user closes the window

while not glfw.window_should_close(window):

# Render here

width, height = glfw.get_framebuffer_size(window)

ratio = width / float(height)

gl.glViewport(0, 0, width, height)

gl.glClear(gl.GL_COLOR_BUFFER_BIT)

gl.glClearColor(0.0,0.0,4.0,0.0)

firstCircle0 = FirstCircle(c)

# render

firstCircle0.render(0)

# Swap front and back buffers

glfw.swap_buffers(window)

# Poll for and process events

glfw.poll_events()

glfw.terminate()

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的python画平面直角坐标系_Python之OpenGL笔记(20):画平面直角坐标系的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。