Python之OpenGL笔记(21):描点法画圆

一、目的

1、描点法画圆;

二、程序运行结果

Python之OpenGL笔记(21):描点法画圆_第1张图片

三、说明

   描点法画圆函数drawCircle( x0,y0,R)
   圆心坐标x0,y0 ;半径R
   等分角A=2π/等分数;则圆上任一点坐标 x=x0+RcosA;y=y0+RsinA;z=0
   将圆上的每个点坐标存入数组返回。

四、glDrawArrays方法

   在OpenGl中所有的图形都是通过分解成三角形的方式进行绘制。
   绘制图形通过GL20类中的glDrawArrays方法实现,
   该方法原型: glDrawArrays(int mode, int first,int count)
   参数1:有7种取值
    1.GL_POINTS 画点
    2.GL_LINES 画线段,每一对顶点被解释为一条直线;
    3.GL_LINE_LOOP 用给出的点绘制为一个环(所有的点首尾相接)。
    4.GL_LINE_STRIP 一系列的连续直线;
    5.GL_TRIANGLES:每三个顶之间绘制三角形,之间不连接
    6.GL_TRIANGLE_STRIP 顺序在每三个顶点之间均绘制三角形。这个方法可以保证从相同的方向上所有三角形均被绘制。以V0V1V2,V1V2V3,V2V3V4……的形式绘制三角形;
    7.GL_TRIANGLE_FAN:以V0V1V2,V0V2V3,V0V3V4,……的形式绘制三角形
   参数2:从数组缓存中的哪一位开始绘制,一般都定义为0
   参数3:顶点的数量

五、源代码

"""
dalong2020_2.py
Author: dalong10
Description: Draw a Triagle, 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);
	}
"""

strFS1 = """
#version 330 core
out vec4 color;
void main(){
	color = vec4(1.0, 0.0, 0.0, 1.0);
	}
"""

strFS2 = """
#version 330 core
out vec4 color;
void main(){
	color = vec4(1.0, 1.0, 0.0, 1.0);
	}
"""

class FirstCircle:
    def __init__(self, myFS,myvertices):
        self.myFS = myFS
        self.vertices = myvertices

        # load shaders
        if myFS>0:
            strFS=strFS1
        else:
            strFS=strFS2	
        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
        if myFS>0:
            glDrawArrays(GL_LINE_LOOP, 0, 60 )
        else:
            glDrawArrays(GL_LINES, 0, self.vertices.size )
        # unbind VAO
        glBindVertexArray(0)

def drawCircle( x0,y0,R):
    PI = 3.14159265358979323846264
    statcky=60  #分成60份
    angleHy =  (2*PI)/statcky
    NumAngleHy = 0.0 # 当前横向角度
    d=numpy.array([], numpy.float32)
    for i in range(statcky):    #描点法画圆
        NumAngleHy = angleHy*i  # 
        x=R*np.cos(NumAngleHy)
        y=R*np.sin(NumAngleHy)
        d=np.hstack((d,numpy.array([x0+x,y0+y,0], numpy.float32) ))
    return numpy.array(d, numpy.float32)

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, "fwherr001", 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 ))

    
    Circle1=drawCircle(0.0,0.0,0.1)
    Circle2=drawCircle(0.3,0.4,0.2)
    firstTriangle0 = FirstCircle(0,c)
    firstTriangle1 = FirstCircle(0,Circle1)
    firstTriangle2 = FirstCircle(1,Circle2)
     
    # 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)
        # render
        firstTriangle0.render(0)
        firstTriangle1.render(1)
        firstTriangle2.render(1)

        # Swap front and back buffers
        glfw.swap_buffers(window)

        # Poll for and process events
        glfw.poll_events()

    glfw.terminate()

六、参考文献

1、learnopengl教程https://learnopengl-cn.readthedocs.io/zh/latest/02%20Lighting/04%20Lighting%20maps/
2、大龙10简书 https://www.jianshu.com/p/4382b25ad797

你可能感兴趣的:(Python,OpenGL)