# coding: utf-8
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import sys
from ctypes import sizeof, c_float, c_void_p, c_uint, string_at
from shader import *
def loadImage(imageName = "beau.jpg" ):
"""Load an image from a file using PIL.
This is closer to what you really want to do than the
original port's crammed-together stuff that set global
state in the loading method. Note the process of binding
the texture to an ID then loading the texture into memory.
This didn't seem clear to me somehow in the tutorial.
"""
try:
from PIL.Image import open
except ImportError:
from Image import open
im = open(imageName)
try:
ix, iy, image = im.size[0], im.size[1], im.tobytes("raw", "RGBA", 0, -1)
except SystemError:
ix, iy, image = im.size[0], im.size[1], im.tobytes("raw", "RGBX", 0, -1)
# generate a texture ID
ID=glGenTextures(1)
# make it current
glBindTexture(GL_TEXTURE_2D, ID)
glPixelStorei(GL_UNPACK_ALIGNMENT,1)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
# copy the texture into the current texture ID
glTexImage2D(GL_TEXTURE_2D, 0, 3, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image)
print(ix,iy)
#glGenerateMipmap(GL_TEXTURE_2D)
# return the ID for use
return ID
myvertices = [
0.5, 0.5, 0.0,0.0,1.0, # top right
0.5, -0.5, 0.0,1.0,1.0, # bottom right
-0.5, 0.5, 0.0, 1.0,0.0, # top left
0.5, -0.5, 0.0,0.0,1.0, # bottom right
-0.5, -0.5, 0.0,0.0,0.0, # bottom left
-0.5, 0.5, 0.0,1.0,0.0 # top left
]
def mydraw():
# clear
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# view
#glMatrixMode(GL_MODELVIEW)
#glLoadIdentity()
buffers = glGenBuffers(1)
float_size = sizeof(c_float)
vertex_offset = c_void_p(0 * float_size)
text_offset=c_void_p(4 * float_size)
glBindBuffer(GL_ARRAY_BUFFER, buffers)
glBufferData(GL_ARRAY_BUFFER,
len(myvertices)*4, # byte size
(ctypes.c_float*len(myvertices))(*myvertices),
GL_STATIC_DRAW)
glVertexAttribPointer(0,3,GL_FLOAT, False, 5 * float_size, vertex_offset)
glEnableVertexAttribArray(0)
glVertexAttribPointer(1, 2, GL_FLOAT, False, 5 * float_size, text_offset);
glEnableVertexAttribArray(1);
global shader
if shader==None:
shader=Shader()
shader.initShader('''
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;
out vec2 TexCoord;
void main()
{
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
TexCoord = aTexCoord;
}
''',
'''
out vec4 FragColor;
in vec2 TexCoord;
uniform sampler2D ourTexture;
void main()
{
FragColor =texture(ourTexture, TexCoord);
}
''')
shader.begin()
glDrawArrays(GL_TRIANGLES, 0, 6)
shader.end()
glDisableVertexAttribArray(0)
glDisableVertexAttribArray(1)
def disp_func():
mydraw()
glutSwapBuffers()
if __name__=="__main__":
glutInit(sys.argv)
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
glutInitWindowSize(960, 960)
glutCreateWindow(b"vbo")
glEnable(GL_DEPTH_TEST)
loadImage("122.jpg")
glutDisplayFunc(disp_func)
glutIdleFunc(disp_func)
#glutReshapeFunc(reshape_func)
glutMainLoop()
以上为122.jpg原图,texture后的效果图