Need help with setting up shaders on linux


mjohansson

Supporter
Joined
Feb 10, 2011
Messages
409
Hey, I have managed to not get any complaints from compiling, and I get a triangle with the cordinates I want, but its white color, nomatter what I try its always white...


I cant find any clues on google, so help?


my main file is as:


#include "SDL/SDL.h"


#include "GL/glew.h"


#include "math.h"


#include "iostream"


#include "fstream"


std::fstream myfile;


enum {ATTRIB_VERTEX,ATTRIB_COLOR};


float piover180=0.0174532925f;


using namespace std;


int main(){


unsigned short screen_width = 1000, screen_height = 600;


SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);


SDL_Surface* sl1=SDL_SetVideoMode(screen_width,screen_height,16,SDL_HWSURFACE|SDL_OPENGL|SDL_NOFRAME|SDL_DOUBLEBUF);


SDL_Event gameover;


Uint8* key;


bool quit=false;


unsigned char framerate=10;//1000/10=100fps


unsigned int oldTime=0,currentTime=0;


glewInit();


glClearColor(0.0f,0.0f,0.2f,0.0f);


glClearDepth(1.0f);


glEnable(GL_DEPTH_TEST);


//glEnable(GL_TEXTURE_2D);


//glShadeModel(GL_SMOOTH);


glDepthFunc(GL_LEQUAL);//New pixel passes if depth value less or equal


//glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);


float e,xpos=0,zpos=-2,yrot=0,y=0.0f,q=0,fspeed=0.04f,xx=1.0f,nrot=0;


/* SDL_Surface* texturesar[1];


GLuint texture[1];


texturesar[0]=SDL_LoadBMP("mud.bmp");


glGenTextures(1,&texture[0]);


glBindTexture(GL_TEXTURE_2D, texture[0]);


glTexImage2D(GL_TEXTURE_2D,0,3,


texturesar[0]->w,texturesar[0]->h,0,


GL_BGR,GL_UNSIGNED_BYTE,texturesar[0]->pixels);


glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);


glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);


free(texturesar[0]);*/


glBlendFunc(GL_SRC_ALPHA,GL_ONE);


static GLint T0=0,Frames=0;


char vs[9999];


char fs[9999];


myfile.open("versha.txt");


for(int i=0;i<9999;i++){


myfile >> vs;


}myfile.close();






myfile.open("frasha.txt");



for(int i=0;i<9999;i++){



myfile >> fs
;


}myfile.close();






const char * vv=vs;const char * ff=fs;



char pos[4],col[4],mfc[12];






GLuint v=glCreateShader(GL_VERTEX_SHADER),f=glCreateShader(GL_FRAGMENT_SHADER);






glShaderSource(v, 1, &vv, NULL);



glShaderSource(f, 1, &ff, NULL);






glCompileShader(v); glCompileShader(f);






GLuint p = glCreateProgram();



glAttachShader(p,v);



glAttachShader(p,f);



glBindAttribLocation(p,ATTRIB_VERTEX,pos);



glBindAttribLocation(p,ATTRIB_COLOR,col);






glLinkProgram(p);






// GAME LOOP STARTS HERE **************************************



while(quit==false){while(SDL_PollEvent(&gameover))



{if(gameover.type==SDL_QUIT){quit=true;}}






key=SDL_GetKeyState(NULL); // CONTROLS



if(key[sDLK_ESCAPE]){quit=true;}



if(key[sDLK_UP]){



xpos -= ( float )sin( yrot * piover180 ) * fspeed;



zpos += ( float )cos( yrot * piover180 ) * fspeed;}



if(key[sDLK_DOWN]){



xpos += ( float )sin( yrot * piover180 ) * fspeed;



zpos -= ( float )cos( yrot * piover180 ) * fspeed;}



if(key[sDLK_LEFT]){



yrot-=1.0f;if(yrot<0.0f){yrot+=360.0f;}}



if(key[sDLK_RIGHT]){



yrot+=1.0f;if(yrot>360.0f){yrot-=360.0f;}}



if(key[sDLK_a]){nrot=180;}



if(key[sDLK_z]){nrot=360;}



if(key[sDLK_q]){nrot=0;}



if(key[sDLK_e]){e=0.0f;}



if(key[sDLK_w]){nrot+=1.0f;}



if(key[sDLK_s]){e-=0.3f;}



if(key[sDLK_x]){e+=0.3f;}



if(key[sDLK_i]){}



if(key[sDLK_o]){}



if(key[sDLK_p]){}



if(key[sDLK_k]){xx+=1.0f;}



if(key[sDLK_l]){xx-=1.0f;}



if(key[sDLK_j]){xx=1.0f;}// END OF CONTROLS






glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);



/*glMatrixMode(GL_PROJECTION);



glLoadIdentity();



glup(aspect_ratio,1,0.1f,10000.f);



glMatrixMode(GL_MODELVIEW);



glLoadIdentity();*/






static const GLfloat vertices[]={0.0f,0.5f,-0.5f,-0.5f,0.8f,-0.5f};



static const GLfloat colors[]={1.0f,0.0f,0.0f,1.0f, 0.0f,1.0f,0.0f,1.0f, 0.0f,0.0f,1.0f,1.0f};



//glClearColor(0,0,0.2,1);



//glClear(GL_COLOR_BUFFER_BIT);



glUseProgram(p);



glVertexAttribPointer(ATTRIB_VERTEX,2,GL_FLOAT,0,0,vertices);



glVertexAttribPointer(ATTRIB_COLOR,4,GL_FLOAT,0,0,colors);



glEnableVertexAttribArray(ATTRIB_VERTEX);



glEnableVertexAttribArray(ATTRIB_COLOR);



glBindFragDataLocation(p,0,mfc);



glDrawArrays(GL_TRIANGLES,0,3);



glDisableVertexAttribArray(ATTRIB_VERTEX);



glDisableVertexAttribArray(ATTRIB_COLOR);






//cout<<pos<<endl;









SDL_GL_SwapBuffers();



Frames++;



{



GLint t = SDL_GetTicks();



if (t - T0 >= 5000) {



GLfloat seconds = (t - T0) / 1000.0;



GLfloat fps = Frames / seconds;



printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps);



T0 = t;



Frames = 0;



}



}



currentTime=SDL_GetTicks();



if(currentTime-oldTime<framerate)



{SDL_Delay(framerate-(currentTime-oldTime));}



oldTime=SDL_GetTicks();}



SDL_Quit();



return 0;



}












****************************************************



and then I have the shaders as text files:









a



t



t



r



i



b



u



t



e






v



e



c



4






p



o



s



;









a



t



t



r



i



b



u



t



e






v



e



c



4






c



o



l



;









o



u



t






v



a



r



y



i



n



g






v



e



c



4






c



o



l



v



;









v



o



i



d






m



a



i



n



(



)






{






g



l



_



P



o



s



i



t



i



o



n






=






p



o



s



;






c



o



l



v






=






c



o



l



;






}









***********************************************









i



n






v



a



r



y



i



n



g






v



e



c



4






c



o



l



v



;



o



u



t






v



e



c



4






g



l



_



F



r



a



g



C



o



l



o



r



;



o



u



t






v



e



c



4






m



f



c



;






v



o



i



d






m



a



i



n



(



)



{



g



l



_



F



r



a



g



C



o



l



o



r



=



c



o



l



v



;



m



f



c



=



g



l



_



F



r



a



g



C



o



l



o



r



;



}






**********************************************






and for ease of read heres the code inline:






// VERTEX






attribute vec4 pos; attribute vec4 col; out varying vec4 colv;



void main(){gl_Position=pos;colv=col;}






// FRAGMENT



in varying vec4 colv;out vec4 gl_FragColor;out vec4 mfc;






void main(){gl_FragColor=colv;mfc=gl_FragColor;}
 
You should add some error checking to your shader compilations, but may be the actual problem.


The matrix operations your using are depreciated with opengl 2/3, those need to be calculated yourself and passed in the vertex shader to be used on the vertex positions.


Check out this tutorial, i followed it and my opengl 2.0 es code worked on the pandora and on modern desktops: http://pandorawiki.org/GLESGAE
 
Ok, error checking was the thing to do, I started out cout the content of the source for shaders, apparently the file reading I did skipped over whitespaces... I changed the filereading now so I dont have to write everything into single caracter per line wich is nice... Then I had problems linking the program, removing all variables except one did the trick, so now I only have attribute pos, need to go from here to add more inputs and passthrough of data to and between the shaders.


But its up and running now finally the basic triangle atleast, the reason I got a triangle at all before was because glVertexAttribPointer can be used individually for glDrawArrays. The shaders were never running with the above code.


Ill post the updated code later.
 
Wow, I cant believe how much trouble such trivial details gave me, but now its working atleast, but weirdly so, very slow to get up and running, and its not framerate limited perhaps thats why it makes my PC sound weird when it runs, but yea something definitly seem fishy about this program, I will look into getting shaders up and running more later, atleast theres a easy and small context that works now for anyone else that might have had problems with getting shader source codes working for ex.


The main problem have been getting the shader sources into files properly, I noticed when couting out the shadersources that there were weird caracters at the end, I tryed several different ways of reading files until I noticed in tutorials to NULL-terminate the shaadersources, and that finally worked.


In the following code there is the line buffer1[lenght1-1]=0; Thats the NULL-termination and its of utmost importance. Without doing that more often then not wierd caracters will be applied at end of sourcefiles.


The code is as follows:


#include "SDL/SDL.h"


#include "GL/glew.h"


#include "iostream"


#include "fstream"


using namespace std;


enum {ATTRIB_VERTEX=0, ATTRIB_COLOR=1};


int main(){


SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);


SDL_Surface* s=SDL_SetVideoMode(640,480,16,SDL_HWSURFACE|SDL_OPENGL|SDL_DOUBLEBUF);


SDL_Event gameover;


Uint8* key;


int quit=0;


glewInit();


glClearDepth(1.0f);


glEnable(GL_DEPTH_TEST);


glDepthFunc(GL_LEQUAL);


GLfloat colors[]={1.0,0.0,0.0,1.0, 0.0,1.0,0.0,1.0, 0.0,0.0,1.0,1.0};


GLfloat vertices[]={-0.5,-0.5, 0.5,-0.5, 0.0,0.5};


int lenght1,lenght2;


char * buffer1, * buffer2;


ifstream is1,is2,is3,is4;


is1.open("versha4.txt",ios::binary);


is1.seekg(0,ios::end);


lenght1=is1.tellg();


is1.seekg(0,ios::beg);


buffer1=new char [lenght1];


is1.read(buffer1,lenght1);


is1.close();buffer1[lenght1-1]=0;


is2.open("frasha4.txt",ios::binary);


is2.seekg(0,ios::end);


lenght2=is2.tellg();


is2.seekg(0,ios::beg);


buffer2=new char [lenght2];


is2.read(buffer2,lenght2);


is2.close();buffer2[lenght2-1]=0;


const GLchar* vv=buffer1;


const GLchar* ff=buffer2;


cout<<vv<<endl;cout<<ff<<endl;


GLenum v=glCreateShader(GL_VERTEX_SHADER);


GLenum f=glCreateShader(GL_FRAGMENT_SHADER);


glShaderSource(v, 1, &vv, NULL);


glShaderSource(f, 1, &ff, NULL);


glCompileShader(v); glCompileShader(f);


int IsCompiled_FS;


glGetShaderiv(f,GL_COMPILE_STATUS,&IsCompiled_FS);


if(IsCompiled_FS==true){cout<<"FRAG=GOOD!"<<endl;}


else{cout<<"FRAG=BAD..."<<endl;}


glGetShaderiv(v,GL_COMPILE_STATUS,&IsCompiled_FS);


if(IsCompiled_FS==true){cout<<"VERT=GOOD!"<<endl;}


else{cout<<"VERT=BAD..."<<endl;}


GLenum prog = glCreateProgram();


glAttachShader(prog,v);


glAttachShader(prog,f);


glLinkProgram(prog);


glGetProgramiv(prog,GL_LINK_STATUS,(int *)&IsCompiled_FS);


if(IsCompiled_FS==true){cout<<"LINK=GOOD!"<<endl;}


else{cout<<"LINK=BAD..."<<endl;}


glBindAttribLocation(prog,ATTRIB_VERTEX,"pos");


glBindAttribLocation(prog,ATTRIB_COLOR,"col");


while(quit==0){while(SDL_PollEvent(&gameover))


{if(gameover.type==SDL_QUIT){quit=1;}}


key=SDL_GetKeyState(NULL);


if(key[sDLK_ESCAPE]){quit=1;}


glClearColor(0.0f,0.0f,0.2f,0.0f);


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


glVertexAttribPointer(ATTRIB_VERTEX,2,GL_FLOAT,0,0,vertices);


glVertexAttribPointer(ATTRIB_COLOR,4,GL_FLOAT,0,0,colors);


glEnableVertexAttribArray(ATTRIB_VERTEX);


glEnableVertexAttribArray(ATTRIB_COLOR);


glUseProgram(prog);


glDrawArrays(GL_TRIANGLES,0,3);


glDisableVertexAttribArray(ATTRIB_VERTEX);


glDisableVertexAttribArray(ATTRIB_COLOR);


SDL_GL_SwapBuffers();}


delete[] buffer1;


delete[] buffer2;


SDL_Quit();


return 0;


}


VERTEX SHADER :


attribute vec2 pos;


attribute vec4 col;


varying vec4 colv;


void main()


{


gl_Position = vec4(pos.x,pos.y,0.0,1.0);


colv=col;


}


(Its important to have a whitespace added at end after }, otherwise that last } will fall away from sourcefile.)


FRAGMENT SHADER :


varying vec4 colv;


void main()


{


gl_FragColor = colv;


}


(Again, leave a last whitespace to keep the last } in the sourcefile)


Also the vertice adn color data is flipped with eachother, I have no idea why, if anyone does please tell! :)


Also, just in case some beginner programmer should read this, running this in linux is done like this:


install the GLEW dev library by searching for GLEW in the packagemanager, get the DEV package, if you dont know what is what get all GLEW files, then do same with SDL, get a dev library, if theres several versions it probably dosnt matter wich one I think, or get SDL by opening the terminal and type


sudo apt-get install libsdl1.2-dev


Then also write


sudo aptitude install build-essential


I think openGL should be included there and gcc.


Then write the main source as a .cpp file, and shaders as .txt files in KATE wich can be downloaded from packagemanager also. Then go to the directory in the terminal program were files are and type


g++ nameofyourfile.cpp -lSDL -lGLEW


then type ./a.out to run the program wich is temporarily written in the "a" file.


Ill post more beginner files later and as Pandora versions to so new people can start learning programing as easy as possible.
 
Last edited by a moderator:
The problem was an easy fix, move the glBindAttribLocation commands to before glLinkProgram.
 
Rather than requiring whitespace at the end of your text files it's better to just allocate an extra byte for the buffer, that way it doesn't matter if your files have whitespace or not. C strings should always be allocated 1 byte longer than the length of text and null terminated.
 
How do I allocate an extra byte, do I just add 1 to lenght, like buffer1=new char [lenght1+1]; will that work?


I get an error message when I try to compile for the Pandora using EGL port, it says declaration of ...usr/include/GLES2/gl2.h glTexImage2D conflicts with ...usr/include/GLES/gl.h glTexImage2D


I use gl2.h in my source, in the eglport file theres gl.h included, changing that to gl2.h yields lots of undeclared stuff. All the shader stuff is undeclared if I try to use gl.h in my sorce.


Also an update with perspective rendering plus moving camera, this isnt the way its supposed to be done, rotations are doen on cpu rather then gpu, I couldnt get it work in various ways I tried yet, I still think theres some clean and easy to understand usefull stuff here, but I dont know, I will probably not post much more code after this, hopefully it can be an easy start for some people but you need to correct and do things differently probably. I dont know, here it is anyway...


// shader, camera.


#include "SDL/SDL.h"


#include "GL/glew.h"


#include "iostream"


#include "fstream"


#include "math.h"


using namespace std;


enum {ATTRIB_VERTEX=0, ATTRIB_COLOR=1};


float p1=(float)3.141592/180;//0.017453


void setps(float fov,float ar,float& ett,float& tva, float& tre, float& fyr, float& fem, float& sex){


float xmin,xmax,ymin,ymax,zn=1.0f,zf=999999.0f,left,right,top,bottom;ymax=(float)zn*tan(fov*0.00872);


ymin=-ymax;xmin=ymin*ar;xmax=ymax*ar;left=xmin;right=xmax;bottom=ymin;top=ymax;ett=(float)(2*zn)/(right-left);


tva=(float)(2*zn)/(top-bottom);tre=(float)(right+left)/(right-left);fyr=(float)(top+bottom)/(top-bottom);


fem=(float)-((zf+zn)/(zf-zn));sex=(float)-((2*zf*zn)/(zf-zn));}


void cam(float rx,float ry,float rz,


float& t1x,float& t1y,float& t1z,float& t2x,float& t2y,float& t2z,float& t3x,float& t3y,float& t3z){


float sx,cx,sy,cy,sz,cz,theta;theta = rx * 0.017453;sx=sin(theta);cx=cos(theta);theta = ry * 0.017453;


sy=sin(theta);cy=cos(theta);theta = rz * 0.017453;sz=sin(theta);cz=cos(theta);t1x=cz*cy-sz*sx*sy;t1y=sz*cy+cz*sx*sy;


t1z=-cx*sy;t2x=-sz*cx;t2y=cz*cx;t2z=sx;t3x=cz*sy+sz*sx*cy;t3y=sz*sy-cz*sx*cy;t3z=cx*cy;}


void rotate(float xp,float yp,float& ux,float& uy,float degrees){


float bt,lx,ly,rx,ry,nnx,a;if(uy<=yp&&ux>=xp){a=atan((yp-uy)/(ux-xp))*57.295791;lx=ux-xp;ly=yp-uy;}


if(uy<=yp&&xp>ux){a=90.0+(90.0-acos((xp-ux)/sqrt(((yp-uy)*(yp-uy))+((xp-ux)*(xp-ux))))*57.295791);lx=xp-ux;ly=yp-uy;}


if(yp<uy&&ux>=xp){a=360.0-(acos((ux-xp)/sqrt(((uy-yp)*(uy-yp))+((ux-xp)*(ux-xp))))*57.295791);lx=ux-xp;ly=uy-yp;}


if(yp<uy&&xp>ux){a=270.0-(90.0-(atan((uy-yp)/(xp-ux))*57.295791));lx=xp-ux;ly=uy-yp;}bt=sqrt((lx*lx)+(ly*ly));


a+=degrees+45;rx=(cos(a*0.017453))+(sin(a*0.017453));ry=(-sin(a*0.017453))+(cos(a*0.017453));


rx+=0.000001;ry+=0.000001;nnx=sqrt((rx*rx)+(ry*ry));rx/=nnx;ry/=nnx;rx*=bt;ry*=bt;ux=xp+rx;uy=yp-ry;}


int main(){


SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);


SDL_Surface* s=SDL_SetVideoMode(640,480,16,SDL_HWSURFACE|SDL_OPENGL|SDL_DOUBLEBUF);


SDL_Event gameover,mouse;


Uint8* key;


int quit=0;


static GLint T0=0,Frames=0;


unsigned char framerate=10;//1000/10=100fps


unsigned int oldTime=0,currentTime=0;


glewInit();


glClearDepth(1.0f);


glEnable(GL_DEPTH_TEST);


glDepthFunc(GL_LEQUAL);


GLfloat colors[]={1.0,0.0,0.0,1.0, 0.0,1.0,0.0,1.0, 0.0,0.0,1.0,1.0};


GLfloat vertices[]={-0.3,-0.5,-2.0, 0.5,-0.5,-2.0, 0.0,0.5,-2.0,


-0.8,-0.5,-3.0, 0.5,-0.5,-3.0, 0.0,0.5,-3.0};


int lenght1,lenght2,foward=0,backward=0,left=0,right=0,free_float=0,fff=0,ffp=0,c=0;


char * buffer1, * buffer2;


float r1x,r1y,r1z,r2x,r2y,r2z,r3x,r3y,r3z,dett,dtva,dtre,dfyr,dfem,dsex;


float r_fov=45.0f,r_aspectRatio=1.0f;


float xpos=0.0f,ypos=0.0f,zpos=0.0f,xr=0.0f,yr=0.0f,zr=0.0f,ye,rx,ry,nnx,ms=0.1f;


GLfloat mats[16],va[18],v2[9];


ifstream is1,is2;


is1.open("versha4.txt",ios::binary);


is1.seekg(0,ios::end);


lenght1=is1.tellg();


is1.seekg(0,ios::beg);


buffer1=new char [lenght1];


is1.read(buffer1,lenght1);


is1.close();buffer1[lenght1-1]=0;


is2.open("frasha4.txt",ios::binary);


is2.seekg(0,ios::end);


lenght2=is2.tellg();


is2.seekg(0,ios::beg);


buffer2=new char [lenght2];


is2.read(buffer2,lenght2);


is2.close();buffer2[lenght2-1]=0;


const GLchar* vv=buffer1;


const GLchar* ff=buffer2;


//cout<<vv<<endl;cout<<ff<<endl;


GLenum v=glCreateShader(GL_VERTEX_SHADER);


GLenum f=glCreateShader(GL_FRAGMENT_SHADER);


glShaderSource(v, 1, &vv, NULL);


glShaderSource(f, 1, &ff, NULL);


glCompileShader(v); glCompileShader(f);


int IsCompiled_FS;


glGetShaderiv(f,GL_COMPILE_STATUS,&IsCompiled_FS);


if(IsCompiled_FS==true){cout<<"FRAG=GOOD!"<<endl;}


else{cout<<"FRAG=BAD..."<<endl;}


glGetShaderiv(v,GL_COMPILE_STATUS,&IsCompiled_FS);


if(IsCompiled_FS==true){cout<<"VERT=GOOD!"<<endl;}


else{cout<<"VERT=BAD..."<<endl;}


GLenum prog = glCreateProgram();


glAttachShader(prog,v);


glAttachShader(prog,f);


glBindAttribLocation(prog,ATTRIB_VERTEX,"pos");


glBindAttribLocation(prog,ATTRIB_COLOR,"col");


glLinkProgram(prog);


glGetProgramiv(prog,GL_LINK_STATUS,(int *)&IsCompiled_FS);


if(IsCompiled_FS==true){cout<<"LINK=GOOD!"<<endl;}


else{cout<<"LINK=BAD..."<<endl;}


float mx=0.2f,my=0.2f;


SDL_WM_GrabInput(SDL_GRAB_ON);


SDL_WarpMouse(320,240);


SDL_ShowCursor(0);


while(quit==0){


while(SDL_PollEvent(&mouse)){switch(mouse.type){


case SDL_MOUSEMOTION:


xr+=mouse.motion.xrel*mx;


yr+=mouse.motion.yrel*my;


break;}}


while(SDL_PollEvent(&gameover))


{if(gameover.type==SDL_QUIT){quit=1;}}


key=SDL_GetKeyState(NULL);


if(key[sDLK_ESCAPE]){quit=1;}


if(key[sDLK_UP]){foward=1;}else{foward=0;}


if(key[sDLK_DOWN]){backward=1;}else{backward=0;}


if(key[sDLK_LEFT]){left=1;}else{left=0;}


if(key[sDLK_RIGHT]){right=1;}else{right=0;}


if(key[sDLK_b]){zr=0.0f;}


if(key[sDLK_m]){zr-=0.5f;}


if(key[sDLK_n]){zr+=0.5f;}


if(key[sDLK_f]){fff=1;}else{ffp=0;}


if(key[sDLK_d]){ypos=0.0f;}


if(fff==1&&ffp==0)


{if(free_float==1){free_float=0;}else{free_float=1;}ffp=1;cout<<"FREE_FLOAT = "<<free_float<<endl;}fff=0;


// MOVEMENT PROCESSING ::::::::::::::::::::::::::::::::::::::::::::::::::::


while(xr>359.999999f){xr-=360.0f;}while(xr<0.0f){xr+=360.0f;}


while(yr>359.999999f){yr-=360.0f;}while(yr<0.0f){yr+=360.0f;}


while(zr>359.999999f){zr-=360.0f;}while(zr<0.0f){zr+=360.0f;}


if(yr<270.0f&&yr>180.0f){yr=270.0f;}


if(yr>90.0f&&yr<=180.0f){yr=90.0f;}


if(yr<=90.0f){if(yr==90.0f){ye=0.0f;}else{ye=(float)(90.0f-yr)/90.0f;}}


else{if(yr==270.0f){ye=0.0f;}else{ye=(float)(yr-270.0f)/90.0f;}}


if(foward==1){


rx=(float)(cos((xr+135)*0.017453))+(sin((xr+135)*0.017453));


ry=(float)(-sin((xr+135)*0.017453))+(cos((xr+135)*0.017453));


nnx=sqrt((rx*rx)+(ry*ry));rx/=nnx;ry/=nnx;rx*=ms;ry*=ms;


if(free_float==0){xpos-=rx;zpos+=ry;}else{


xpos-=rx*ye;zpos+=ry*ye;if(yr<=90.0f){ypos-=ms*(1.0f-ye);}else{ypos+=ms*(1.0f-ye);}}}


if(backward==1){


rx=(float)(cos((xr+135)*0.017453))+(sin((xr+135)*0.017453));


ry=(float)(-sin((xr+135)*0.017453))+(cos((xr+135)*0.017453));


nnx=sqrt((rx*rx)+(ry*ry));rx/=nnx;ry/=nnx;rx*=ms;ry*=ms;


if(free_float==0){xpos+=rx;zpos-=ry;}else{


xpos+=rx*ye;zpos-=ry*ye;if(yr>90.0f){ypos-=ms*(1.0f-ye);}else{ypos+=ms*(1.0f-ye);}}}


if(left==1){


rx=(float)(cos((xr+225)*0.017453))+(sin((xr+225)*0.017453));


ry=(float)(-sin((xr+225)*0.017453))+(cos((xr+225)*0.017453));


nnx=sqrt((rx*rx)+(ry*ry));rx/=nnx;ry/=nnx;rx*=ms;ry*=ms;xpos+=rx;zpos-=ry;}


if(right==1){


rx=(float)(cos((xr+45)*0.017453))+(sin((xr+45)*0.017453));


ry=(float)(-sin((xr+45)*0.017453))+(cos((xr+45)*0.017453));


nnx=sqrt((rx*rx)+(ry*ry));rx/=nnx;ry/=nnx;rx*=ms;ry*=ms;xpos+=rx;zpos-=ry;}


// RENDERING SET UP ::::::::::::::::::::::::::::::::::::::::::::::


setps(r_fov,r_aspectRatio,dett,dtva,dtre,dfyr,dfem,dsex);


mats[0]=dett;mats[1]=0.0f;mats[2]=0.0f;mats[3]=0.0f;


mats[4]=0.0f;mats[5]=dtva;mats[6]=0.0f;mats[7]=0.0f;


mats[8]=dtre;mats[9]=dfyr;mats[10]=dfem;mats[11]=-1.0f;


mats[12]=0.0f;mats[13]=0.0f;mats[14]=dsex;mats[15]=0.0f;


cam(yr,xr,zr,r1x,r1y,r1z,r2x,r2y,r2z,r3x,r3y,r3z);


while(c<18){// ROTATE VERTICES AND TRANSLATE FOR CAMERA :::::::


va[c]=(r1x*(vertices[c]-xpos))+(r2x*(vertices[c+1]-ypos))+(r3x*(vertices[c+2]-zpos));


va[c+1]=(r1y*(vertices[c]-xpos))+(r2y*(vertices[c+1]-ypos))+(r3y*(vertices[c+2]-zpos));


va[c+2]=(r1z*(vertices[c]-xpos))+(r2z*(vertices[c+1]-ypos))+(r3z*(vertices[c+2]-zpos));


c+=3;}c=0;


int location1=glGetUniformLocation(prog,"matp");


glUniformMatrix4fv(location1, 1, 1, mats);


glClearColor(0.0f,0.0f,0.2f,0.0f);


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


while(c<9){v2[c]=va[c];c+=1;}c=0;


glVertexAttribPointer(ATTRIB_VERTEX,3,GL_FLOAT,0,0,v2);


glVertexAttribPointer(ATTRIB_COLOR,4,GL_FLOAT,0,0,colors);


glEnableVertexAttribArray(ATTRIB_VERTEX);


glEnableVertexAttribArray(ATTRIB_COLOR);


glUseProgram(prog);


glDrawArrays(GL_TRIANGLES,0,3);


while(c<9){v2[c]=va[c+9];c+=1;}c=0;


glVertexAttribPointer(ATTRIB_VERTEX,3,GL_FLOAT,0,0,v2);


glDrawArrays(GL_TRIANGLES,0,3);


glDisableVertexAttribArray(ATTRIB_VERTEX);


glDisableVertexAttribArray(ATTRIB_COLOR);


SDL_GL_SwapBuffers();


Frames++;


{


GLint t = SDL_GetTicks();


if (t - T0 >= 5000) {


GLfloat seconds = (t - T0) / 1000.0;


GLfloat fps = Frames / seconds;


printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps);


T0 = t;


Frames = 0;


}


}


