• Advertisement
Sign in to follow this  

OpenGL Strange texture flickering/repositioning problem

This topic is 1244 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

EDIT: Unless I figure something else out, I'm chalking this up to a bad card or drivers.  Using only vertex and fragment shaders works fine, but the presence of a geometry shader makes the card glitch.


Hello, I'm new to the forums and hope that I have the right place. I've searched, but didn't find a topic that seemed to be about the problem I'm having.  I'm using Java and LWJGL, which uses OpenGL, and I get a strange flickering or repositioning problem with one of the video cards on which I've tested.


My game uses the NES resolution of 256x224, and I'm using a tile atlas texture that is 256x256 pixels and contains textures that are 16x16 pixels.  I'm passing a vertex buffer containing map tile codes and their map coordinates to my shaders.  The geometry shader then emits a 4 vertex triangle strip to make a quad from the given point and calculates the UV texture coordinates based on the map tile passed it.


I have tested my code on two systems, both Win 7x64.  The problem occurs only on one machine, which has an NVIDIA Quadro NVS 295, and it has this problem using both 311.35 and 340.66 drivers.  The system that works has an NVIDIA GeForce 9400 GT with 311.06.


It's hard for me to describe the problem, so I've attached an image of what it should look like (screen-good.png) and a shot of the flickering when it occurs (screen-bad.png).  This flicker only happens for a frame and it's not always the same spot on the screen.  I'm not changing any values and I've tried to strip things down to the most basic code in an attempt to track down the problem.  As the screenshot shows, it's sometimes drawing the wrong tiles in the wrong place and not drawing certain areas.  My "map" is 100 tiles by 14 tiles, so it is drawing outside the viewport/screen.


As a side note, which may or may not be important, if I use "FragColor = vec4(1.0, 1.0, 1.0, 1.0);" as my fragment shader, the screen will be white as expected and no flickering will occur.


My render code:

		// Start the shader
		GL20.glUniformMatrix4(this.shader.projectionMatrixLocation, false, DisplayHelper.screenProjectionMatrix);
		GL20.glUniform1i(this.shader.textureSamplerLocation, 0);
		// Set up the texture.
		GL11.glBindTexture(GL11.GL_TEXTURE_2D, this.texture.textureID);
		// Set up the data buffer.
		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, this.bufferHandle);
		GL15.glBufferData(GL15.GL_ARRAY_BUFFER, this.buffer, GL15.GL_STATIC_DRAW);
		// Set up the vertex attrib pointers.
		GL20.glVertexAttribPointer(0, 1, GL11.GL_FLOAT, false, TILE_CODE_STRIDE, TILE_CODE_OFFSET);
		GL20.glVertexAttribPointer(1, 2, GL11.GL_FLOAT, false, POSITION_STRIDE, POSITION_OFFSET);
		// Draw.
		GL11.glDrawArrays(GL11.GL_POINTS, 0, TILES_ON_SCREEN);
		// Clean up.
		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
		GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);

