python画平面直角坐标系_Python之OpenGL笔记(20):画平面直角坐标系
一、目的
1、畫平面直角坐標系;
二、程序運行結(jié)果
平面直角坐標系
三、numpy.hstack()函數(shù)
1、函數(shù)
函數(shù)原型:numpy.hstack(tup),其中tup是arrays序列,陣列必須具有相同的形狀,除了對應(yīng)于軸的維度(默認情況下,第一個)。
等價于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 函數(shù)
1、函數(shù)原型:
GL_APICALL void GL_APIENTRY glDrawArrays (GLenum mode, GLint first, GLsizei count);
提供繪制功能。當采用頂點數(shù)組方式繪制圖形時,使用該函數(shù)。該函數(shù)根據(jù)頂點數(shù)組中的坐標數(shù)據(jù)和指定的模式,進行繪制。
相似功能的函數(shù)是 glDrawElements。
2、參數(shù)說明:
mode,繪制方式,OpenGL2.0以后提供以下參數(shù):GL_POINTS、GL_LINES、GL_LINE_LOOP、GL_LINE_STRIP、GL_TRIANGLES、GL_TRIANGLE_STRIP、GL_TRIANGLE_FAN。
first,從數(shù)組緩存中的哪一位開始繪制,一般為0。
count,數(shù)組中頂點的數(shù)量。
五、源代碼
"""
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()
超強干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的python画平面直角坐标系_Python之OpenGL笔记(20):画平面直角坐标系的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 无法想mysql进行插入_mysql 无
- 下一篇: python输出奇数数字序位_pytho