2013-10-28 78 views
0

更新2級的作品,這是一個錯誤的阿勒特問題產生的紋理

更新2(VBO使用和vertex-片段着色器),但它仍然沒有工作

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

#include <iostream> 

GLint attribute; 
GLuint program; 

void gen_texture(GLuint &texture, int width, int height) 
{ 
    GLuint fb; 

    glGenFramebuffers(1, &fb); 
    glGenTextures(1, &texture); 

    glBindFramebuffer(GL_FRAMEBUFFER, fb); 

    glBindTexture(GL_TEXTURE_2D, texture); 
    glTexImage2D(GL_TEXTURE_2D, 
       0, 
       GL_RGBA, 
       width, height, 
       0, 
       GL_RGBA, 
       GL_UNSIGNED_BYTE, 
       0); 

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
    glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0); 

    glBindTexture(GL_TEXTURE_2D, 0); 
    glEnable(GL_TEXTURE_2D); 
    glBindFramebuffer(GL_FRAMEBUFFER, fb); 

    glClearColor(1,1,1,0); 
    glClear(GL_COLOR_BUFFER_BIT); 

    glUseProgram(program); 
    glEnableVertexAttribArray(attribute); 
    GLfloat vertex_data[] 
    { 
     1.0f, 0.0f, 
     0.0f, 0.0f, 
     0.0f, 1.0f, 

     1.0f, 0.0f, 
     0.0f, 1.0f, 
     1.0f, 1.0f 
    }; 

    glVertexAttribPointer(
     attribute, 
     2, 
     GL_FLOAT, 
     GL_FALSE, 
     0, 
     vertex_data 
    ); 

    glDrawArrays(GL_TRIANGLES, 0, 6); 
    glDisableVertexAttribArray(attribute); 
} 

void init_layout() 
{ 
    GLint compile_ok = GL_FALSE, link_ok = GL_FALSE; 

    GLuint vs, fs; 

    vs = glCreateShader(GL_VERTEX_SHADER); 
    const char *vs_source = 
    "#version 120\n" // OpenGL 2.1 
    "attribute vec2 coord2d;     " 
    "void main(void) {      " 
    " gl_Position = vec4(coord2d, 0.0, 1.0); " 
    "}"; 
    glShaderSource(vs, 1, &vs_source, 0); 
    glCompileShader(vs); 
    glGetShaderiv(vs, GL_COMPILE_STATUS, &compile_ok); 
    if (0 == compile_ok) 
    { 
     std::cerr << "[texture_layout/init_layout] fehler im vertex shader\n"; 
     exit(1); 
    } 

    fs = glCreateShader(GL_FRAGMENT_SHADER); 
    const char *fs_source = 
    "#version 120\n" // OpenGL 2.1 
    "void main(void) {  " 
    " gl_FragColor[0] = 0.8f; " 
    " gl_FragColor[1] = 0.5f;" 
    " gl_FragColor[2] = 0.0f; " 
    "}"; 
    glShaderSource(fs, 1, &fs_source, 0); 
    glCompileShader(fs); 
    glGetShaderiv(fs, GL_COMPILE_STATUS, &compile_ok); 
    if (0 == compile_ok) 
    { 
     std::cerr << "[texture_layout/init_layout] fehler im fragment shader\n"; 
     exit(1); 
    } 

    program = glCreateProgram(); 
    glAttachShader(program, vs); 
    glAttachShader(program, fs); 
    glLinkProgram(program); 
    glGetProgramiv(program, GL_LINK_STATUS, &link_ok); 
    if (!link_ok) 
    { 
     std::cerr << "[texture_layout/init_layout] fehler in glLinkProgram\n"; 
     exit(1); 
    } 

    const char* attribute_name = "coord2d"; 
    attribute = glGetAttribLocation(program, attribute_name); 
    if (attribute == -1) { 
     std::cerr << "Could not bind attribute " << attribute_name << "\n"; 
     exit(1); 
    } 
} 


int main(int argc, char **argv) 
{ 
    glutInit (&argc, argv); 
    glutInitContextProfile(GLUT_CORE_PROFILE); 

    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA); 
    glutInitWindowSize (500, 500); 
    glutCreateWindow (""); 

    glewExperimental=GL_TRUE; 
    GLenum err=glewInit(); 
    if(err!=0) 
    { 
     std::cerr << glewGetErrorString(err) << std::endl; 
     exit(1); 
    } 

    GLenum error; 

    GLuint texture; 

    while ((error = glGetError()) != GL_NO_ERROR) 
    { 
     std::cerr << std::hex << error << "\n"; 
    } 
    init_layout(); 
    gen_texture(texture, 200, 200); 

    GLvoid *tex_data = new GLubyte[4*200*200]; 

    glBindTexture(GL_TEXTURE_2D, texture); 
    glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_data); 


    return 0; 
} 

更新1(使用VBO而不是在glBegin)

現在我的代碼應該借鑑採用VBO一個紅色三角形,但它並不

void gen_texture(GLuint &color, int width, int height) 
{ 
    GLuint fb; 

    glGenFramebuffers(1, &fb); 
    glGenTextures(1, &color); 

    glBindFramebuffer(GL_FRAMEBUFFER, fb); 

    glBindTexture(GL_TEXTURE_2D, color); 
    glTexImage2D(GL_TEXTURE_2D, 
     0, 
     GL_RGBA, 
     width, height, 
     0, 
     GL_RGBA, 
     GL_UNSIGNED_BYTE, 
     0); 

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
    glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0); 

    glBindTexture(GL_TEXTURE_2D, 0); 
    glEnable(GL_TEXTURE_2D); 
    glBindFramebuffer(GL_FRAMEBUFFER, fb); 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity; 
    glViewport(0,0,width,height); 
    glOrtho(0,width,0,height,0,128); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 


    glClearColor(0, 0, 0, 0); 
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 

    glDisable(GL_DEPTH_TEST); 
    //glDisable(GL_CULL_FACE); 

    glColor3f(1.f, .0f, .0f); 
    GLfloat vertices[6] = 
    { 
     0, 0, 
     0, (GLfloat)height, 
     (GLfloat)width, (GLfloat)height, 
    }; 

    unsigned short indices[] = {0, 1, 2}; 

    GLuint vbo; 

    glGenBuffersARB(1, &vbo); 
    glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo); 
    glBufferDataARB(GL_ARRAY_BUFFER_ARB, 6*sizeof(GLfloat), vertices, GL_STATIC_DRAW_ARB); 

    glEnableClientState(GL_VERTEX_ARRAY); 
    glVertexPointer(3, GL_FLOAT, 0, 0); 
    glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, vertices); 
} 

原件2:

我有一個函數應該用glBegin指令將紅色正方形繪製到紋理中。但我唯一能夠工作的是

glClearColor(0, 0, 0, 0); 
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 

我已經測試過多種顏色。但是,這部分不工作:

glMatrixMode(GL_PROJECTION); 
glLoadIdentity; 
glViewport(0,0,width,height); 
glOrtho(0,width,0,height,0,128); 

glMatrixMode(GL_MODELVIEW); 
glLoadIdentity(); 

glDisable(GL_DEPTH_TEST); 

glColor3f(1.f, .0f, .0f); 
glBegin(GL_QUADS); 
    glVertex2f(0, 0); 
    glVertex2f(0, height); 
    glVertex2f(width, height); 
    glVertex2f(width, 0); 
glEnd(); 

以下是完整的功能:

void gen_texture(GLuint &color, int width, int height) 
{ 
    GLuint fb; 

    glGenFramebuffers(1, &fb); 
    glGenTextures(1, &color); 

    glBindFramebuffer(GL_FRAMEBUFFER, fb); 

    glBindTexture(GL_TEXTURE_2D, color); 
    glTexImage2D(GL_TEXTURE_2D, 
     0, 
     GL_RGBA, 
     width, height, 
     0, 
     GL_RGBA, 
     GL_UNSIGNED_BYTE, 
     0); 

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
    glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0); 

    glBindTexture(GL_TEXTURE_2D, 0); 
    glEnable(GL_TEXTURE_2D); 
    glBindFramebuffer(GL_FRAMEBUFFER, fb); 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity; 
    glViewport(0,0,width,height); 
    glOrtho(0,width,0,height,0,128); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 


    glClearColor(0, 0, 0, 0); 
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 

    glDisable(GL_DEPTH_TEST); 
    //glDisable(GL_CULL_FACE); 

    glColor3f(1.f, .0f, .0f); 
    glBegin(GL_QUADS); 
     glVertex2f(0, 0); 
     glVertex2f(0, height); 
     glVertex2f(width, height); 
     glVertex2f(width, 0); 
    glEnd(); 
} 

原貼:

我應該畫一個紅色的方形到紋理的功能,但是當我調用生成紋理的函數,然後我想用glGetTexImage函數檢查生成的紋理數據,我得到一個空指針。

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

#include <iostream> 

GLuint texture; 
GLvoid *tex_data; 


void gen_texture(GLuint &color, int width, int height) 
{ 
    GLuint fb; 

    glGenFramebuffers(1, &fb); 
    glGenTextures(1, &color); 

    glBindFramebuffer(GL_FRAMEBUFFER, fb); 

    glTexImage2D(GL_TEXTURE_2D, 
     0, 
     GL_RGBA, 
     width, height, 
     0, 
     GL_RGBA, 
     GL_UNSIGNED_BYTE, 
     0); 

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
    glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0); 

    glBindTexture(GL_TEXTURE_2D, 0); 
    glEnable(GL_TEXTURE_2D); 
    glBindFramebuffer(GL_FRAMEBUFFER, fb); 

    glViewport(0, 0, width, height); 

    glClearColor(1,1,1,0); 
    glClear(GL_COLOR_BUFFER_BIT); 

    glMatrixMode (GL_PROJECTION); 

    glLoadIdentity(); 

    glOrtho (0, width, height, 0, 0, 1); 

    glMatrixMode (GL_MODELVIEW); 

    glDisable(GL_DEPTH_TEST); 
    glDisable(GL_CULL_FACE); 

    glColor4f(1.f, .0f, .0f, .5f); 
    glBegin(GL_QUADS); 
     glVertex2f(0, 0); 
     glVertex2f(0, height); 
     glVertex2f(width, height); 
     glVertex2f(width, 0); 
    glEnd(); 
} 

int main(int argc, char **argv) 
{ 
    glutInit (&argc, argv); 
    glutInitContextVersion(3, 2); 
    glutInitContextProfile(GLUT_CORE_PROFILE); 

    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA); 
    glutInitWindowSize (500, 500); 
    glutCreateWindow (""); 

    glewExperimental=GL_TRUE; 
    GLenum err=glewInit(); 
    if(err!=0) 
    { 
     std::cerr << glewGetErrorString(err) << std::endl; 
     exit(1); 
    } 

    GLenum error; 

    while ((error = glGetError()) != GL_NO_ERROR) 
    { 
     std::cerr << std::hex << error << "\n"; 
    } 

    gen_texture(texture, 200, 200); 

    glBindTexture(GL_TEXTURE_2D, texture); 

    glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_data); 

    if (tex_data == 0) 
     std::cerr << "Captain, eine Null an Board!\n"; 

    return 0; 
} 

的tex_data現在是一個空指針

我究竟做錯了???

+0

因爲您將紋理變量傳遞給gen_textures(),您是否實際上填充了tex_data?這是一個錯字嗎? –

+0

@Full Frontal裸露我填充了tex_data,以便可以在調試器中檢查紋理,並使指針指向紋理數據。在gen_texture()中應該生成紋理。 – user2798943

+0

哦,我忘記了在調用glTexImage2d之前調用glBindTexture,我已經看到我必須自己爲紋理數據分配內存。但現在我有問題,glBegin和glEnd之間的繪圖指令似乎沒有對紋理 – user2798943

回答

0
glutInitContextVersion(3, 2); 
glutInitContextProfile(GLUT_CORE_PROFILE); 
         ^^^^^^^^^^^^^^^^^ ok... 
.... 
glBegin(GL_QUADS); 
     ^^^^^^^^ wat 

glBegin()和朋友在Core上下文中無效呼叫。

如果你堅持使用Core,你將不得不在shader和VBOs上運行起來。

+0

我改變了代碼,但它不起作用,我認爲我用錯誤的方式使用vbo。 (請參閱編輯的問題文章) – user2798943

+0

我沒有看到你設置了任何着色器。 Core中沒有任何「默認」着色器。你還有固定功能的顏色和矩陣調用。 – genpfault

+0

我更改了代碼,但它不起作用。 – user2798943