mjohansson
Supporter
- Joined
- Feb 10, 2011
- Messages
- 409
I get the window open and random colored pixels and then the Panda freezes.
My error check messages dont get printed out in the terminal, but the EGL messages do.
I use the old EGL port and have blanked out the line with close() cos it said it was undefined, I have changed in .h from include GLES to GLES2/gl2.h I think, and I type -lGLESv2 when compiling, no complaints from compiler.
And the code is the following:
main cpp:
#include "SDL/SDL.h"
#include "eglport.c"
//#include "eglport.c"
//#include "GLES2/gl2.h"
#include "iostream"
#include "fstream"
#include "math.h"
#include "unistd.h"
using namespace std;
//enum {ATTRIB_VERTEX=0, ATTRIB_COLOR=1};
int verta=0,cola=1;
void setps(float fov,float ar,float zn,float& left,float& right,float& bottom,float& top){
float xmin,xmax,ymin,ymax;ymax=(float)zn*tan(fov*0.00872);
ymin=-ymax;xmin=ymin*ar;xmax=ymax*ar;left=xmin;right=xmax;bottom=ymin;top=ymax;}
int main(){
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
EGL_Open();
SDL_Surface* sl1;
sl1=SDL_SetVideoMode(400,380,0,SDL_HWSURFACE);
EGL_Init();
// SDL_Surface* s=SDL_SetVideoMode(640,480,16,SDL_HWSURFACE|SDL_OPENGL|SDL_DOUBLEBUF);
SDL_Event gameover;
Uint8* key;
int quit=0;
static int T0=0,Frames=0;
unsigned char framerate=10;//1000/10=100fps
unsigned int oldTime=0,currentTime=0;
//glewInit();
glClearDepthf(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
float 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};
float vertices[]={-0.5,-0.5,-2.0, 0.5,-0.5,-2.0, 0.0,0.5,-2.0};
int lenght1,lenght2,foward=0,backward=0,left=0,right=0,free_float=0;
char * buffer1, * buffer2;
float r_fov=45.0f,r_aspectRatio=1.0f,rn=1.0f,rf=200000.0f,rleft,rright,rbottom,rtop;
float tx,ty,tz,ta,xpos=0.0f,ypos=0.0f,zpos=0.0f,xr=0.0f,yr=0.0f,zr=0.0f,ye,rx,ry,nnx,ms=1.0f;
float matr[16],matt[16],mats[16];
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 char* vv=buffer1;
const char* ff=buffer2;
cout<<vv<<endl;cout<<ff<<endl;
int v=glCreateShader(GL_VERTEX_SHADER);
int 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;}
int prog = glCreateProgram();
glAttachShader(prog,v);
glAttachShader(prog,f);
glBindAttribLocation(prog,verta,"pos");
glBindAttribLocation(prog,cola,"col");
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;}
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_m]){xr-=0.5f;}
if(key[sDLK_n]){xr+=0.5f;}
// 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(ry<=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(ry<=90.0f){ypos+=ms*(1.0f-ye);}else{ypos-=ms*(1.0f-ye);}}}
if(left==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;}
if(right==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;}
tx=0;ty=0;tz=0;ta=xr;
// CREATE PERSPECTIVE RENDERING :::::::::::::::::::::::::::::::::::::::::::
setps(r_fov,r_aspectRatio,rn,rleft,rright,rbottom,rtop);
matr[0]=(float)(2*rn)/(rright-rleft);
matr[1]=0.0f;
matr[2]=0.0f;
matr[3]=0.0f;
matr[4]=0.0f;
matr[5]=(float)(2*rn)/(rtop-rbottom);
matr[6]=0.0f;
matr[7]=0.0f;
matr[8]=(float)(rright+rleft)/(rright-rleft);
matr[9]=(float)(rtop+rbottom)/(rtop-rbottom);
matr[10]=(float)-((rf+rn)/(rf-rn));
matr[11]=-1.0f;
matr[12]=0.0f;
matr[13]=0.0f;
matr[14]=(float)-((2*rf*rn)/(rf-rn));
matr[15]=0.0f;
int location1=glGetUniformLocation(prog,"matp");
glUniformMatrix4fv(location1, 1, 1, matr);
glClearColor(0.0f,0.0f,0.2f,0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glVertexAttribPointer(verta,3,GL_FLOAT,0,0,vertices);
glVertexAttribPointer(cola,4,GL_FLOAT,0,0,colors);
glEnableVertexAttribArray(verta);
glEnableVertexAttribArray(cola);
glUseProgram(prog);
glDrawArrays(GL_TRIANGLES,0,3);
glDisableVertexAttribArray(verta);
glDisableVertexAttribArray(cola);
// SDL_GL_SwapBuffers();
EGL_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;
}
****************************************************************************************************************
versha4.txt:
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;
}
****************************************************************************************************************
frasha4.txt:
varying vec4 colv;
void main()
{
gl_FragColor = vec4(colv.xyz,1.0);
}
My error check messages dont get printed out in the terminal, but the EGL messages do.
I use the old EGL port and have blanked out the line with close() cos it said it was undefined, I have changed in .h from include GLES to GLES2/gl2.h I think, and I type -lGLESv2 when compiling, no complaints from compiler.
And the code is the following:
main cpp:
#include "SDL/SDL.h"
#include "eglport.c"
//#include "eglport.c"
//#include "GLES2/gl2.h"
#include "iostream"
#include "fstream"
#include "math.h"
#include "unistd.h"
using namespace std;
//enum {ATTRIB_VERTEX=0, ATTRIB_COLOR=1};
int verta=0,cola=1;
void setps(float fov,float ar,float zn,float& left,float& right,float& bottom,float& top){
float xmin,xmax,ymin,ymax;ymax=(float)zn*tan(fov*0.00872);
ymin=-ymax;xmin=ymin*ar;xmax=ymax*ar;left=xmin;right=xmax;bottom=ymin;top=ymax;}
int main(){
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
EGL_Open();
SDL_Surface* sl1;
sl1=SDL_SetVideoMode(400,380,0,SDL_HWSURFACE);
EGL_Init();
// SDL_Surface* s=SDL_SetVideoMode(640,480,16,SDL_HWSURFACE|SDL_OPENGL|SDL_DOUBLEBUF);
SDL_Event gameover;
Uint8* key;
int quit=0;
static int T0=0,Frames=0;
unsigned char framerate=10;//1000/10=100fps
unsigned int oldTime=0,currentTime=0;
//glewInit();
glClearDepthf(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
float 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};
float vertices[]={-0.5,-0.5,-2.0, 0.5,-0.5,-2.0, 0.0,0.5,-2.0};
int lenght1,lenght2,foward=0,backward=0,left=0,right=0,free_float=0;
char * buffer1, * buffer2;
float r_fov=45.0f,r_aspectRatio=1.0f,rn=1.0f,rf=200000.0f,rleft,rright,rbottom,rtop;
float tx,ty,tz,ta,xpos=0.0f,ypos=0.0f,zpos=0.0f,xr=0.0f,yr=0.0f,zr=0.0f,ye,rx,ry,nnx,ms=1.0f;
float matr[16],matt[16],mats[16];
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 char* vv=buffer1;
const char* ff=buffer2;
cout<<vv<<endl;cout<<ff<<endl;
int v=glCreateShader(GL_VERTEX_SHADER);
int 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;}
int prog = glCreateProgram();
glAttachShader(prog,v);
glAttachShader(prog,f);
glBindAttribLocation(prog,verta,"pos");
glBindAttribLocation(prog,cola,"col");
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;}
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_m]){xr-=0.5f;}
if(key[sDLK_n]){xr+=0.5f;}
// 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(ry<=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(ry<=90.0f){ypos+=ms*(1.0f-ye);}else{ypos-=ms*(1.0f-ye);}}}
if(left==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;}
if(right==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;}
tx=0;ty=0;tz=0;ta=xr;
// CREATE PERSPECTIVE RENDERING :::::::::::::::::::::::::::::::::::::::::::
setps(r_fov,r_aspectRatio,rn,rleft,rright,rbottom,rtop);
matr[0]=(float)(2*rn)/(rright-rleft);
matr[1]=0.0f;
matr[2]=0.0f;
matr[3]=0.0f;
matr[4]=0.0f;
matr[5]=(float)(2*rn)/(rtop-rbottom);
matr[6]=0.0f;
matr[7]=0.0f;
matr[8]=(float)(rright+rleft)/(rright-rleft);
matr[9]=(float)(rtop+rbottom)/(rtop-rbottom);
matr[10]=(float)-((rf+rn)/(rf-rn));
matr[11]=-1.0f;
matr[12]=0.0f;
matr[13]=0.0f;
matr[14]=(float)-((2*rf*rn)/(rf-rn));
matr[15]=0.0f;
int location1=glGetUniformLocation(prog,"matp");
glUniformMatrix4fv(location1, 1, 1, matr);
glClearColor(0.0f,0.0f,0.2f,0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glVertexAttribPointer(verta,3,GL_FLOAT,0,0,vertices);
glVertexAttribPointer(cola,4,GL_FLOAT,0,0,colors);
glEnableVertexAttribArray(verta);
glEnableVertexAttribArray(cola);
glUseProgram(prog);
glDrawArrays(GL_TRIANGLES,0,3);
glDisableVertexAttribArray(verta);
glDisableVertexAttribArray(cola);
// SDL_GL_SwapBuffers();
EGL_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;
}
****************************************************************************************************************
versha4.txt:
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;
}
****************************************************************************************************************
frasha4.txt:
varying vec4 colv;
void main()
{
gl_FragColor = vec4(colv.xyz,1.0);
}