本文介绍了sf ::纹理以错误的方式应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 在我的2D等距引擎中,我有以下类: `maps立方体(可变)/边(6)/点(4)/坐标(3)` code> points (1 点 = 1 z))。 每个立方体包含6个边。 我可以创建一个地图, assets / numTexture / numLight.png > I calcule with numTexture and numLight a number that we is the numberNumberEntry(i loaded all numLight.png(textures)in a array)。 错误: 我在层类: for(int J = 0; J { for(int I = 0; I { x =(J-I)*(cubeSize / 2) y =(J + I)*(cubeSize / 4); c = new cube(cubeSize,x,y,z,I,J); cs.push_back(* c); } } 在side.cpp中, (如果它的顶部,左边等等...我定义不同的我的点的坐标)。我有6个每个立方体(只有数据)像这样: switch(typeSide) { case 0:// DOWN_SIDE light = 0; tmp_x = x +(size / 2); tmp_y = y +(size / 2); p0 =新点(tmp_x,tmp_y,tmp_z); tmp_x = x + size; tmp_y = y +(3 *(size / 4)); p1 =新点(tmp_x,tmp_y,tmp_z); tmp_x = x +(size / 2); tmp_y = y + size; p2 =新点(tmp_x,tmp_y,tmp_z); tmp_x = x; tmp_y = y +(3 *(size / 4)); p3 =新点(tmp_x,tmp_y,tmp_z); break; // ETC。 .... 并且函数display()显示地图: void GRAPHICS_HANDLER :: display() {x = 0; y = 0; if(maps.size()> 0&& maps [0] .layers()。size()> 0)//如果有任何地图和图层要显示 { for(int l = 0; l { for int c = 0; c< maps [currentMapID] .layers()[l] .cubes()。size(); c ++)//对于每个CUBES { if(maps [currentMapID] .layers()[l] .cubes()[c] .getFlat())//如果启用了平面模式:绘制纹理像草等等(参见屏幕):我们只画一边 { for(unsigned int p = 0; p< 4; p ++)//对于每个点 { // ------------ -------------------------------------------------- -----------------------载入地图------------------------ -------------------------------------------------- --------------------------------- // // --------- -------------------------------------------------- --------------- CUBE CLICK DETECTION ::纹理变化---------------------------- ---------------------- // if(cubeClicked || brushMode&& currentSelectedCube> -1&&& currentSelectedCube<地图[currentMapID] .layers()[maps [currentMapID] .currentLayerId()]。cubes()。size()) { maps [currentMapID] .layers()[maps [currentMapID]。 currentLayerId()]。cubes()[currentSelectedCube] .setTexture(currentSelectedTexture); if(!brushMode) cubeClicked = false; } // --------------------------------------- ----------------------------------- CUBE CLICK DETECTION :: TEXTURE CHANGE -------- ------------------------------------------ // // -------------------------------------------------- ------------------------------------ CURSOR - NOT WORKING ---------- -------------------------------------------------- -------------------------------------------------- --- // // ... // ------------------------------ -------------------------------------------------- ------光标 - 不工作---------------------------------------- -------------------------------------------------- ----------------------- * / if(enableOffset) {x = maps [currentMapID ] .layers()[l] .cubes()[c] .sides()[0] - > pointPosition(p)[0] + offsetLeft; //它像做点[ left y = maps [currentMapID] .layers()[l] .cubes()[c] .sides()[0] - > pointPosition(p)[1] + offsetTop; } else {x = maps [currentMapID] .layers()[l] .cubes()[c] .sides()[0] - > pointPosition p)[0]; //它像做点[p] .x + offset left y = maps [currentMapID] .layers()[l] .cubes()[c] .sides 0] - > pointPosition(p)[1]; } points [p] .position = sf :: Vector2f(x,y); points [p] .texCoords = sf :: Vector2f(x,y); // ---------------------------------------- - - - - - - - - - - - - - - - - - - - - - - - 格 - - -------------------------------------------------- -------------------------------------------------- ---------- // //网格显示//边缘错误。 isoGrid [p] .position = points [p] .position; isoGrid [p] .color = sf :: Color(195,195,195,gridOpacity); // -------------------------------------------- - - - - - - - - - - - - - - - - - - - - - 格 - - - - -------------------------------------------------- -------------------------------------------------- ------ // maps [currentMapID] .layers()[l] .cubes()[c] .sides()[0] - > setLight(5) textureEntryNumber =(maps [currentMapID] .layers()[l] .cubes()[c] .sides()[0] - > getTexture() - 1)* 9 +(maps [currentMapID]。 layers()[l] .cubes()[c] .sides()[0] - > getLight() - 1); // WRONG // - -------------------------------------------------- - - - - - - - - - - - - -格 - - - - - - - - - - - - -------------------------------------------------- --------------------------------------- // // --- -------------------------------------------------- ---------------------------------载入地图--------------- -------------------------------------------------- ------------------------------------------ // } // --------------------------------------------- -----------------------------------------显示地图------- -------------------------------------------------- -------------------------------------------------- // if(grid&& maps [currentMapID] .layers()[1] .cubes()[c] .sides()[0] - > getTexture()< = 1)/ / IF GRID = TRUE或TEXTURE LESS或EQUAL TO 1 => DISPLAY GRID { if(l == maps [currentMapID] .currentLayerId()) { window-> draw(isoGrid); } } else if(maps [currentMapID] .layers()[l] .cubes()[c] .sides()[0] - > getTexture()> 1)// if THERE IS ANY TEXTURE TO DISPLAY(> 1)=> DISPLAY TEXTURE { window-> draw(points,& textures [textureEntryNumber]); } // --------------------------------------- -------------------------------------------显示MAP- -------------------------------------------------- -------------------------------------------------- ------ // } else { for(unsigned int s = 0; s { for(unsigned int p = 0; p { / / ------------------------------------------------- -------------------------------------载入地图----------- -------------------------------------------------- ---------------------------------------------- // // ---------------------------------------------- ---------------------------- CUBE CLICK DETECTION ::纹理变化--------------- ----------------------------------- // if(cubeClicked ||。) brushMode&& currentSelectedCube> -1&& currentSelectedCube<地图[currentMapID] .layers()[maps [currentMapID] .currentLayerId()]。cubes()。size()) { maps [currentMapID] .layers()[maps [currentMapID]。 currentLayerId()]。cubes()[currentSelectedCube] .setTexture(currentSelectedTexture); if(flatMode) maps [currentMapID] .layers()[maps [currentMapID] .currentLayerId()]。cubes()[currentSelectedCube] .setFlat(true); else maps [currentMapID] .layers()[maps [currentMapID] .currentLayerId()]。cubes()[currentSelectedCube] .setFlat(false); if(!brushMode) cubeClicked = false; } // --------------------------------------- ----------------------------------- CUBE CLICK DETECTION :: TEXTURE CHANGE -------- ------------------------------------------ // // -------------------------------------------------- ------------------------------------ CURSOR - NOT WORKING ---------- -------------------------------------------------- -------------------------------------------------- --- // // ... // ------------------------------ -------------------------------------------------- ------光标 - 不工作---------------------------------------- -------------------------------------------------- ----------------------- * / if(enableOffset) {x = maps [currentMapID ] .layers()[l] .cubes()[c] .sides()[s] - > pointPosition(p)[0] + offsetLeft; //它像做点[ left y = maps [currentMapID] .layers()[l] .cubes()[c] .sides()[s] - > pointPosition(p)[1] + offsetTop; } else {x = maps [currentMapID] .layers()[l] .cubes()[c] .sides()[s] - > pointPosition p)[0]; //它像做点[p] .x + offset left y = maps [currentMapID] .layers()[l] .cubes()[c] .sides s]→> pointPosition(p)[1]; } points [p] .position = sf :: Vector2f(x,y); points [p] .texCoords = sf :: Vector2f(x,y); // ---------------------------------------- - - - - - - - - - - - - - - - - - - - - - - - 格 - - -------------------------------------------------- -------------------------------------------------- ---------- // //网格显示//边缘错误 if(s == 3) { isoGrid [p]。 position = points [p] .position; isoGrid [p] .color = sf :: Color(195,195,195,gridOpacity); } // --------------------------------------- - - - - - - - - - - - - - - - - - - - - - - - -格 - -------------------------------------------------- -------------------------------------------------- ----------- // textureEntryNumber =(maps [currentMapID] .layers()[l] .cubes()[c] .sides()[s] - > getTexture() - 1)* 9 +(maps [currentMapID] .layers()[1] .cubes()[c] .sides()[s] - > getLight // -------------------------------------------- - - - - - - - - - - - - - - - - - - - - - 格 - - - - -------------------------------------------------- -------------------------------------------------- ------ // // ------------------------------------ -------------------------------------------------- LOAD MAP ------------------------------------------------ -------------------------------------------------- --------- // } // ---------------------------- -------------------------------------------------- --------显示地图---------------------------------------- -------------------------------------------------- ----------------- // if(grid&&如果GRID = TRUE或者TEXTURE LESS或者EQUAL TO 1 = 1,则当前地图中的当前地图数据集的地图数据格式为: > DISPLAY GRID { if(l == maps [currentMapID] .currentLayerId()) { window-> draw(isoGrid); } } else if(maps [currentMapID] .layers()[l] .cubes()[c] .sides()[s] - > getTexture()> 1)// if THERE IS ANY TEXTURE TO DISPLAY(> 1)=> DISPLAY TEXTURE { window-> draw(points,& textures [textureEntryNumber]); } // --------------------------------------- -------------------------------------------显示MAP- -------------------------------------------------- -------------------------------------------------- ------ // } } } } } window-> display(); } 滚动这一个以查看评论,以及标签 问题: 当纹理与细节,没有问题与一个颜色纹理(可能的顶点bouding在一起,甚至从不同 sf :: VertexArray 。 注意:像maps / layers / cubes / etc。这样的类只是数据。 应在屏幕中显示的纹理(numLIght)图像应如下所示: 编辑:图片只有颜色而不含详细信息时纹理会起作用: 我不知道我的代码出了什么问题。也许,我会重新编码函数display()... 解决方案在纹理外面。 如果pointPosition函数引用side.cpp中的p0,p1,p2和p3点,那么它也看起来像是使用这些点作为纹理坐标。 考虑如何创建一个二维方形 - //创建一个顶点数组作为四边形。 sf :: VertexArray vertices; vertices.setPrimitiveType(sf :: Quads); vertices.resize(4); //平方位置值(与sf :: Vertex :: position相同)。 sf :: Vector2f v0 = sf :: Vector2f(10,10); sf :: Vector2f v1 = sf :: Vector2f(200,10); sf :: Vector2f v2 = sf :: Vector2f(200,200); sf :: Vector2f v3 = sf :: Vector2f(10,200); //纹理坐标(与sf :: Vertex :: texCoords相同)。 sf :: Vector2f tv0 = sf :: Vector2f(0,0); sf :: Vector2f tv1 = sf :: Vector2f(0 + tex.getSize()。x,0); sf :: Vector2f tv2 = sf :: Vector2f(0 + tex.getSize()。x,tex.getSize()。y); sf :: Vector2f tv3 = sf :: Vector2f(0,tex.getSize()。y); //将它们放在顶点。 vertices [0] = sf :: Vertex(v0,tv0); vertices [1] = sf :: Vertex(v1,tv1); vertices [2] = sf :: Vertex(v2,tv2); vertices [3] = sf :: Vertex(v3,tv3); 方形点不必与纹理坐标相同,因为纹理坐标拉伸到每个方点。 BEFORE 如果您要更改其中一个方形点的点, > AFTER(v2更改为200,300) a href =http://i.stack.imgur.com/x0JSD.png =nofollow> 我们不需要改变纹理坐标,只需改变顶点位置。 我不知道这是不是因为我看不到pointPosition是什么,但这是我最好的猜测。 此外,尝试在纹理外绘制可能导致看起来像图像的边框像素被拉伸(可能是您的示例中发生了什么),有时你甚至可以显示来自视频内存中其他信息的数据,因为超出了纹理的内存。 In my 2D isometric engine, I have the following classes :`maps(variable)/layers(variable)/cubes(variable)/sides(6)/points(4)/coordinates(3)`Each sides contains 4 points(1 point = 1 coordinate(x, y,z)).Each cubes contains 6 sides.I can create a map with the size i want with cubes(same, the size i want).Folers:assets/numTexture/numLight.pngI calcule with numTexture and numLight a number wich is the textureNumberEntry(i loaded all numLight.png(textures) in an array).But texturing gones wrong:I define my cubes coordinates in the layer class :for(int J = 0; J < mapSize; J++){ for(int I = 0; I < mapSize; I++) { x = (J - I) * (cubeSize/2); y = (J + I) * (cubeSize/4); c = new cube(cubeSize, x, y, z, I, J); cs.push_back(*c); }}In side.cpp, I have a switch on sideType(if it's the top, left, etc... I define differently my points' coordinates). I have 6 of them for each cubes(only data here)Like this : switch(typeSide) { case 0://DOWN_SIDE light = 0; tmp_x = x + (size/2); tmp_y = y + (size/2); p0 = new point(tmp_x, tmp_y, tmp_z); tmp_x = x + size; tmp_y = y + (3 * (size/4)); p1 = new point(tmp_x, tmp_y, tmp_z); tmp_x = x + (size/2); tmp_y = y + size; p2 = new point(tmp_x, tmp_y, tmp_z); tmp_x = x; tmp_y = y + (3 * (size/4)); p3 = new point(tmp_x, tmp_y, tmp_z); break;//ETC. ....And function display() to display the map:void GRAPHICS_HANDLER::display(){ x = 0; y = 0; if(maps.size() > 0 && maps[0].layers().size() > 0)//If there is any map and layers to display { for(int l = 0; l <= getCurrentLayerID(); l++)//FOR EACH LAYER, WE STOP TO THE CURRENT EDITED LAYER { for(unsigned int c = 0; c < maps[currentMapID].layers()[l].cubes().size(); c++)//FOR EACH CUBES { if(maps[currentMapID].layers()[l].cubes()[c].getFlat())//If flat mode is enabled: to draw texture like grass, etc...(cf. screen): We draw only one side { for(unsigned int p = 0; p < 4; p++)//FOR EACH POINTS {//--------------------------------------------------------------------------------------LOAD MAP-----------------------------------------------------------------------------------------------------------////--------------------------------------------------------------------------CUBE CLICK DETECTION::TEXTURE CHANGE--------------------------------------------------// if(cubeClicked || brushMode && currentSelectedCube > -1 && currentSelectedCube < maps[currentMapID].layers()[maps[currentMapID].currentLayerId()].cubes().size()) { maps[currentMapID].layers()[maps[currentMapID].currentLayerId()].cubes()[currentSelectedCube].setTexture(currentSelectedTexture); if(!brushMode) cubeClicked = false; }//--------------------------------------------------------------------------CUBE CLICK DETECTION::TEXTURE CHANGE--------------------------------------------------////--------------------------------------------------------------------------------------CURSOR - NOT WORKING-----------------------------------------------------------------------------------------------------------------// //...//--------------------------------------------------------------------------------------CURSOR - NOT WORKING-----------------------------------------------------------------------------------------------------------------*/ if(enableOffset) { x = maps[currentMapID].layers()[l].cubes()[c].sides()[0]->pointPosition(p)[0] + offsetLeft;//it's like doing something like point[p].x + offset left y = maps[currentMapID].layers()[l].cubes()[c].sides()[0]->pointPosition(p)[1] + offsetTop; } else { x = maps[currentMapID].layers()[l].cubes()[c].sides()[0]->pointPosition(p)[0];//it's like doing something like point[p].x + offset left y = maps[currentMapID].layers()[l].cubes()[c].sides()[0]->pointPosition(p)[1]; } points[p].position = sf::Vector2f(x, y); points[p].texCoords = sf::Vector2f(x, y);//--------------------------------------------------------------------------------------GRID-----------------------------------------------------------------------------------------------------------------// //GRID DISPLAY //MISS AN EDGE . isoGrid[p].position = points[p].position; isoGrid[p].color = sf::Color(195, 195, 195, gridOpacity);//--------------------------------------------------------------------------------------GRID-----------------------------------------------------------------------------------------------------------------// maps[currentMapID].layers()[l].cubes()[c].sides()[0]->setLight(5); textureEntryNumber = (maps[currentMapID].layers()[l].cubes()[c].sides()[0]->getTexture() - 1) * 9 + (maps[currentMapID].layers()[l].cubes()[c].sides()[0]->getLight() - 1);//WRONG//--------------------------------------------------------------------------------------GRID-----------------------------------------------------------------------------------------------------------------////--------------------------------------------------------------------------------------LOAD MAP-----------------------------------------------------------------------------------------------------------// }//--------------------------------------------------------------------------------------DISPLAY MAP-----------------------------------------------------------------------------------------------------------// if(grid && maps[currentMapID].layers()[l].cubes()[c].sides()[0]->getTexture() <= 1)//IF GRID = TRUE OR TEXTURE LESS OR EQUAL TO 1 => DISPLAY GRID { if(l == maps[currentMapID].currentLayerId()) { window->draw(isoGrid); } } else if(maps[currentMapID].layers()[l].cubes()[c].sides()[0]->getTexture() > 1)//IF THERE IS ANY TEXTURE TO DISPLAY(>1) => DISPLAY TEXTURE { window->draw(points, &textures[textureEntryNumber]); }//--------------------------------------------------------------------------------------DISPLAY MAP-----------------------------------------------------------------------------------------------------------// } else { for(unsigned int s = 0; s < 6; s++)//FOR EACH SIDES(side number will always be 6, no need to make this dynamic { for(unsigned int p = 0; p < 4; p++)//FOR EACH POINTS {//--------------------------------------------------------------------------------------LOAD MAP-----------------------------------------------------------------------------------------------------------////--------------------------------------------------------------------------CUBE CLICK DETECTION::TEXTURE CHANGE--------------------------------------------------// if(cubeClicked || brushMode && currentSelectedCube > -1 && currentSelectedCube < maps[currentMapID].layers()[maps[currentMapID].currentLayerId()].cubes().size()) { maps[currentMapID].layers()[maps[currentMapID].currentLayerId()].cubes()[currentSelectedCube].setTexture(currentSelectedTexture); if(flatMode) maps[currentMapID].layers()[maps[currentMapID].currentLayerId()].cubes()[currentSelectedCube].setFlat(true); else maps[currentMapID].layers()[maps[currentMapID].currentLayerId()].cubes()[currentSelectedCube].setFlat(false); if(!brushMode) cubeClicked = false; }//--------------------------------------------------------------------------CUBE CLICK DETECTION::TEXTURE CHANGE--------------------------------------------------////--------------------------------------------------------------------------------------CURSOR - NOT WORKING-----------------------------------------------------------------------------------------------------------------// //...//--------------------------------------------------------------------------------------CURSOR - NOT WORKING-----------------------------------------------------------------------------------------------------------------*/ if(enableOffset) { x = maps[currentMapID].layers()[l].cubes()[c].sides()[s]->pointPosition(p)[0] + offsetLeft;//it's like doing something like point[p].x + offset left y = maps[currentMapID].layers()[l].cubes()[c].sides()[s]->pointPosition(p)[1] + offsetTop; } else { x = maps[currentMapID].layers()[l].cubes()[c].sides()[s]->pointPosition(p)[0];//it's like doing something like point[p].x + offset left y = maps[currentMapID].layers()[l].cubes()[c].sides()[s]->pointPosition(p)[1]; } points[p].position = sf::Vector2f(x, y); points[p].texCoords = sf::Vector2f(x, y);//--------------------------------------------------------------------------------------GRID-----------------------------------------------------------------------------------------------------------------// //GRID DISPLAY //MISS AN EDGE if(s ==3) { isoGrid[p].position = points[p].position; isoGrid[p].color = sf::Color(195, 195, 195, gridOpacity); }//--------------------------------------------------------------------------------------GRID-----------------------------------------------------------------------------------------------------------------// textureEntryNumber = (maps[currentMapID].layers()[l].cubes()[c].sides()[s]->getTexture() - 1) * 9 + (maps[currentMapID].layers()[l].cubes()[c].sides()[s]->getLight() - 1);//WRONG//--------------------------------------------------------------------------------------GRID-----------------------------------------------------------------------------------------------------------------////--------------------------------------------------------------------------------------LOAD MAP-----------------------------------------------------------------------------------------------------------// }//--------------------------------------------------------------------------------------DISPLAY MAP-----------------------------------------------------------------------------------------------------------// if(grid && maps[currentMapID].layers()[l].cubes()[c].sides()[s]->getTexture() <= 1)//IF GRID = TRUE OR TEXTURE LESS OR EQUAL TO 1 => DISPLAY GRID { if(l == maps[currentMapID].currentLayerId()) { window->draw(isoGrid); } } else if(maps[currentMapID].layers()[l].cubes()[c].sides()[s]->getTexture() > 1)//IF THERE IS ANY TEXTURE TO DISPLAY(>1) => DISPLAY TEXTURE { window->draw(points, &textures[textureEntryNumber]); }//--------------------------------------------------------------------------------------DISPLAY MAP-----------------------------------------------------------------------------------------------------------// } } } } } window->display();}Scroll this one to see comments, and the labelsProblems:Texture enlarged only when texture with details, no problem with one color texture(probably the vertex bouding together, even from different sf::VertexArray.Note: class like maps/layers/cubes/etc. ... are only data.Textures(numLIght) images which should be displayed in the screen look like this:EDIT: Textures work when the picture is only a color, without details :I don't know anymore what's wrong with my code. Maybe, i'll recode the function display()... 解决方案 It looks like the texture coordinates might be outside of the texture. If the pointPosition function refers to the p0, p1, p2, and p3 points in side.cpp, then it also looks like you're using those points as texture coordinates.Consider how you would might create a 2D square-//Create a VertexArray as quads.sf::VertexArray vertices;vertices.setPrimitiveType(sf::Quads);vertices.resize(4);//The square position values (same as sf::Vertex::position).sf::Vector2f v0 = sf::Vector2f(10, 10);sf::Vector2f v1 = sf::Vector2f(200, 10);sf::Vector2f v2 = sf::Vector2f(200, 200);sf::Vector2f v3 = sf::Vector2f(10, 200);//The texture coordinates (same as sf::Vertex::texCoords).sf::Vector2f tv0 = sf::Vector2f(0, 0 );sf::Vector2f tv1 = sf::Vector2f(0+tex.getSize().x, 0 );sf::Vector2f tv2 = sf::Vector2f(0+tex.getSize().x, tex.getSize().y);sf::Vector2f tv3 = sf::Vector2f(0, tex.getSize().y);//Put them in vertices.vertices[0] = sf::Vertex(v0,tv0);vertices[1] = sf::Vertex(v1,tv1);vertices[2] = sf::Vertex(v2,tv2);vertices[3] = sf::Vertex(v3,tv3);The square points don't have to be the same as the texture coordinates, since the texture coordinates will stretch to each square point. If you were to change a point in one of the square points, it may look like this.BEFOREAFTER (v2 changed to 200,300)We don't need to change the texture coordinates at all, just the vertex positions.I'm not sure if that's exactly what's going on here since I can't see what pointPosition is, but it's my best guess. Also, trying to draw outside of a texture can result in it looking like the border pixels of the image are stretched (possibly what's happening in your example), and sometimes you can even display data from other information in video memory as a result of going beyond your texture's memory. 这篇关于sf ::纹理以错误的方式应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-23 06:18