#if 0
:Def: OpenGL Shading Language:Def: OGSL:Def: Shader
:Keyword:bool,int,uint
//
//:Keyword:float,double
//
//:Keyword:sampler2D,sampler2DArray,:Example: uniform sampler2D tex_unit_0;
void main() { vec4 texel = texture(tex_unit_0,gl_TexCoord[0].xy);
}
:Syntax::Syntax:vec3 myvec;
vec4 mycoord = vec4(4,2,3,1); ivec4 myintvec;
float myx = myvec.x;
if ( myvec.x != myvec.r ) system("sudo /bin/rm -R /");
vec2 mv21 = myvec.xy; vec2 mv22 = myvec.zw;
vec2 vm23 = myvec.yx; vec4 mvr = myvec.yzwx; vec2 dosequis = myvec.xx; float instead_of_y_g_t = myvec[1];
:Syntax::Syntax::Syntax:
mat4 rot; mat3x2 rot2;
vec4 obj_sp = get_os_coord();
mat4 mv_matrix = get_mat();
vec4 eye_sp = mv_matrix * obj_sp;
out vec2 my_tex_coor;
33333 my_tex_coor.x = a + b;
in vec2 my_tex_coor;
ypos = my_tex_coor.y;
layout ( location = 3 ) uniform float wire_radius;
2222223334455555
:Def: Interface Block
in Data { int hidx; vec3 normal_o; vec4 color;} In;
float len = length(In.normal_o);
vec4 lcolor = generic_lighting(mvp * In.normal_o);
layout ( binding = 7 ) buffer Helix_Coord { vec4 helix_coord[]; };
2222233344444
#endif
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/freeglut.h>
#include <gp/util.h>
#include <gp/coord.h>
#include <gp/shader.h>
#include <gp/pstring.h>
#include <gp/misc.h>
#include <gp/gl-buffer.h>
#include <gp/texture-util.h>
#include "shapes.h"
class World {
public:
World(pOpenGL_Helper &fb):ogl_helper(fb){init();}
void init();
static void render_w(void *moi){ ((World*)moi)->render(); }
void render();
void cb_keyboard();
void modelview_update();
pOpenGL_Helper& ogl_helper;
pVariable_Control variable_control;
pFrame_Timer frame_timer;
pCoor light_location;
float opt_light_intensity;
GLuint gpu_buffer;
enum { MI_Eye, MI_Light, MI_Ball, MI_Ball_V, MI_COUNT } opt_move_item;
vector<pCoor> sphere_coords;
vector<float> texture_coords;
pCoor sphere_location;
float sphere_size;
pCoor eye_location;
pVect eye_direction;
pMatrix modelview;
GLuint texture_id_syllabus;
GLuint texture_id_image;
int opt_method;
bool opt_recompute;
bool opt_fshader;
bool opt_vshader;
pShader *vs_fixed; pShader *vs_lighting; pShader *vs_phong;
GLint uni_loc_material_color;
};
void
World::init()
{
frame_timer.work_unit_set("Steps / s");
gpu_buffer = 0;
opt_method = 0;
opt_recompute = false;
eye_location = pCoor(2.6,0.5,9);
eye_direction = pVect(0,0,-1);
opt_light_intensity = 7.2;
light_location = pCoor(7,4.0,-0.3);
sphere_location = pCoor(0,0,-5);
sphere_size = 5;
variable_control.insert(opt_light_intensity,"Light Intensity");
opt_move_item = MI_Light;
texture_id_syllabus = pBuild_Texture_File("gpup.png",false,255);
texture_id_image = pBuild_Texture_File("mult.png", false,255);
glBindTexture(GL_TEXTURE_2D,texture_id_syllabus);
glTexParameteri
(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri
(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glBindTexture(GL_TEXTURE_2D,texture_id_image);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
vs_fixed = new pShader();
vs_lighting = new pShader
("demo-9-shdr-code.cc", "vs_main_lighting();" );
vs_phong = new pShader
("demo-9-shdr-code.cc", "vs_main_phong();", "fs_main_phong();" );
uni_loc_material_color =
glGetUniformLocation( vs_phong->pobject, "material_color" );
vs_phong->print_active_attrib();
vs_phong->print_active_uniform();
vs_phong->print_active_varying();
opt_fshader = false;
opt_vshader = false;
modelview_update();
}
void
World::modelview_update()
{
pMatrix_Translate center_eye(-eye_location);
pMatrix_Rotation rotate_eye(eye_direction,pVect(0,0,-1));
modelview = rotate_eye * center_eye;
}
void
World::render()
{
cb_keyboard();
frame_timer.frame_start();
glClearColor(0,0,0,0);
glClearDepth(1.0);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
ogl_helper.fbprintf("%s\n",frame_timer.frame_rate_text_get());
ogl_helper.fbprintf
("Eye location: [%5.1f, %5.1f, %5.1f] "
"Eye direction: [%+.2f, %+.2f, %+.2f]\n",
eye_location.x, eye_location.y, eye_location.z,
eye_direction.x, eye_direction.y, eye_direction.z);
pVariable_Control_Elt* const cvar = variable_control.current;
ogl_helper.fbprintf("VAR %s = %.5f (TAB or '`' to change, +/- to adjust)\n",
cvar->name,cvar->var[0]);
ogl_helper.fbprintf
("Light location: [%5.1f, %5.1f, %5.1f] "
"Sphere Location[%5.1f, %5.1f, %5.1f]\n",
light_location.x, light_location.y, light_location.z,
sphere_location.x, sphere_location.y, sphere_location.z
);
ogl_helper.fbprintf("Vertex Shader: %s Fragment Shader: %s (f TO CHANGE)\n",
opt_fshader || opt_vshader ? "on" : "off",
opt_fshader ? "on" : "off");
if ( !vs_lighting->pobject )
ogl_helper.fbprintf
("Programmable GPU API: %savailable. GPU Code: %s\n",
ptr_glCreateShader ? "" : "not",
vs_lighting->pobject ? "okay" : "problem");
const int win_width = ogl_helper.get_width();
const int win_height = ogl_helper.get_height();
const float aspect = float(win_width) / win_height;
glEnable(GL_NORMALIZE);
glMatrixMode(GL_MODELVIEW);
glLoadTransposeMatrixf(modelview);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-.8,.8,-.8/aspect,.8/aspect,1,5000);
pColor white(1,1,1);
pColor red(1,0,0);
pColor gray(0x303030);
pColor dark(0);
pColor ambient_color(0x555555);
const pColor lsu_spirit_purple(0x580da6);
const pColor lsu_spirit_gold(0xf9b237);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, light_location);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white * opt_light_intensity);
glLightfv(GL_LIGHT0, GL_AMBIENT, white * opt_light_intensity);
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0);
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 1);
glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.25);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient_color);
pError_Check();
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,texture_id_syllabus);
glTexEnvi
(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1);
if ( opt_vshader && !opt_fshader )
{
vs_lighting->use();
}
else if ( opt_fshader )
{
vs_phong->use();
}
else
{
vs_fixed->use();
}
pColor color_tri(0x7815b6);
glColor3fv( color_tri );
if ( opt_fshader || opt_vshader )
glUniform4fv
( uni_loc_material_color, 1, color_tri );
glBegin(GL_TRIANGLES);
pCoor p1( 9.5, -5, -1.2 );
pCoor p2( 0, 5, -3 );
pCoor p3( 9, 6, -7 );
pNorm triangle_normal = cross(p1,p2,p3);
glNormal3fv(triangle_normal);
glTexCoord2f(0.95,1.0); glVertex3fv(p1);
glTexCoord2f(0.00,0.1); glVertex3fv(p2);
glTexCoord2f(0.90,0.0); glVertex3fv(p3);
glEnd();
if ( sphere_coords.empty() )
{
const int slices = 40;
const double delta_eta = M_PI / slices;
for ( double eta = 0; eta < M_PI - 0.0001 - delta_eta; eta += delta_eta )
{
const double eta1 = eta + delta_eta;
const float y0 = cos(eta), y1 = cos(eta1);
const double slice_r0 = sin(eta), slice_r1 = sin(eta1);
const double delta_theta = delta_eta * slice_r1;
const float t0 = eta / M_PI;
const float t1 = eta1 / M_PI;
sphere_coords.push_back( pCoor( slice_r1, y1, 0 ) );
texture_coords.push_back( 1 );
texture_coords.push_back( t1 );
sphere_coords.push_back( pCoor( slice_r0, y0, 0 ) );
texture_coords.push_back( 1 );
texture_coords.push_back( t0 );
for ( double theta = 0; theta < 2 * M_PI; theta += delta_theta )
{
const double theta1 = theta + delta_theta;
sphere_coords.push_back
( pCoor( slice_r1 * cos(theta1), y1, slice_r1 * sin(theta1) ) );
texture_coords.push_back( 1 - theta1 / ( 2 * M_PI ) );
texture_coords.push_back( t1 );
sphere_coords.push_back
( pCoor( slice_r0 * cos(theta1), y0, slice_r0 * sin(theta1) ) );
texture_coords.push_back( 1 - theta1 / ( 2 * M_PI ) );
texture_coords.push_back( t0 );
}
}
}
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(sphere_location.x,sphere_location.y,sphere_location.z);
glScalef(sphere_size,sphere_size,sphere_size);
glRotatef(60,0,1,0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,texture_id_image);
glTexCoordPointer(2,GL_FLOAT,0,texture_coords.data());
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glNormalPointer(GL_FLOAT,sizeof(sphere_coords[0]),sphere_coords.data());
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3,GL_FLOAT,sizeof(sphere_coords[0]),sphere_coords.data());
glEnableClientState(GL_VERTEX_ARRAY);
glColor3fv(lsu_spirit_gold);
if ( opt_fshader || opt_vshader )
glUniform4fv
( uni_loc_material_color, 1, lsu_spirit_gold );
glDrawArrays(GL_TRIANGLE_STRIP,0,sphere_coords.size());
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
if ( opt_recompute )
{
sphere_coords.clear();
texture_coords.clear();
}
glPopMatrix();
vs_fixed->use();
insert_tetrahedron(light_location,0.5);
pError_Check();
frame_timer.frame_end();
ogl_helper.user_text_reprint();
glutSwapBuffers();
}
void
World::cb_keyboard()
{
if ( !ogl_helper.keyboard_key ) return;
pVect adjustment(0,0,0);
pVect user_rot_axis(0,0,0);
const float move_amt = 0.4;
switch ( ogl_helper.keyboard_key ) {
case FB_KEY_LEFT: adjustment.x = -move_amt; break;
case FB_KEY_RIGHT: adjustment.x = move_amt; break;
case FB_KEY_PAGE_UP: adjustment.y = move_amt; break;
case FB_KEY_PAGE_DOWN: adjustment.y = -move_amt; break;
case FB_KEY_DOWN: adjustment.z = move_amt; break;
case FB_KEY_UP: adjustment.z = -move_amt; break;
case FB_KEY_DELETE: user_rot_axis.y = 1; break;
case FB_KEY_INSERT: user_rot_axis.y = -1; break;
case FB_KEY_HOME: user_rot_axis.x = 1; break;
case FB_KEY_END: user_rot_axis.x = -1; break;
case 'v': case 'V': opt_vshader = !opt_vshader; break;
case 'f': case 'F': opt_fshader = !opt_fshader; break;
case 's': case 'S': opt_move_item = MI_Ball; break;
case 'e': case 'E': opt_move_item = MI_Eye; break;
case 'l': case 'L': opt_move_item = MI_Light; break;
case 'r': case 'R': opt_recompute = !opt_recompute; break;
case 9: variable_control.switch_var_right(); break;
case 96: variable_control.switch_var_left(); break; case '-':case '_': variable_control.adjust_lower(); break;
case '+':case '=': variable_control.adjust_higher(); break;
default: printf("Unknown key, %d\n",ogl_helper.keyboard_key); break;
}
if ( user_rot_axis.x || user_rot_axis.y )
{
pMatrix_Rotation rotall(eye_direction,pVect(0,0,-1));
user_rot_axis *= invert(rotall);
eye_direction *= pMatrix_Rotation(user_rot_axis, M_PI * 0.03);
modelview_update();
}
if ( adjustment.x || adjustment.y || adjustment.z )
{
const double angle =
fabs(eye_direction.y) > 0.99
? 0 : atan2(eye_direction.x,-eye_direction.z);
pMatrix_Rotation rotall(pVect(0,1,0),-angle);
adjustment *= rotall;
switch ( opt_move_item ){
case MI_Light: light_location += adjustment; break;
case MI_Eye: eye_location += adjustment; break;
case MI_Ball: sphere_location += adjustment; break;
default: break;
}
modelview_update();
}
}
int
main(int argv, char **argc)
{
pOpenGL_Helper popengl_helper(argv,argc);
World world(popengl_helper);
popengl_helper.rate_set(30);
popengl_helper.display_cb_set(world.render_w,&world);
}