0

I am rendering 3 squares on which I have stretched the texture, and as I move them in space, I change the coordinates of the vertices of these squares. Everything works, but as soon as I want to render another image, the texture of which is already on the stage, for some reason it is not displayed in the position where I need it, but is superimposed on the same texture on the stage. That is, when I want to render two identical images in different parts of the scene, they overlap each other (I see this due to the fact that there are almost transparent pixels in the image, and when I add it, it can be seen that they overlap each other), although in the console I see that their coordinates are different. What's wrong?

I use SharpGL

private void DrawTexture(int idTexture)
{  
    gl.PushMatrix();
    gl.LoadIdentity();
    gl.Scale(-0.15, 0.28, 1);
    gl.Rotate(180, 0, 0, 0);
    gl.BindTexture(GL_TEXTURE_2D, _textureParams[idTexture].textures);
    gl.Begin(GL_QUADS);
    float dist = DistanceTextureMultipler;
    gl.TexCoord(0, 0); gl.Vertex(_textureParams[idTexture].textureScaleX * (-1 + _textureParams[idTexture].texturePostitonX / dist), _textureParams[idTexture].textureScaleY * (-1 + _textureParams[idTexture].texturePostitonY / dist));
    gl.TexCoord(1, 0); gl.Vertex(_textureParams[idTexture].textureScaleX * (1 + _textureParams[idTexture].texturePostitonX / dist), _textureParams[idTexture].textureScaleY * (-1 + _textureParams[idTexture].texturePostitonY / dist));
    gl.TexCoord(1, 1); gl.Vertex(_textureParams[idTexture].textureScaleX * (1 + _textureParams[idTexture].texturePostitonX / dist), _textureParams[idTexture].textureScaleY * (1 + _textureParams[idTexture].texturePostitonY / dist));
    gl.TexCoord(0, 1); gl.Vertex(_textureParams[idTexture].textureScaleX * (-1 + _textureParams[idTexture].texturePostitonX / dist), _textureParams[idTexture].textureScaleY * (1 + _textureParams[idTexture].texturePostitonY / dist));
    gl.End();
    gl.PopMatrix();
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
Vera
  • 11
  • 1
  • 1. you never select matrix which you are using expecting something like: `glMatrixMode(GL_MODELVIEW);` without it you are using lastly selected matrix so once your code select different one you get screwed ... 2. its absolutely unclear what you are trying to do and what is wrong with it. Your example renders single Quad so how can it show more? – Spektre Mar 15 '22 at 10:18
  • Im use glMatrixMode in resize func: `public void Resized() { gl.MatrixMode(GL_PROJECTION); gl.LoadIdentity(); float w = _width; float h = _height + 20; gl.Viewport(0, 0, (int)w, (int)h); gl.MatrixMode(GL_MODELVIEW); } ` – Vera Mar 15 '22 at 10:33
  • 1
    That is OK but once your code grows you might change the matrix elsewhere and such code relaying on setting it might get you headaches latter therefore its better to set it before use ... to avoid problems in future – Spektre Mar 15 '22 at 10:35
  • The current solution allows you to output two or more different images. But when trying to display two identical images, they overlap each other – Vera Mar 15 '22 at 10:35
  • The position of the vertices is different. For the sake of experiment, I first left the drawing code of the first image to make sure where it is, then I did the same with the second image. Each part of the code draws an image at different points, then when you try to draw them together, the first image is displayed in its place, and the second on top of it – Vera Mar 15 '22 at 10:41
  • That code you describe is not in your question ... You can use `glTranslate` to shift the next Quad to next position ... or change the hardcoded +/-1 position in your `glVertex` calls to use some `x,y` parameter passed along with `int idTexture` – Spektre Mar 15 '22 at 10:41
  • I don't know why, but when using glTranslate, I shift all the squares. As for the vertices, they are different for each idTexture. I made a simple motion function for the first image, each frame the first image changes position, while the second remains in place. In the console, I see that the coordinates of the vertices of the first image change, and the second one remains in place, but still I only see how the first image moves, and the second one is superimposed on it, although the coordinates of the vertices are different – Vera Mar 15 '22 at 10:49
  • that is because you push/pop the matrix at wrong place ... you should push matrix, for loop { render the quad, and shift position } pop matrix ... that is why I missing your code to render multiple Quads you just show function renderig single one in weird manner ... The problem lies in cooperation of `DrawTexture(int idTexture)` and code you did not share – Spektre Mar 15 '22 at 10:51
  • I have an array of texture parameters that I iterate through in the drawing cycle and send for rendering. Where will it be correct to place pop/push func?` public void DrawLoop() { gl.Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); DrawAllTextures(); OnDrawLoop?.Invoke(); gl.Flush(); }` – Vera Mar 15 '22 at 10:56
  • Here is the very part of the code: [link](https://imgur.com/a/SgKzAac) Here's how the first image is drawn: [link](https://imgur.com/a/AgtEI9i) Here's how the second image is drawn: [link](https://imgur.com/a/RP5YhMG) and here is the case when I call the rendering of two images at the same time: [link](https://imgur.com/a/LSkfo99) – Vera Mar 15 '22 at 11:08
  • what is inside `_textureParams[idTexture].texturePostitonX` and `_textureParams[idTexture].texturePostitonY` ? also in glVertex you are multiplying the offset position by scale? that might be wrong logic ... as the position will change with scale ... I expect only +/-1 should be multiplied by scale – Spektre Mar 15 '22 at 11:14
  • Thanks a lot, I found my mistake, it consisted of the function of setting the order of drawing. I wanted to ask finally if I did the right thing. Each texture has a sorting order parameter, with the help of it I wanted to adjust the sorting of the image, that is, if the images overlap, then there will be an image with a larger sorting order. I have not come up with anything better than calculating each frame which texture to send first for rendering, that is, the logic boils down to calculating in what sequence to call functions depending on the sorting order parameter. Is this the right way? – Vera Mar 15 '22 at 11:27
  • You can use depth buffer instead ... so no sorting you just provide z coordinate in range without perspective `<-1,+1>` and GL will do the rest ... just `glEnable(GL_DEPTH_TEST);` and have some bits allocated to depth buffer during GL context creation (16 or 24 bit is usual)... btw you might want to check this [complete GL+GLSL+VAO/VBO C++ example](https://stackoverflow.com/a/31913542/2521214) and this [Understanding 4x4 homogenous transform matrices](https://stackoverflow.com/a/28084380/2521214) for some inspiration and ideas – Spektre Mar 15 '22 at 11:55
  • also if you solved your problem you should post it as answer for others to see this is solved ... You can accept your own answer IIRC 2 days after posting ... – Spektre Mar 15 '22 at 12:00

0 Answers0