如果CCRenderTexture不是完整的窗口大小,则glDrawArrays输出较小并且处于奇怪的角度。在我的测试代码中,对角线应该以45度角从一个角落到另一个角落。如何正确绘制这条平滑线?我是cocos2d的新手,非常感谢任何帮助。
//compute vertex points for smooth line triangle strip
CGPoint start = CGPointMake(0., 0.);
CGPoint end = CGPointMake(200., 200.);
float lineWidth = 10.0;
float deltaX = end.x - start.x;
float deltaY = end.y - start.y;
float length = sqrtf(deltaX*deltaX+deltaY*deltaY);
if (length < 0.25) return; //line too small to show on display
float offsetX = -lineWidth*deltaY/length;
float offsetY = lineWidth*deltaX/length;
GLfloat lineVertices[12]; //6 vertices x,y values
lineVertices[0] = start.x + offsetX;
lineVertices[1] = start.y + offsetY;
lineVertices[2] = end.x + offsetX;
lineVertices[3] = end.y + offsetY;
lineVertices[4] = start.x;
lineVertices[5] = start.y;
lineVertices[6] = end.x;
lineVertices[7] = end.y;
lineVertices[8] = start.x - offsetX;
lineVertices[9] = start.y - offsetY;
lineVertices[10] = end.x - offsetX;
lineVertices[11] = end.y - offsetY;
ccColor4F colorVertices[6];
ccColor4F color1 = {1., 0., 0., 0.};
ccColor4F color2 = {1., 0., 0., 1.};
colorVertices[0] = color1;
colorVertices[1] = color1;
colorVertices[2] = color2;
colorVertices[3] = color2;
colorVertices[4] = color1;
colorVertices[5] = color1;
CCRenderTexture *rtx = [CCRenderTexture renderTextureWithWidth:200 height:200];
[rtx beginWithClear:1. g:1. b:1. a:1.];
[shaderProgram_ use];
ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position | kCCVertexAttribFlag_Color);
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, lineVertices);
glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_FALSE, 0, colorVertices);
glViewport(0,0, screenWidth, screenHeight); //dimensions of main screen
glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);
[rtx end];
[rtx saveToFile:@"lineDrawTest" format:kCCImageFormatPNG];
答案 0 :(得分:0)
我添加了一个glViewport调用,将视图设置为原始全屏大小。 glDrawArrays现在绘制具有正确大小和角度的平滑线。