OpenGL glDrawArrays not working

Apprentice
Posts: 11
Joined: 2012.12
Post: #1
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?
Quote this message in a reply
Moderator
Posts: 702
Joined: 2002.04
Post: #2
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.

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2012.12
Post: #3
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?
Quote this message in a reply
Moderator
Posts: 702
Joined: 2002.04
Post: #4
(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

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2012.12
Post: #5
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
Quote this message in a reply
Moderator
Posts: 702
Joined: 2002.04
Post: #6
(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...

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2012.12
Post: #7
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
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #8
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.
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #9
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.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2012.12
Post: #10
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
Quote this message in a reply
Moderator
Posts: 702
Joined: 2002.04
Post: #11
(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.

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2012.12
Post: #12
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.
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #13
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.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Drawing using glDrawArrays agreendev 9 17,589 Jul 17, 2010 05:20 AM
Last Post: Bersaelor
  glColor4f not working after glDrawArrays Technoman 2 6,733 Aug 15, 2009 08:09 AM
Last Post: Technoman
  VBOs and Vertex Arrays in OpenGL not working Talyn 6 6,686 Jul 8, 2008 01:14 PM
Last Post: Fenris
  glDrawArrays Muffinking 24 16,097 Oct 28, 2003 01:47 PM
Last Post: Mars_999
  glDrawElements vs. glDrawArrays - The numbers are in! inio 22 20,785 Jul 19, 2003 10:00 AM
Last Post: Josh