Hi!
I want to use VBOs under Linux/C and found some code samples that use SDL
to call the functions of the openGL extension.
Do you know another way, without SDL? If not, is this the right way to use
it?
It crashes while calling glDrawArrays
Any suggestions?
I just put the interesting lines below.
thx
Christiane.
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
//#include âglext.hâ
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h> //cpp
#include ââŚ/rawlib/rawFile.hâ
#include <SDL.h>
//#include <SDL_opengl.h>
#define GL_ARRAY_BUFFER_ARB 0x8892
#define GL_STATIC_DRAW_ARB 0x88E4
typedef void (APIENTRY * PFNGLBINDBUFFERARBPROC) (GLenum target, GLuint
buffer);
typedef void (APIENTRY * PFNGLDELETEBUFFERSARBPROC) (GLsizei n, const GLuint
*buffers);
typedef void (APIENTRY * PFNGLGENBUFFERSARBPROC) (GLsizei n, GLuint *buffers);
typedef void (APIENTRY * PFNGLBUFFERDATAARBPROC) (GLenum target, int size,
const GLvoid *data, GLenum usage);
// VBO Extension Function Pointers
PFNGLGENBUFFERSARBPROC glGenBuffersARB = NULL;
// VBO Name Generation Procedure
PFNGLBINDBUFFERARBPROC glBindBufferARB = NULL;
// VBO Bind Procedure
PFNGLBUFFERDATAARBPROC glBufferDataARB = NULL;
// VBO Data Loading Procedure
PFNGLDELETEBUFFERSARBPROC glDeleteBuffersARB = NULL;
// VBO Deletion Procedure
GLuint vertex_VBO;
GLfloat vertex_array[3*(RAW_HEIGHT/2)*(RAW_WIDTH/2)];
void buildVertexArrays(int fieldType)
{
int x,y;
int i=0;
//draw the points
for ( y = 0; y < RAW_HEIGHT/2; y++ )
for (x = 0; x < RAW_WIDTH/2; x++)
{
vertex_array[i++]= x - RAW_WIDTH/4;
switch (fieldType)
{
case âAâ: vertex_array[i++] = fieldA[y][x];break;
case âBâ: vertex_array[i++] = fieldB[y][x];break;
case âCâ: vertex_array[i++] = fieldC[y][x];break;
case âDâ: vertex_array[i++] = fieldD[y][x];break;
}
vertex_array[i++]= y - RAW_HEIGHT/4;
}
}
void myInit(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT); //???
glPointSize(1.0);
buildVAs(âAâ);
}
void make_VBOs(){
printf(âUploading Data to the Graphics CardâŚ\nâ);fflush(stdout);
// Generate And Bind The Vertex Buffer
glGenBuffersARB( 1, &vertex_VBO );
// Get A Valid Name
glBindBufferARB( GL_ARRAY_BUFFER_ARB, vertex_VBO );
// Bind The Buffer
// Load The Data
glBufferDataARB( GL_ARRAY_BUFFER_ARB,
(RAW_HEIGHT/2)*(RAW_WIDTH/2)3sizeof(GLfloat),
vertex_array, GL_STATIC_DRAW_ARB );
}
void myDisplay(void)
{
int x,y;
double eyeX = 0, eyeY = 0, eyeZ = 0;
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
//eye x,y,z; center x,y,z; up x,y,z
if (topView)
{eyeX = 0.0; eyeY = CCD_MAX+7000.0; eyeZ = 0.0;}
else
{eyeX = 0.0; eyeY = CCD_MAX/2.0; eyeZ = 7000.0;}
gluLookAt(eyeX - moveX, eyeY - moveY, eyeZ - moveZ, -moveX, CCD_MAX/2.0
-moveY, -moveZ, 0.0, 1.0, 0.0);
//glScalef(1.0, 2.0, 1.0);
glRotatef(1.0rotX,1.0,0.0,0.0);
glRotatef(1.0rotY,0.0,1.0,0.0);
glRotatef(1.0*rotZ,0.0,0.0,1.0);
âŚ
glEnableClientState( GL_VERTEX_ARRAY );
// Enable Vertex Arrays
glBindBufferARB( GL_ARRAY_BUFFER_ARB, vertex_VBO );
glVertexPointer( 3, GL_FLOAT, 0, NULL );
// Set The Vertex Pointer To The Vertex Buffer
glDrawArrays(GL_POINTS, 0, (RAW_HEIGHT/2)*(RAW_WIDTH/2));
glutSwapBuffers();
glFlush();
}
void myReshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
gluPerspective(40.0, (GLsizei) w/(GLsizei) h, 0.0, 10000.0);
glMatrixMode(GL_MODELVIEW);
//glLoadIdentity();
//gluLookAt(0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
int main(int argc, char** argv)
{
int retVal;
int VBOSupported;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(1000, 1000);
glutInitWindowPosition(100, 150);
glutCreateWindow(âRawViewerâ);
myInit();
int i;
for(i=0;i<3*(RAW_HEIGHT/2)*(RAW_WIDTH/2);i+=3)
printf(âx %f, y %f, z %f\nâ, vertex_array[i],
vertex_array[i+1],vertex_array[i+2]);
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, âSDL konnte nicht initialisiert werden: %s\nâ,
SDL_GetError());
exit(1);
}
if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1)<0)
{
printf(âUnable to set GL attribute : %s\nâ,SDL_GetError());
exit(1);
}
if (SDL_SetVideoMode(1000,1000,0,SDL_OPENGL)==NULL)
{
printf(âUnable to open video mode : %s\nâ,SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
VBOSupported = isExtensionSupported( âGL_ARB_vertex_buffer_objectâ );
if(VBOSupported)
{
// Get Pointers To The GL Functions
glGenBuffersARB = (PFNGLGENBUFFERSARBPROC)
SDL_GL_GetProcAddress(âglGenBuffersARBâ);
glBindBufferARB = (PFNGLBINDBUFFERARBPROC)
SDL_GL_GetProcAddress(âglBindBufferARBâ);
glBufferDataARB = (PFNGLBUFFERDATAARBPROC)
SDL_GL_GetProcAddress(âglBufferDataARBâ);
glDeleteBuffersARB = (PFNGLDELETEBUFFERSARBPROC)
SDL_GL_GetProcAddress(âglDeleteBuffersARBâ);
make_VBOs();
}
glutDisplayFunc(myDisplay);
glutReshapeFunc(myReshape);
glutMouseFunc(mouseButtonCallback);
glutMotionFunc(mouseMotionCallback);
glutKeyboardFunc(myKeyboardCallback);
glutSpecialFunc(mySpecialFuncCallback);
glutMainLoop();
exit(0);
}