pyopengl纹理,带有纹理的PyOpenGL球体

I want to use Python PyOpenGL for generating a scene of three spheres. Two on the side with color (red and green). Middle one with any texture on it (bricks texture which is actually square jpg file in the same directory as code is).

There is what I have done so far:

g0Oa7.png

From some reason texture in the middle sphere generated by gluSphere() has strange behaviour. There are some small green triangles instead of my texture file.

This is my texture file:

lLTEB.jpg

I have tried to map this file as surface, e.g. texture on the middle sphere. I don't know where the problem is.

There is my code, I have no idea what I miss or what is wrong to get proper texture mapping on middle sphere:

from OpenGL.GLUT import *

from OpenGL.GLU import *

from OpenGL.GL import *

import sys

from PIL import Image as Image

import numpy

name = 'Navigation paradigm'

def main():

glutInit(sys.argv)

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)

glutInitWindowSize(800, 800)

glutInitWindowPosition(350, 200)

glutCreateWindow(name)

glClearColor(0., 0., 0., 1.)

glShadeModel(GL_SMOOTH)

glEnable(GL_CULL_FACE)

glEnable(GL_DEPTH_TEST)

glEnable(GL_LIGHTING)

lightZeroPosition = [10., 4., 10., 1.]

lightZeroColor = [0.8, 1.0, 0.8, 1.0]

glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition)

glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor)

glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1)

glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05)

glEnable(GL_LIGHT0)

glutDisplayFunc(display_scene)

glMatrixMode(GL_PROJECTION)

gluPerspective(40., 1., 1., 40.)

glMatrixMode(GL_MODELVIEW)

gluLookAt(0, 0, 10,

0, 0, 0,

0, 1, 0)

glPushMatrix()

glutMainLoop()

return

def display_scene():

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

glPushMatrix()

# Textured thing

tex = read_texture('brick.jpg')

qobj = gluNewQuadric()

gluQuadricTexture(qobj, GL_TRUE)

glEnable(GL_TEXTURE_2D)

glBindTexture(GL_TEXTURE_2D, tex)

glBegin(GL_TRIANGLES)

gluSphere(qobj, 1, 50, 50)

gluDeleteQuadric(qobj)

glDisable(GL_TEXTURE_2D)

# Left sphere

color = [1.0, 0.0, 0.0, 1.0]

glMaterialfv(GL_FRONT, GL_DIFFUSE, color)

glTranslatef(-2, 0, 0)

glutSolidSphere(1, 100, 20)

# Right sphere

color = [0.0, 1.0, 0.0, 1.0]

glMaterialfv(GL_FRONT, GL_DIFFUSE, color)

glTranslatef(4, 0, 0)

glutSolidSphere(1, 100, 20)

glPopMatrix()

glutSwapBuffers()

return

def read_texture(filename):

img = Image.open(filename)

img_data = numpy.array(list(img.getdata()), numpy.int8)

textID = glGenTextures(1)

glPixelStorei(GL_UNPACK_ALIGNMENT, 1)

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.size[0], img.size[1], 0, GL_RGB, GL_UNSIGNED_BYTE, img_data)

return textID

if __name__ == '__main__':

main()

解决方案

I know that this is in relation to your new question.

But there is a slightly different "problem" at hand. So just in case someone else has the same problem, I'm going to answer it.

First of all, as mentioned in the comments you don't need to call glBegin(). Because gluSphere() already manages that. Additionally if you were to call glBegin() you'd also need to call glEnd(). So the reason you're seeing something, is basically because you're confusing your driver, and it then does something it isn't supposed to do.

In read_texture() after you generate a texture name you don't bind it. So the subsequent texture related calls are going to the default texture and not your newly created texture.

textID = glGenTextures(1)

glBindTexture(GL_TEXTURE_2D, textID) # This is what's missing

After adding the missing glBindTexture() call, you'll get a result looking like this.

80e105cd304e1c3e46e861e0ac1c18c4.png

Now it might look a bit weird, and it might look like something is wrong. But the fact of the matter is that everything is working as it should. The problem lies with the texture itself.

The "problem" is that your brick texture isn't a spherical mapped texture. If we now just only replace your brick texture with a spherical mapped texture, then well get the following.

f4137af161bf2ca57e34aacf48f48794.png

Here's the spherical mapped texture used above:

4da3aa15d7b09afca08d23f629e3abb6.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值