1
쉐이더에서 텍스처를 읽으려고합니다. 그리고 나는 단 하나의 스크린 만 얻는다 (단지 하나의 값이 읽혀지는 것 같다). 셰이더 선 선언 (64에서 74)에 주석을 달면 텍스처가 올바르게 표시됩니다. 그래서 정확하게 선언 된 것 같습니다. 여기 GLSL에 텍스처 전달
내 코드입니다 :#!/usr/bin/python
import sys
import numpy as np
try:
from OpenGL.GL import *
from OpenGL.GL import shaders
from OpenGL.GLU import *
from OpenGL.GLUT import *
except:
print '''ERROR: PyOpenGL not installed properly.'''
################################################################################
# GLOBALS
screen_w = 800
screen_h = 600
################################################################################
# SHADERS
f_shader = """
#version 120
uniform sampler2D texture_w;
void main() {
vec2 c = vec2(int(gl_FragCoord[0]),int(gl_FragCoord[1]));
vec4 t = texture2D(texture_w, c);
gl_FragColor = t;
}
"""
v_shader = """
#version 120
void main() {
gl_FrontColor = gl_Color;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
"""
################################################################################
# FUNCS
def setup():
glEnable(GL_DEPTH_TEST)
FRAGMENT_SHADER = shaders.compileShader(f_shader, GL_FRAGMENT_SHADER)
VERTEX_SHADER = shaders.compileShader(v_shader, GL_VERTEX_SHADER)
# Weight texture declaration
img_data = np.random.rand(screen_w, screen_h, 3)
img_data2 = img_data*255.0
texture = glGenTextures(1)
glPixelStorei(GL_UNPACK_ALIGNMENT,1)
glBindTexture(GL_TEXTURE_2D, texture)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, screen_w, screen_h, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data2)
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_MAG_FILTER, GL_LINEAR)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
shader = shaders.compileProgram(VERTEX_SHADER, FRAGMENT_SHADER)
shaders.glUseProgram(shader)
u_loc = glGetUniformLocation(shader, "texture_w")
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, texture)
glUniform1i(u_loc, 0)
def display():
"Global Display function"
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glEnable(GL_TEXTURE_2D)
glColor(1,1,1)
glBegin(GL_TRIANGLES)
glTexCoord2f(0, 0)
glVertex(-1,-screen_w/float(screen_h),-2)
glTexCoord2f(1, 1)
glVertex(1,screen_w/float(screen_h),-2)
glTexCoord2f(0, 1)
glVertex(-1,screen_w/float(screen_h),-2)
glTexCoord2f(0, 0)
glVertex(-1,-screen_w/float(screen_h),-2)
glTexCoord2f(1, 0)
glVertex(1,-screen_w/float(screen_h),-2)
glTexCoord2f(1, 1)
glVertex(1,screen_w/float(screen_h),-2)
glEnd()
glutSwapBuffers()
def reshape (w, h):
global screen_w, screen_h
glViewport (0, 0, w, h)
screen_w, screen_h = w, h
glMatrixMode (GL_PROJECTION)
glLoadIdentity()
glOrtho(-1, 1, -float(w)/float(h), float(w)/float(h), 1, 10)
glMatrixMode (GL_MODELVIEW)
def keyboard(key, x, y):
global mode
if key == chr(27):
sys.exit(0)
elif key == 'f':
glutFullScreen()
else:
print key
################################################################################
# MAIN
glutInit(sys.argv)
glutInitDisplayString("double rgba depth samples=4")
glutInitWindowSize (screen_w, screen_h)
glutCreateWindow ('Weights')
setup()
glutDisplayFunc(display)
glutReshapeFunc(reshape)
glutKeyboardFunc(keyboard)
glutMainLoop()
사람이 어떤 생각을 가지고 있습니까?