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;}
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;}