Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2012
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.49 KB | None | 0 0
  1. VERTEX_SHADER = shaders.compileShader('''
  2.        uniform float xpos;
  3.        uniform float ypos;
  4.        attribute vec3 position;
  5.        attribute vec2 texcoords;
  6.        attribute int texid;
  7.        varying vec2 texcoords_u;
  8.        varying int texid_u;
  9.        void main() {
  10.            gl_Position = gl_ModelViewProjectionMatrix * vec4(position,1.0) + vec4(
  11.                xpos, ypos, 0.0, 0.0
  12.            );
  13.            //gl_TexCoord[0] = texcoords;
  14.            //??
  15.            
  16.            texcoords_u = texcoords;
  17.            texid_u = texid;
  18.        }''', GL_VERTEX_SHADER)
  19.  
  20.     FRAGMENT_SHADER = shaders.compileShader('''
  21.        varying vec2 texcoords_u;
  22.        varying int texid_u;
  23.        
  24.        uniform sampler2D thetexture;//s[2]
  25.        void main() {
  26.            
  27.            gl_FragColor = texture2D(thetexture, texcoords_u);
  28.            //textures[texid_u]
  29.            
  30.        }''', GL_FRAGMENT_SHADER)
  31.  
  32.  
  33.  
  34.     position = glGetAttribLocation(
  35.         shader, 'position'
  36.     )
  37.     texcoords = glGetAttribLocation(
  38.         shader, 'texcoords'
  39.     )
  40.     texid = glGetAttribLocation(
  41.         shader, 'texid'
  42.     )
  43.     avbo =  vbo.VBO(
  44.         numpy.array( [ #x, y, z ; texx, texy, texid  -- pretty sure texture coords are -1 to 1...
  45.             [  0, 1, 0,   0, 1, 0 ],#top
  46.             [ -1,-1, 0,  -1,-1, 0 ],#botleft
  47.             [  1,-1, 0,   1,-1, 0 ],#botright
  48.            
  49.             [  2,-1, 0,  -1,-1, 1 ],#botleft
  50.             [  4,-1, 0,  1,-1,  1 ],#botright
  51.             [  4, 1, 0,  1,1,   1 ],#topright
  52.             [  2,-1, 0,  -1,-1, 1 ],#botleft
  53.             [  4, 1, 0,  1,1,   1 ],#topright
  54.             [  2, 1, 0,  -1,1,  1 ],#topleft
  55.         ], 'f')
  56.     )#a triangle and a square
  57.     UNIFORM_LOCATIONS = { #set up some linkages!
  58.         'xpos': glGetUniformLocation( shader, 'xpos' ),
  59.         'ypos': glGetUniformLocation( shader, 'ypos' ),
  60.         'thetexture': glGetUniformLocation( shader, 'thetexture' ),
  61.     }
  62.  
  63.  
  64.  
  65. '''And then in the run:'''
  66.     shaders.glUseProgram(shader)#bind the shader
  67.         glUniform1f(UNIFORM_LOCATIONS['xpos'],xpos)
  68.         glUniform1f(UNIFORM_LOCATIONS['ypos'],ypos)
  69.         glUniform1i(UNIFORM_LOCATIONS['thetexture'],texture)
  70.         try:
  71.             avbo.bind()#bind the vertex object
  72.             try:
  73.                
  74.                
  75.                 glEnableVertexAttribArray( position )
  76.                 glEnableVertexAttribArray( texcoords )
  77.                 glEnableVertexAttribArray( texid )
  78.                
  79.                 #4 bytes per number, 6 numbers, so 4*6 bytes between the start of each record.
  80.                 stride = 6*4
  81.                 glVertexAttribPointer(
  82.                     position,
  83.                     3, GL_FLOAT,False, stride, avbo
  84.                 )
  85.                 glVertexAttribPointer(
  86.                     texcoords,
  87.                     2, GL_FLOAT,False, stride, avbo+12
  88.                 )
  89.                 glVertexAttribPointer(
  90.                     texid,
  91.                     1, GL_INT,False, stride, avbo+20
  92.                 )
  93.                 glDrawArrays(GL_TRIANGLES, 0, 9)
  94.  
  95.                
  96.                
  97.             finally:
  98.                 avbo.unbind() #unbind the vertex objct
  99.                 glDisableVertexAttribArray( position )
  100.                 glDisableVertexAttribArray( texcoords )
  101.                 glDisableVertexAttribArray( texid )
  102.  
  103.         finally:
  104.             shaders.glUseProgram( 0 )#unbind the shader
  105.  
  106. ''' this compiles, but renders blank. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement