PDA

View Full Version : Vertex Buffer object



Janani
11-05-2009, 09:33 PM
Hi
I have written a coding using opengl to draw 5 circle at a distance of 50 km each.

I have implemented this using vertex array , it is working fine
I am trying to do this using vertex buffer object, i have created vbo ,binded it with array vertices but i am not getting proper circle.

I am attaching my code with this if someone knows what mistake i am doing kindly reply

CODING



/////////////////Declartions///////////////
void createVBO(GLuint* vboId);
GLuint vboId;
float circlevertexarray[5000];

void circledata();
void display();

////////////////////Function to calculate circle vertices///////////////////
void circledata()
{
float v1,v2;
for(int no=1;no<6;no++)
{

for(float angle=0;angle<360.0*(3.14/180);angle=angle+0.02)
{
v1=no*0.42*cos(angle);
v2=no*0.42*sin(angle);
circlevertexarray[(j*2)+0]=v1;
circlevertexarray[(j*2)+1]=v2;
j++;
}
}
indices=j+1;
printf("%d\n",indices);

}

//////////////////////Function to create VBO//////////////////////////////
void createVBO(GLuint* vboId)
{
// create buffer object
glGenBuffers(1,vboId);
glBindBuffer(GL_ARRAY_BUFFER, *vboId);
// initialize buffer object
glBufferData(GL_ARRAY_BUFFER,2*indices*sizeof(floa t),circlevertexarray ,GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
#if 1
int bufferSize = 0;

glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &amp;bufferSize);

if(indices != bufferSize)
{
glDeleteBuffersARB(1, vboId);
vboId = 0;
printf("datasize mismatch\n");
}
else
{
printf("matching\n");
}
#endif
}


/////////////////Main///////////////////////////////////
int main(int argc, char** argv)
{
initGL(argc, argv);
circledata();
createVBO(&amp;vboId);

MulticastInit();
threadid=pthread_create(&amp;Receivethread,NULL,&amp;sweep ,NULL);
glutDisplayFunc(display);
glutMainLoop();
}


//////////////////////DISPLAY Function/////////////////
void display()
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
while(1)
{
//usleep(10000);
glClearColor(0.0,0.0,0.0,1.0);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


////////////////////Vertex Buffer Object////////////////////////
#if 1
glColor3f(1.0,1.0,1.0);
glBindBuffer(GL_ARRAY_BUFFER,vboId);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0,0);
glDrawArrays(GL_POINTS,0,indices);
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
#endif


///////////////////////Vertex array//////////////////////////
#if 0
glColor3f(1.0f,1.0f,1.0f);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0,circlevertexarray );
glDrawArrays(GL_POINTS,0,indices);
glDisableClientState(GL_VERTEX_ARRAY);


#endif
}
}

.

Thank You
Janani

scratt
11-06-2009, 01:12 AM
Is indices a global variable?
As a side note you need to tidy up your use of global and local variables IMO. :)

What are you actually seeing when you draw?

On the surface your GL code looks ok... Perhaps try swapping the bind and enable around in the draw function?

I suspect the problem has something to with initialising / enabling the VBO as your data creation obviously works for vertex arrays.

Also, just to be sure, do you check that you get a valid buffer ID back when you create it?

Janani
11-06-2009, 02:05 AM
Thank you for ur reply..

Yes indices is a global variable
When drawing i see some half drawn 2 circle.
I got vboid as 1.

I am attaching entire coding.


// includes, system
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <pthread.h>
#include <sys/time.h>
#include <unistd.h>


// includes, GL
#include <GL/glew.h>

#if defined (__APPLE__) || defined(MACOSX)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

// includes

#include <rendercheck_gl.h>


///////////////////////////constants///////////////////
#define PRT 3200;
#define RANGECELL 6000;
const unsigned int window_width =700;
const unsigned int window_height =700;


//////////////////User Defined Function////////////////
void MulticastInit();
void circledata();
void display();
void *sweep(void * );
void *video(void *);
int SetTimer(struct timeval &amp;tv, int usec);
int CheckTimer(struct timeval &amp;tv, time_t usec);
void Reverse2Byte(char data[],int number);


//////////////////// GL functionality////////////////////
bool initGL(int argc, char** argv);
void createVBO(GLuint* vboId);
void deleteVBO(GLuint* vboId);
///////////////////// declaration///////////////////


///////////////////GLDeclaration////////////////////
GLuint vboId;
GLuint idcheck;

int threadid;
int sockfd;
struct sockaddr_in address;
struct ip_mreq group;
int len,result;
int reuse=1;
char buffer[200];
pthread_t Receivethread;
struct timeval tv;
int cpivalue=0;
int i;
int indices=0;
float circlevertexarray[5000];
int j=0;
int videodata[3200][6000];
static int prtcounter,rangecellcounter;
struct senddata
{
unsigned short m[10];
}swapeddata;
int count;
////////////////////////circledata////////////////////////////
void circledata()
{
float v1,v2;
for(int no=1;no<6;no++)
{

for(float angle=0;angle<360.0*(3.14/180);angle=angle+0.02)
{
v1=no*0.42*cos(angle);
v2=no*0.42*sin(angle);
circlevertexarray[(j*2)+0]=v1;
circlevertexarray[(j*2)+1]=v2;
j++;
}
}
indices=j+1;
printf("%d\n",indices);

}


////////////////////////create VBO///////////////////////////////

void createVBO(GLuint* vboId)
{
// create buffer object
glGenBuffers(1,vboId);
glBindBuffer(GL_ARRAY_BUFFER, *vboId);
// initialize buffer object
glBufferData(GL_ARRAY_BUFFER,2*indices*sizeof(floa t),circlevertexarray ,GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
#if 1
int bufferSize = 0;

glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &amp;bufferSize);

if(indices != bufferSize)
{
glDeleteBuffersARB(1, vboId);
vboId = 0;
printf("datasize mismatch\n");
}
else
{
printf("matching\n");
}
#endif
}



/////////////////////////MulticastInit///////////////////////////

void MulticastInit()
{
printf("Hello I am Multicast Init\n");
//************Open Socket************//
sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd < 0)
{
perror("Opening datagram socket error");
exit(1);
}
else
{
printf("......Socket Opened Successfully..........\n");
printf("sockfd=%d\n",sockfd);
}

//************Set Socket For reuse********//
if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(char *)&amp;reuse, sizeof(reuse))<0)
{
perror("Reuse Error\n");
}
else
{
printf(".......Reuse Socket Set Successfully........\n");
}

address.sin_family=AF_INET;
address.sin_port=htons(2222);
address.sin_addr.s_addr=INADDR_ANY;
len=sizeof(struct sockaddr);
printf("Length=%d\n",len);

//**********Connect Socket***********//
if(bind(sockfd, (struct sockaddr *)&amp;address, sizeof(address))<0)
{
printf("Socket Connection Failed \n");
close(sockfd);
exit(1);
}
else
{
printf("--------Socket Binded-----------\n");
}


group.imr_multiaddr.s_addr = inet_addr("225.0.0.1");
group.imr_interface.s_addr = inet_addr("192.168.100.185");
if(setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&amp;group, sizeof(group)) < 0)
{
perror("Adding multicast group error");
close(sockfd);
exit(1);
}
else
{
printf("----------Adding Multicast group Ok----\n");
}

printf("--------Mul check------------\n");
}

////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////
// Program main
////////////////////////////////////////////////////////////////////////////////
int main(int argc, char** argv)
{
initGL(argc, argv);
circledata();
createVBO(&amp;vboId);

MulticastInit();
threadid=pthread_create(&amp;Receivethread,NULL,&amp;sweep ,NULL);
glutDisplayFunc(display);
glutMainLoop();
}




////////////////////////////////////////////////////////////////////////////////
//! Initialize GL
////////////////////////////////////////////////////////////////////////////////
bool initGL(int argc, char **argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(window_width, window_height);
glutCreateWindow("Test");
glutDisplayFunc(display);

// initialize necessary OpenGL extensions
glewInit();

float aspectratio=window_width/window_height;

// default initialization
glClearColor(0.0, 0.0, 0.0, 1.0);

// viewport
glViewport(0, 0, window_width, window_height);

// projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glOrtho(-3.0,3.0,-3.0/aspectratio,3.0/aspectratio,-200.0,200.0);
// glOrtho(-3.0,3.0,-3.0,3.0,-200.0,200.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

}




////////////////////////////////////////////////////////////////////////////////
//! Display callback
////////////////////////////////////////////////////////////////////////////////

void display()


{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
while(1)
{
//usleep(10000);
glClearColor(0.0,0.0,0.0,1.0);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

#if 1
glColor3f(1.0,1.0,1.0);
glBindBuffer(GL_ARRAY_BUFFER,vboId);
// glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
// glNormalPointer(GL_FLOAT,0,(void*)sizeof(indices)) ;
glVertexPointer(2, GL_FLOAT,0,0);
glDrawArrays(GL_POINTS,0,indices);
glDisableClientState(GL_VERTEX_ARRAY);
// glDisableClientState(GL_NORMAL_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
#endif

#if 0
glColor3f(1.0f,1.0f,1.0f);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glNormalPointer(GL_FLOAT,0,circlevertexarray);
glVertexPointer(2, GL_FLOAT, 0,circlevertexarray );
glDrawArrays(GL_POINTS,0,indices);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);

glBegin(GL_LINES);
glVertex2f(0.0,0.0);
glVertex2f(0.0,2.1);
glVertex2f(0.0,0.0);
glVertex2f(2.1,0.0);
glVertex2f(0.0,0.0);
glVertex2f(0.0,-2.1);
glVertex2f(0.0,0.0);
glVertex2f(-2.1,0.0);
glEnd();
#endif
#if 0


//circles
glColor3f(1.0,1.0,1.0);
glBegin(GL_POINTS);
for(int i=0;i<6;i++)
{
for(float angle=0;angle<360.0*(3.14/180);angle=angle+0.0157)
{
glVertex2f(i*0.42*cos(angle),i*0.42*sin(angle) );
}
}
glEnd();




//line
glBegin(GL_LINES);
glVertex2f(0.0,0.0);
glVertex2f(0.0,2.1);
glVertex2f(0.0,0.0);
glVertex2f(2.1,0.0);
glVertex2f(0.0,0.0);
glVertex2f(0.0,-2.1);
glVertex2f(0.0,0.0);
glVertex2f(-2.1,0.0);
glEnd();

#endif
///////////////////////
#if 0

glBegin(GL_QUAD_STRIP);
glColor3f(0.0,0.7,0.0);
glVertex2f(0,0);
glVertex2f((5*0.42)*sin((cpivalue*0.0157)),(5*0.42 )*cos((cpivalue*0.0157)));
glColor3f(0.0,0.6,0.0);
glVertex2f(0,0);
glVertex2f((5*0.42)*sin(((cpivalue-10)*0.0157)),(5*0.42)*cos(((cpivalue-10)*0.0157)));
glColor3f(0.0,0.5,0.0);
glVertex2f(0,0);
glVertex2f((5*0.42)*sin(((cpivalue-20)*0.0157)),(5*0.42)*cos(((cpivalue-20)*0.0157)));
glColor3f(0.0,0.0,0.0);
glVertex2f(0,0);
glVertex2f((5*0.42)*sin(((cpivalue-30)*0.0157)),(5*0.42)*cos(((cpivalue-30)*0.0157)));
glEnd();
#endif
glutSwapBuffers();

}//while


}//Display Function

rakesh_thp
11-06-2009, 02:25 AM
i also get only part of my mesh displayed.. just now i posted my problem.. You got any solution..??

scratt
11-06-2009, 03:33 AM
I am wondering if this is your problem and you are drawing an invalid VBO because you have deleted it:

glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &amp;bufferSize);

if(indices != bufferSize)
{
glDeleteBuffersARB(1, vboId);

GL_BUFFER_SIZE is your size in bytes of the VBO, and indices is the number of indices. So they will not be the same....

Janani
11-06-2009, 04:06 AM
Thank you very much,
Problem solved, I am getting the circle properly.

It was Indices and buffersize mismatch.

Now i will proceed with FBO concept, I want to implement Multilayering concept in my application.

Bye..