2013-04-05 2 views
0

Если CCRenderTexture не является полным размером окна, выход glDrawArrays меньше и находится под странным углом. В моем тестовом коде диагональная линия должна проходить из угла в угол под углом 45 градусов. Как правильно рисовать эту гладкую линию? Я новичок в cocos2d, и любая помощь очень ценится.glDrawArrays искажает вывод на CCRenderTexture

//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

Я добавил вызов glViewport, чтобы установить вид на полный размер всего экрана. glDrawArrays теперь рисует гладкую линию с правильным размером и углом.

Смежные вопросы