-
-
Save Yangff/8144486 to your computer and use it in GitHub Desktop.
where is wrong..
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 3 | |
0 0 10 | |
1 0 8 | |
2 0 9 | |
0 2 10 | |
1 2 10 | |
2 2 10 | |
0 3 9 | |
1 3 9 | |
2 5 11 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import OpenGL | |
import sys | |
import os | |
import ctypes | |
import math | |
from OpenGL.GL import * | |
from OpenGL.GLU import * | |
from OpenGL.GLUT import * | |
from ctypes import * | |
rate = 1 | |
PlayerX = 0.0 | |
PlayerY = 0.0 | |
PlayerZ = 0.0 | |
PlayerPitch = 0.0 | |
PlayerYaw = 0.0 | |
def RenderScene(): | |
global rate, PlayerX, PlayerY, PlayerZ, PlayerPitch, PlayerYaw, trangleBuffer,aVertexPosition, vecCount, ymax1, uColor; | |
glViewport(0,0,800,600); | |
#~ print(PlayerX, PlayerY, PlayerZ, PlayerYaw, PlayerPitch) | |
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT) | |
glPushMatrix(); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity() | |
gluPerspective(math.pi / 4.0, 800.00 / 600.00, 0.01, 100); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity() | |
glScale(rate,rate,rate); | |
glRotate(-PlayerPitch,1,0,0); | |
glRotate(-PlayerYaw,0,1,0); | |
glTranslate(-PlayerX, -PlayerY, -PlayerZ) | |
glUniform1f(uColor, *[zmax1]) | |
glBindBuffer(GL_ARRAY_BUFFER, trangleBuffer) | |
glVertexAttribPointer(aVertexPosition, 3, GL_FLOAT, GL_FALSE, 0, [trangleBuffer]) | |
glDrawArrays(GL_TRIANGLES, 0, vecCount) | |
glPopMatrix(); | |
glutSwapBuffers() | |
def Keyboard(key, x, y): | |
global PlayerX,PlayerY,PlayerZ,PlayerYaw,PlayerPitch | |
# camera rotate | |
if (key == 101): | |
# up | |
PlayerPitch += 0.1; | |
if (key == 103): | |
# down | |
PlayerPitch -= 0.1; | |
if (key == 100): | |
# left | |
PlayerYaw += 0.1; | |
if (key == 102): | |
#right | |
PlayerYaw -= 0.1; | |
print(key); | |
glutPostRedisplay() | |
def myCompileShader(source, type): | |
shader = glCreateShader(type) | |
glShaderSource(shader, source) | |
glCompileShader(shader) | |
return shader | |
glutInit(sys.argv); | |
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH); | |
glutInitWindowSize(800,600); | |
glutCreateWindow(b'Show'); | |
glutDisplayFunc(RenderScene); | |
glutKeyboardFunc(Keyboard); | |
glutSpecialFunc(Keyboard); | |
glEnable(GL_DEPTH_TEST) | |
glClearColor(0.0,0.0,0.0,1.0); | |
# init G | |
n = m = 0 | |
ary = [] | |
f = open("G.in", "r"); | |
xmax = 0 | |
xmin = 1000000 | |
ymax = 0 | |
ymin = 1000000 | |
zmax = 0 | |
zmin = 1000000 | |
for l in f : | |
l = l.split(); | |
if (len(l) == 2): | |
n = int(l[0]); | |
m = int(l[1]); | |
else: | |
ary.append(float(l[0])) #x | |
xmax = max([float(l[0]), xmax]) | |
xmin = min([float(l[0]), xmin]) | |
ary.append(float(l[1])) #y | |
ymax = max([float(l[1]), zmax]) | |
ymin = min([float(l[1]), zmin]) | |
ary.append(float(l[2])) #z | |
zmax = max([float(l[2]), ymax]) | |
zmin = min([float(l[2]), ymin]) | |
zmax1 = 0 | |
for i in range(0, len(ary)): | |
if (i % 3 == 2): | |
ary[i] = zmax - ary[i] | |
zmax1 = max([ary[i], zmax1]) | |
buf = [] | |
PlayerX = (xmax + xmin) / 2.0 | |
PlayerY = (ymax + ymin) / 2.0 | |
PlayerZ = (zmax1 + 5) | |
print(PlayerX, PlayerY, PlayerZ) | |
vecCount = 0 | |
for l in range(0,(m * (n - 1))): | |
if ((l + 1) % m == 0): | |
continue; | |
buf.append(ary[l * 3]) | |
buf.append(ary[l * 3 + 1]) | |
buf.append(ary[l * 3 + 2]) | |
buf.append(ary[(l + 1) * 3]) | |
buf.append(ary[(l + 1) * 3 + 1]) | |
buf.append(ary[(l + 1) * 3 + 2]) | |
buf.append(ary[(l + m) * 3]) | |
buf.append(ary[(l + m) * 3 + 1]) | |
buf.append(ary[(l + m) * 3 + 2]) | |
buf.append(ary[(l + 1) * 3]) | |
buf.append(ary[(l + 1) * 3 + 1]) | |
buf.append(ary[(l + 1) * 3 + 2]) | |
buf.append(ary[(l + m) * 3]) | |
buf.append(ary[(l + m) * 3 + 1]) | |
buf.append(ary[(l + m) * 3 + 2]) | |
buf.append(ary[(l + m + 1) * 3]) | |
buf.append(ary[(l + m + 1) * 3 + 1]) | |
buf.append(ary[(l + m + 1) * 3 + 2]) | |
vecCount+=6 | |
trangleBuffer = glGenBuffers(1); | |
glBindBuffer(GL_ARRAY_BUFFER, trangleBuffer); | |
glBufferData(GL_ARRAY_BUFFER, (c_float*len(buf))(*buf), GL_STATIC_DRAW) | |
#shader | |
VERTEX_SHADER = myCompileShader(""" | |
attribute vec3 aVertexPosition; | |
uniform float uColor; | |
varying vec4 vColor; | |
void main(void) { | |
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * vec4(aVertexPosition, 1.0); | |
vColor = vec4(1.0,1.0,1.0,1.0); | |
// vColor = vec4(aVertexPosition.y / uColor, aVertexPosition.y / uColor, aVertexPosition.y / uColor, 1.0); | |
} | |
""", GL_VERTEX_SHADER) | |
print(glGetShaderInfoLog(VERTEX_SHADER)) | |
FRAGMENT_SHADER = myCompileShader(""" | |
varying vec4 vColor; | |
void main(void) { | |
gl_FragColor = vColor; | |
} | |
""", GL_FRAGMENT_SHADER) | |
print(glGetShaderInfoLog(FRAGMENT_SHADER)) | |
PROGRAM = glCreateProgram() | |
glAttachShader(PROGRAM, VERTEX_SHADER) | |
glAttachShader(PROGRAM, FRAGMENT_SHADER) | |
glLinkProgram(PROGRAM) | |
glUseProgram(PROGRAM) | |
aVertexPosition = glGetAttribLocation(PROGRAM, b"aVertexPosition") | |
glEnableVertexAttribArray(aVertexPosition) | |
uColor = glGetUniformLocation(PROGRAM, b"uColor") | |
# matrix | |
glutMainLoop(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment