実現したいこと
このpython scriptを実行したい
発生している問題・分からないこと
segmentation faultのエラー出て実行できない
エラーメッセージ
error
1segmentation fault
該当のソースコード
python
1from OpenGL.GL import * 2from OpenGL.GLUT import * 3from OpenGL.GLU import * 4import math 5import numpy as np 6 7rot_y = 20.0 8rot_x = -60.0 9bgn_y = 0.0 10bgn_x = 0.0 11mouse_x = 0 12mouse_y = 0 13i = 0 14j = 0 15dt = 0 16hk = 15.0 17 18 19def initialize(): 20 # glClear(GL_COLOR_BUFFER_BIT) 21 glClearColor(1.0, 1.0, 1.0, 0.0) 22 23 glMatrixMode(GL_MODELVIEW) 24 glLoadIdentity() 25 gluLookAt(50.0, 50.0, 50.0, 26 0.0, 0.0, 0.0, 27 1.0, 1.0, 1.414) 28 29 glEnable(GL_DEPTH_TEST) 30 glEnable(GL_CULL_FACE) 31 glEnable(GL_BLEND) 32 # glEnable(GL_LIGHTING) 33 # glEnable(GL_LIGHT0) 34 # glEnable(GL_NORMALIZE) 35 # glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE) 36 # glEnable(GL_COLOR_MATERIAL) 37 38def displayall(): 39 h1 = 180 40 l1 = 0 41 h2 = 180 42 l2 = 0 43 44 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) 45 glPushMatrix() 46 glRotatef(rot_x, 1.0, 0.0, 0.0) 47 glRotatef(rot_y, 0.0, 1.0, 0.0) 48 49 # 軸と球を描く 50 glColor4f(1.0, 0.0, 0.0, 1) 51 glBegin(GL_LINES) 52 glVertex3f(0.0, 0.0, 0.0) 53 glVertex3f(100.0, 0.0, 0.0) 54 55 glVertex3f(0.0, 0.0, 0.0) 56 glVertex3f(0.0, 100.0, 0.0) 57 58 glVertex3f(0.0, 0.0, 0.0) 59 glVertex3f(0.0, 0.0, 100.0) 60 glEnd() 61 62 glColor3f(0.2,0.2,0.0) 63 glRotatef(-90,1,0,0) 64 glutSolidCone(2,30,10,10) 65 glRotatef(90,1,0,0) 66 glTranslatef(0,29,0) 67 glRotatef(90,0,1,0) 68 glutSolidCone(1,3,10,10) 69 glRotatef(-90,0,1,0) 70 glTranslatef(0,-29,0) 71 72 deg1 = np.zeros(360) 73 deg2 = np.zeros(360) 74 rad1 = np.zeros(360) 75 rad2 = np.zeros(360) 76 jugo = 15 * 3.1416 / 180 77 78 r = [[0 for _ in range(360)] for _ in range(360)] 79 s = [[0 for _ in range(360)] for _ in range(360)] 80 x = [[0 for _ in range(360)] for _ in range(360)] 81 y = [[0 for _ in range(360)] for _ in range(360)] 82 z = [[0 for _ in range(360)] for _ in range(360)] 83 xs = [[0 for _ in range(360)] for _ in range(360)] 84 ys = [[0 for _ in range(360)] for _ in range(360)] 85 zs = [[0 for _ in range(360)] for _ in range(360)] 86 cen = [[0 for _ in range(360)] for _ in range(360)] 87 scen = [[0 for _ in range(360)] for _ in range(360)] 88 xcen = [[0 for _ in range(360)] for _ in range(360)] 89 ycen = [[0 for _ in range(360)] for _ in range(360)] 90 zcen = [[0 for _ in range(360)] for _ in range(360)] 91 xscen = [[0 for _ in range(360)] for _ in range(360)] 92 yscen = [[0 for _ in range(360)] for _ in range(360)] 93 zscen = [[0 for _ in range(360)] for _ in range(360)] 94 95 for i in range(l1, h1): 96 deg1[i] = i 97 rad1[1] = deg1[i] * 3.1416 / 180 98 99 for j in range(l2, h2): 100 deg2[j] = j 101 rad2[j] = deg2[j] * 3.1416 / 180 102 103 for i in range(l1, h1): 104 for j in range(l2, h2): 105 r[i][j] = 0 106 s[i][j] = 0 107 cen[i][j] = 0 108 scen[i][j] = 0 109 110 r = np.zeros((121, 121)) 111 s = np.zeros((121, 121)) 112 113 r[0][0] =28 114 r[30][0] =35 115 r[60][0] =29 116 r[90][0] =35 117 r[120][0] =16 118 r[30][30] =22 119 r[60][30] =18 120 r[90][30] =26 121 r[120][30] =27 122 r[30][60] =27 123 r[60][60] =11 124 r[90][60] =17 125 r[30][90] =6 126 r[60][90] =-27 127 128 s[0][0] =-33 129 s[30][0] =-54 130 s[60][0] =-53 131 s[90][0] =-64 132 s[120][0] =-57 133 s[30][30] =-60 134 s[60][30] =-64 135 s[90][30] =-62 136 s[120][30] =-28 137 s[30][60] =-93 138 s[60][60] =-37 139 s[90][60] =-14 140 s[30][90] =-111 141 s[60][90] =-93 142 143 for i in range(l1, h1): 144 for j in range(l2, h2): 145 r[i][j] = 0 146 s[i][j] = 0 147 cen[i][j] = r[i][j] = 20 + hk 148 scen[i][j] = r[i][j] = 20 + hk 149 150 for i in range(l1, h1, 30): 151 if i + 30 < h1 and (r[i][j] != hk and r[i+30][j] != hk): 152 for j in range(l2, h2, 30): 153 if j + 30 < h2 and (r[i][j] != hk and r[i][j+30] != hk): 154 cen[i][j] = (r[i][j] + r[i+30][j] + r[i][j+30] + r[i+30][j+30]) / 4 155 scen[i][j] = (s[i][j] + s[i+30][j] + s[i][j+30] + s[i+30][j+30]) / 4 156 157 for i in range(l1, h1): 158 for j in range(l2, h2): 159 y[i][j] = r[i][j] * np.cos(rad1[i]) 160 x[i][j] = r[i][j] * np.sin(rad1[i]) * np.cos(rad2[j]) 161 z[i][j] = r[i][j] * np.sin(rad1[i]) * np.sin(rad2[j]) 162 163 ys[i][j] = s[i][j] * np.cos(rad1[i]) 164 xs[i][j] = s[i][j] * np.sin(rad1[i]) * np.cos(rad2[j]) 165 zs[i][j] = s[i][j] * np.sin(rad1[i]) * np.sin(rad2[j]) 166 ycen[i][j] = cen[i][j] * np.cos(rad1[i] + jugo) 167 xcen[i][j] = cen[i][j] * np.sin(rad1[i] + jugo) * np.cos(rad2[j] + jugo) 168 zcen[i][j] = cen[i][j] * np.sin(rad1[i] + jugo) * np.sin(rad2[j] + jugo) 169 yscen[i][j] = scen[i][j] * np.cos(rad1[i] + jugo) 170 xscen[i][j] = scen[i][j] * np.sin(rad1[i] + jugo) * np.cos(rad2[j] + jugo) 171 zscen[i][j] = scen[i][j] * np.sin(rad1[i] + jugo) * np.sin(rad2[j] + jugo) 172 173 glColor3f(1.0, 1.0, 0.0) 174 for i in range(h1): 175 glBegin(GL_QUAD_STRIP) 176 for j in range(h2): 177 if r[i][j] != hk: 178 glVertex3f(x[i][j], y[i][j], z[i][j]) 179 glVertex3f(xs[i][j], ys[i][j], zs[i][j]) 180 glEnd() 181 182 glBegin(GL_QUAD_STRIP) 183 for j in range(h2): 184 if r[i][j] != hk: 185 glVertex3f(xs[i][j], ys[i][j], zs[i][j]) 186 glVertex3f(x[i][j], y[i][j], z[i][j]) 187 glEnd() 188 189 for j in range(h2): 190 glBegin(GL_QUAD_STRIP) 191 for i in range(h1): 192 if r[i][j] != hk: 193 glVertex3f(x[i][j], y[i][j], z[i][j]) 194 glVertex3f(xs[i][j], ys[i][j], zs[i][j]) 195 glEnd() 196 197 glBegin(GL_QUAD_STRIP) 198 for i in range(h1): 199 if r[i][j] != hk: 200 glVertex3f(xs[i][j], ys[i][j], zs[i][j]) 201 glVertex3f(x[i][j], y[i][j], z[i][j]) 202 glEnd() 203 204 glColor3f(0.2, 0.2, 0.2) 205 206 glBegin(GL_TRIANGLES) 207 glVertex3f(x[0][0], y[0][0], z[0][0]) 208 glVertex3f(x[30][0], y[30][0], z[30][0]) 209 glVertex3f(x[30][30], y[30][30], z[30][30]) 210 glVertex3f(x[0][0], y[0][0], z[0][0]) 211 glVertex3f(x[30][30], y[30][30], z[30][30]) 212 glVertex3f(x[30][0], y[30][0], z[30][0]) 213 214 glVertex3f(x[0][0], y[0][0], z[0][0]) 215 glVertex3f(x[30][30], y[30][30], z[30][30]) 216 glVertex3f(x[30][60], y[30][60], z[30][60]) 217 glVertex3f(x[0][0], y[0][0], z[0][0]) 218 glVertex3f(x[30][60], y[30][60], z[30][60]) 219 glVertex3f(x[30][30], y[30][30], z[30][30]) 220 221 glVertex3f(x[0][0], y[0][0], z[0][0]) 222 glVertex3f(x[30][60], y[30][60], z[30][60]) 223 glVertex3f(x[30][90], y[30][90], z[30][90]) 224 glVertex3f(x[0][0], y[0][0], z[0][0]) 225 glVertex3f(x[30][90], y[30][90], z[30][90]) 226 glVertex3f(x[30][60], y[30][60], z[30][60]) 227 glEnd() 228 229 for i in range(0, h1, 30): 230 for j in range(0, h2, 30): 231 if r[i][j] != hk and r[i + 30][j] != hk and r[i][j + 30] != hk and r[i + 30][j + 30] != hk: 232 glBegin(GL_TRIANGLE_STRIP) 233 glVertex3f(x[i][j], y[i][j], z[i][j]) 234 glVertex3f(x[i][j + 30], y[i][j + 30], z[i][j + 30]) 235 glVertex3f(xcen[i][j], ycen[i][j], zcen[i][j]) 236 glVertex3f(x[i + 30][j + 30], y[i + 30][j + 30], z[i + 30][j + 30]) 237 glVertex3f(x[i + 30][j], y[i + 30][j], z[i + 30][j]) 238 glVertex3f(xcen[i][j], ycen[i][j], zcen[i][j]) 239 glVertex3f(x[i][j], y[i][j], z[i][j]) 240 glEnd() 241 242 glBegin(GL_TRIANGLE_STRIP) 243 glVertex3f(x[i][j], y[i][j], z[i][j]) 244 glVertex3f(xcen[i][j], ycen[i][j], zcen[i][j]) 245 glVertex3f(x[i + 30][j], y[i + 30][j], z[i + 30][j]) 246 glVertex3f(x[i + 30][j + 30], y[i + 30][j + 30], z[i + 30][j + 30]) 247 glVertex3f(xcen[i][j], ycen[i][j], zcen[i][j]) 248 glVertex3f(x[i][j + 30], y[i][j + 30], z[i][j + 30]) 249 glVertex3f(x[i][j], y[i][j], z[i][j]) 250 glEnd() 251 252 glColor3f(1.0, 1.0, 1.0) 253 254 glBegin(GL_LINES) 255 glVertex3f(x[0][0], y[0][0], z[0][0]) 256 glVertex3f(x[30][0], y[30][0], z[30][0]) 257 glVertex3f(x[0][0], y[0][0], z[0][0]) 258 glVertex3f(x[30][30], y[30][30], z[30][30]) 259 glVertex3f(x[0][0], y[0][0], z[0][0]) 260 glVertex3f(x[30][60], y[30][60], z[30][60]) 261 glVertex3f(x[0][0], y[0][0], z[0][0]) 262 glVertex3f(x[30][90], y[30][90], z[30][90]) 263 glEnd() 264 265 ami = 1 266 for i in range(0, h1, ami): 267 glBegin(GL_LINE_STRIP) 268 for j in range(0, h2, ami): 269 if r[i][j] != hk: 270 glVertex3f(x[i][j], y[i][j], z[i][j]) 271 glEnd() 272 273 for j in range(0, h2, ami): 274 glBegin(GL_LINE_STRIP) 275 for i in range(0, h1, ami): 276 if r[i][j] != hk: 277 glVertex3f(x[i][j], y[i][j], z[i][j]) 278 glEnd() 279 280 glColor3f(0.8, 0.8, 0.2) 281 282
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
メモリ関係と考えpip install PyOpenGL_accelerateをしようとしたのですがなぜかインストールできません。(PyOpenGLはインストールできています)
補足
特になし
コードの続き
glBegin(GL_LINES)
glVertex3f(xs[0][0], ys[0][0], zs[0][0])
glVertex3f(xs[30][0], ys[30][0], zs[30][0])
glVertex3f(xs[0][0], ys[0][0], zs[0][0])
glVertex3f(xs[30][30], ys[30][30], zs[30][30])
glVertex3f(xs[0][0], ys[0][0], zs[0][0])
glVertex3f(xs[30][60], ys[30][60], zs[30][60])
glVertex3f(xs[0][0], ys[0][0], zs[0][0])
glVertex3f(xs[30][90], ys[30][90], zs[30][90])
glEnd()
for i in range(h1):
glBegin(GL_LINE_STRIP)
for j in range(h2):
if s[i][j] != hk:
glVertex3f(xs[i][j], ys[i][j], zs[i][j])
glEnd()
for j in range(h2):
glBegin(GL_LINE_STRIP)
for i in range(h1):
if s[i][j] != hk:
glVertex3f(xs[i][j], ys[i][j], zs[i][j])
glEnd()
glColor3f(0.5, 0.5, 0.8)
glBegin(GL_LINES)
for i in range(h1):
for j in range(h2):
if r[i][j] != hk:
glVertex3f(x[i][j], y[i][j], z[i][j])
glVertex3f(xs[i][j], ys[i][j], zs[i][j])
glEnd()
glLineWidth(1)
glColor4f(0.8, 0.8, 0.8, 0.6)
glutSolidSphere(14.9, 18, 18)
glRotatef(90, 1.0, 0.0, 0.0)
glColor4f(0.0, 0.8, 0.8, 1.0)
glutWireSphere(15.0, 18, 18)
glColor4f(0.8, 0.9, 0.8, 1.0)
glutWireSphere(hk + 4.5, 12, 12)
glColor4f(0.8, 0.8, 0.9, 1.0)
glutWireSphere(hk - 4.5, 12, 12)
glRotatef(-90, 1.0, 0.0, 1.0)
glPopMatrix()
glutSwapBuffers()
if __name__ == "__main__":
displayall()
https://teratail.com/help/question-tips#questionTips11
https://teratail.com/help/question-tips#questionTips12
https://teratail.com/help/question-tips#questionTips2
https://teratail.com/help/question-tips#questionTips34
https://teratail.com/help/question-tips
は読みましたか?
読んだ結果を本文に反映ください。解決のヒントを探れるかもしれません。
pythonは使ったことはないのですがsegmentationfaultはそれだけで何のエラーをあらわすかわからないので質問がよく分からないことになっています。といったけどメモリかんけいらしいですね。いちおう。実行したいなら一文一文や部品ごとに実行する必要があるのではないでしょうか。(実行がうまくいったからあってるかは知りませんが。念のため。)間違えてたら申し訳ありませんが蛇足です。書き直してあってるあってるとなってしまい内容が薄くなってしまいました。glBegin(GL_TRIANGLE)のVertex3fの頂点は3つ書いたら三角形のポリゴンでそれ以上はTRIANGLEで存在するんでしょうか。3つのglvertexでglendで閉じるべきでは。
すいません。そちらの考えで、あっているようです。連続して書ける
glpushmatrix()は自身が成長しそうなので書きません。蛇足で間違いがあったら教えてください。コメントなので直します。
pure な python が segmentation fault を起こすことはレアなので、おそらく OpenGL のどこかで起こっているんじゃないかと推測します。関数の呼び出し方がおかしいのか、もしかするとライブラリのバグなのではないかと。
とりあえず、プログラムの行を削ってみるとか print() を挟むとかして、どこで発生しているかを調べるといいと思います。
![guest](/img/icon/icnUserSample.jpg)
あなたの回答
tips
プレビュー