一、 目的
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()