My shader code. I have them all in one file and separate them by name using anything above the first name (#version 330 core) in all three.

#version 330 core

>> vertex <<

layout(location = 0) attribute in float TileCode;
layout(location = 1) attribute in vec2 VertexPosition;

const float TILE_DRAW_SIZE = 16.0;

out int tile;

void main() {
	tile = int(TileCode);
	gl_Position = vec4(VertexPosition, 0.0, 1.0);

>> geometry <<

layout(points) in;
layout(triangle_strip, max_vertices=4) out;

const int TILES_PER_TEXTURE_ROW = 16;
const float TILE_DRAW_SIZE = 16.0;
const float TEXTURE_SIZE = 256.0;

uniform mat4 projectionMatrix;

in int tile[];

out vec2 texCoord;

void main() {
	int code = tile[0];
	if (code == 0) {
	// Set up the tile bound points.
	vec4 topLeft = gl_in[0].gl_Position;
	topLeft.x = topLeft.x * TILE_DRAW_SIZE;
	topLeft.y = topLeft.y * TILE_DRAW_SIZE;
	vec4 bottomRight = topLeft;
	bottomRight.x += TILE_DRAW_SIZE;
	bottomRight.y += TILE_DRAW_SIZE;
	// Calculate tile texture coordinates.
	int cx = code % TILES_PER_TEXTURE_ROW;
	int cy = code / TILES_PER_TEXTURE_ROW;
	float tx = (float(cx) * TILE_DRAW_SIZE) / TEXTURE_SIZE;
	float ty = (float(cy) * TILE_DRAW_SIZE) / TEXTURE_SIZE;
	float tx2 = (float(cx + 1) * TILE_DRAW_SIZE) / TEXTURE_SIZE;
	float ty2 = (float(cy + 1) * TILE_DRAW_SIZE) / TEXTURE_SIZE;
	// Top left.
	gl_Position = projectionMatrix * vec4(topLeft.x, topLeft.y, 0.0, 1.0);
	texCoord = vec2(tx, ty);
	// Top right.
	gl_Position = projectionMatrix * vec4(bottomRight.x, topLeft.y, 0.0, 1.0);
	texCoord = vec2(tx2, ty);
	// Bottom left.
	gl_Position = projectionMatrix * vec4(topLeft.x, bottomRight.y, 0.0, 1.0);
	texCoord = vec2(tx, ty2);
	// Bottom right.
	gl_Position = projectionMatrix * vec4(bottomRight.x, bottomRight.y, 0.0, 1.0);
	texCoord = vec2(tx2, ty2);

>> fragment <<

layout(location = 0) out vec4 FragColor;

uniform sampler2D Texture0;

in vec2 texCoord;

void main() {
//	FragColor = vec4(1.0, 1.0, 1.0, 1.0);
	FragColor = texture2D(Texture0, texCoord);

Am I missing something that might be causing things to fail on one card, but not the other?

If I need to post more code, let me know.  I posted what I thought would be most relavent.


Thanks for any help!

Edited by adambiser

Share this post

Link to post
Share on other sites

Looks like that row of tiles that's missing has been moved up two tiles.


Strange.. could be just a driver problem, but try glVertexAttribIPointer and use actual integer data, and write your shader code so there are no conversions from floats to integers. If it's still strange try to redesign so you don't use integers at all. It looks like that card is pretty old, and probably driver-updates don't address it anymore.

Share this post

Link to post
Share on other sites

My guess is a driver problem as well, but this is my first attempt at using geometry shaders and getting away from immediate mode, so I thought it could easily be something I've overlooked.


Since it was quicker to change the shader code so it only uses floats, I did that first and the flickering still persists.  I will try passing integer data only soon.


The only work around I've found is to do this:

		for (int y = 0; y < 14; y++) {
			GL11.glDrawArrays(GL11.GL_POINTS, y * TILES_WIDE, 16);

Since 16 is the width of the viewport in tiles.  (Hard coded numbers just for demonstration.)  The glitch doesn't become noticeable until I change the count from 16 to 20, which would mean 4 tiles off the right side of the viewport.


I'll have to dig around and see about finding some older drivers for this card, too.

Share this post

Link to post
Share on other sites

I tried passing in integer data, but could not get that to work either because of my card/drivers or because of something I overlooked in the code.


I'm wondering if the geometry shader support in this card is just really bad.  If I glBufferData my data right after loading it and then remove the glBufferData call from my render loop, I get the glitching back even with small sizes.  But if I do a glBufferData right before glDrawArrays. the glitching stops.  I'll have to try things out without a geometry shader when I get the chance to see if that's really the issue.


It's really pointing toward a bad card / driver, but I've not noticed these issues before.

Share this post

Link to post
Share on other sites

A geometry shader is definitely not needed and not very useful, but doesn't seem to be your problem. Try hardcoding the texture coords so it always pics a specific tile. Is part of your texture atlas black? Can you make it green to very it is actually using your texture and not some random texture/memory that happens to be cleared to all black?

Share this post

Link to post
Share on other sites

A geometry shader is definitely not needed and not very useful, but doesn't seem to be your problem. Try hardcoding the texture coords so it always pics a specific tile. Is part of your texture atlas black? Can you make it green to very it is actually using your texture and not some random texture/memory that happens to be cleared to all black?

I saw the geometry shader as something that would make a simple tile-based game very easy.

Every tile on the atlas had something in it, even if just grid lines at the border.


Things I've noticed:

  • Hard-coding the geometry shader to always choose the same tile still glitches.
  • When I change the fragment shader to use a solid color instead of the texture, every tile will be that color and there will be no glitching.
  • Hard-coding the fragment shader to use the same texture coordinates everywhere (using a spot that always has color), it still glitches.
  • If I move texture coordinate calculation into the vertex shader (removing it from the geometry shader), the glitching reduces, but is still there.
  • If I get rid of the geometry shader and instead calculate all the vertex and texture coordinates in my code and send them via buffer that to the shader (after making adjustments to handle the new data in the shader also), things work as expected.

I chose to do the last one for now, but left things so that I can switch over to try out new ideas to fix the geometry shader problem as I think of them.


If I figure out the cause, I will be sure to post here.  However, I think this can probably just be chalked up as a bad video card or bad drivers and I haven't found drivers to fix it yet.  The problematic system is not one on which I play games often or use graphics-intensive software (obviously, given the age), so I don't know if this glitching occurs in other programs or not.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By LifeArtist
      Good Evening,
      I want to make a 2D game which involves displaying some debug information. Especially for collision, enemy sights and so on ...
      First of I was thinking about all those shapes which I need will need for debugging purposes: circles, rectangles, lines, polygons.
      I am really stucked right now because of the fundamental question:
      Where do I store my vertices positions for each line (object)? Currently I am not using a model matrix because I am using orthographic projection and set the final position within the VBO. That means that if I add a new line I would have to expand the "points" array and re-upload (recall glBufferData) it every time. The other method would be to use a model matrix and a fixed vbo for a line but it would be also messy to exactly create a line from (0,0) to (100,20) calculating the rotation and scale to make it fit.
      If I proceed with option 1 "updating the array each frame" I was thinking of having 4 draw calls every frame for the lines vao, polygons vao and so on. 
      In addition to that I am planning to use some sort of ECS based architecture. So the other question would be:
      Should I treat those debug objects as entities/components?
      For me it would make sense to treat them as entities but that's creates a new issue with the previous array approach because it would have for example a transform and render component. A special render component for debug objects (no texture etc) ... For me the transform component is also just a matrix but how would I then define a line?
      Treating them as components would'nt be a good idea in my eyes because then I would always need an entity. Well entity is just an id !? So maybe its a component?
    • By QQemka
      Hello. I am coding a small thingy in my spare time. All i want to achieve is to load a heightmap (as the lowest possible walking terrain), some static meshes (elements of the environment) and a dynamic character (meaning i can move, collide with heightmap/static meshes and hold a varying item in a hand ). Got a bunch of questions, or rather problems i can't find solution to myself. Nearly all are deal with graphics/gpu, not the coding part. My c++ is on high enough level.
      Let's go:
      Heightmap - i obviously want it to be textured, size is hardcoded to 256x256 squares. I can't have one huge texture stretched over entire terrain cause every pixel would be enormous. Thats why i decided to use 2 specified textures. First will be a tileset consisting of 16 square tiles (u v range from 0 to 0.25 for first tile and so on) and second a 256x256 buffer with 0-15 value representing index of the tile from tileset for every heigtmap square. Problem is, how do i blend the edges nicely and make some computationally cheap changes so its not obvious there are only 16 tiles? Is it possible to generate such terrain with some existing program?
      Collisions - i want to use bounding sphere and aabb. But should i store them for a model or entity instance? Meaning i have 20 same trees spawned using the same tree model, but every entity got its own transformation (position, scale etc). Storing collision component per instance grats faster access + is precalculated and transformed (takes additional memory, but who cares?), so i stick with this, right? What should i do if object is dynamically rotated? The aabb is no longer aligned and calculating per vertex min/max everytime object rotates/scales is pretty expensive, right?
      Drawing aabb - problem similar to above (storing aabb data per instance or model). This time in my opinion per model is enough since every instance also does not have own vertex buffer but uses the shared one (so 20 trees share reference to one tree model). So rendering aabb is about taking the model's aabb, transforming with instance matrix and voila. What about aabb vertex buffer (this is more of a cosmetic question, just curious, bumped onto it in time of writing this). Is it better to make it as 8 points and index buffer (12 lines), or only 2 vertices with min/max x/y/z and having the shaders dynamically generate 6 other vertices and draw the box? Or maybe there should be just ONE 1x1x1 cube box template moved/scaled per entity?
      What if one model got a diffuse texture and a normal map, and other has only diffuse? Should i pass some bool flag to shader with that info, or just assume that my game supports only diffuse maps without fancy stuff?
      There were several more but i forgot/solved them at time of writing
      Thanks in advance
    • By RenanRR
      Hi All,
      I'm reading the tutorials from learnOpengl site (nice site) and I'm having a question on the camera (https://learnopengl.com/Getting-started/Camera).
      I always saw the camera being manipulated with the lookat, but in tutorial I saw the camera being changed through the MVP arrays, which do not seem to be camera, but rather the scene that changes:
      Vertex Shader:
      #version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec2 aTexCoord; out vec2 TexCoord; uniform mat4 model; uniform mat4 view; uniform mat4 projection; void main() { gl_Position = projection * view * model * vec4(aPos, 1.0f); TexCoord = vec2(aTexCoord.x, aTexCoord.y); } then, the matrix manipulated:
      ..... glm::mat4 projection = glm::perspective(glm::radians(fov), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); ourShader.setMat4("projection", projection); .... glm::mat4 view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp); ourShader.setMat4("view", view); .... model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f)); ourShader.setMat4("model", model);  
      So, some doubts:
      - Why use it like that?
      - Is it okay to manipulate the camera that way?
      -in this way, are not the vertex's positions that changes instead of the camera?
      - I need to pass MVP to all shaders of object in my scenes ?
      What it seems, is that the camera stands still and the scenery that changes...
      it's right?
      Thank you
    • By dpadam450
      Sampling a floating point texture where the alpha channel holds 4-bytes of packed data into the float. I don't know how to cast the raw memory to treat it as an integer so I can perform bit-shifting operations.

      int rgbValue = int(textureSample.w);//4 bytes of data packed as color
      // algorithm might not be correct and endianness might need switching.
      vec3 extractedData = vec3(  rgbValue & 0xFF000000,  (rgbValue << 8) & 0xFF000000, (rgbValue << 16) & 0xFF000000);
      extractedData /= 255.0f;
    • By Devashish Khandelwal
      While writing a simple renderer using OpenGL, I faced an issue with the glGetUniformLocation function. For some reason, the location is coming to be -1.
      Anyone has any idea .. what should I do?
  • Advertisement