将纹理坐标渲染到帧缓冲区

时间:2016-05-29 22:11:09

标签: c++ opengl framebuffer texture2d

我的程序基本上应该将纹理坐标渲染为帧缓冲图像

现在它全部运行并给出结果然而结果非常奇怪,我不确定是什么导致奇怪的结果

这是程序

主文件

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;


    }

这是包含着色器

的renderBuffer.h文件
 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);  

}

以下是结果http://pastebin.com/RakWQkE0

的一小部分内容

正如你所看到的那样,在整个输出中持续了一段时间,重复了0.001953

所以我不确定我做错了什么

基本上我想要的就是打印纹理坐标

1 个答案:

答案 0 :(得分:0)

您对returned数据的索引不正确。预期的数据结构是:256行,每行由256个像素组成,每个像素由3 float组成。因此,正确的索引如下所示:

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];
    }
}