Example #1
0
int main(int argc, char **argv) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowPosition(100,100);
	glutInitWindowSize(320,320);
	glutCreateWindow("MM 2004-05");

	glutDisplayFunc(renderScene);
	glutIdleFunc(renderScene);
	glutReshapeFunc(changeSize);
	glutKeyboardFunc(processNormalKeys);

	glEnable(GL_DEPTH_TEST);
	glClearColor(1.0,1.0,1.0,1.0);
//	glEnable(GL_CULL_FACE);

	glewInit();
	if (glewIsSupported("GL_VERSION_2_0"))
		printf("Ready for OpenGL 2.0\n");
	else {
		printf("OpenGL 2.0 not supported\n");
		exit(1);
	}
	setShaders();

	glutMainLoop();

	// just for compatibiliy purposes
	return 0;
}
///////////////////////////////////////////////////////////////////////
//main, setup and execution of environment
int main(int argc, char **argv) {

    printf("calling putenv with: LIBGL_ALWAYS_SOFTWARE=1 \n");
    if(putenv("LIBGL_ALWAYS_SOFTWARE=1")!=0)
    {
      fprintf(stderr,"putenv failed\n");
    }

	if(argc < 2)
	{
		printf("please select illumination model 1 2 or 3\n");
		printf("usage: cgExercise02 <model number>\n");
		return EXIT_FAILURE;
	}
	shader = atoi(argv[1]);

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowPosition(100,100);
	glutInitWindowSize(320,320);
	glutCreateWindow("Computer Graphics");

	glutDisplayFunc(renderScene);
	glutIdleFunc(renderScene);
	glutReshapeFunc(changeSize);
	glutKeyboardFunc(processNormalKeys);

	glutMouseFunc(mouseClick);
	glutMotionFunc(mouseMotion);

	glEnable(GL_DEPTH_TEST);
	glClearColor(0.0,0.0,0.0,1.0);

	glewInit();
	if (GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader)
		printf("Ready for GLSL\n");
	else {
		printf("No GLSL support\n");
		exit(1);
	}

	if (glewIsSupported("GL_VERSION_3_1"))
		printf("Ready for OpenGL 3.1\n");
	else {
		printf("OpenGL 3.1 not supported\n");
		exit(1);
	}
	if (GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader && GL_EXT_geometry_shader4)
		printf("Ready for GLSL - vertex, fragment, and geometry units\n");
	else {
		printf("Not totally ready :( \n");
		exit(1);
	}
	initialize();
	setShaders();

	glutMainLoop();
	return EXIT_SUCCESS;
}
Example #3
0
///////////////////////////////////////////////////////////////////////
//main, setup and execution of environment
int main(int argc, char **argv) {

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowPosition(100,100);
	glutInitWindowSize(568,320);
	glutCreateWindow("Computer Graphics");

	glutDisplayFunc(renderScene);
	glutIdleFunc(renderScene);
	glutReshapeFunc(changeSize);
	glutKeyboardFunc(processNormalKeys);

	glutMouseFunc(mouseClick);
	glutMotionFunc(mouseMotion);

	glEnable(GL_DEPTH_TEST);
	glClearColor(0.0,0.0,0.0,1.0);

	glewInit();
	if (GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader)
		printf("Ready for GLSL\n");
	else {
		printf("No GLSL support\n");
		exit(1);
	}

	if (glewIsSupported("GL_VERSION_3_3"))
		printf("Ready for OpenGL 3.3\n");
	else {
		printf("OpenGL 3.3 not supported\n");
		exit(1);
	}
	if (GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader && GL_EXT_geometry_shader4)
		printf("Ready for GLSL - vertex, fragment, and geometry units\n");
	else {
		printf("Not totally ready :( \n");
		exit(1);
	}

	if (GL_ARB_fragment_program && GL_ARB_vertex_program && GL_EXT_framebuffer_object)
		printf("Ready for FrameBuffer\n");
	else {
		printf("Not totally ready :( \n");
		exit(1);
	}


	initialize();
#ifdef __linux__
  int i=pthread_getconcurrency();
#endif
	setShaders();

	glutMainLoop();
	return EXIT_SUCCESS;
}
Example #4
0
void initShadowmapShaders(void)
{
	extern GLuint setShaders(char*,char*);
	program = setShaders ("shadowmap.vert", "shadowmap.frag");
	glUseProgram (program);
	glUniform1i (glGetUniformLocation (program, "shadowMap"), 3);   // set shadowmap to texture unit 3

	glUseProgram(0);   // disable shader for now
}
Example #5
0
int main(int argc, char *argv[])
{
    //SDL_Surface *screen;
    SDL_Window *window;
    SDL_GLContext context;

    assert(SDL_Init(SDL_INIT_VIDEO) == 0);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    //screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL );
    //assert(screen);

    window = SDL_CreateWindow("sdlglshader", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_OPENGL);
    assert(window);

    context = SDL_GL_CreateContext(window);

    glClearColor(0, 0, 0, 0);
    glViewport(0, 0, 640, 480);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, 640, 480, 0, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glClear(GL_COLOR_BUFFER_BIT);

    initARB();
    setShaders();

    glColor3f(0, 1, 1); // is overridden by the shader, useful for debugging native builds
    glBegin( GL_TRIANGLES );
      glTexCoord2i(0, 0); glVertex3f( 10,  10,  0);
      glTexCoord2i(1, 0); glVertex3f( 300, 10,  0);
      glTexCoord2i(1, 1); glVertex3f( 300, 328, 0);
    glEnd();

    glColor3f(1, 1, 0); // is overridden by the shader, useful for debugging native builds
    glBegin( GL_TRIANGLES );
        glTexCoord2f(0, 0.5); glVertex3f(410, 10,  0);
        glTexCoord2f(1, 0.5); glVertex3f(600, 10,  0);
        glTexCoord2f(1, 1  ); glVertex3f(630, 400, 0);
    glEnd();

    SDL_GL_SwapWindow(window);

#ifndef __EMSCRIPTEN__
    SDL_Delay(3000);
#endif

    SDL_Quit();
    return 0;
}
Example #6
0
/* A general OpenGL initialization function.  Sets all of the initial parameters. */
void InitGL(int Width, int Height) // We call this right after our OpenGL window is created.
{
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black
	glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
	glDepthFunc(GL_GREATER); // The Type Of Depth Test To Do
	glEnable(GL_DEPTH_TEST); // Enables Depth Testing
	glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity(); // Reset The Projection Matrix

	gluPerspective(30.0f, (GLfloat) Width / (GLfloat) Height, 0.1f, 100.0f); // Calculate The Aspect Ratio Of The Window

	glMatrixMode(GL_MODELVIEW);

	//http://cirl.missouri.edu/gpu/glsl_lessons/glsl_geometry_shader/index.html

	setShaders(&ProgramObject);

	//http://www.opengl.org/wiki/Texture_Sampling
	//Setting shader's uniform variables
	GLint volume_location = glGetUniformLocation(ProgramObject,
			"volume_texture");
	GLint tf_location =
			glGetUniformLocation(ProgramObject, "transfer_function");

	//Checking if the state of the shader is also consider invalid.
	int isValid;
	glValidateProgram(ProgramObject);
	glGetProgramiv(ProgramObject, GL_VALIDATE_STATUS, &isValid);
	if (isValid)
		printf("Shader is valid!\n");
	else {
		printf("Shader isn't Valid!");
		exit(1);
	}

	// So, to set up those uniforms, bind the shader and call glUniform1i since they are considered as integers
	glUseProgram(ProgramObject);
	//Bind to tex unit 0
	glUniform1i(volume_location, 0);
	//Bind to tex unit 1
	glUniform1i(tf_location, 1);

	InitTexture();

	InitDraw();

}
Example #7
0
///////////////////////////////////////////////////////////////////////
//main, setup and execution of environment
int main(int argc, char **argv) {
	//in case somebody wants to try the example implementation of Exercise 3:
	subdivLevel = 2.0;
	if(argc > 1)
		subdivLevel = atoi(argv[1]);

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowPosition(100,100);
	glutInitWindowSize(320,320);
	glutCreateWindow("Computer Graphics");

	glutDisplayFunc(renderScene);
	glutIdleFunc(renderScene);
	glutReshapeFunc(changeSize);
	glutKeyboardFunc(processNormalKeys);

	glutMouseFunc(mouseClick);
	glutMotionFunc(mouseMotion);

	glEnable(GL_DEPTH_TEST);
	glClearColor(0.0,0.0,0.0,1.0);

	glewInit();
	if (GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader)
		printf("Ready for GLSL\n");
	else {
		printf("No GLSL support\n");
		exit(1);
	}

	if (glewIsSupported("GL_VERSION_2_1"))
		printf("Ready for OpenGL 2.1\n");
	else {
		printf("OpenGL 2.1 not supported\n");
		exit(1);
	}
	if (GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader && GL_EXT_geometry_shader4)
		printf("Ready for GLSL - vertex, fragment, and geometry units\n");
	else {
		printf("Not totally ready :( \n");
		exit(1);
	}
	initialize();
	setShaders();

	glutMainLoop();
	return EXIT_SUCCESS;
}
Example #8
0
int main(int argc, char *argv[])
{
    SDL_Surface *screen;

    assert(SDL_Init(SDL_INIT_VIDEO) == 0);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL );
    assert(screen);
    
    glClearColor(0, 0, 0, 0);
    glViewport(0, 0, 640, 480);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, 640, 480, 0, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glClear(GL_COLOR_BUFFER_BIT);

    initARB();
    setShaders();

    glColor3f(0, 1, 1); // is overridden by the shader, useful for debugging native builds
    glBegin( GL_TRIANGLES );
      glTexCoord2i(0, 0); glVertex3f( 10,  10,  0);
      glTexCoord2i(1, 0); glVertex3f( 300, 10,  0);
      glTexCoord2i(1, 1); glVertex3f( 300, 328, 0);
    glEnd();

    glColor3f(1, 1, 0); // is overridden by the shader, useful for debugging native builds
    glBegin( GL_TRIANGLES );
        glTexCoord2f(0, 0.5); glVertex3f(410, 10,  0);
        glTexCoord2f(1, 0.5); glVertex3f(600, 10,  0);
        glTexCoord2f(1, 1  ); glVertex3f(630, 400, 0);
    glEnd();

    // Test https://github.com/kripken/emscripten/issues/3693
    glBegin( GL_TRIANGLES );
    glEnd();

    SDL_GL_SwapBuffers();
    
#ifndef __EMSCRIPTEN__
    SDL_Delay(3000);
#endif

    SDL_Quit();
    return 0;
}
Example #9
0
void initOpenGL(int argc, char** argv) {
    srandom(123456789);
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_MULTISAMPLE | GLUT_ACCUM);
    glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    glutInitWindowPosition(100, 50);
    glutCreateWindow("The Welsh Dragons");

    glClearColor(0.0, 0.0, 0.0, 0.0);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_MULTISAMPLE_ARB);

    doLights();
    setShaders();
    doViewVolume();
}
Example #10
0
void init()
{
	glewInit();
	if (glewIsSupported("GL_VERSION_2_0"))
		printf("Ready for OpenGL 2.0\n");
	else {
		printf("OpenGL 2.0 not supported\n");
		exit(1);
	}

	if (!loadTGA ("color_map_512.tga", color_map))
		printf ("color_map_512.tga not found!\n");
	if (!loadTGA ("normal_map_512.tga", normal_map))
		printf ("normal_map_512.tga not found!\n");

	setShaders();
}
Example #11
0
void initRendering(char** argv)
{
	glClearColor(0.678431, 0.847059, 0.901961, 1.0);
	glShadeModel(GL_SMOOTH);

	glEnable(GL_DEPTH_TEST);
	ReadMap("hole.01.db");
	
	ImportObj temp;
	ImportObj Tee = getTeeBuffer();
	load_obj("BallSmall.obj", temp.Vertices, temp.Indices, glm::vec3(0, 0.08, 0));
	temp.CalculateNormals();
	GolfBall = Ball(temp, Tee.Coordinate, getTiles()[getTeeBuffer().TileID - 1]);
	//The starting tile is the tile of the Tee

	setShaders();
}
Example #12
0
int main(int argc, char **argv)
{	
	// glut init
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);

	// create window
	glutInitWindowPosition(500, 100);
	glutInitWindowSize(600, 600);
	glutCreateWindow("10420 CS550000 CG HW2 Haley");

	glewInit();
	if(glewIsSupported("GL_VERSION_2_0")){
		printf("Ready for OpenGL 2.0\n");
	}
	else{
		printf("OpenGL 2.0 not supported\n");
		system("pause");
		exit(1);
	}
	mm.showHelpMenu();

	puts("\n\n  ====== Translation Mode Start ======\n");
	mm.startManaging();

	// register glut callback functions
	glutDisplayFunc(onDisplay);
	glutIdleFunc(onIdle);
	glutKeyboardFunc(onKeyboard);
	glutSpecialFunc(onKeyboardSpecial);
	glutMouseFunc(onMouse);
	glutMotionFunc(onMouseMotion);
	glutReshapeFunc(onWindowReshape);

	// set up shaders here
	setShaders();

	glEnable(GL_DEPTH_TEST);

	// main loop
	glutMainLoop();

	return 0;
}
Example #13
0
int main(int argc, char **argv)
{
	// glut init
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);

	// create window
	glutInitWindowPosition(800, 150);
	glutInitWindowSize(uiWidth, uiHeight);
	glutCreateWindow("10420 CS550000 CG HW4 TA");

	glewInit();
	if(glewIsSupported("GL_VERSION_2_0")){
		printf("Ready for OpenGL 2.0\n");
	}else{
		printf("OpenGL 2.0 not supported\n");
		system("pause");
		exit(1);
	}

	glutDisplayFunc(onDisplay);
	glutIdleFunc(onDisplay);
	glutReshapeFunc(onWindowReshape);
	glutMouseFunc(onMouse);
	glutMotionFunc(onMouseMotion);
	glutKeyboardFunc(onKeyboard);
	glutSpecialFunc(onKeyboardSpecial);

	// init
	preTranslateX = preTranslateY = 0;
	scale = 1.0;
	currentModel = 0;
	loadModel(currentModel);
	setShaders();

	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);
	glCullFace(GL_BACK);

	glutMainLoop();

	glmDelete(OBJ);
	return 0;
}
Example #14
0
int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(320,320);
    glutCreateWindow("Hello Shader");
    
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutReshapeFunc(reshape);
    glEnable(GL_DEPTH_TEST);
    glClearColor(1.0,1.0,1.0,1.0);
    
    setShaders();
    
    glutMainLoop();
    
    return 0;
}
Example #15
0
int main(int argc, char **argv) {
	// glut init
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);

	// create window
	glutInitWindowPosition(460, 40);
	glutInitWindowSize(800, 800);
	glutCreateWindow("10320 CS550000 CG HW2 TA");

	glewInit();
	if(glewIsSupported("GL_VERSION_2_0")){
		printf("Ready for OpenGL 2.0\n");
	}else{
		printf("OpenGL 2.0 not supported\n");
		system("pause");
		exit(1);
	}

	// load obj models through glm
	loadOBJModel();

	// register glut callback functions
	glutDisplayFunc (renderScene);
	glutIdleFunc    (idle);
	glutKeyboardFunc(processNormalKeys);
	glutMouseFunc   (processMouse);
	glutMotionFunc  (processMouseMotion);

	glEnable(GL_DEPTH_TEST);

	// set up shaders here
	setShaders();

	// main loop
	glutMainLoop();

	// free
	glmDelete(OBJ);

	return 0;
}
Example #16
0
//Pozzer
//----------------------------------------------
Glsl::Glsl(char *vert, char *frag)
{
   isActive  = false;

   if (showInfo == false)
   {
	  showInfo = true;
	  printf("\nYou are using OpenGL %s\n\n", glGetString(GL_VERSION));
   }
        
   setShaders(vert, frag);

   //printShaderInfoLog(v);
   //printShaderInfoLog(f);
   printProgramInfoLog(p);

   //http://www.delorie.com/gnu/docs/gcc/cpp_21.html
   //http://www.thescripts.com/forum/thread212429.html
   //http://www.codeguru.com/forum/showthread.php?t=231043
   //printf("\n%s  %d  %s  %s \n", __FILE__, __LINE__, __FUNCTION__, __TIMESTAMP__);

   printOglError(__FILE__, __LINE__); 
}
Example #17
0
/*update*/
void SphereShape::update()
{

	OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
	if(!_dirty)
		return;

	float radius = _radius;
	osg::Vec3 center = _center;
	osg::Vec4 color = _color;
	std::string vert = _vertex_shader;
	std::string frag = _fragment_shader;

	setParameter("radius", radius);

	setParameter("x", center.x());
	setParameter("y", center.y());
	setParameter("z", center.z());

	setParameter("r1", color[0]);
	setParameter("g1", color[1]);
	setParameter("b1", color[2]);
	setParameter("a1", color[3]);
	
	setParameter("vertex", vert);
	setParameter("fragment", frag);

	setPosition(center, radius);
	setShapeColor(color);
	setShaders(vert, frag);
	dirtyBound();

	//reset flag
	_dirty = false;

}
Example #18
0
// this function should only call stuff that needs to be called ONCE!
void gameInitialize(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);

    GLfloat lightAmbient[4] = { 0.2f, 0.2f, 0.2f, 1.0f };
    GLfloat lightDiffuse[4] = { 1.0f, 1.0f, 1.0f, 1.0f };

    glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
    glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);

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

    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);

    glEnable(GL_NORMALIZE);

    Program1 = setShaders("data/vertex.glsl", "data/fragment.glsl");

}
Example #19
0
CPBox::CPBox() :
    m_funcs(0),
    m_program(0)
{
    setShaders();
}
Example #20
0
//===================================================================
void Mesh::create()
{
    // create the mesh from the vertices

    if (mVertices.count() == 0) {
        qWarning() << Q_FUNC_INFO << "No vertices defined for" << objectName();
        return;
    }

    if (mDebugView)
        setShaders(":/shaders/light_vertex.vsh", ":/shaders/debug_fragment.fsh",":/shaders/debug_geometry.gsh" );

    else
        setDefaultShaders();



    if (!mTextureImage.isNull()) {   // ca crash si on test pas ...
        mTexture  = new QOpenGLTexture(mTextureImage);
        mTexture->create();

    }

    mVertexBuffer.create();
    mVertexBuffer.bind();
    mVertexBuffer.allocate(mVertices.data(), mVertices.count() * sizeof(Vertex));
    mVertexBuffer.release();

    if (mIndices.count()) {
        mIndexBuffer.create();
        mIndexBuffer.bind();
        mIndexBuffer.allocate(mIndices.data(), mIndices.count() * sizeof(GLuint));
        mIndexBuffer.release();
    }

    mVao.create();
    mVao.bind();

    mIndexBuffer.bind();
    mVertexBuffer.bind();

    mShaderProgram->bind();
    mShaderProgram->enableAttributeArray("position");
    mShaderProgram->setAttributeBuffer("position", GL_FLOAT, 0, 3, sizeof(Vertex));

    mShaderProgram->enableAttributeArray("color");
    mShaderProgram->setAttributeBuffer("color", GL_FLOAT, 3 * 4, 3, sizeof(Vertex));

    mShaderProgram->enableAttributeArray("uv");
    mShaderProgram->setAttributeBuffer("uv", GL_FLOAT, 6 * 4, 2, sizeof(Vertex));

    mShaderProgram->enableAttributeArray("normal");
    mShaderProgram->setAttributeBuffer("normal", GL_FLOAT, 8 * 4, 2, sizeof(Vertex));

    mShaderProgram->release();
    mIndexBuffer.release();
    mVertexBuffer.release();
    mVao.release();

    if (glGetError() != GL_NO_ERROR) {
        qFatal("Mesh::Create: OpenGL error");
        exit(QtFatalMsg);
    }

    qDebug() << Q_FUNC_INFO << "Mesh" << objectName() << "created";
}
Example #21
0
//===================================================================
void Mesh::setDefaultShaders()
{
    // set the vertex and fragment program names

    setShaders(":/shaders/light_vertex.vsh", ":/shaders/light_fragment.fsh" );
}
bool ofxPostGlitch::setup(ofFbo *buffer_, const string & shaderDirectory)
{
    setFbo(buffer_);
    return setShaders(shaderDirectory);
}
void SceneDrawer::glInitWithShaders( int argc, char **argv )
{

   glutInit(&argc, argv);
   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
   glutInitWindowSize(GL_WIN_SIZE_X, GL_WIN_SIZE_Y);
   //glutInitWindowPosition(100, 100);
   glutCreateWindow ("User Selection Sample");
   glutSetCursor( GLUT_CURSOR_NONE );

   glutKeyboardFunc( glutKeyboard );
   glutDisplayFunc( glutDisplayWithShaders ); 
   glutIdleFunc( glutIdle );
   glutReshapeFunc(resize);												// Added Code

	GLint GlewInitResult = glewInit();
	if (GLEW_OK != GlewInitResult) 
	{
		printf("ERROR: %s\n",glewGetErrorString(GlewInitResult));
		exit(EXIT_FAILURE);
	}

	// Initialize the texture
	InitTexture();

	//----------------------------------------------------------------------
	// BEGIN Background Shader Setup Code
	//----------------------------------------------------------------------

	XnUInt16 g_nXRes; 
	XnUInt16 g_nYRes; 
	m_pUserTrackerObj->GetImageRes(g_nXRes,g_nYRes);

	//Vertex Array
	const GLfloat textureSurface[] = {
    0.0,		0.0,		0.0,								// Top Left point 
    g_nXRes,	0.0,		0.0,								// Top Right point 
	g_nXRes,	g_nYRes,	0.0,								// Bottom Right point 
    0.0,		g_nYRes,	0.0 };								// Bottom Left point 

	// Color array for the vertex buffer object
	const GLubyte colors[] = {
    static_cast<GLubyte>(1),		static_cast<GLubyte>(0),		static_cast<GLubyte>(0),								// Red 
    static_cast<GLubyte>(0),		static_cast<GLubyte>(1),		static_cast<GLubyte>(0),								// Green 
	static_cast<GLubyte>(1),		static_cast<GLubyte>(1),		static_cast<GLubyte>(1),								// Yellow 
    static_cast<GLubyte>(1),		static_cast<GLubyte>(1),		static_cast<GLubyte>(1) };								// White

	const GLfloat texCoords[] = {
	0.0,							0.0,
	(float)g_nXRes/texWidth,		0.0,
	(float)g_nXRes/texWidth,		(float)g_nYRes/texHeight,
	0.0,							(float)g_nYRes/texHeight };

	// Indices for the quadrilateral surface
	const GLuint indices[] = {
		0, 1, 2,									// Top right triangle
		0, 3, 2										// Bottom left triangle
	};

	// Find the number of vertices in the array
	g_numberVertices_Background = (GLuint)SizeOfArray( textureSurface ) / DIM_POSITION;

	const GLuint numberVertices_Background = g_numberVertices_Background;

	// Set the size of the interleaved array to the number of vertices
	//g_background = new vertexStruct[g_numberVertices_Background];
	
	vertexStruct g_background[4];

	// Find the number of indices in the geometry
	g_numberIndices_Background = SizeOfArray( indices );

	//Fill the interleaved array.
	for( int i = 0; i < g_numberVertices_Background; i++ ){
		for( int j = 0; j < DIM_POSITION; j++ ){
			g_background[i].Position[j] = textureSurface[i * DIM_POSITION + j];
		}

		for( int j = 0; j < DIM_NORMAL; j++ ){
			g_background[i].Normal[j] = (GLfloat) 0.0f;		// Not using surface normals in this implementation
		}

		for( int j = 0; j < DIM_COLOR; j++ ){
			g_background[i].Color[j] = colors[i * DIM_COLOR + j];
		}

		for( int j = 0; j < DIM_TEXCOORD; j++ ){
			g_background[i].TexCoord[j] = texCoords[i * DIM_TEXCOORD + j];
		}
	}

	// Check the contents of the interleaved array
	//for( int i = 0; i < g_numberVertices_Background; i++ ){
	//	cout << "VERTEX:\t" << i << endl;
	//	cout << "POS:\tX:" << g_background[i].Position[0] << '\t' << g_background[i].Position[1] << '\t' << g_background[i].Position[2] << endl;
	//	cout << "NORM:\tX:" << g_background[i].Normal[0] << '\t' << g_background[i].Normal[1] << '\t' << g_background[i].Normal[2] << endl;
	//	cout << "COL:\tX:" << static_cast<GLfloat>(g_background[i].Color[0]) << '\t' << static_cast<GLfloat>(g_background[i].Color[1]) << '\t' << static_cast<GLfloat>(g_background[i].Color[2]) << endl;
	//	cout << "TEX:\tX:" << g_background[i].TexCoord[0] << '\t' << g_background[i].TexCoord[1] << endl;
	//	cout << endl;
	//}

	//Set the shaders
	setShaders("backgroundTexture.vs", "backgroundTexture.fs", &g_programHandle_Background, &g_vertexShaderHandle_Background, &g_fragmentShaderHandle_Background);

	// Get Attribute Locations
	g_vertexLocation_Background = glGetAttribLocation( g_programHandle_Background, "in_Position" );
	g_colorLocation_Background = glGetAttribLocation( g_programHandle_Background, "in_Color" );
	g_texCoordLocation_Background = glGetAttribLocation( g_programHandle_Background, "in_TexCoord" );
	g_textureLocation_Background = glGetUniformLocation( g_programHandle_Background, "uImageUnit" );

	glGenVertexArrays( 1, &g_vao[0]);
	glBindVertexArray( g_vao[0] );

	// Generate and Fill the Buffers
	
	static GLsizei stride = sizeof( vertexStruct );

	// INTERLEAVED ARRAYS
	// Position
	glGenBuffers(1, &g_verticesVBO_Background );
	glBindBuffer( GL_ARRAY_BUFFER, g_verticesVBO_Background );
	glBufferData( GL_ARRAY_BUFFER, sizeof(g_background), g_background, GL_STATIC_DRAW );
	glEnableVertexAttribArray( g_vertexLocation_Background );
	glVertexAttribPointer( 
		g_vertexLocation_Background,	// attribute
		DIM_POSITION,					// number of elements per vertex 
		GL_FLOAT,						// type of each element
		GL_FALSE,						// normalized? 
		stride,							// next vertex location appears every 3 floats  
		(void*)offsetof( vertexStruct, Position )								// offset of the first element
		);

	// Color
	glEnableVertexAttribArray( g_colorLocation_Background );
	glVertexAttribPointer( 
		g_colorLocation_Background,		// attribute
		DIM_COLOR,						// number of elements per vertex 
		GL_UNSIGNED_BYTE,				// type of each element
		GL_FALSE,						// normalized? 
		stride,							// next vertex location appears every 3 floats  
		(void*)offsetof( vertexStruct, Color )								// offset of the first element
		);

	// Texture
	glEnableVertexAttribArray( g_texCoordLocation_Background );
	glVertexAttribPointer( 
		g_texCoordLocation_Background,		// attribute
		DIM_TEXCOORD,						// number of elements per vertex 
		GL_FLOAT,							// type of each element
		GL_FALSE,							// normalized? 
		stride,								// next vertex location appears every 3 floats  
		(void*)offsetof( vertexStruct, TexCoord )								// offset of the first element
		);

	glGenBuffers( 1, &g_indicesVBO_Background );
	glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO_Background );
	glBufferData( GL_ELEMENT_ARRAY_BUFFER, SizeOfArray( indices ) * sizeof( GLuint ), indices, GL_STATIC_DRAW );

	GLenum error=glGetError();

	glBindVertexArray( 0 );
	glBindBuffer( GL_ARRAY_BUFFER, 0 );
	glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );

	//----------------------------------------------------------------------
	// BEGIN Skeleton Shader Setup Code
	//----------------------------------------------------------------------

	setShaders( "skeleton.vs", "skeleton.fs", &g_programHandle_Skeleton, &g_vertexShaderHandle_Skeleton, &g_fragmentShaderHandle_Skeleton );

	//Get the attribute locations
	g_vertexLocation_Skeleton = glGetAttribLocation( g_programHandle_Skeleton, "in_Position" );

	// Bind the buffers, fill with data, and attach to the program
	glGenVertexArrays( 1, &g_vao[1]);
	glBindVertexArray( g_vao[1] );
	glGenBuffers( 1, &g_verticesVBO_Skeleton );
	glBindBuffer( GL_ARRAY_BUFFER, g_verticesVBO_Skeleton );
	glBufferData( GL_ARRAY_BUFFER, 3 * MAX_JOINTS * sizeof(GLfloat), NULL, GL_STREAM_DRAW );
	glVertexAttribPointer( g_vertexLocation_Skeleton, 3, GL_FLOAT, 0, 0, 0 );
	glEnableVertexAttribArray( g_vertexLocation_Skeleton );

	glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
	glClearDepth( 1.0f );

	glEnable( GL_DEPTH_TEST );
	glEnable( GL_CULL_FACE );
	glEnable( GL_VERTEX_PROGRAM_POINT_SIZE );
	glEnable( GL_BLEND );
	glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
}
Example #24
0
unsigned char* convert_pixels(unsigned char* src,
                     CameraPixelCoding src_coding,
                     size_t dest_stride,
                     unsigned char* dest, int force_copy) {
  static int gave_error=0;
  unsigned char *src_ptr;
  static int attempted_to_start_glsl_program=0;
  GLubyte* rowstart;
  int i,j;
  int copy_required;
  GLint firstRed;

  copy_required = force_copy || (dest_stride!=stride);
  src_ptr = src;

  switch (src_coding) {
  case CAM_IFACE_MONO8:
    switch (gl_data_format) {
    case GL_LUMINANCE:
      if (copy_required) {
        do_copy();
        return dest;
      }
      return src; /* no conversion necessary*/
      break;
    default:
      fprintf(stderr,"ERROR: will not convert MONO8 image to non-luminance\n");
      exit(1);
      break;
    }
    break;
  case CAM_IFACE_YUV422:
    switch (gl_data_format) {
    case GL_LUMINANCE:
      yuv422_to_mono8(src_ptr, dest, width, height, stride, dest_stride);
      return dest;
      break;
    case GL_RGB:
      yuv422_to_rgb8(src_ptr, dest, width, height, stride, dest_stride);
      return dest;
      break;
    case GL_RGBA:
      yuv422_to_rgba8(src_ptr, dest, width, height, stride, dest_stride);
      return dest;
      break;
    default:
      fprintf(stderr,"ERROR: invalid conversion at line %d\n",__LINE__);
      exit(1);
      break;
    }
    break;
  case CAM_IFACE_RGB8:
    switch (gl_data_format) {
    case GL_RGB:
      if (copy_required) {
        // update data directly on the mapped buffer
        GLubyte* rowstart = dest;
        for (i=0; i<height; i++) {
          memcpy(rowstart, src_ptr, width*3 );
          rowstart += dest_stride;
          src_ptr += stride;
        }
        return dest;
      } else {
        return src; /* no conversion necessary*/
      }
      break;
    case GL_RGBA:
      if (copy_required) {
        // update data directly on the mapped buffer
        GLubyte* rowstart = dest;
        int j;
        for (i=0; i<height; i++) {
          for (j=0; j<width; j++) {
            rowstart[j*4] = src_ptr[j*3];
            rowstart[j*4+1] = src_ptr[j*3+1];
            rowstart[j*4+2] = src_ptr[j*3+2];
            rowstart[j*4+3] = 255;
          }
          rowstart += dest_stride;
          src_ptr += stride;
        }
        return dest;
      } else {
        return src; /* no conversion necessary*/
      }
      break;
    default:
      fprintf(stderr,"ERROR: invalid conversion at line %d\n",__LINE__);
      exit(1);
      break;
    }
    break;
#ifdef USE_GLEW
  case CAM_IFACE_MONO8_BAYER_BGGR:
  case CAM_IFACE_MONO8_BAYER_RGGB:
  case CAM_IFACE_MONO8_BAYER_GRBG:
  case CAM_IFACE_MONO8_BAYER_GBRG:
    //FIXME: add switch (gl_data_format)
    if (!attempted_to_start_glsl_program) {
      setShaders();
      if (use_shaders) {
        firstRed = glGetUniformLocation(glsl_program,"firstRed");
        switch(src_coding) {
        case CAM_IFACE_MONO8_BAYER_BGGR:
          glUniform2f(firstRed,0,0);
          break;
        case CAM_IFACE_MONO8_BAYER_RGGB:
          glUniform2f(firstRed,1,1);
          break;
        case CAM_IFACE_MONO8_BAYER_GRBG:
          glUniform2f(firstRed,0,1);
          break;
        case CAM_IFACE_MONO8_BAYER_GBRG:
        default:
          glUniform2f(firstRed,1,0);
          break;
        }
      } else {
        fprintf(stderr,"ERROR: Failed to start GLSL Bayer program\n");
      }
      attempted_to_start_glsl_program=1;
    }
    do_copy();
    return dest;
    break;
#endif
  case CAM_IFACE_MONO16:
    switch (gl_data_format) {
    case GL_LUMINANCE:
      rowstart = dest;
      for (i=0; i<height; i++) {
        for (j=0; j<width; j++) {
          rowstart[j] = (src + (stride*i))[j*2];
        }
        rowstart += dest_stride;
      }
      return dest;
      break;
    default:
      fprintf(stderr,"ERROR: invalid conversion at line %d\n",__LINE__);
      exit(1);
      break;
    }
  default:
    if (!gave_error) {
      fprintf(stderr,"ERROR: unsupported pixel coding %d\n",src_coding);
      gave_error=1;
    }
    if (copy_required) {
      do_copy();
      return dest;
    } else {
      return src; /* no conversion necessary*/
    }
    break;
  }
}
void SceneDrawer::DrawDepthMapTextureWithShaders()
{
    XnUInt16 g_nXRes;
    XnUInt16 g_nYRes;
    m_pUserTrackerObj->GetImageRes(g_nXRes,g_nYRes);

    if (g_bDrawPixels)
    {
        m_pUserTrackerObj->FillTexture(pDepthTexBuf,texWidth,texHeight,g_bDrawBackground);
    }
    else
    {
        xnOSMemSet(pDepthTexBuf, 0, 3*2*g_nXRes*g_nYRes); // makes the texture empty.
    }

    // makes sure we draw the relevant texture
    glBindTexture(GL_TEXTURE_2D, depthTexID);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, pDepthTexBuf);

    //----------------------------------------------------------------------
    // BEGIN DEFAULT FIXED FUNCTION DRAWING CODE
    //----------------------------------------------------------------------

    //// Display the OpenGL texture map
    //glColor4f(0.75,0.75,0.75,1);

    //glEnable(GL_TEXTURE_2D);

    //glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    //glTexCoordPointer(2, GL_FLOAT, 0, texcoords);

    //GLfloat verts[8] = { g_nXRes, g_nYRes, g_nXRes, 0, 0, 0, 0, g_nYRes };
    //glVertexPointer(2, GL_FLOAT, 0, verts);
    //glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

    ////TODO: Maybe glFinish needed here instead - if there's some bad graphics crap
    //glFlush();
    //glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    //glDisable(GL_TEXTURE_2D);


    //----------------------------------------------------------------------
    // BEGIN SHADER CODE
    //----------------------------------------------------------------------

    glEnable(GL_TEXTURE_2D);

    // Error debugging
    int IsLinked;
    int maxLength;
    char *shaderProgramInfoLog;

    //Vertex Array
    const GLfloat textureSurface[9] = {
        20.0, 20.0, 0.0,								/* Top point */
        g_nXRes - 20.0, 20.0, 0.0,						/* Right point */
        g_nXRes - 20.0, g_nYRes - 20.0, 0.0
    };			/* Bottom point */
    //{  20.0, g_nYRes - 20.0, 0.0  } };			/* Left point */

    // Color array for the vertex buffer object
    const GLfloat colors[9] = {
        1.0,  0.0,  0.0, /* Red */
        0.0,  1.0,  0.0, /* Green */
        0.0,  0.0,  1.0
    }; /* Blue */
    //{  1.0,  1.0,  1.0  } }; /* White */

    const GLubyte indices[3] = {
        0, 1, 2
    };

    // Address integers for the buffer objects
    GLuint vao, vbo[2];

    // Generate the Vertex Array Object
    glGenVertexArrays( 1, &vao );

    // Bind the Vertex Array Object
    glBindVertexArray( vao );

    //Create the shaders
    setShaders("simpleColorizer.vs", "passThrough.fs");
    // TODO: add multiple shader integer passing

    //Load and use the compiled shaders
    glLinkProgram( programHandle );
    glGetProgramiv(programHandle, GL_LINK_STATUS, (int *)&IsLinked);

    if(IsLinked == FALSE)
    {
        /* Noticed that glGetProgramiv is used to get the length for a shader program, not glGetShaderiv. */
        glGetProgramiv(programHandle, GL_INFO_LOG_LENGTH, &maxLength);

        /* The maxLength includes the NULL character */
        shaderProgramInfoLog = (char *)malloc(maxLength);

        /* Notice that glGetProgramInfoLog, not glGetShaderInfoLog. */
        glGetProgramInfoLog(programHandle, maxLength, &maxLength, shaderProgramInfoLog);

        std::cout << shaderProgramInfoLog << std::endl;

        /* Handle the error in an appropriate way such as displaying a message or writing to a log file. */
        /* In this simple program, we'll just leave */
        free(shaderProgramInfoLog);
        return;
    }

    GLuint vertexBuffer;
    GLuint vertexLoc = glGetAttribLocation( programHandle, "in_Position" );
    glGenBuffers(1, &vertexBuffer );
    glBindBuffer( GL_ARRAY_BUFFER, vertexBuffer );
    glBufferData( GL_ARRAY_BUFFER, 9 * sizeof( GLfloat ), textureSurface, GL_STATIC_DRAW );
    glEnableVertexAttribArray( vertexLoc );
    glVertexAttribPointer( vertexLoc, 3, GL_FLOAT, 0, 0, 0);

    GLuint colorBuffer;
    GLuint colorLoc = glGetAttribLocation( programHandle, "in_Color" );
    glGenBuffers(1, &colorBuffer );
    glBindBuffer( GL_ARRAY_BUFFER, colorBuffer );
    glBufferData( GL_ARRAY_BUFFER, 9 * sizeof( GLfloat ), colors, GL_STATIC_DRAW );
    glEnableVertexAttribArray( colorLoc );
    glVertexAttribPointer( colorLoc, 3, GL_FLOAT, 0, 0, 0 );

    glUseProgram( programHandle );

    //char *name;
    //GLint active_attribs;

    //glGetProgramiv(programHandle, GL_ACTIVE_ATTRIBUTES, &active_attribs);
    //glGetProgramiv(programHandle, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &maxLength );

    //std::cout << active_attribs << std::endl;

    //GLuint texLoc = glGetUniformLocation( programHandle, "uImageUnit" );


    //glUniform1i( texLoc, depthTexID );

    glDrawArrays( GL_TRIANGLES, 0, 3 );

    //glBegin(GL_QUADS);
    //  glColor3f(1.0, 0.0, 0.0);
//     glTexCoord3f( 0.0, 0.0, 0.0 );
    //  glVertex3f( 20.0, 20.0, 0.0 );
    //
    //  glColor3f(0.0, 1.0, 0.0);
//     glTexCoord3f( 1.0, 0.0, 0.0 );
    //  glVertex3f( g_nXRes - 20.0, 20.0, 0.0 );
    //
    //  glColor3f(0.0, 0.0, 1.0);
//     glTexCoord3f( 1.0, 1.0, 0.0 );
    //  glVertex3f(g_nXRes - 20.0, g_nYRes - 20.0, 0.0);
    //
    //  glColor3f(1.0, 1.0, 0.0);
//     glTexCoord3f( 0.0, 1.0, 0.0 );
    //  glVertex3f( 20.0, g_nYRes - 20.0, 0.0 );
//  glEnd();

    glFlush();

    // Cleanup the shader code
    glUseProgram(0);
    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
    glDeleteBuffers(2, vbo );
    glDeleteVertexArrays( 1, &vao );
    glDisable(GL_TEXTURE_2D);
}