PDA

View Full Version : Help with glDrawElements



Paras_wolf
03-05-2011, 12:33 AM
Hi, I am new to openGL, and trying to generate triangular mesh
using glDrawElements().

Currently my code just tries to display one triangle using the
glDrawElemsnts().

I ran the program using glBegin(GL_TRIANGLES), it works fine,

but when I use glDrawElemnts(), I cant see anything on screen.

Please Help!!.




#ifdef _WIN32
#include <GL/freeglut.h>
#else
#include <GL/glut.h>
#endif


#include <windows.h>
#include <iostream>

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <ctime>

#include "glext.h"

using namespace std;





GLint vertices[] = { 10,20,
300,400,
10,260};

GLuint indices[] = {1,2,3};




void display(void)
{


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glColor3f(0,1,0);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_INT,0,vertices);
glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_INT,indi ces);
glDisableClientState(GL_VERTEX_ARRAY);
glutSwapBuffers();
}//EOF



void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, (GLdouble)w, 0, (GLdouble)h, -5.0, 5.0);
glMatrixMode(GL_MODELVIEW);

}
int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE |GLUT_RGB | GLUT_DEPTH );
glEnable(GL_DEPTH_TEST);

glutInitWindowSize(500,500);
glutInitWindowPosition( 200,200);
glutCreateWindow("Hello_World");
glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutIdleFunc(display);
// glutPassiveMotionFunc(&amp;mouse_move);



glutMainLoop();

return 0;
}

//EOF

Alfonse Reinheart
03-05-2011, 01:18 AM
GLuint indices[] = {1,2,3};

Zero base indices (http://en.wikipedia.org/wiki/Array_data_type#Index_origin).

Paras_wolf
03-05-2011, 01:22 AM
ok, solves the problem,
thanks..