Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pyopengl-纹理未按其应有的方式进行渲染_Python_Opengl_Textures_Pyopengl - Fatal编程技术网

Python pyopengl-纹理未按其应有的方式进行渲染

Python pyopengl-纹理未按其应有的方式进行渲染,python,opengl,textures,pyopengl,Python,Opengl,Textures,Pyopengl,这是原始的2d 256x256图像,我试图在屏幕上纹理。但由于某些原因,如果没有glTexParameteri(GL_TEXTURE_2D、GL_TEXTURE_MIN_FILTER、GL_LINEAR)线,它就无法进行渲染。当它的渲染(因为pic是2d)时,我发送属性指针uv的大小为2,带有glVertexAttribPointer(属性纹理,2,GL_浮点,GL_假,0,无) 但事实证明是这样的。 当我把它调到3只是为了尝试,它是这样的 无论我尝试了什么,都没有改变 我做错了什么?我真的需要

这是原始的2d 256x256图像,我试图在屏幕上纹理。但由于某些原因,如果没有glTexParameteri(GL_TEXTURE_2D、GL_TEXTURE_MIN_FILTER、GL_LINEAR)线,它就无法进行渲染。当它的渲染(因为pic是2d)时,我发送属性指针uv的大小为2,带有glVertexAttribPointer(属性纹理,2,GL_浮点,GL_假,0,无) 但事实证明是这样的。 当我把它调到3只是为了尝试,它是这样的 无论我尝试了什么,都没有改变 我做错了什么?我真的需要另一双眼睛。非常感谢。 这是我的代码:

import glfw
from OpenGL.GL import *
# from OpenGL.GL.shaders import compileShader, compileProgram
import numpy as np
from math import radians
from pyrr import matrix44, Vector3
from PIL import Image
# ----------------------------------------------------------------------
if not glfw.init():
    raise Exception("GLFW not initialized")
window = glfw.create_window(800, 600, "personal", None, None)
if not window:
    glfw.terminate()
    raise Exception("window did not created")
glfw.set_window_pos(window, xpos=200, ypos=50)
glfw.make_context_current(window)
# ----------------------------------------------------------------------
vertices = np.array([-0.7, 0.7, 0.0,
                     -0.7, -0.7, 0.0,
                     0.7, -0.7, 0.0,
                     0.7, 0.7, 0.0], dtype=np.float32)
indices = np.array([0, 1, 3,
                    3, 1, 2], dtype=np.uint32)
# color = np.array([0.0, 0.0, 0.0,
#                   0.0, 0.0, 0.0,
#                   0.0, 0.0, 0.0,
#                   0.0, 0.0, 0.0], dtype=np.float32)
texture_coord = np.array([0, 0,
                          0, 1,
                          1, 1,
                          1, 0], dtype=np.uint32)
# --------------------------------------------------------------------- TRANSFORMATION CALCULATION
matrix = matrix44.create_identity(dtype=np.float32)
matrix = np.dot(matrix44.create_from_translation(Vector3([0.0, 0.0, 0.0])), matrix)
matrix = np.dot(matrix44.create_from_x_rotation(radians(0)), matrix)
matrix = np.dot(matrix44.create_from_y_rotation(radians(0)), matrix)
matrix = np.dot(matrix44.create_from_z_rotation(radians(0)), matrix)
matrix = np.dot(matrix44.create_from_scale(Vector3([1, 1, 1])), matrix)
# ---------------------------------------------------------------------
vertex_shader_src = """
#version 330 core
layout(location = 0)in vec3 position;
layout(location = 1)in vec2 texture;
//in vec3 color;//for using color equal it to toFColor
uniform mat4 trans;
out vec3 toFColor;
out vec2 passTexCoord;
void main(){
gl_Position = trans * vec4(position.x,position.y,position.z,1.0f);
//toFColor=color;
passTexCoord=texture;
}
"""
# ---------------------------------------------------------------------
fragment_shader_src = """
#version 330 core
//in vec3 toFColor;
in vec2 passTexCoord;
//uniform vec3 triColor;
uniform sampler2D texture_sampler;
out vec4 outColor;
void main(){
//outColor = vec4(toFColor,1.0);
outColor = texture(texture_sampler, passTexCoord);
}
"""
# ---------------------------------------------------------------------PyOpenGL working shader program
# shader_program = compileProgram(compileShader(vertex_shader_src, GL_VERTEX_SHADER),
#                         compileShader(fragment_shader_src, GL_FRAGMENT_SHADER))
# ---------------------------------------------------------------------tutorial way shader program
vertex_shader_id = glCreateShader(GL_VERTEX_SHADER)
glShaderSource(vertex_shader_id, vertex_shader_src)
glCompileShader(vertex_shader_id)
if glGetShaderiv(vertex_shader_id, GL_COMPILE_STATUS) == GL_FALSE:
    print(glGetShaderInfoLog(vertex_shader_id))
    print("cant compile shader")
fragment_shader_id = glCreateShader(GL_FRAGMENT_SHADER)
glShaderSource(fragment_shader_id, fragment_shader_src)
glCompileShader(fragment_shader_id)
if glGetShaderiv(fragment_shader_id, GL_COMPILE_STATUS) == GL_FALSE:
    print(glGetShaderInfoLog(fragment_shader_id))
    print("cant compile shader")