currentTime=SDL_GetTicks();


if(currentTime-oldTime<framerate)


{SDL_Delay(framerate-(currentTime-oldTime));}


oldTime=SDL_GetTicks();}


delete[] buffer1;


delete[] buffer2;


SDL_Quit();


return 0;


}


*******************************************************************************************************************


The VERTEX shader now looks like this:


attribute vec3 pos;


attribute vec4 col;


varying vec4 colv;


uniform mat4 matp;


void main()


{


gl_Position = vec4(pos.x,pos.y,pos.z,1.0)*matp;


colv=col;


}
 
Yes just make your buffer as size+1, and once you have loaded the data to it set the last byte in the buffer to null.


I have a better eglport I will put up today that has proper support for both gles versions.


Matrix operations are not run on the gpu anyways, as Ive come to understand it even the matrix functions (i.e. glRotate) are run on the cpu through the driver. I found a nice matrix math library called cml, that you may want to look at.
 
Matrix operations are not run on the gpu anyways, as Ive come to understand it even the matrix functions (i.e. glRotate) are run on the cpu through the driver.
The only operations that are run on the gpu are the final vertexData * transformMatrix & lighting/texture (or anything you put in shader code for gles2). It's only when you submit verticies that the current transform matrix and other lighting, texture, etc. settings get sent to the gpu.
 
Back
Top