Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: GLSL shader texture , what is wrong with my code

  1. #1
    Junior Member Newbie
    Join Date
    Aug 2010
    Posts
    6

    GLSL shader texture , what is wrong with my code

    What is wrong on my code ?
    Any ideea ?
    Code :
    import sys
    from ctypes import *
    from OpenGL.GL import *
    from OpenGL.GLU import *
    from OpenGL.GLUT import *
    from OpenGL import platform
    gl = platform.OpenGL
    GL_FRAGMENT_SHADER_ARB = 0x8B30
    GL_VERTEX_SHADER_ARB = 0x8B31
    GL_OBJECT_COMPILE_STATUS_ARB= 0x8B81
    GL_OBJECT_LINK_STATUS_ARB = 0x8B82
    GL_INFO_LOG_LENGTH_ARB = 0x8B84
    import Image
    import pygame
    from pygame.locals import *
    glCreateShaderObjectARB = gl.glCreateShaderObjectARB
    glShaderSourceARB = gl.glShaderSourceARB
    glShaderSourceARB.argtypes = [c_int, c_int, POINTER(c_char_p), POINTER(c_int)]
    glCompileShaderARB = gl.glCompileShaderARB
    glGetObjectParameterivARB = gl.glGetObjectParameterivARB
    glGetObjectParameterivARB.argtypes = [c_int, c_int, POINTER(c_int)]
    glCreateProgramObjectARB = gl.glCreateProgramObjectARB
    glGetInfoLogARB = gl.glGetShaderInfoLog
    glGetInfoLogARB.argtypes = [c_int, c_int, POINTER(c_int), c_char_p]
    glAttachObjectARB = gl.glAttachObjectARB
    glLinkProgramARB = gl.glLinkProgramARB
    glDeleteObjectARB = gl.glDeleteObjectARB
    glGetError = gl.glGetError
    glUseProgramObjectARB = gl.glUseProgramObjectARB
     
    def loadTexture ( fileName ):
        image  = Image.open ( fileName )
        width  = image.size [0]
        height = image.size [1]
        image  = image.tostring ( "raw", "RGBX", 0, -1 )
        texture = glGenTextures ( 1 )
        glBindTexture     ( GL_TEXTURE_2D, texture )   # 2d texture (x and y size)
        glPixelStorei     ( GL_UNPACK_ALIGNMENT,1 )
        glTexParameterf   ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT )
        glTexParameterf   ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT )
        glTexParameteri   ( GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR )
        glTexParameteri   ( GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR )
        gluBuild2DMipmaps ( GL_TEXTURE_2D, 3, width, height, GL_RGBA, GL_UNSIGNED_BYTE, image )
        return texture
     
    def compile_shader(source, shader_type):
        shader = glCreateShaderObjectARB(shader_type)
        source = c_char_p(source)
        length = c_int(-1)
        glShaderSourceARB(shader, 1, byref(source), byref(length))
        glCompileShaderARB(shader)
        status = c_int()
        glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB,
            byref(status))
        if (not status.value):
            raise SystemExit
        return shader
     
    def compile_program(vertex_source, fragment_source):
        vertex_shader = None
        fragment_shader = None
        program = glCreateProgramObjectARB()
     
        if vertex_source:
            vertex_shader = compile_shader(vertex_source, GL_VERTEX_SHADER_ARB)
            glAttachObjectARB(program, vertex_shader)
        if fragment_source:
            fragment_shader = compile_shader(fragment_source,
                GL_FRAGMENT_SHADER_ARB)
            glAttachObjectARB(program, fragment_shader)
        glLinkProgramARB(program)
     
        if vertex_shader:
            glDeleteObjectARB(vertex_shader)
        if fragment_shader:
            glDeleteObjectARB(fragment_shader)
     
        return program
     
    if __name__ == '__main__':
    	glutInit(sys.argv)
    	width, height = 600, 400
    	pygame.init()
    	pygame.display.set_mode((width, height), OPENGL | DOUBLEBUF)
    	texel = loadTexture ("111.jpg")
    	glBindTexture   ( GL_TEXTURE_2D, texel )
    	glEnable  ( GL_TEXTURE_2D )
    	prog = compile_program('''
        const float PI = 3.14159265;
        varying vec3 normal;
        void main() {
            normal = gl_NormalMatrix * gl_Normal;
            gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
            gl_FrontColor = gl_Color;
        }
         ''', '''
        varying vec3 normal;
        void main() {
            float intensity;
            vec4 color,texel;;
            vec3 n = normalize(normal);
            vec3 l = normalize(gl_LightSource[0].position).xyz;
    		texel = texture2D(texUnit0, gl_TexCoord[0].xy);
    		color *= texel;
            gl_FragColor = color;
            }
        ''')	
    	glMatrixMode(GL_PROJECTION)
    	glLoadIdentity()
    	gluPerspective(90.0, width/float(height), 1.0, 100.0)
    	glMatrixMode(GL_MODELVIEW)
    	glEnable(GL_DEPTH_TEST)
    	quit = False
    	while not quit:
    		for e in pygame.event.get():
    			if e.type in (QUIT, KEYDOWN):
    				quit = True
    		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    		glLoadIdentity()
    		glTranslate(0.0, 0.0, -2.5)
    		glUseProgramObjectARB(prog)
    		glutSolidTeapot(1.0)
    		pygame.display.flip()

  2. #2
    Junior Member Newbie
    Join Date
    Aug 2010
    Posts
    6

    Re: GLSL shader texture , what is wrong with my code

    Maybe I should say I am beginner and I got to this impasse. I need a specialist opinion. I can not find the appropriate method of repair but this script is a test script I use to learn more.
    The script is python with pyopengl module.
    Respectfully

  3. #3
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    2,882

    Re: GLSL shader texture , what is wrong with my code

    Quote Originally Posted by mythcat
    What is wrong on my code ? Any ideea ?
    You might want to tell us what you don't like about it.

    Personally, one thing that's wrong is that it has no comments

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •