2016-05-29 13 views
-1

Mein Programm Framebuffer im Grunde TexturRendering Texturkoordinaten

nun alle Läufe zu einem Framebuffer Bildkoordinaten machen soll und gibt ein Ergebnis aber die Ergebnisse sind sehr seltsam und ia nicht sicher, was die seltsamen Ergebnisse verursacht

hier ist das Programm

die Hauptdatei

renderbuffer.cpp

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <unistd.h> 
#include <X11/Xlib.h> 
#include <X11/Xutil.h> 
#include <GL/glew.h> 
#include <GL/glx.h> 
#include <glm/glm.hpp> 
#include <time.h> 
#include <math.h> 

#include "glx2.c" 
#include "renderBuffer.h" 
#include "new.hpp" 

    int main(){ 



     init(256,256); 
     createShaders(); 

     //load names of files into program 
     char name[4][25]; 

     float *returned = (float *)malloc(3*256*256*sizeof(float)); 

     strcpy(name[0],"back.bmp"); 
     strcpy(name[1],"256x256.bmp"); 
     strcpy(name[2],"ryu2.bmp"); 
     strcpy(name[3],"pacman.bmp"); 


     GLuint frameBuffer; 

     glGenFramebuffers(1, &frameBuffer); 


     //makes the application supplied framebuffer object the current framebuffer(instead of the default one) 
     //to get back to rendering to default call glBindFramebuffer with 0 for the second parameter 
     glBindFramebuffer(GL_FRAMEBUFFER,frameBuffer); 

     GLuint renderTexture; 
     //GLuint depth_texture; 

     glGenTextures(1, &renderTexture); 

     glBindTexture(GL_TEXTURE_2D, renderTexture); 

     glTexStorage2D(GL_TEXTURE_2D,1, GL_RGB32F, 256,256); 

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

     //glGenTextures(1, &depth_texture); 
     //glBindTexture(GL_TEXTURE_2D, depth_texture); 
     //glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT32F, 1080, 720); 

     //glFramebufferTexture(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,depth_texture, 0); 



     glFramebufferTexture(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,renderTexture,0); 

     static const GLenum draw_buffers[] = { GL_COLOR_ATTACHMENT0 }; 
     glDrawBuffers(1, draw_buffers); 

     glBindFramebuffer(GL_FRAMEBUFFER,frameBuffer); 
     if(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE) 
      printf("works fine\n"); 
     else if(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT) 
      printf("GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT\n"); 
     else if(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT) 
      printf("GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS\n"); 
     else if(glCheckFramebufferStatus(GL_FRAMEBUFFER) ==GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT) 
      printf("GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT\n"); 
     else if(glCheckFramebufferStatus(GL_FRAMEBUFFER) ==GL_FRAMEBUFFER_UNSUPPORTED) 
      printf("GL_FRAMEBUFFER_UNSUPPORTED\n"); 



     GLfloat vertices[] = { 
     // X  Y  U  V 
     //triangle 1 
      -1.0, -1.0, 0.0, 0.0, 
      1.0, -1.0, 1.0, 0.0, 
      -1.0, 1.0, 0.0, 1.0, 
      1.0, 1.0, 1.0, 1.0,}; 



     GLuint vao1 = createVao(); 
     GLuint vbo1 = createVbo(); 

     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); 

     GLuint tex1 = createTexture(name[0]); 
     GLuint tex2 = createTexture(name[1]); 
     GLuint tex3 = createTexture(name[2]); 
     GLuint tex4 = createTexture(name[3]); 


     //set up data format in opengl and save in vao 
     glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), 0); 
     glEnableVertexAttribArray(0); 

     glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (const GLvoid*)(2 * sizeof(GLfloat))); 
     glEnableVertexAttribArray(1); 

     glBindTexture(vao1, tex2); 

     glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer); 
     glViewport(0,0,256,256); 

     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); 

     glBindFramebuffer(GL_FRAMEBUFFER,0); 

     glBindTexture(GL_TEXTURE_2D, renderTexture); 

     glGetTexImage(GL_TEXTURE_2D, 0,GL_RGB,GL_FLOAT,(void *)returned); 

     int i = 0,j=0; 


     for(i=0;i<256;i++){ 
      for(j=0;j<256;j++){ 
       printf("%.10lf %.10lf\n",returned[i],returned[j]); 
      } 
     } 




     glXMakeCurrent(dpy, 0, 0); 
     glXDestroyContext(dpy, ctx); 
     glXDestroyWindow(dpy, glxWin); 
     XDestroyWindow(dpy, win); 
     XFreeColormap(dpy, cmap); 
     XCloseDisplay(dpy); 
     free(returned); 

     return 0; 


    } 

hier ist die renderBuffer.h-Datei, die den Shadern

const char* vertex_shader = 
     "#version 330 core\n" 
     "layout(location = 0) in vec2 vp;" 
     "layout(location = 1) in vec2 tex;" 
     "uniform vec4 disp;" 
     "out vec2 texCoord;" 
     "void main() {" 
     " vec4 correct = vec4 (vp, 0.0f, 1.0f);" 
     " gl_Position = disp+correct;" 
     " texCoord = tex; " 
     "}"; 

    const char* fragment_shader = 
     "#version 330 core\n" 
     "uniform sampler2D s;" 
     "in vec2 texCoord;" 
     "out vec4 color;" 
     "void main() {" 
     "color = vec4(texCoord.st,0.0f,1.0f);" 
     "}"; 


GLuint vs; 
GLuint fs; 
GLuint shader_program; 


void createShaders(){ 

    GLint result; 
    GLsizei log_length; 
    GLchar data[255]; 
    GLchar data2[255]; 

    vs = glCreateShader (GL_VERTEX_SHADER); 
    glShaderSource (vs, 1, &vertex_shader, NULL); 
    glCompileShader (vs); 

    glGetShaderiv(vs, GL_COMPILE_STATUS,&result); 
    if(result == GL_FALSE){ 
     glGetShaderiv(vs, GL_INFO_LOG_LENGTH, &log_length); 
     glGetShaderInfoLog(vs, log_length, NULL, data); 
     printf("vertex shader %s\n", data); 
    } 

    fs = glCreateShader (GL_FRAGMENT_SHADER); 
    glShaderSource (fs, 1, &fragment_shader, NULL); 
    glCompileShader (fs); 

    glGetShaderiv(fs, GL_COMPILE_STATUS,&result); 
    if(result == GL_FALSE){ 
     glGetShaderiv(fs, GL_INFO_LOG_LENGTH, &log_length); 
     glGetShaderInfoLog(fs, log_length, NULL, data2); 
     printf("fragment shader %s\n", data2); 
    } 

    shader_program = glCreateProgram(); 
    glAttachShader (shader_program, fs); 
    glAttachShader (shader_program, vs); 
    glLinkProgram (shader_program); 

    glUseProgram (shader_program); 

} 

enthält und hier ist ein kleiner Ausschnitt der Ergebnisse http://pastebin.com/RakWQkE0

denen, wie Sie überall in dem eine Zeit lang weiter sehen kann, hat 0,001953 wiederholt in die volle Leistung

so bin ich nicht sicher, was ich falsch gemacht habe

im Grunde alles, was ich will, ist Textur auszudrucken Koordinaten

Antwort

0

Ihre Indizierung der returned Daten ist falsch. Die erwartete Struktur der Daten ist: 256 Reihen, wobei jede Reihe aus 256 Pixeln besteht, wobei jeder Pixel aus 3 float s besteht. Die richtige Indexierung sieht also so aus:

for (int y = 0; y < 256; ++y) { 
    for (int x = 0; x < 256; ++x) { 
     int index = ((y * 256) + x) * 3; 
     float r = returned[index + 0]; 
     float g = returned[index + 1]; 
     float b = returned[index + 2]; 
    } 
} 
+1

'GL_PACK_ALIGNMENT' steuert die Ausrichtung von Zeilen, nicht die Ausrichtung jedes Pixels. –

+0

@RetoKoradi Danke! Ich habe diesen Teil aus der Antwort entfernt. – jsb

+0

Ich versuchte es und die Ergebnisse, wo alle Mehrheitsnan mit ein paar Zahlen zufällig mit zufälligen Werten verstreut und einfach riesig und bedeutungslos – looking4answers