2014-05-15 5 views
2

간단한 삼각형 렌더링에 문제가 있습니다. 삼각형이 없다는 것을 제외하고는 아래의 코드가 컴파일되어 실행됩니다. 검정색 배경. 당신이 쉐이더를 가지고 있지 않기 때문에삼각형이 렌더링되지 않습니다.

GLuint VBO; 

static void RenderSceneCB() 
{ 
    //glClear sets the bitplane area of the window to values previously selected by glClearColor, glClearDepth, and glClearStencil. 
    glClear(GL_COLOR_BUFFER_BIT); 

    glEnableVertexAttribArray(0); 
    glBindBuffer(GL_ARRAY_BUFFER, VBO); 
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); 

    glDrawArrays(GL_TRIANGLES, 0, 3); 

    glDisableVertexAttribArray(0); 

    //swaps the buffers of the current window if double buffered. 
    glutSwapBuffers(); 
} 

static void InitializeGlutCallbacks() 
{ 
    glutDisplayFunc(RenderSceneCB); 
} 

static void CreateVertexBuffer() 
{ 
    Vector3f Vertices[3]; 
    Vertices[0] = Vector3f(-1.0f, -1.0f, 0.0f); 
    Vertices[1] = Vector3f(1.0f, -1.0f, 0.0f); 
    Vertices[2] = Vector3f(0.0f, 1.0f, 0.0f); 

    glGenBuffers(1, &VBO); 
    glBindBuffer(GL_ARRAY_BUFFER, VBO); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW); 
} 

int main(int argc, char** argv) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA); 
    glutInitWindowSize(1024, 768); 
    glutInitWindowPosition(100, 100); 
    glutCreateWindow("Tutorial 02"); 

    InitializeGlutCallbacks(); 

    // Must be done after glut is initialized! 
    GLenum res = glewInit(); 
    if (res != GLEW_OK) { 
     fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res)); 
     return 1; 
    } 
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 

    CreateVertexBuffer(); 

    glutMainLoop(); 

    return 0; 
} 
+4

저는 OpenGL에 대해 처음 보았습니다 만, 쉐이더가 필요한 "새로운"OpenGL과 비슷합니다. 꽤 좋은 튜토리얼을 위해 나는 [this one] (http://arcsynthesis.org/gltut/index.html)을 추천하고, [ "Hello triangle"] (http://arcsynthesis.org/gltut)을 읽고 싶을지도 모른다. /Basics/Tutorial%2001.html) 장. –

+0

아마도 glClearColor()를 사용하여 전체 화면 색상을 변경하고 glClear'glLoadIdentity' 뒤에이 함수를 호출하십시오. – mr5

+0

@RetoKoradi - 고마워요. – SINGULARITY

답변

3

, OpenGL은 단지 등의 위치, 색상, 알고 아니라 일반적인 속성에 대한로, 정점 속성 0을 해석하는 방법을 알고하지 않습니다. 일부 GPU에서는 일반 속성을 동일한 "슬롯"에 매핑하므로 0이 위치로 해석됩니다 (일반적으로 NVidia는 이러한 문제에 덜 엄격합니다).

당신은 CreateVertexBuffer(); 뒤에 다음 코드를 넣어 MiniShader을 사용할 수 있습니다 : 나는 경우 내 머리의 상단에서 약간의 오류가 쓴

minish::InitializeGLExts(); // initialize shading extensions 
const char *vs = 
    "layout(location = 0) in vec3 pos;\n" 
    // the layout specifier binds this variable to vertex attribute 0 
    "void main()\n" 
    "{\n" 
    " gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 1.0);\n" 
    "}\n"; 
const char *fs = "void main() { gl_FragColor=vec4(.0, 1.0, .0, 1.0); }"; // green 
minish::CompileShader(vs, fs); 

참고, 의견을주십시오.

+0

이것이 중복으로 확인 되었기 때문에 원래 질문에 대한 답변을 추가하는 것이 더 좋았습니까? 나는 그 코드를 이전 질문의 것과 구분 짓고, 그것은 절대적으로 동일하다. –

+0

@RetoKoradi 네 말이 맞아, 나는 그 사본을 발견하지 못했고, 내 대답을 쓴 후에 그 질문은 표시되었다. 나는 내 대답을 연장 http://stackoverflow.com/questions/23097007/how-can-i-make-opengl-draw-something-using-vbos-in-xcode/23696380#23696380. –

관련 문제