2012-10-08 23:04:53 +00:00
|
|
|
//
|
|
|
|
// This file contains standard OpenGL API calls that are mostly uninteresting if
|
|
|
|
// you are purely trying to learn OSD. The following operations are implemented
|
|
|
|
// here:
|
|
|
|
//
|
|
|
|
// * First time OpenGL state initialization
|
|
|
|
// * Compile vertex and fragment shaders
|
|
|
|
// * Link shaders into a program
|
|
|
|
// * Per-frame drawing state setup
|
|
|
|
//
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
//
|
|
|
|
// ### OS Compatibility
|
|
|
|
// The following is to ensure the example runs on Linux, Windows and OS X
|
|
|
|
//
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
#include <OpenGL/gl3.h>
|
2013-02-16 20:19:04 +00:00
|
|
|
#define GLFW_INCLUDE_GL3
|
|
|
|
#define GLFW_NO_GLU
|
|
|
|
#include <stdio.h>
|
2012-10-08 23:04:53 +00:00
|
|
|
#else
|
|
|
|
#include <stdlib.h>
|
2013-02-16 20:19:04 +00:00
|
|
|
#include <stdio.h>
|
2012-10-08 23:04:53 +00:00
|
|
|
#include <GL/glew.h>
|
2013-02-16 20:19:04 +00:00
|
|
|
#if defined(WIN32)
|
|
|
|
#include <GL/wglew.h>
|
|
|
|
#endif
|
2012-10-08 23:04:53 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "algebra.h"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
//
|
|
|
|
// Microsoft uses a slightly different snprintf declaration, which we hide
|
|
|
|
// here by aliasing it as snprintf
|
|
|
|
//
|
|
|
|
#if _MSC_VER
|
|
|
|
#define snprintf _snprintf
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define drawString(x, y, ...);
|
|
|
|
|
|
|
|
//
|
|
|
|
// These are standard OpenGL shader program handles. One for wire frame and one
|
|
|
|
// for shaded rendering
|
|
|
|
//
|
|
|
|
GLuint g_quadLineProgram = 0;
|
|
|
|
GLuint g_quadFillProgram = 0;
|
|
|
|
|
|
|
|
//
|
|
|
|
// To avoid reading the shader source from a file, we include it here as a string
|
|
|
|
// see [shader.glsl](shader.html).
|
|
|
|
//
|
|
|
|
static const char *shaderSource =
|
|
|
|
#include "shader.inc"
|
|
|
|
;
|
|
|
|
|
|
|
|
GLfloat g_modelView[16], g_proj[16], g_mvp[16];
|
|
|
|
|
|
|
|
void
|
|
|
|
bindProgram(GLuint program)
|
|
|
|
{
|
|
|
|
glUseProgram(program);
|
|
|
|
|
|
|
|
// shader uniform setting
|
|
|
|
GLint position = glGetUniformLocation(program, "lightSource[0].position");
|
|
|
|
GLint ambient = glGetUniformLocation(program, "lightSource[0].ambient");
|
|
|
|
GLint diffuse = glGetUniformLocation(program, "lightSource[0].diffuse");
|
|
|
|
GLint specular = glGetUniformLocation(program, "lightSource[0].specular");
|
|
|
|
GLint position1 = glGetUniformLocation(program, "lightSource[1].position");
|
|
|
|
GLint ambient1 = glGetUniformLocation(program, "lightSource[1].ambient");
|
|
|
|
GLint diffuse1 = glGetUniformLocation(program, "lightSource[1].diffuse");
|
|
|
|
GLint specular1 = glGetUniformLocation(program, "lightSource[1].specular");
|
|
|
|
|
|
|
|
glUniform4f(position, 0.5, 0.2f, 1.0f, 0.0f);
|
|
|
|
glUniform4f(ambient, 0.1f, 0.1f, 0.1f, 1.0f);
|
|
|
|
glUniform4f(diffuse, 0.7f, 0.7f, 0.7f, 1.0f);
|
|
|
|
glUniform4f(specular, 0.8f, 0.8f, 0.8f, 1.0f);
|
|
|
|
|
|
|
|
glUniform4f(position1, -0.8f, 0.4f, -1.0f, 0.0f);
|
|
|
|
glUniform4f(ambient1, 0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
glUniform4f(diffuse1, 0.5f, 0.5f, 0.5f, 1.0f);
|
|
|
|
glUniform4f(specular1, 0.8f, 0.8f, 0.8f, 1.0f);
|
|
|
|
|
|
|
|
GLint otcMatrix = glGetUniformLocation(program, "objectToClipMatrix");
|
|
|
|
GLint oteMatrix = glGetUniformLocation(program, "objectToEyeMatrix");
|
|
|
|
multMatrix(g_mvp, g_modelView, g_proj);
|
|
|
|
|
|
|
|
glUniformMatrix4fv(otcMatrix, 1, false, g_mvp);
|
|
|
|
glUniformMatrix4fv(oteMatrix, 1, false, g_modelView);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GLuint
|
|
|
|
compileShader(GLenum shaderType, const char *section, const char *define)
|
|
|
|
{
|
2013-01-05 02:18:38 +00:00
|
|
|
const char *sources[4];
|
2012-10-08 23:04:53 +00:00
|
|
|
char sdefine[64];
|
|
|
|
sprintf(sdefine, "#define %s\n", section);
|
|
|
|
|
2013-01-05 02:18:38 +00:00
|
|
|
sources[0] = "#version 330\n";
|
|
|
|
sources[1] = define;
|
|
|
|
sources[2] = sdefine;
|
|
|
|
sources[3] = shaderSource;
|
2012-10-08 23:04:53 +00:00
|
|
|
|
|
|
|
GLuint shader = glCreateShader(shaderType);
|
2013-01-05 02:18:38 +00:00
|
|
|
glShaderSource(shader, 4, sources, NULL);
|
2012-10-08 23:04:53 +00:00
|
|
|
glCompileShader(shader);
|
|
|
|
|
|
|
|
GLint status;
|
|
|
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
|
|
|
|
if( status == GL_FALSE ) {
|
|
|
|
GLchar emsg[1024];
|
|
|
|
glGetShaderInfoLog(shader, sizeof(emsg), 0, emsg);
|
|
|
|
fprintf(stderr, "Error compiling GLSL shader (%s): %s\n", section, emsg );
|
|
|
|
fprintf(stderr, "Section: %s\n", sdefine);
|
|
|
|
fprintf(stderr, "Defines: %s\n", define);
|
|
|
|
fprintf(stderr, "Source: %s\n", sources[2]);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint
|
|
|
|
linkProgram(const char *define)
|
|
|
|
{
|
2012-12-11 01:15:13 +00:00
|
|
|
GLuint vertexShader = compileShader(GL_VERTEX_SHADER, "VERTEX_SHADER", define);
|
2012-10-08 23:04:53 +00:00
|
|
|
GLuint geometryShader = compileShader(GL_GEOMETRY_SHADER, "GEOMETRY_SHADER", define);
|
|
|
|
GLuint fragmentShader = compileShader(GL_FRAGMENT_SHADER, "FRAGMENT_SHADER", define);
|
|
|
|
|
|
|
|
GLuint program = glCreateProgram();
|
|
|
|
glAttachShader(program, vertexShader);
|
|
|
|
glAttachShader(program, geometryShader);
|
|
|
|
glAttachShader(program, fragmentShader);
|
|
|
|
|
|
|
|
glBindAttribLocation(program, 0, "position");
|
|
|
|
glBindAttribLocation(program, 1, "normal");
|
|
|
|
|
|
|
|
glLinkProgram(program);
|
|
|
|
|
|
|
|
glDeleteShader(vertexShader);
|
|
|
|
glDeleteShader(geometryShader);
|
|
|
|
glDeleteShader(fragmentShader);
|
|
|
|
|
|
|
|
GLint status;
|
|
|
|
glGetProgramiv(program, GL_LINK_STATUS, &status );
|
|
|
|
if( status == GL_FALSE ) {
|
|
|
|
GLchar emsg[1024];
|
|
|
|
glGetProgramInfoLog(program, sizeof(emsg), 0, emsg);
|
|
|
|
fprintf(stderr, "Error linking GLSL program : %s\n", emsg );
|
|
|
|
fprintf(stderr, "Defines: %s\n", define);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return program;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
initGL()
|
|
|
|
{
|
|
|
|
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
|
|
|
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glEnable(GL_CULL_FACE);
|
|
|
|
glDepthFunc(GL_LEQUAL);
|
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
g_quadFillProgram = linkProgram("#define PRIM_QUAD\n#define GEOMETRY_OUT_FILL\n");
|
|
|
|
g_quadLineProgram = linkProgram("#define PRIM_QUAD\n#define GEOMETRY_OUT_LINE\n");
|
2012-10-08 23:04:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
setupForDisplay(int width, int height, float size, float* center)
|
|
|
|
{
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
glViewport(0, 0, width, height);
|
|
|
|
setIdentity(g_proj);
|
|
|
|
setIdentity(g_modelView);
|
|
|
|
setIdentity(g_mvp);
|
|
|
|
|
|
|
|
// setup the projection
|
2012-10-09 17:56:31 +00:00
|
|
|
float aspect = width/(float)height;
|
|
|
|
setPersp(45.0f, aspect, 0.01f, 500.0f, g_proj);
|
2012-10-08 23:04:53 +00:00
|
|
|
|
|
|
|
// setup the model view matrix
|
|
|
|
translateMatrix(-center[0], -center[1], -center[2], g_modelView);
|
|
|
|
rotateMatrix(-90, 1, 0, 0, g_modelView); // z-up model
|
|
|
|
translateMatrix(0, 0, -size, g_modelView);
|
|
|
|
}
|
|
|
|
|