Don’t use [ quote ] for code blocks. Use [ code ]. Keeps the formatting, and gives you a scroll window so it doesn’t take up so much thread space.
Here’s your code with the Windows-isms and CReader (which you didn’t provide) hacked out of it so it should be cross platform:
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glut.h>
#ifdef WIN32
# include <windows.h>
#else
# include <unistd.h>
#endif
#define MAX_INFO_LOG_SIZE 2048
GLuint v,f,p;
float lpos[4] = {1,0.5,1,0};
float a = 0;
GLuint time_id;
GLfloat lightPos[] = { 140.0f, 250.0f, 140.0f, 1.0f};
GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f};
GLfloat diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f};
GLfloat specularMaterial[] = { 1.0f, 1.0f, 1.0f, 1.0f};
GLfloat cameraPos[] = { 100.0f, 75.0f, 150.0f, 1.0f};
GLint windowWidth = 1024; // window size
GLint windowHeight = 768;
GLint success;
static const char Vertex_src[] =
"//passthrough.vert
"
"void main()"
"{
"
" gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;"
"}";
static const char Fragment_src[] =
"//uniform.frag
"
"void main()"
"{"
" float gray = dot(gl_Color.rgb, vec3(0.299, 0.587, 0.114));"
" gl_FragColor = vec4(gray, gray, gray, 1.0);"
"}";
void mySleep( int sec )
{
#ifdef WIN32
Sleep( sec * 1000 );
#else
sleep( sec );
#endif
}
void DrawModel()
{
// Draw plane that the objects rest on
glColor3f(0.0f, 0.0f, 0.90f); // Blue
glNormal3f(0.0f, 1.0f, 0.0f);
glBegin(GL_QUADS);
glVertex3f(-100.0f, -25.0f, -100.0f);
glVertex3f(-100.0f, -25.0f, 100.0f);
glVertex3f(100.0f, -25.0f, 100.0f);
glVertex3f(100.0f, -25.0f, -100.0f);
glEnd();
// Draw red cube
glColor3f(1.0f, 0.0f, 0.0f);
glutSolidCube(48.0f);
// Draw green sphere
glColor3f(0.0f, 1.0f, 0.0f);
glPushMatrix();
glTranslatef(-60.0f, 0.0f, 0.0f);
glutSolidSphere(25.0f, 50, 50);
glPopMatrix();
// Draw magenta torus
glColor3f(1.0f, 0.0f, 1.0f);
glPushMatrix();
glTranslatef(0.0f, 0.0f, 60.0f);
glutSolidTorus(8.0f, 16.0f, 50, 50);
glPopMatrix();
// Draw yellow cone
glColor3f(1.0f, 1.0f, 0.0f);
glPushMatrix();
glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
glTranslatef(60.0f, 0.0f, -24.0f);
glutSolidCone(25.0f, 50.0f, 50, 50);
glPopMatrix();
// Draw cyan teapot
glColor3f(0.0f, 1.0f, 1.0f);
glPushMatrix();
glTranslatef(0.0f, 0.0f, -60.0f);
glutSolidTeapot(25.0f);
glPopMatrix();
}
void changeSize(int w, int h) {
// Prevent a divide by zero, when window is too short
if(h == 0) h = 1;
float ratio = 1.0* w / h;
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Set the correct perspective.
gluPerspective(45,ratio,1,1000);
glMatrixMode(GL_MODELVIEW);
}
void renderScene(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(cameraPos[0], cameraPos[1], cameraPos[2],
0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
glViewport(0, 0, windowWidth, windowHeight);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
DrawModel();
glUniform1f(time_id, a);
glutSwapBuffers();
}
void setShaders()
{
char *vs = NULL,*fs = NULL;
v = glCreateShader(GL_VERTEX_SHADER);
f = glCreateShader(GL_FRAGMENT_SHADER);
const char * vv = Vertex_src;
const char * ff = Fragment_src;
glShaderSource(v, 1, &vv,NULL);
glShaderSource(f, 1, &ff,NULL);
free(vs);free(fs);
glCompileShader(v);
glGetShaderiv(v, GL_COMPILE_STATUS, &success);
if (!success)
{
GLchar infoLog[MAX_INFO_LOG_SIZE];
glGetShaderInfoLog(v, MAX_INFO_LOG_SIZE, NULL, infoLog);
fprintf(stderr, "Error in fragment shader #%s compilation!
", "v");
fprintf(stderr, "Info log: %s
", infoLog);
mySleep(10);
exit(0);
}
glCompileShader(f);
glGetShaderiv(f, GL_COMPILE_STATUS, &success);
if (!success)
{
GLchar infoLog[MAX_INFO_LOG_SIZE];
glGetShaderInfoLog(f, MAX_INFO_LOG_SIZE, NULL, infoLog);
fprintf(stderr, "Error in fragment shader #%s compilation!
", "f");
fprintf(stderr, "Info log: %s
", infoLog);
mySleep(10);
exit(0);
}
p = glCreateProgram();
glAttachShader(p,v);
glAttachShader(p,f);
glLinkProgram(p);
glUseProgram(p);
time_id = glGetUniformLocation(p, "v_time");
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(windowWidth,windowHeight);
glutCreateWindow("GPGPU Tutorial");
glutDisplayFunc(renderScene);
/*glutIdleFunc(renderScene);*/
glutReshapeFunc(changeSize);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_NORMALIZE);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specularMaterial);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 128.0f);
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(0.0,0.0,1.0);
glEnable(GL_CULL_FACE);
glewInit();
setShaders();
glutMainLoop();
return 0;
}