2012-07-23 60 views
0

我正在學習opengl es 2.0。 我正在嘗試在opengl es 2.0中應用正投影 我畫了一個sqaure,但實際上屏幕上沒有方形。 而我不確定我失蹤的部分。 謝謝你的幫助! setupRenderingEnv中有一些我沒有發佈的方法。但這些方法是用於設置框架,它工作正常。 和m_program被創建好。 再次感謝您的幫助。ios opengl es 2.0,當我申請ortho時,我的矩形不是正方形

// my vertex shader 
attribute vec4 Position; 
attribute vec4 SourceColor; 

uniform mat4 Projection; 

varying vec4 DestinationColor; 

void main(void) 
{ 
    DestinationColor = SourceColor; 
    gl_Position = Projection * Position; 
} 

// my drawing file 
typedef struct 
{ 
    float Position[3]; 
    float Color[4]; 
}Vertex; 

const Vertex Vertices[] = 
{ 
    {{100, -100, 0}, {1, 0, 0, 1}}, 
    {{100, 100, 0}, {0, 1, 0, 1}}, 
    {{-100, 100, 0}, {0, 0, 1, 1}}, 
    {{-100, -100, 0}, {0, 0, 0, 1}} 
}; 

const GLubyte Indices[] = 
{ 
    0, 1, 2, 
    2, 3, 0 
}; 

- (void)setupRenderingEnv 
{ 
    [super setupRenderingEnv]; 
    [self setupVertexBufferObjects]; 
    [self setupRunLoop]; 

    [self applyOrthoWithX:self.frame.size.width andY:self.frame.size.height]; 

    glViewport(0, 0, self.frame.size.width, self.frame.size.height); 

} 


//-- used for applying ortho in opengl es 2.0 
- (void)applyOrthoWithX:(float)maxX andY:(float)maxY 
{ 
    float a = 1.0f/maxX; 
    float b = 1.0f/maxY; 
    float ortho[16] = 
    { 
     a, 0, 0, 0, 
     0, b, 0, 0, 
     0, 0, -1, 0, 
     0, 0, 0, 1 
    }; 

    GLint projectionUniform = glGetUniformLocation(super.m_programHandle, "Projection"); 
    glUniformMatrix4fv(projectionUniform, 1, 0, &ortho[0]); 
} 


//-- overriding drawCandle. it render image, draw candle 
- (void)drawCandle 
{ 
    glClearColor(0, 104.0/255, 55.0/255, 1.0); 
    glClear(GL_COLOR_BUFFER_BIT); 

    GLuint positionSlot = glGetAttribLocation(super.m_programHandle, "Position"); 
    GLuint colorSlot = glGetAttribLocation(super.m_programHandle, "SourceColor"); 

    glEnableVertexAttribArray(positionSlot); 
    glEnableVertexAttribArray(colorSlot); 

    glVertexAttribPointer(positionSlot, 3, GL_FLOAT, GL_FALSE, 
          sizeof(Vertex), 0); 
    glVertexAttribPointer(colorSlot, 4, GL_FLOAT, GL_FALSE, 
          sizeof(Vertex), (GLvoid *)(sizeof(float) *  3)); 


    glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0); 

    glDisableVertexAttribArray(positionSlot); 
    glDisableVertexAttribArray(colorSlot); 

    [super drawCandle]; 
} 

回答

2

是什麼形狀的視口?如果不是正方形,那就是問題所在。您創建的矩陣 - 通過反向寬度和反向高度進行縮放 - 將使寬度始終爲1單位寬度,高度爲1單位高度。如果寬度和高度不是相同的像素數量,那麼正方形將不會畫平方。您需要考慮視口的縱橫比。關閉我的頭頂,我認爲這將是更多的東西是這樣的:

float ortho [ 16 ] = { 
    a/b, 0, 0, 0, 
    0, b, 0, 0, 
    0, 0, -1, 0, 
    0, 0, 0, 1 
}; 

(我可能有A/B倒 - 記不清了)

+0

我跑這在iPhone模擬器5.1 – 2012-07-23 15:07:21

+0

而視口是屏幕尺寸。 – 2012-07-23 15:08:06

+0

這是320×480 ..但這真的是問題?? – 2012-07-23 15:12:15

相關問題