2013-09-21 4 views
0

프레임 버퍼로 텍스처 렌더링을 테스트하는 프로그램을 작성했습니다. render_texture() 함수에서 텍스처에 삼각형을 렌더링하고 싶지만 표시 함수에 렌더링 된 텍스처를 표시 할 때 나는 단순한 노란색 사각형을 얻습니다.opengl : 텍스처 렌더링이 잘못되었습니다.

#include "GL/glew.h" 
#include "GL/glext.h" 
#include "GL/glu.h" 
#include "GL/freeglut.h" 

#include <cstdio> 

uint16_t tex_width = 75; 
uint16_t tex_height = 24; 

GLuint texture; 


int w1; 
int h1; 

void orthogonalStart (int w, int h) { 
    glMatrixMode(GL_PROJECTION); 
    glPushMatrix(); 
    glLoadIdentity(); 
    gluOrtho2D(0, w, 0, h); 
    glScalef(1, -1, 1); 
    glTranslatef(0, -h1, 0); 
    glMatrixMode(GL_MODELVIEW); 
} 

void orthogonalEnd (void) { 
    glMatrixMode(GL_PROJECTION); 
    glPopMatrix(); 
    glMatrixMode(GL_MODELVIEW); 
} 

void display (void) { 
    glClearColor (0.0,0.0,0.0,1.0); 
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glLoadIdentity(); 

    orthogonalStart(w1, h1); 

    glBegin(GL_QUADS); 
    glTexCoord2f(0, 0); glVertex2f(125, 125); 
    glTexCoord2f(0, 1); glVertex2f(125, 125+tex_height); 
    glTexCoord2f(1, 1); glVertex2f(125+tex_width, 125+tex_height); 
    glTexCoord2f(1, 0); glVertex2f(125+tex_width, 125); 
    glEnd(); 

    orthogonalEnd(); 

    glutSwapBuffers(); 
} 

void reshape (int w, int h) { 
    glViewport (0, 0, (GLsizei)w, (GLsizei)h); 
    glMatrixMode (GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective (60, (GLfloat)w/(GLfloat)h, 0.1, 1000.0); 
    w1 = w; 
    h1 = h; 
    glMatrixMode (GL_MODELVIEW); 
} 

void render_texture() 
{ 
    GLuint framebuffer = 0; 

    glGenFramebuffers(1, &framebuffer); 
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); 

    glGenTextures(1, &texture); 

    glBindTexture(GL_TEXTURE_2D, texture); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_width, tex_height, 0, GL_RGBA, 
       GL_UNSIGNED_BYTE, 0); 

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 

    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0); 
    GLenum draw_buffers[1] = {GL_COLOR_ATTACHMENT0}; 
    glDrawBuffers(1, draw_buffers); 

    if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) 
    { 
     fprintf(stderr, "[render_texture] Fehler im Framebuffer\n"); 
     exit(1); 
    } 

    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); 
    glViewport(0, 0, tex_width, tex_height); 

    glClearColor (0.0,0.0,0.0,1.0); 

    orthogonalStart(tex_width, tex_height); 

    glColor4f(1, 1, 0, 0); 

    glBegin(GL_TRIANGLES); 
    glVertex2f(0.f,0.f); 
    glVertex2f(tex_width, 0.0f); 
    glVertex2f(tex_width, (GLfloat)tex_height/2.f); 
    //glVertex2f(tex_width-(GLfloat)tex_height/2.f, tex_height); 
    //glVertex2f(0, tex_height); 
    glEnd(); 

    orthogonalEnd(); 

    glBindFramebuffer(GL_FRAMEBUFFER, 0); 
} 

int main (int argc, char **argv) { 
    glutInit (&argc, argv); 
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA); 
    glutInitWindowSize (500, 500); 
    glutInitWindowPosition (100, 100); 
    glutCreateWindow (""); 
    glutDisplayFunc (display); 
    glutIdleFunc (display); 
    glutReshapeFunc (reshape); 

    glewExperimental=true; 
    GLenum err=glewInit(); 
    if(err!=GLEW_OK) 
    { 
     //Problem: glewInit failed, something is seriously wrong. 
     fprintf(stderr, "glewInit failed, aborting.\n"); 
    } 

    render_texture(); 

    glutMainLoop(); 
    return 0; 
} 

편집 1 : 지금 난 그냥 검은 색 빈 화면 당신은 명확하게 투영 행렬의 설정에서 뭔가 잘못하고있는

#include "GL/glew.h" 
#include "GL/glext.h" 
#include "GL/glu.h" 
#include "GL/freeglut.h" 

#include <cstdio> 

uint16_t tex_width = 75; 
uint16_t tex_height = 24; 

GLuint texture; 

void orthogonalStart (int w, int h) { 
    glMatrixMode(GL_PROJECTION); 
    glPushMatrix(); 
    glLoadIdentity(); 
    gluOrtho2D(0, w, 0, h); 
    glScalef(1, -1, 1); 
    glTranslatef(0, -glutGet(GLUT_WINDOW_HEIGHT), 0); 
    glMatrixMode(GL_MODELVIEW); 
} 

