Internet Computer Mobile Games

Diffuse color texture using UV maps in GLSL shaders

Hi!
I want to add diffuse color texture to my object in GLSL shader code.
Code:

import bge
 
cont = bge.logic.getCurrentController()
 
VertexShader = """
        uniform mat4 viewMatrix; // world to view transformation
        uniform mat4 viewMatrixInverse;
            // view to world transformation
 
        varying vec3 viewDirection; // direction in world space
            // in which the viewer is looking
        varying vec3 normalDirection; // normal vector in world space
        varying vec3 normal;
        varying vec3 view;
 
        void main()
        {
            vec4 positionInViewSpace = gl_ModelViewMatrix * gl_Vertex;
              // transformation of gl_Vertex from object coordinates
              // to view coordinates
 
            vec4 viewDirectionInViewSpace = positionInViewSpace
              - vec4(0.0, 0.0, 0.0, 1.0);
              // camera is always at (0,0,0,1) in view coordinates;
              // this is the direction in which the viewer is looking
              // (not the direction to the viewer)
 
            viewDirection =
              vec3(viewMatrixInverse * viewDirectionInViewSpace);
              // transformation from view coordinates
              // to world coordinates
 
            vec3 normalDirectionInViewSpace =
              gl_NormalMatrix * gl_Normal;
              // transformation of gl_Normal from object coordinates
              // to view coordinates
 
            normalDirection = normalize(vec3(
              vec4(normalDirectionInViewSpace, 0.0) * viewMatrix));
              // transformation of normal vector from view coordinates
              // to world coordinates with the transposed
              // (multiplication of the vector from the left) of
              // the inverse of viewMatrixInverse, which is viewMatrix
             
            normal = normalize(gl_NormalMatrix * gl_Normal);
            view = -normalize(vec3(gl_ModelViewMatrix * gl_Vertex));
 
            gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
        }
"""
 
FragmentShader = """
        varying vec3 viewDirection;
        varying vec3 normalDirection;
        varying vec3 normal;
        varying vec3 view;
        uniform samplerCube cubeMap;
 
        void main()
        {
            float fP = 0.5;
            float metallic = 0.0;
            vec3 reflectedDirection = reflect(viewDirection, normalize(normalDirection));
            vec4 color = vec4(0.0, 0.5, 0.5, 1.0);
            float fresnel = dot(view, normal);
            float fresnelPower = pow(fresnel, fP);
 
            vec3 specularColor = textureCube(cubeMap, vec3(reflectedDirection.x, -reflectedDirection.z, reflectedDirection.y));
            vec4 specular = vec4(specularColor.xyz, 1.0);
            gl_FragColor = mix(specular, mix(color + (specular * 0.04), specular, metallic), fresnelPower);
        }
"""
 
mesh = cont.owner.meshes[0]
for mat in mesh.materials:
    shader = mat.getShader()
    if shader != None:
        if not shader.isValid():
            shader.setSource(VertexShader, FragmentShader, 1)
            shader.setSampler('cubeMap', 0)
        viewMatrix = bge.logic.getCurrentScene().active_camera.world_to_camera
        shader.setUniformMatrix4('viewMatrix', viewMatrix)
        viewMatrixInverse = bge.logic.getCurrentScene().active_camera.camera_to_world
        shader.setUniformMatrix4('viewMatrixInverse', viewMatrixInverse)

In the code you can see 'color' variable(vec4) that needs to be replaced with a texture in slot 1(not slot 0 as 0 is cubemap). How can I do it? It should use UVs as coordinates.

Top similar posts to Diffuse color texture using UV maps in GLSL shaders

GLSL-Shaders-Mod-1.6.4

People!!! Help me, plz. I try to set my new GLSL-Shaders-Mod-1.6.4, but... look this picture - it is horror! What I do wrong?! Why it so mixed? http://i63.fastpic.ru/big/2014/0707/a8/dfd3f1c570251ef086785dbbf56253a8.png http://i63.fastpic.ru/big/2014/0707/ff/5f38b0a61fa84b0de8c424858094e5ff.png http://i63.fastpic.ru/big/2014/0707/7d/768212252b800ca87f4782a8a2b91c7d.png...

Apple maps dark color bug

Ever since I started using iOS 8 Apple maps is very dark and unreadable in standard view. Anyone know how to fix this?Attachment 506440 Attached Thumbnails   ...

Back ground color for GLSL rendering

hi there, can any one help me explaining the set up to get desired color for the back ground of the GLSL rendering (image sequence rendered for checking animation, that I meant!) I am always getting the grey color. but some times I would like to have white, black or some other color.. Is it possible? thank you... ...

default light set in glsl texture paint mode

hi guys, as you probably see in my videos I often use Blender for paint my textures. Blender has a great texture paint toolset BUT... Am I the only one that when start to paint in GLSL mode have to set up a rig of lights, or fake a matcap to see normal map and blend effects in viewport? Some time I want to show the 'texture paint mode' to friends at work, ad actually the setup before starting is pretty clunky. I think Blender should have a default light set when no lights in the s...

how to add normal/displacement/specularity maps to a texture in blender 2.72b?

Hi, I'm trying to work on a blender tutorial called "The secrets of realistic texturing in blender" (Link: http://www.youtube.com/watch?v=W07H7xeUnGE) everything was going fine up until the 20 minute mark, wherein i was trying to add a normal texture map to my diffuse map, but cant quite figure out to. The tutorial is in a earlier version of blender (Im working in blender 2.72b), and i cant figure out how to apply my normal map to the diffuse texture pattern. If someone could tell m...

Nodes/Normal Maps/Textures - How to apply all the set ups to the actual texture?!

So basically l have the normal maps and the textures all set up in Nodes and ready to apply into my actual mesh - but for some reason it won't? Here is my file to show how its all set up - if anyone could PLEASE tell/show me how to set it up right and how to apply it all to the mesh it would be GREATLY appreciated! Here is the link to my blender file: http://pasteall.org/blend/index.php?id=32661 ...

Page TEXTURE, COLOR not working

I have MSWord 2010 on W8 and cannot make the software print a page texture or color. I previously had an older version on XP and there was no problem. Using the same file now, I cannot print a texture, or page color, in the background of my sheet. When on XP, I was using an HP OfficeJet 6700 and it printed perfectly, and had the same printer after the conversion to W8, but would not print texture or page color. I now have an HP OfficeJet 6830, still no page texture or color. It does appear ...

Design Apple Development Security Automobile Network Photography Health Money Travel Shopping Issues Operating systems Drivers Software Programming Tech Home Science Sport Solution