shader_program = glCreateProgram()
glAttachShader(shader_program, vertex_shader_id)
glAttachShader(shader_program, fragment_shader_id)
glBindAttribLocation(shader_program, 0, "position")
glBindAttribLocation(shader_program, 1, "texture")
glLinkProgram(shader_program)
glValidateProgram(shader_program)
glUseProgram(shader_program)
# --------------------------------------------------------------------- my trying for VAO vertex positions
VAO = glGenVertexArrays(1)  # generate vao
glBindVertexArray(VAO)  # ready VAO to use
VBO1 = glGenBuffers(1)  # generate vbo
glBindBuffer(GL_ARRAY_BUFFER, VBO1)  # binding vbo for use
glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW)  # setting VBO what to carry
attribute_position = glGetAttribLocation(shader_program, "position")  # taking attribute name position from shader
glEnableVertexAttribArray(attribute_position)  # ready to use attribute
glVertexAttribPointer(attribute_position, 3, GL_FLOAT, GL_FALSE, 0,
                      None)  # telling OpenGL how to read data on given uniform
# ---------------------------------------------------------------------- indexing positions
EBO = glGenBuffers(1)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.nbytes, indices, GL_STATIC_DRAW)
# ---------------------------------------------------------------------- coloring instead of texture
# i am closing this because i will use texture
# VBO2 = glGenBuffers(1)
# glBindBuffer(GL_ARRAY_BUFFER, VBO2)
# glBufferData(GL_ARRAY_BUFFER, color.nbytes, color, GL_STATIC_DRAW)
# attribute_color = glGetAttribLocation(shader_program, "color")  # taking uniform name color from shader
# glEnableVertexAttribArray(attribute_color)  # ready to use uniform
# glVertexAttribPointer(attribute_color, 3, GL_FLOAT, GL_FALSE, 0,
#                       None)  # telling OpenGL how to read data on given attribute
# ---------------------------------------------------------------------- Texture pressing
TEX_BO = glGenTextures(1)
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, TEX_BO)
attribute_texture = glGetAttribLocation(shader_program, "texture")
glEnableVertexAttribArray(attribute_texture)
glVertexAttribPointer(attribute_texture, 3, GL_FLOAT, GL_FALSE, 0, None)
texture = Image.open("../res/pic1.png", "r")
texture = texture.transpose(Image.FLIP_TOP_BOTTOM)
image_data = texture.convert("RGBA").tobytes()
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture.width, texture.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
# Set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
# ---------------------------------------------------------------------- TRANSFORMATION MATRIX -------------------------
loc_uniform_transformation = glGetUniformLocation(shader_program, "trans")
# ----------------------------------------------------------------------
while not glfw.window_should_close(window):
    glfw.poll_events()
    glClearColor(0.15, 0.15, 0.15, 1.0)
    glClear(GL_COLOR_BUFFER_BIT)
    glUniformMatrix4fv(loc_uniform_transformation, 1, GL_FALSE, matrix44.create_identity())
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, None)
    glfw.swap_buffers(window)
# ------------------------------------------------------------------------------ Cleaning
glDeleteTextures(1, int(TEX_BO))
glDetachShader(shader_program, vertex_shader_id)
glDetachShader(shader_program, fragment_shader_id)
glDeleteShader(vertex_shader_id)
glDeleteShader(fragment_shader_id)
glDeleteProgram(shader_program)
glDeleteBuffers(1, int(VBO1))
# glDeleteBuffers(1, int(VBO2)) # if use colors
glDeleteBuffers(1, int(EBO))
glDeleteVertexArrays(1, int(VAO))
# ------------------------------------------------------------------------------
glfw.destroy_window(window)
glfw.terminate()

您必须设置
glTexParameteri(GL\u TEXTURE\u 2D,GL\u TEXTURE\u MIN\u FILTER,GL\u LINEAR)
,因为默认的缩小函数是
GL\u NEAREST\u MIPMAP\u LINEAR
。由于不生成Mipmap,如果不将最小化函数更改为
GL\u NEAREST
GL\u LINEAR
,纹理将是“Mipmap complete”

纹理坐标数组的类型必须是
np.float32
,而不是
np.uint32

texture\u coord=np.array([0,0,
0, 1,
1, 1,
1,0],dtype=np.32)
您没有为纹理坐标创建缓冲区对象:

VBO2=glGenBuffers(1)#生成vbo
glBindBuffer(GL_数组_缓冲区,VBO2)#绑定vbo以供使用
glBufferData(GL_数组_缓冲区、纹理_坐标.n字节、纹理_坐标、GL_静态_绘图)
属性\纹理=GLGetAttributeLocation(着色器\程序,“纹理”)
GlenableVertexAttributeArray(属性\纹理)
GLVertexAttributePointer(属性\u纹理,2,GL\u浮动,GL\u假,0,无)

调用时,当前绑定到目标
GL\u ARRAY\u buffer
的缓冲区对象与指定的顶点属性相关联。

当然,您必须设置
glTexParameteri(GL\u TEXTURE\u 2D,GL\u TEXTURE\u MIN\u FILTER,GL\u LINEAR)
。默认的缩小功能是
GL\u NEAREST\u MIPMAP\u LINEAR
。由于您没有生成mipmap,纹理是mipmap不完整的。谢谢,它解决了这个问题。但我想知道为什么文本坐标是浮动的,它们不是将顶点绑定到纹理的索引吗?glgenTextures不是vbo吗self@AAA
vec2
是一种浮点数据类型。纹理坐标指定纹理上的相对坐标。(0.0,0.0)是左下角。(1.0,1.0)是右上角。顶点坐标和纹理坐标是属性。索引指定属性的顺序