It is currently Sat Aug 22, 2020 12:24 pm


All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Texturing + OpenGL
PostPosted: Thu Nov 11, 2010 9:19 pm 

Joined: Thu Nov 11, 2010 9:03 pm
Posts: 8
Hi, I have been trying to make a game using the Minecraft look , but i have run into some issues regarding the texturing of the cubes.

I'm using OpenGL and sending the vertex array and index array generated for the CubicSurfaceExtractor, the new one which don't generate normals.
Now i am trying to get the right texture coordinates using the vertex shader (GLSL), but the closer that i have come to the right answer is this:

Image
The faces are supposed to look exactly the same.
As you can see, the textures are only correctly mapped in the faces who are facing the camera, that is, the z axis.

The code I am using in my shaders is:

Vertex Shader
Code:
#version 110

attribute vec4 gl_Vertex;
uniform mat4 gl_ProjectionMatrix;

void main()
{
   gl_Position = ftransform();
   gl_TexCoord[0] = gl_Vertex.xyzw;
}


Fragment Shader
Code:
#version 110
uniform sampler2D tex;

void main()
{
   gl_FragColor = texture2D(tex, gl_TexCoord[0].st);
}


Thanks to anyone who tries to help me, and forgive me if I can't speak proper English, since I'm not a native speaker :D


Last edited by Metanoia on Thu Nov 11, 2010 9:59 pm, edited 1 time in total.

Top
Offline Profile  
Reply with quote  
 Post subject: Re: Texturing + OpenGL
PostPosted: Thu Nov 11, 2010 9:34 pm 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
Well, you are one the right path. I'm assuming the back faces are also textured, so that you have 2 faces textured and 4 faces not textured? In order to texture all the faces you need to sample you texture map three times. That means you need three seperate calls to 'texture2D', and for each call you have to pass in a different set of texture coordinates. One set of coordinates is for XY, one is for XZ, and one is for YZ.

Then you should have faces on all sides but they will be overlapping and mixing together. You need to use the surface normals to fix this. But get the first part working first...


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Texturing + OpenGL
PostPosted: Thu Nov 11, 2010 10:20 pm 

Joined: Thu Nov 11, 2010 9:03 pm
Posts: 8
Thanks David!

Your answer put me in the right path and now I can texture all the faces switching between
Code:
   gl_FragColor = texture2D(tex, gl_TexCoord[0].xy);

Code:
   gl_FragColor = texture2D(tex, gl_TexCoord[0].xz);

and
Code:
   gl_FragColor = texture2D(tex, gl_TexCoord[0].yz);


Now I only need to be able to determine which direction is facing each pixel in the fragment shader. I know how to do it using normals, but I can't get to work the normals generation code i saw in other post, since I am still a begginer at GLSL.

I could switch to the CubicSurfaceExtractorWithNormals, but I really like the way the new CubicSurfaceExtractor works, so I would love to be able to calculate the normals in my shaders.

Thanks you again for your time!


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Texturing + OpenGL
PostPosted: Thu Nov 11, 2010 10:28 pm 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
The code fragment I posted was Cg, unfortunatly I don't know GLSL so I can't convert it. But I'm sure if you ask on an OpenGL forum then someone will be able to help you...

In the mean time you should probably try the CubicSurfaceExtractorWithNormals, at least you can then makes sure the rest of your code works properly.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Texturing + OpenGL
PostPosted: Fri Nov 12, 2010 7:18 pm 

Joined: Thu Nov 11, 2010 9:03 pm
Posts: 8
I finally got it to work without help! :D

The problem I had was that I was trying to compute the normals using gl_FragCoords, but that was wrong. Instead, I should have calculated the in-world position of each pixel in the vertex shader, using:

Code:
   realPos = gl_ModelViewMatrix * gl_Vertex;
   realPos = gl_ModelViewMatrixInverse * realPos;


and pass that as a varying variable to the fragment shader.

The you can just do:

Code:
   vec3 inWorldNormal = cross(dFdy(realPos.xyz), dFdx(realPos.xyz));
   inWorldNormal = normalize(inWorldNormal);

in the fragment shader to get the normals working!

Finally, you just have to choose the right texture coordinates using the normals ;)

And it's lightning fast, it's running at over 8300 FPS on my 5 years old machine!

Thanks you for building such an amazing library!

EDIT: That's what it looks like now!

Image

EDIT: As i should have remembered from my maths class, a matrix multiplied by its inverse equals 1. :roll:

So you can just use:
Code:
realPos = gl_Vertex;

to access that variable from the fragment shader. It will be faster.


Last edited by Metanoia on Fri Nov 12, 2010 9:44 pm, edited 1 time in total.

Top
Offline Profile  
Reply with quote  
 Post subject: Re: Texturing + OpenGL
PostPosted: Fri Nov 12, 2010 8:46 pm 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
Excellent, I'm glad you got it working :-) This will be useful for anyone else who uses OpenGL in the future. But do let us know if you have any more questions.


Top
Offline Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Theme created StylerBB.net