summaryrefslogtreecommitdiffstats
path: root/headtrack_stereo_demo/shadertest/shadertest.cpp
blob: 28ca58d805f32d688b2833feab650bc7f8760c4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <GL/glew.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include "textfile.h"
GLuint          g_GreyScaleShaderProgram;   //handle to the grayscale shader program
GLint           g_GSConvScaleLoc;           //handle to the g_ConversionScale variable in the shader

void Display()
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  float ambdif[] = {0.0, 0.0, 1.0, 1.0},
        em[]     = {0.0, 0.0, 0.0, 1.0},
        spec[]   = {1.0, 1.0, 1.0, 1.0};

  glPushMatrix();

    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, ambdif);
    glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, em);
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec);
    glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 64.0);

    //glColor3d(0.0, 0.0, 1.0);
    glTranslated(0.0, 0.0, -30.0);
    glRotated(-60.0, 0.0, 1.0, 0.0);
    glutSolidTeapot(20.0);
  glPopMatrix();

  glutSwapBuffers();
}

void Idle()
{
  glutPostRedisplay();
}

void Reshape(int w, int h)
{
  glViewport(0, 0, w, h);

  //should we set a new projection matrix?
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

  glFrustum(-20.0, 20.0, -15.0, 15.0, 100.0, 1100.0);
  glTranslated(0.0, 0.0, -100.0);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void printShaderInfoLog(GLuint obj)
{
    int infologLength = 0;
    int charsWritten  = 0;
    char *infoLog;

	glGetShaderiv(obj, GL_INFO_LOG_LENGTH,&infologLength);

    if (infologLength > 0)
    {
        infoLog = (char *)malloc(infologLength);
        glGetShaderInfoLog(obj, infologLength, &charsWritten, infoLog);
		printf("%s\n",infoLog);
        free(infoLog);
    }
}

void printProgramInfoLog(GLuint obj)
{
    int infologLength = 0;
    int charsWritten  = 0;
    char *infoLog;

	glGetProgramiv(obj, GL_INFO_LOG_LENGTH,&infologLength);

    if (infologLength > 0)
    {
        infoLog = (char *)malloc(infologLength);
        glGetProgramInfoLog(obj, infologLength, &charsWritten, infoLog);
		printf("%s\n",infoLog);
        free(infoLog);
    }
}

bool LoadGreyScaleShader()
{
  GLint l_bStatus;
  char *vs_text = NULL,*fs_text = NULL;
  GLuint  vs = glCreateShader(GL_VERTEX_SHADER),
	        fs = glCreateShader(GL_FRAGMENT_SHADER);
	
	vs_text = textFileRead("shaders\\greyscale.vert");
  if (vs_text == NULL)
  {
    return false;
  }
	fs_text = textFileRead("shaders\\greyscale.frag");
  if (fs_text == NULL)
  {
    free(vs_text);
    return false;
  }

	glShaderSource(vs, 1, (const char**)&vs_text,NULL);
	glShaderSource(fs, 1, (const char**)&fs_text,NULL);
	
	free(vs_text);
  free(fs_text);

	glCompileShader(vs);
  printShaderInfoLog(vs);
  glGetShaderiv(vs, GL_COMPILE_STATUS, &l_bStatus);
  if (l_bStatus == GL_FALSE)
  {
    return false;
  }

	glCompileShader(fs);
  printShaderInfoLog(fs);
  glGetShaderiv(fs, GL_COMPILE_STATUS, &l_bStatus);
  if (l_bStatus == GL_FALSE)
  {
    return false;
  }
	
  g_GreyScaleShaderProgram = glCreateProgram();
	glAttachShader(g_GreyScaleShaderProgram,fs);
	glAttachShader(g_GreyScaleShaderProgram,vs);

	glLinkProgram(g_GreyScaleShaderProgram);
  printProgramInfoLog(g_GreyScaleShaderProgram);
  glGetProgramiv(g_GreyScaleShaderProgram, GL_LINK_STATUS, &l_bStatus);
  if (l_bStatus == GL_FALSE)
  {    
    return false;
  }

  //g_GSConvScaleLoc = glGetUniformLocation(g_GameState.m_GreyScaleShaderProgram, "g_ConversionWeights");



  return true;
}

int main (int argc, char** argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
  
  glutCreateWindow("HeadTrackDemo");
  glutReshapeWindow(800, 600);

  //register GLUT callback routines
  glutDisplayFunc(Display);
  glutReshapeFunc(Reshape);
  glutIdleFunc(Idle);

  //init openGL state variables
  //InitGL();
  glClearColor(1.0, 1.0, 1.0, 1.0);
  glClearDepth(1000.0);
  glShadeModel(GL_SMOOTH);
  glEnable(GL_DEPTH_TEST);

  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);


  //glEnable(GL_COLOR_MATERIAL);
  //glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);

  //init the GL Extension Wrangler library
  glewInit();

  //check for OpenGL 2.0 support
	if (!glewIsSupported("GL_VERSION_2_0"))
  {
		printf("Your graphics hardware/software does not support OpenGL 2.0\n");
		exit(1);
	}

  //try to load the grayscale shader
	if (!LoadGreyScaleShader())
  {
    printf("Failed to load vertex/fragment shader.\n");
		exit(1);
  }

  //glutFullScreen();
  

  glUseProgram(g_GreyScaleShaderProgram);

  //action!!
  glutMainLoop();

  return 0;
}