简体   繁体   English

纹理化OpenGL / WebGL矩形

[英]Texturing OpenGL/WebGL rectangle

I have drawn a rectangle, which spins. 我画了一个旋转的矩形。 Also I have set the texture for it, but it does look like very ugly (very low resolution). 我也为其设置了纹理,但是它看起来确实很丑陋(非常低的分辨率)。

The table of vertex for rectangle is the next: 矩形的顶点表是下一个:

1.0,  1.0,  0.0,
-1.0,  1.0,  0.0,
1.0, -1.0,  0.0,
-1.0, -1.0,  0.0

This is the coordinates for the texture mapping the above rectangle: 这是上面矩形绘制纹理的坐标:

0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,

The size of texture is 512x512 (it's too large!!!, so there mustn't be such problems with the size exactly). 纹理的尺寸为512x512(太大!!!因此,尺寸一定不会出现此类问题)。

The full source code locates here: 完整的源代码位于:

http://pastebin.com/qXJFNe1c http://pastebin.com/qXJFNe1c

I clearly understand, that is my fault, but I don't get where exactly is the fault. 我清楚地知道,那是我的错,但是我不知道问题到底出在哪里。

PS I think, that such a problem isn't related strongly to WebGL, I think that some pure OpenGL developers could give me a piece of advice also. PS:我认为,这种问题与WebGL无关,我认为某些纯OpenGL开发人员也可以给我一些建议。

If you want to test it live, you may check it via: 如果要实时测试,可以通过以下方法进行检查:

http://goo.gl/YpXyPl http://goo.gl/YpXyPl

When testing, [.WebGLRenderingContext]RENDER WARNING: texture bound to texture unit 0 is not renderable. It maybe non-power-of-2 and have incompatible texture filtering or is not 'texture complete' 77.246.234.123:8893/plane/:1 测试时, [.WebGLRenderingContext]RENDER WARNING: texture bound to texture unit 0 is not renderable. It maybe non-power-of-2 and have incompatible texture filtering or is not 'texture complete' 77.246.234.123:8893/plane/:1 [.WebGLRenderingContext]RENDER WARNING: texture bound to texture unit 0 is not renderable. It maybe non-power-of-2 and have incompatible texture filtering or is not 'texture complete' 77.246.234.123:8893/plane/:1 , but it does render a texture at least, so maybe that's about something else. [.WebGLRenderingContext]RENDER WARNING: texture bound to texture unit 0 is not renderable. It maybe non-power-of-2 and have incompatible texture filtering or is not 'texture complete' 77.246.234.123:8893/plane/:1 ,但是它至少渲染了纹理,所以也许是其他原因。

Also the viewport seems to be 300 x 150. The canvas rendering width/height does not match the width/height within the html page. 此外,视口似乎是300 x150。画布渲染的宽度/高度与html页面中的宽度/高度不匹配。 Try this: 尝试这个:

function updateCanvasSize(canvas)
{
    if (canvas.width != canvas.clientWidth || canvas.height != canvas.clientHeight)
    {
        canvas.width = canvas.clientWidth;
        canvas.height = canvas.clientHeight;
        gl.viewportWidth = canvas.width;
        gl.viewportHeight = canvas.height;
        //might want to update the projection matrix to fix the new aspect ratio too
    }
}

and in the a draw or update function (I doubt this would impact performance)... 并在绘图或更新功能中(我怀疑这会影响性能)...

var canvas = document.getElementById("canvas-main"); //or store "canvas" globally
updateCanvasSize(canvas);

The incorrect canvas size occurs because at the time webGLStart is called, the canvas is actually 300x150 for whatever reason. 出现错误的画布大小是因为在调用webGLStart时,无论出于何种原因,画布实际上是300x150。 Either 1. You actually want a fixed size render target (give the canvas widht/height in pixels and all is well) or 2. you want it to take up the whole window, in which case the user may want to resize the window which needs to be handled (js probably has a resize event you could use too instead of polling). 1.您实际上想要一个固定大小的渲染目标(以像素为单位的画布宽度/高度,并且一切都很好),或者2.您希望它占据整个窗口,在这种情况下,用户可能希望调整窗口的大小,需要处理(js可能有一个resize事件,您也可以使用它来代替轮询)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM