我已经用python和OpenGL开发了一个项目。我以前也发布过类似的问题,但后来我做了更多的研究,转而使用非弃用的函数。在this tutorial之后(显然将其转换为Python版本),我得到了以下代码:
import sys
import OpenGL
from OpenGL.GL import *
from OpenGL.GL.shaders import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from OpenGL.GLUT.freeglut import *
from OpenGL.arrays import vbo
import pygame
import Image
import numpy
class AClass:
def __init__(self):
self.Splash = True #There's actually more here, but it's impertinent
def TexFromPNG(self, filename):
img = Image.open(filename) # .jpg, .bmp, etc. also work
img_data = numpy.array(list(img.getdata()), 'B')
texture = glGenTextures(1)
glPixelStorei(GL_UNPACK_ALIGNMENT,1)
glBindTexture(GL_TEXTURE_2D, texture)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.size[0], img.size[1], 0, GL_RGB, GL_UNSIGNED_BYTE, img_data)
return texture
def MakeBuffer(self, target, data, size):
TempBuffer = glGenBuffers(1)
glBindBuffer(target, TempBuffer)
glBufferData(target, size, data, GL_STATIC_DRAW)
return TempBuffer
def ReadFile(self, filename):
tempfile = open(filename,'r')
source = tempfile.read()
temprfile.close()
return source
def run(self):
glutInitDisplayMode(GLUT_RGBA)
glutInitWindowSize(256,244)
self.window = glutCreateWindow("test")
glutReshapeFunc(self.reshape)
glutDisplayFunc(self.draw)
glutKeyboardFunc(self.keypress)
self.MainTex = glGenTextures(1)
self.SplashTex = self.TexFromPNG("Resources/Splash.png")
MainVertexData = numpy.array([-1,-1,1,-1,-1,1,1,1],numpy.float32)
FullWindowVertices = numpy.array([0,1,2,3],numpy.ushort)
self.MainVertexData = self.MakeBuffer(GL_ARRAY_BUFFER,MainVertexData,len(MainVertexData))
self.FullWindowVertices = self.MakeBuffer(GL_ELEMENT_ARRAY_BUFFER,FullWindowVertices,len(FullWindowVertices))
self.BaseProgram = compileProgram(compileShader(self.ReadFile("Shaders/Mainv.glsl"),
GL_VERTEX_SHADER),
compileShader(self.ReadFile("Shaders/Mainf.glsl"),
GL_FRAGMENT_SHADER))
glutMainLoop()
def reshape(self, width, height):
self.width = width
self.height = height
glutPostRedisplay()
def draw(self):
glViewport(0, 0, self.width, self.height)
glClearDepth(1)
glClearColor(0,0,0,0)
glClear(GL_COLOR_BUFFER_BIT)
glEnable(GL_TEXTURE_2D)
if self.Splash:
glUseProgram(self.BaseProgram)
pos = glGetAttribLocation(self.BaseProgram, "position")
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, self.SplashTex)
glUniform1i(glGetUniformLocation(self.BaseProgram,"texture"), 0)
glBindBuffer(GL_ARRAY_BUFFER,self.MainVertexData)
glVertexAttribPointer(pos,
2,
GL_FLOAT,
GL_FALSE,
0,
numpy.array([0],numpy.uint8))
glEnableVertexAttribArray(pos)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,self.FullWindowVertices)
glDrawElements(GL_TRIANGLE_STRIP,
4,
GL_UNSIGNED_SHORT,
numpy.array([0],numpy.uint8))
glDisableVertexAttribArray(pos)
else:
glBindTexture(GL_TEXTURE_2D, self.MainTex)
glutSwapBuffers()
glutInit(sys.argv)
test = AClass()
test.run()
Shaders/Mainv.glsl和Shaders/Mainf.glsl包含:
#version 110
attribute vec2 position;
varying vec2 texcoord;
void main()
{
gl_Position = vec4(position, 0.0, 1.0);
texcoord = position * vec2(0.5) + vec2(0.5);
}
以及:
#version 110
uniform sampler2D texture;
varying vec2 texcoord;
void main()
{
gl_FragColor = texture2D(texture, texcoord);
}
分别是。
这段代码为我设置了一个(清晰的颜色)过剩窗口,这似乎表明它并没有出于某种原因渲染我的三角形,但我不知道为什么会这样,我也找不到PyOpenGL的任何不使用不推荐的函数的示例,所以我看不出它们是否做了与我不同的我缺少的东西。
最佳答案
我设法让你的代码工作后修复下列事项:
glBufferData以字节(而不是元素)表示数据的大小:顶点数据(float)需要乘以4,索引数据(short ints)需要乘以2
如果使用VBOs(您是VBOs),glVertexAttribPointer的最后一个参数是当前绑定到数组缓冲区的目标的偏移量;在您的情况下,它应该是一个空指针,即使用pyOpenGL时没有
gldrawerElements的行为方式相同:最后一个参数是元素数组缓冲区的偏移量,在本例中为None
我的测试图像在那之后显示,尽管颜色混乱。我没有尝试调试它,因为用于解码png文件的选项可能特定于您的数据:)
关于python - PyOpenGL:渲染...好吧...真正的一切,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5973710/