LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-22-2004, 04:21 AM   #1
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Rep: Reputation: 30
Post [opengl]glDrawElements();


Hello
Could anyone tell me why the following piece of code doesn't work?
Code:
glEnableClientState(GL_VERTEX_ARRAY);
float test[9] = { 0.0, 0.5, 0.0,
         -0.5, 0.0, 0.0,
         0.5, 0.0, 0.0 };
glVertexPointer(3, GL_FLOAT, 0, test);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, test);
glDisableClientState(GL_VERTEX_ARRAY);
Thanx Hylke
 
Old 06-23-2004, 04:33 AM   #2
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Original Poster
Rep: Reputation: 30
So does anyone have an idea?
 
Old 06-23-2004, 11:21 AM   #3
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Try changing GL_UNSIGNED_BYTE to GL_FLOAT, since test is a float array, not a unsigned char array.

Other than that, there are many other things that could cause it to not work, but seeing only that code segment won't help. Are you sure you have a GL context, etc?
 
Old 06-24-2004, 03:34 AM   #4
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Original Poster
Rep: Reputation: 30
Maybe it's because i have an old version of opengl because glMultiDrawElements(); doesn't work at all(gives compilation errors).
So i'il check if nvidia has released a new opengl version this month.
Hylke
 
Old 06-24-2004, 12:44 PM   #5
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Did you try changing that constant? Because that is one definite problem in the small code segment you posted.

Just off the top of my head I can think of a number of possible reasons it's not displaying anything.
  • OpenGL isn't initialized, thus no context to render to
  • You have your MODELVIEW and PROJECTION matrices setup wrong
  • It could be getting clipped by the front or back clipping plane
  • You are using double buffering but never flip the buffer
  • You have lighting enabled, but no light
  • Your color/material settings are set to the same as your background color

There are also many other reasons that you may not be seeing anything. You will have to give more details of your code to be able to determine the exact problem.

About glMultiDrawElements(), I think this is a relatively new method for OpenGL, though I can't tell you what version it was added in. You can check the OpenGL specs, and check the version of your OpenGL implementation with glGetString(GL_VERSION);
 
Old 06-24-2004, 12:57 PM   #6
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by deiussum
Did you try changing that constant? Because that is one definite problem in the small code segment you posted.

Just off the top of my head I can think of a number of possible reasons it's not displaying anything.
  • OpenGL isn't initialized, thus no context to render to
  • You have your MODELVIEW and PROJECTION matrices setup wrong
  • It could be getting clipped by the front or back clipping plane
  • You are using double buffering but never flip the buffer
  • You have lighting enabled, but no light
  • Your color/material settings are set to the same as your background color

There are also many other reasons that you may not be seeing anything. You will have to give more details of your code to be able to determine the exact problem.

About glMultiDrawElements(), I think this is a relatively new method for OpenGL, though I can't tell you what version it was added in. You can check the OpenGL specs, and check the version of your OpenGL implementation with glGetString(GL_VERSION);
neither of the things you came up with was wrong.
I thought glMultiDrawElements came with one of the latest versions.
b.t.w. i've just formatted my pc and forgot to back up my codes.So i'il make a new one and post it here.
Hylke
 
Old 06-24-2004, 01:31 PM   #7
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Original Poster
Rep: Reputation: 30
Here's a code that i just wrote and didn't work(just like the other one)
Code:
#include <GL/glut.h>

void init();
void reshape(int w, int h);
void display();

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}

void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
}

void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-0.5, 5.0 ,-0.5, 5.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
GLfloat triangle[] = { 0.0, 0.5, 0.0,
			-0.5, 0.0, 0.0,
			0.5, 0.0, 0.0 };
glVertexPointer(3 ,GL_FLOAT, 0, triangle);
glDrawElements(GL_TRIANGLES, 9, GL_FLOAT, triangle);
glDisableClientState(GL_VERTEX_ARRAY);
glFlush();
}
 
Old 06-24-2004, 02:26 PM   #8
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Ok. I see what the problem is. You are trying to use glDrawElements like glDrawArrays. (I get the 2 mixed up myself all the time too, and should have looked at the man pages first thing to see if that's what the problem was.)

You have 2 options.

1. Just use glDrawArrays like so:

glDrawArrays(GL_TRIANGLES, 0, 3);

2. Create an array of INDICES into your triangles array like so:

unsigned int indices[3] = {0, 1, 2};

glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, indices);

My modified display function looks like so:

