PDA

View Full Version : glDrawElements(....)



matman
12-30-2001, 01:29 PM
I have created this little program. It was supposed to display a cube, eventually spin it when moust button pressed. But no box appears. Here's the code (it was written for borland c++ builder for win):

#include <windows.h>
#include <dos.h>
#include <stdlib.h>
#include <stdio.h>
#pragma hdrstop
#include <condefs.h>

#define GLUT_USE_SGI_OPENGL
#include <GL/glut.h>
//---------------------------------------------------------------------------
static GLfloat obrot = 0.0;

static GLfloat kolorB[8] ={(((GLfloat)random(1000))/1000.0),
(((GLfloat)random(1000))/1000.0),
(((GLfloat)random(1000))/1000.0),
(((GLfloat)random(1000))/1000.0),
(((GLfloat)random(1000))/1000.0),
(((GLfloat)random(1000))/1000.0),
(((GLfloat)random(1000))/1000.0),
(((GLfloat)random(1000))/1000.0)};

static GLfloat kolor[24]={ kolorB[0],
kolorB[1],
kolorB[2],
kolorB[3],
kolorB[7],
kolorB[6],
kolorB[5],
kolorB[4],
kolorB[4],
kolorB[0],
kolorB[3],
kolorB[7],
kolorB[1],
kolorB[5],
kolorB[6],
kolorB[2],
kolorB[4],
kolorB[5],
kolorB[1],
kolorB[0],
kolorB[3],
kolorB[2],
kolorB[6],
kolorB[7]};


static GLfloat punkty[24] = {25.0, 25.0, 25.0,
-25.0, 25.0, 25.0,
-25.0,-25.0, 25.0,
25.0,-25.0, 25.0,
25.0, 25.0,-25.0,
-25.0, 25.0,-25.0,
-25.0,-25.0,-25.0,
25.0,-25.0,-25.0};

static GLboolean flagi[24] ={GL_TRUE,
GL_TRUE,
GL_TRUE,
GL_TRUE,
GL_TRUE,
GL_TRUE,
GL_TRUE,
GL_TRUE,
GL_TRUE,
GL_TRUE,
GL_TRUE,
GL_TRUE,
GL_TRUE,
GL_TRUE,
GL_TRUE,
GL_TRUE,
GL_TRUE,
GL_TRUE,
GL_TRUE,
GL_TRUE,
GL_TRUE,
GL_TRUE,
GL_TRUE,
GL_TRUE};

static GLint szescian[24] = {0, 1, 2, 3,
7, 6, 5, 4,
4, 0, 3, 7,
1, 5, 6, 2,
4, 5, 1, 0,
3, 2, 6, 7};

void initialize(void) {
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_EDGE_FLAG_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, punkty);
glColorPointer(3, GL_FLOAT, 0, kolor);
glEdgeFlagPointer(0, flagi); }

void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.0, 0.0, -200.0);
//glColor3f(1.0, 1.0, 1.0);
//glRectf(-25.0, -25.0, 25.0, 25.0);
glRotatef(obrot, 1.0, 0.0, 0.0);
glRotatef(obrot, 0.0, 1.0, 0.0);
glRotatef(obrot, 0.0, 0.0, 1.0);
glDrawElements(GL_QUADS, 24, GL_INT, szescian);
glPopMatrix();
glutSwapBuffers(); }

void reshape(int w, int h) {
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,500.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); }

void rotateLeft(void) {
obrot += 0.2;
if (obrot > 360)
obrot -= 360;
for (int i=0; i<24; i++)
kolor[i] += 0.05;
glutPostRedisplay(); }

void rotateRight(void) {
obrot -= 0.2;
if (obrot < 0)
obrot += 360;
for (int i=0; i<24; i++)
kolor[i] += 0.05;
glutPostRedisplay(); }

void faluj(void) {
for (int i=0; i<24; i++)
kolor[i] += 0.05;
glutPostRedisplay(); }

void mouse(int button, int state, int x, int y) {
switch (button) {
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(rotateLeft);
break;
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(rotateRight);
break;
default:
break; }}

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
glutInit(&_argc, _argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(640, 480);
glutInitWindowPosition(200, 100);
glutCreateWindow("hardman");
initialize();
glutDisplayFunc(display);
glutIdleFunc(faluj);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}