0

Update 2 works, it was a wrong allert

Update 2 (using vbo vertex- and fragment-shaders) but it still don't works

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

Update 1 (using vbo instead of glBegin)

now my code should draw a red triangle using vbo, but it doesn't

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

Original 2:

I have a function that should draw a red square into a texture with the glBegin directive. But the only thing I got to work is

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

I've tested it with multiple colors. But that part do not work:

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();

Here is the full function:

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();
}

Original Post:

I have a function that should draw a red square into a texture, but when I call the function for generating the texture and then I want to check the generated texture data, with the glGetTexImage function, I get a null-pointer.

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

the tex_data is now a null-pointer

what am I doing wrong???

user2798943
  • 197
  • 2
  • 11
  • DO you actually fill tex_data, because you pass the texture variable to gen_textures(). Is this a typo? – Full Frontal Nudity Oct 28 '13 at 09:44
  • @Full Frontal Nudity I fill tex_data to make it possible to check the the texture in a debugger, having a pointer addressing the texture data. In gen_texture() the texture should be generated. – user2798943 Oct 28 '13 at 10:05
  • Oh, i've forgotten to call glBindTexture before calling glTexImage2d and I've seen that i must allocate the memory for the texture data by my own. But now I have the problem that the drawing instructions between glBegin and glEnd seems to have no effect on the texture – user2798943 Oct 28 '13 at 13:30

1 Answers1

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

glBegin() and friends aren't valid calls in a Core context.

You'll have to get spun up on shaders and VBOs if you insist on Core.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • I changed the code, but It does not work, I think i'm using vbo the wrong way. (see edited question post) – user2798943 Oct 28 '13 at 16:25
  • I'm not seeing you set any shaders. There aren't any "default" shaders in Core. And you still have fixed-function color and matrix calls in there. – genpfault Oct 28 '13 at 16:28