void orthogonalEnd (void) { 
    glMatrixMode(GL_PROJECTION); 
    glPopMatrix(); 
    glMatrixMode(GL_MODELVIEW); 
} 

void display (void) { 
    glClearColor (0.0,0.0,0.0,1.0); 
    glClear (GL_COLOR_BUFFER_BIT); 
    glLoadIdentity(); 

    orthogonalStart(glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT)); 

    glColor3f(1,1,1); 
    glEnable(GL_TEXTURE_2D); 
    glBindTexture(GL_TEXTURE_2D, texture); 

    glBegin(GL_QUADS); 
    glTexCoord2f(0, 0); glVertex2f(125, 125); 
    glTexCoord2f(0, 1); glVertex2f(125, 125+tex_height); 
    glTexCoord2f(1, 1); glVertex2f(125+tex_width, 125+tex_height); 
    glTexCoord2f(1, 0); glVertex2f(125+tex_width, 125); 
    glEnd(); 

    glDisable(GL_TEXTURE_2D); 

    orthogonalEnd(); 

    glutSwapBuffers(); 
} 

void reshape (int w, int h) { 
    glViewport (0, 0, (GLsizei)w, (GLsizei)h); 
    glMatrixMode (GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective (60, (GLfloat)w/(GLfloat)h, 0.1, 1000.0); 
    glMatrixMode (GL_MODELVIEW); 
} 

void render_texture() 
{ 
    GLuint framebuffer = 0; 

    glGenFramebuffers(1, &framebuffer); 
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); 

    glGenTextures(1, &texture); 

    glBindTexture(GL_TEXTURE_2D, texture); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_width, tex_height, 0, GL_RGBA, 
       GL_UNSIGNED_BYTE, 0); 

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 

    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0); 
    GLenum draw_buffers[1] = {GL_COLOR_ATTACHMENT0}; 
    glDrawBuffers(1, draw_buffers); 

    if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) 
    { 
     fprintf(stderr, "[render_texture] Fehler im Framebuffer\n"); 
     exit(1); 
    } 

    glViewport(0, 0, tex_width, tex_height); 
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); 

    orthogonalStart(tex_width, tex_height); 

    glColor4f(1, 1, 0, 0); 

    glBegin(GL_TRIANGLES); 
    glVertex2f(0.f,0.f); 
    glVertex2f(tex_width, 0.0f); 
    glVertex2f(tex_width, (GLfloat)tex_height/2.f); 
    //glVertex2f(tex_width-(GLfloat)tex_height/2.f, tex_height); 
    //glVertex2f(0, tex_height); 
    glEnd(); 

    orthogonalEnd(); 

    glBindFramebuffer(GL_FRAMEBUFFER, 0); 
} 

int main (int argc, char **argv) { 
    glutInit (&argc, argv); 
    glutInitContextVersion(3, 1); 
    glutInitContextProfile(GLUT_CORE_PROFILE); 
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA); 
    glutInitWindowSize (500, 500); 
    glutCreateWindow (""); 
    glutDisplayFunc (display); 
    glutIdleFunc (display); 
    glutReshapeFunc (reshape); 

    glewExperimental=true; 
    GLenum err=glewInit(); 
    if(err!=GLEW_OK) 
    { 
     //Problem: glewInit failed, something is seriously wrong. 
     fprintf(stderr, "glewInit failed, aborting.\n"); 
    } 

    //glEnable(GL_TEXTURE_2D); 

    render_texture(); 

    glutMainLoop(); 
    return 0; 
} 
+1

그냥 내 표준 제안 : 그 코드를 재구성에서 디스플레이로 이동하면 행렬을 밀고 게시하는 번거 로움을 줄일 수 있습니다. glutGet (GLUT_WINDOW_ {WIDTH, HEIGHT})를 사용하여 창 크기를 가져옵니다. – datenwolf

답변

0

이 코드를 업데이트했습니다. 왜 내가 glScalef() 및 glTranslatef()를 호출하는지 모르겠다. orthogonalStart() 함수에서이 줄을 주석 처리하면 삼각형이 생길 것이다.

(!) 참고에
void orthogonalStart (int w, int h) { 
    glMatrixMode(GL_PROJECTION); 
    glPushMatrix(); 
    glLoadIdentity(); 
    gluOrtho2D(0, w, 0, h); 
    //glScalef(1, -1, 1); 
    //glTranslatef(0, -glutGet(GLUT_WINDOW_HEIGHT), 0); 
    glMatrixMode(GL_MODELVIEW); 
} 

, glBindFramebuffer (GL_FRAMEBUFFER, 프레임 버퍼) 당신의 render_texture() 함수 호출에 2 시간을 호출 할 필요가 없습니다.

또한 오브젝트를 그리기 위해 버텍스 버퍼에 대해 배우기를 권합니다. 더 정교한 것은 here입니다.