Jump to content
  • Advertisement
Sign in to follow this  
wforl

OpenGL glDrawElements

This topic is 4017 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im following the Red Book, OPengl Programming Guide, and when it comes to explaining the glDrawElements function, it didn't give a full example code. And then through google i have discovered that i have to use the glIndexPointer, this makes sence now :), but i was wondering if that is correct. My code works fine, but its odd that the author didn't specify that it was needed, or should i have known that? :) anyway here is my code, is this the correct way to use the function
#include <GL/glut.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

#define red 1.0, 0.0, 0.0
#define green 0.0, 1.0, 0.0
#define blue 0.0, 0.0, 1.0
#define yellow 1.0, 1.0, 0.2
#define brown 0.4, 0.09, 0.0
#define vanilla 1.0, 0.95, 0.56



void display(void)
{

   glClear (GL_COLOR_BUFFER_BIT);
   glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

glColor3f(yellow);

GLfloat vertex[] ={	0.25, 0.25,
					0.75, 0.25,
					0.75, 0.75,
					0.25, 0.75,};

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

glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_INDEX_ARRAY );

glIndexPointer( GL_UNSIGNED_BYTE, 0, indices );
glVertexPointer( 2, GL_FLOAT, 0, vertex );

glDrawElements( GL_QUADS, 4, GL_UNSIGNED_BYTE, indices );


glFlush ();
}

void init (void) 
{
/* select clearing color 	*/
   glClearColor (0.0, 0.0, 0.0, 0.0);

/* initialize viewing values  */
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);

}

int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
   glutInitWindowSize (250, 250); 
   glutInitWindowPosition (100, 100);
   glutCreateWindow ("hello");
   init ();
   glutDisplayFunc(display); 
   glutMainLoop();
  
   return 0;   /* ANSI C requires main to return int. */
}


Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!