Simply Texturing

Hi all! I want some suggestion about my first fragment shader using textures.

The problem is that I cannot texture a simple box. I’ve checked everything, but I still miss something…

I use a forward compatible context.

Here is the vertex shader code:


#version 150

// Pragmas directives
#pragma optimization(off)
#pragma debug(on)


// Transform uniform state
uniform mat4 dsModelProjection;

vec4 ProjectModelVertexPosition(vec3 v) {
	return (dsModelProjection * vec4( v.xyz , 1.0 ) );
}

// Vertex shader inputs & outputs

in vec3 dvi_Position;
in vec2 texcoordV;
centroid out vec2 texcoordF;

// Vertex shader etry point

void main() {

	// Compute transformed vertex position
	gl_Position = ProjectModelVertexPosition(dvi_Position);


	texcoordF = texcoordV;

}

Fragment shader:


#version 150

// Pragmas directives
#pragma optimization(off)
#pragma debug(on)


// Fragment shader inputs & outputs

out vec4 FragmentColor;

uniform sampler2D texsample;
centroid in vec2 texcoordF;

void main() { 
	FragmentColor = texture(texsample, texcoordF);
}


I see my rotating cube, I can transform it, coloring it with uniform color and per vertex color. Varying are set correctly and uniform correctly then.
But when I use texture (it is 480x480 (white or random color, RGB 24 bit, internal format RGB8)) I see the cube all black.
Every operation is executed in the same thread. Shader, Texture and objects are loaded in the same thread.
Each operation is checked with GetError and every go well.

I don’t really know how to solve this problem.

Another thing: why tex2D, texture2D and texture sampler functions are the same (all fragment shader compile correctly if I use any those functions…), links…

I want simply texturing,
… but outputs black fragments… sigh

If anyone can imaging what am I missing, very thanks!

EDIT: for the sake of completeness, I’ve logged the OpenGL calls. Values are in decimal and they cannot be decoded easily, but better than nothing! :wink:

The log is a rendering loop cycle (many calls are for debugging purpose).

glClear(16640, )
glGetError() = 0
glViewport(0, 0, 800, 600, )
glGetError() = 0
glGetError() = 0
glGetError() = 0
glClearColor(1, 1, 1, 0, )
glEnable(2929, )
glGetError() = 0
glDepthFunc(513, )
glGetError() = 0
glClearColor(1, 1, 1, 0, )
glUseProgram(2, )
glGetError() = 0
glGetIntegerv(35725, 2, )
glGetError() = 0
glGetUniformLocation(2, dsModelProjection, ) = 0
glGetError() = 0
glGetProgramiv(2, 35719, 18, )
glGetError() = 0
glGetActiveUniform(2, 0, 18, 17, 1, 35676, dsModelProjection, )
glGetError() = 0
glUniformMatrix4fv(0, 1, False, System.Single, )
glGetError() = 0
glValidateProgram(2, )
glGetError() = 0
glGetProgramiv(2, 35715, 1, )
glGetError() = 0
// Apply Texture State
glActiveTexture(33984, )
glGetError() = 0
glBindTexture(3553, 1, )
glGetError() = 0
glGetIntegerv(35725, 2, )
glGetError() = 0
glGetUniformLocation(2, texsample, ) = 1
glGetError() = 0
glGetProgramiv(2, 35719, 18, )
glGetError() = 0
glGetActiveUniform(2, 1, 18, 9, 1, 35678, texsample, )
glGetError() = 0
glUniform1i(1, 0, )
glGetError() = 0
glValidateProgram(2, )
glGetError() = 0
glGetProgramiv(2, 35715, 1, )
glGetError() = 0
// Setup Shader Inputs
glBindVertexArray(1, )
glGetError() = 0
glDrawArrays(4, 0, 36, )
glGetError() = 0
glUseProgram(0, )
glGetError() = 0

You can note I use vertex arrays. Here is the log when creating vertex array:

// Setup Shader Inputs
glGenVertexArrays(1, 1, )
glGetError() = 0
glBindVertexArray(1, )
glGetError() = 0
glGenBuffers(1, 1, )
glGetError() = 0
glBindBuffer(34962, 1, )
glGetError() = 0
glBufferData(34962, 432, Derm.Render.Vertex3f, 35046, )
glGetError() = 0
glGetIntegerv(35725, 2, )
glGetError() = 0
glBindBuffer(34962, 1, )
glGetError() = 0
glGetError() = 0
glEnableVertexAttribArray(0, )
glGetError() = 0
glBindBuffer(34962, 0, )
glGetError() = 0
glValidateProgram(2, )
glGetError() = 0
glGetProgramiv(2, 35715, 1, )
glGetError() = 0
glGenBuffers(1, 2, )
glGetError() = 0
glBindBuffer(34962, 2, )
glGetError() = 0
glBufferData(34962, 288, Derm.Render.Vertex2f, 35046, )
glGetError() = 0
glGetIntegerv(35725, 2, )
glGetError() = 0
glBindBuffer(34962, 2, )
glGetError() = 0
glGetError() = 0
glEnableVertexAttribArray(2, )
glGetError() = 0
glBindBuffer(34962, 0, )
glGetError() = 0
glValidateProgram(2, )
glGetError() = 0
glGetProgramiv(2, 35715, 1, )
glGetError() = 0

Your shader code looks reasonable. Except, what happens if you remove the “centroid” qualifier in both the vert and frag shaders?

If the “centroid” usage is not the problem then in Post269644 I placed a simple 3.2 example of texturing a triangle – maybe looking at that may help you to compare against your non-vert/fragment shader code.

Removing the centroid doesn’t change the result. I tried also to disable multisampling, but the result is the same.

I tried to specify a fixed coordinate vec2(0.5,0.5) to the “texture” function, but the cube is still completly black… at this point I think the problem reside in the texture specification…

// Create Texture2d
glGenTextures(1, 1, )
glGetError() = 0
glBindTexture(3553, 1, )
glGetError() = 0
glTexImage2D(3553, 0, 32849, 480, 480, 0, 6407, 5121, System.Byte, )
glGetError() = 0
glBindTexture(3553, 0, )
glGetError() = 0

No shader is bound when the texture is specified, and the shader runs for sometime without having set the sampler uniform (because the texture data loading in done in another thread); but I can’t see any problem here.

The texture is 480x480, each pixel is composed by 3 bytes. Internal format requested is RGB8, the format is RGB and the type is UNSIGNED_BYTE. GetError doesn’t notify anything, but maybe the problem is the texture format…

because the texture data loading in done in another thread

This sounds suspicious. How exactly does this work? What happens if you load it in the same thread? Are you sure you are sharing objects correctly?

Mipmaps ?

Maybe you had misunderstood. Bitmap loading is done in another thread, but texture creation is done in the same thread where the shader is compiled, linked, setup and executed. Every OpenGL operation is executed in that thread.

The final result shall work in a multithread design, and actually I create a context “ctx1” in “MainThread” and make it current, then create context “ctx2” sharing with ctx1 and start another thread “RenderThread” (the really drawing thread where all operations are done).

In the “RenderThread” make current “ctx2” and perform rendering loop.

I tried to create “ctx2” without sharing (maybe there is some reason to avoid this), but the result doesn’t change.

Code is more clear without explaining it. Just committed actual code: http://sourceforge.net/projects/genomalib/develop

Actually runs only on Windows. Tell me your impressions!

EDIT: open Simple cube project in the Example dir. Cubs.cs contains a class deriving from Mesh. MeshShader contains the shader code. Rendering operation are effectively done in Render/RenderHierarchy.cs (Render method). Render loop is done in Render/RenderKernel.cs (RunKernels and RunKernelMain).

For texturing managament see Render/Texture/Texture.cs Render/Texture/Texture2d.cs and Render/State/TextureState.cs.

I don’t create mipmaps. AFAIK they are deprecated.

Be sure to check the spec section on sharing in appendix D.

No, where did you get that from ?

But as you don’t create mipmaps, and mipmapping is the default min tex filtering, be sure that explicitely set it like this :
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

No, where did you get that from ?

But as you don’t create mipmaps, and mipmapping is the default min tex filtering, be sure that explicitely set it like this :
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); [/QUOTE]

Eureka! You got the point! Very, Very, Very thanks!!!

For mipmaps, I was wrong (obviously). Automatic mipmap generation is deprecated, not the mpmaps itself :stuck_out_tongue:

Oh yes… what a beatifull day to play with textures! :slight_smile:

Thank to you all! You made a man happy!

Glad that helped :slight_smile:

Just for clarfication, manually triggering of automatic mipmaps generation is not deprecated, use glGenerateMipmaps();

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.