My program is basically supposed to render texture coordinates to a framebuffer image
now it all runs and gives a result however the results are very strange and i a not sure what is causing the strange results
here is the program
the main file
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
");
else if(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT)
printf("GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT
");
else if(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT)
printf("GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS
");
else if(glCheckFramebufferStatus(GL_FRAMEBUFFER) ==GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT)
printf("GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT
");
else if(glCheckFramebufferStatus(GL_FRAMEBUFFER) ==GL_FRAMEBUFFER_UNSUPPORTED)
printf("GL_FRAMEBUFFER_UNSUPPORTED
");
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
",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;
}
here is the renderBuffer.h file which contains the shaders
const char* vertex_shader =
"#version 330 core
"
"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
"
"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
", 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
", data2);
}
shader_program = glCreateProgram ();
glAttachShader (shader_program, fs);
glAttachShader (shader_program, vs);
glLinkProgram (shader_program);
glUseProgram (shader_program);
}
the results are really funny, they repeat(which is expected) but not in the order expected rather very odd and random i will post a part of the results below so you can see what is going on
and what you can see is that 0.001953 repeated throughout which continues for a while in the full output
so i am not sure what i did wrong
basically all i want is to print out texture coordinates