iDevGames Forums
OpenGL glDrawArrays not working - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: OpenGL glDrawArrays not working (/thread-10481.html)



OpenGL glDrawArrays not working - dotbianry - Dec 16, 2012 05:53 AM

Hello!

I'm trying to render some pixels on screen using glDrawArrays like this:

Code:
const GLbyte Points[] = {
       10, 10,
      150, 110,
      110, 150,
      150, 150,
    };

// this goes before the program loop
glEnableClientState(GL_COLOR_ARRAY);

// this goes in my programn loop
glVertexPointer(2, GL_UNSIGNED_BYTE, 0, Points);
glDrawArrays(GL_POINTS, 0, 3);

The problem is that on runtime the program crashes.
Does anyone know how to make it work?


RE: OpenGL glDrawArrays not working - sealfin - Dec 16, 2012 06:47 AM

Given the excerpt of your code you've posted, you're missing calls to (at least) glEnableClientState( GL_VERTEX_ARRAY ) and glColorPointer; additionally, man glVertexPointer reports that that function does not support GL_UNSIGNED_BYTE.


RE: OpenGL glDrawArrays not working - dotbianry - Dec 16, 2012 07:35 AM

Thank you, it works now.

Is there a way to draw a bitmap , without making a vertex array with all the positions of all the points?


RE: OpenGL glDrawArrays not working - sealfin - Dec 16, 2012 07:55 AM

(Dec 16, 2012 07:35 AM)dotbianry Wrote:  Is there a way to draw a bitmap , without making a vertex array with all the positions of all the points?

Read the documentation/a few tutorials on the topic of texture mapping Wink


RE: OpenGL glDrawArrays not working - dotbianry - Dec 18, 2012 12:44 PM

I didn't found anything helpful on texture mapping related to glVertexPointer.
Instead I made some loops to fill an array with all the coordonates of all the points but glDrawArrays doesn't draw it right.

Here's how I'm filling the point coordonate array:
Code:
int myclk=0,myclkh=0;
for (int ii=0;ii<786432*2  ;ii+=2 ){
     if(myclk == 1024){ myclk = 0;myclkh++;}
     loc[ii] =myclk;
     loc[ii+1] =  myclkh;
     myclk+=1;
}

Is there any other solution to this?

EDIT:
I found what the problem was, I wasn't filling the color array properly


RE: OpenGL glDrawArrays not working - sealfin - Dec 18, 2012 01:57 PM

(Dec 18, 2012 12:44 PM)dotbianry Wrote:  I didn't found anything helpful on texture mapping related to glVertexPointer.

Really? The first result for searching for OpenGL ES texture mapping tutorial seems to be an adequate introduction to texture mapping – it is for iOS (and I think I remember from another thread that you're using Windows), but almost all of the source code in the tutorial is still relevant to cross-platform OpenGL ES...


RE: OpenGL glDrawArrays not working - dotbianry - Dec 19, 2012 11:07 AM

I have some problems getting the texture mapped on a quad.The program crashes.

My setup is like this:

Code:
GLfloat texture[] = {0,0, 1,0, 1,1, 0,1};
GLint box[] = {
    0,0,
    0,768,
    1024,768,
    1024,0
};  

// before main loop
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glEnable(GL_TEXTURE_2D);
GLuint      tex[1];
glGenTextures(1, &tex[0]);
glBindTexture(GL_TEXTURE_2D, tex[0]);

// in loop
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, video);
glVertexPointer(2, GL_INT, 0,box);
glTexCoordPointer(2, GL_FLOAT, 0, texture);
glDrawArrays(GL_QUADS,0,4);

"video" is the array with RGB values of an image


RE: OpenGL glDrawArrays not working - ThemsAllTook - Dec 19, 2012 11:31 AM

Looks like you're enabling GL_COLOR_ARRAY, but not calling glColorPointer before drawing. For each array that's enabled, whatever value the pointer is set to will be dereferenced, and if you haven't specified one it'll most likely crash.


RE: OpenGL glDrawArrays not working - Skorche - Dec 19, 2012 12:19 PM

Also, you want to load the texture (by calling glTexImage2D) only when you create the texture. In other words, gen, bind, teximage, then loop. The only reason you would want to do that is if you are changing the pixels of the texture every frame on the CPU too.


RE: OpenGL glDrawArrays not working - dotbianry - Dec 20, 2012 04:06 AM

I removed glColorPointer and now it doesn't crash anymore.Now I'm getting a blank white screen,is there anything else to be done for the texture to show up?
And yes the pixels of the texture will change every frame.

EDIT:
I've put this after glBindTexture:
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
and this after glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
now the texture appears but I'm getting a really low frame rate


RE: OpenGL glDrawArrays not working - sealfin - Dec 20, 2012 03:09 PM

(Dec 20, 2012 04:06 AM)dotbianry Wrote:  And yes the pixels of the texture will change every frame.

Could you elaborate on what it is you're trying to achieve?

If you're trying to show an animation, I'd suggest you combine the multiple frames of the animation into a texture (rather than create a texture for every frame of the animation or, worse, change the texture data every frame), a technique known as using a texture atlas.


RE: OpenGL glDrawArrays not working - dotbianry - Dec 21, 2012 04:12 AM

I'm trying to make a 3D engine with software rendering.
From OpenGL I only need to send the frame , rendered offscreen to the video memory,and I'm hoping the transfer is fast enough to achieve a playable frame rate.


RE: OpenGL glDrawArrays not working - Skorche - Dec 21, 2012 09:21 AM

In that case you'll want to try using glTexSubImage() instead. It's supposed to be faster than creating a new texture object using glTexImage2D(), but don't quote me on it. That might not be true anymore for full texture refreshes.