Code:
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glEnableClientState(GL_VERTEX_ARRAY);
    GLfloat triangle[] = { 0.0, 0.5, 0.0,
                -0.5, 0.0, 0.0,
                0.5, 0.0, 0.0 };
    unsigned int indices[] = {0, 1, 2};
    glVertexPointer(3 ,GL_FLOAT, 0, triangle);
    // use either of the 2 lines below
    glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, indices);
    //glDrawArrays(GL_TRIANGLES, 0, 3);
    glDisableClientState(GL_VERTEX_ARRAY);
    glFlush();
}

Last edited by deiussum; 06-24-2004 at 02:27 PM.
 
Old 06-24-2004, 02:58 PM   #9
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Original Poster
Rep: Reputation: 30
So if would add an w value to it would it look like this?
Quote:
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
GLfloat triangle[] = { 0.0, 0.5, 0.0, 1.0,
-0.5, 0.0, 0.0, 1.0,
0.5, 0.0, 0.0, 1.0 };
unsigned int indices[] = {0, 1, 2, 3};
glVertexPointer(4 ,GL_FLOAT, 0, triangle);
// use either of the 2 lines below
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, indices);
//glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableClientState(GL_VERTEX_ARRAY);
glFlush();
}
 
Old 06-24-2004, 03:41 PM   #10
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
It could, but that last 3 in "indices" would never be used. What the indices are is in index into the vertex array. Index 0 indicates the first x elements of your float array. (x being 3 for your first example, 4 when you add a w component)

when you also enable things like texcoord arrays, normal arrays, etc. that index is also an index into that array.

What glDrawElements buys you, is that if you have a number of duplicate vertices, you only need the vertex in the array once, and can simply have multiple values in your index array.
 
Old 06-24-2004, 05:16 PM   #11
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Here... I modified your display a bit to add a color array to try and demonstrate what I meant. Think of it in terms of the following:

vertex0 = pos(0.5, 0.5, 0.0) color(1.0, 0.0, 0.0)
vertex1 = pos(-0.5, 0.0, 0.0) color (0.0, 1.0, 0.0)
vertex2 = pos(0.5, 0.0, 0.0) color(0.0, 0.0, 1.0)
vertex3 = pos(-0.5, 0.5, 0.0) color(1.0, 1.0, 1.0)

the first triangle is drawn with vertices 0, 1, 2.
the second triangle is drawn with vertices 3, 1, 2, translated 1 unit to the right

Here's the code: (BTW, you also have to get rid of glShadeModel(GL_FLAT) to see the different colors, flat shading uses only thet color of one of the vertices for the face.

Code:
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
    GLfloat triangle[] = { 0.5, 0.5, 0.0, // index 0
                -0.5, 0.0, 0.0, // index 1
                0.5, 0.0, 0.0, // index 2
                -0.5, 0.5, 0.0  }; // index 3  
    GLfloat colors[] = { 1.0, 0.0, 0.0, // index 0
        0.0, 1.0, 0.0, // index 1
        0.0, 0.0, 1.0, // index 2
        1.0, 1.0, 1.0 }; // index 3
    unsigned int indices[] = {0, 1, 2};
    unsigned int indices2[] = {3, 1, 2};

    glVertexPointer(3 ,GL_FLOAT, 0, triangle);
    glColorPointer(3, GL_FLOAT, 0, colors);

    // Draw triangle 1 using set of indices
    glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, indices);

    // Draw triangle 2 using set of indices2, moved to the right a bit
    glPushMatrix();
    glTranslatef(1.0, 0.0, 0.0);
    glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, indices2);
    glPopMatrix();


    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);
    glFlush();
}

Last edited by deiussum; 06-24-2004 at 05:17 PM.
 
Old 06-25-2004, 01:55 AM   #12
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Original Poster
Rep: Reputation: 30
Thank you very much, i finaly understand how it works.
Hylke
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
what does OpenGL do? servnov General 7 11-17-2004 01:53 PM
Overwrite Mesa OpenGL with ATI OpenGL Carl-Fredrik Slackware 12 10-01-2004 03:33 PM
Changing from MESA OpenGL to ATI OpenGL tillyoubreakit Linux - Hardware 19 10-07-2003 07:32 PM
OpenGL is needed by plib-1.7.0- but i have opengl ! qwijibow Linux - Newbie 0 08-05-2003 07:12 AM
Where is OpenGL SherylGlas Linux - Newbie 2 03-21-2003 04:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration