void quats2matrices( int n ){
    float  d = 1.0f/n;
    glBegin   ( GL_POINTS   );
    glColor3f ( 0.0f, 0.0f, 0.0f );
    for( int ix=-n; ix<n; ix++ ){
        float x = (ix+0.5f) * d;
        for( int iy=-n; iy<n; iy++ ){
            float y = (iy+0.5f) * d;
            for( int iz=-n; iz<n; iz++ ){
                float z = (iz+0.5f) * d;
                float r2 = x*x + y*y + z*z;
                if( r2 < 1.0f ){
                    Quat4f q;
                    Mat3f  M;
                    q.set( x, y, z, sqrt(1.0f - r2) );
                    q.toMatrix( M );
                    glColor3f ( M.a.x, M.a.y, M.a.z );
                    glVertex3f( (float)M.a.x, (float)M.a.y, (float)M.a.z );
                    //glVertex3f( (float)M.b.x, (float)M.b.y, (float)M.b.z );
                    //glVertex3f( (float)M.c.x, (float)M.c.y, (float)M.c.z );
                }
            }
        }
    }
    glEnd();
}
// FUNCTION ======	inputHanding
void inputHanding(){
    float posstep = 0.1;
	SDL_Event event;
	while(SDL_PollEvent(&event)){
		if( event.type == SDL_KEYDOWN ){
            switch( event.key.keysym.sym ){
                case SDLK_ESCAPE: quit(); break;
                case SDLK_w: modelPos[1] +=posstep; break;
                case SDLK_s: modelPos[1] -=posstep; break;
                case SDLK_a: modelPos[0] +=posstep; break;
                case SDLK_d: modelPos[0] -=posstep; break;
                case SDLK_q: modelPos[2] +=posstep*10; break;
                case SDLK_e: modelPos[2] -=posstep*10; break;
                //case SDLK_r:  world.fireProjectile( warrior1 ); break;
            }
		}
		if( event.type == SDL_QUIT){ quit();  };
	}

	int dmx,dmy;
	SDL_GetMouseState( &mouseX, &mouseY );
    Uint32 buttons = SDL_GetRelativeMouseState( &dmx, &dmy);
    //printf( " %i %i \n", mx,my );
    float mouseRotSpeed = 0.01;
    if ( buttons & SDL_BUTTON(SDL_BUTTON_RIGHT)) {
        Quat4f q; q.fromTrackball( 0, 0, -dmx*mouseRotSpeed, dmy*mouseRotSpeed );
        //printf( " %i %i  (%3.3f,%3.3f,%3.3f,%3.3f) \n", dmx,dmy, q.x,q.y,q.z,q.w );
        //qCamera.qmul_T( q );
        qCamera.qmul( q );
        //qCamera.normalize();
    }
}
void matrices2quats( int n ){
    glBegin   ( GL_POINTS   );
    for( int i=0; i<n; i++ ){
        Mat3f  M;   M.fromRand( {randf(),randf(),randf()} );
        Quat4f q;   q.fromMatrix(M);
        glVertex3f( (float)q.x, (float)q.y, (float)q.z );
    }
    glEnd();
}
void setup(){


    mesh.fromFileOBJ( "common_resources/turret.obj" );
    mesh.polygonsToTriangles();
    mesh.tris2normals(true);
    object1 = new GLObject( );
    object1->nVert    = mesh.points.size();
    //object1->nInd     = mesh.triangles.size();

    //object1->setIndexes( 16, (int*)&mesh.triangles[0] );
    object1->setIndexes( mesh.triangles.size()*3, (int*)&mesh.triangles[0] );
    printf( "nVert %i nInd %i \n", object1->nVert, object1->nInd);
    object1->buffs[0].setup(0,3,GL_FALSE,(double*)&(mesh.points [0]), object1->nVert, 'v'); // vertexes
    object1->buffs[1].setup(1,3,GL_FALSE,(double*)&(mesh.normals[0]), object1->nVert, 'n'); // normals
    //object1->indexes .setup(2,3,GL_FALSE,(void*  )&(mesh.triangles[0]), 'i'); // indexes
    //object1->index_cbuff = (GLuint *)&mesh.triangles[0];
    //object1->nVert = 5;


    /*
    object1 = new GLObject( );
    object1->nVert    = nVert;
    object1->buffs[0].setup(0,3,GL_FALSE,vertexes,'v'); // vertexes
    object1->buffs[1].setup(1,3,GL_FALSE,vertexes,'c'); // colors
    object1->init();
    */

    object1->init();

    if ( render_type == 0      ){
        // --- vertex const color
        shader1=new Shader();
        shader1->init( "shaders/basicColor3D_vert.c", "shaders/basicColor3D_frag.c" );
        glUseProgram(shader1->shaderprogram);
    }else if ( render_type == 1 ){
        // --- shading
        shader1=new Shader();
        shader1->init( "shaders/basicShading3D_vert.c", "shaders/basicShading3D_frag.c" );
        glUseProgram(shader1->shaderprogram);
    };
	//mesh.fromFileOBJ("common_resources/turret.obj");

	qCamera.setOne();
}
void draw(){
    glClearColor(0.0, 0.0, 1.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT  );

    glEnable( GL_DEPTH_TEST );
    glDepthFunc( GL_LESS );

    float camMat[16];
    getPerspectiveMatrix( -WIDTH, WIDTH, -HEIGHT, HEIGHT, 1.0, 10.0, camMat );

    //Quat4f qCamera_; convert(qCamera,qCamera_);
    Mat3f mouseMat; qCamera.toMatrix(mouseMat);
    //printf( " (%3.3f,%3.3f,%3.3f,%3.3f) \n", qCamera.x,qCamera.y,qCamera.z,qCamera.w );
    //printf( " (%3.3f,%3.3f,%3.3f,%3.3f) \n", qCamera.x,qCamera.y,qCamera.z,qCamera.w );
    //printf( "mouseMat (%3.3f,%3.3f,%3.3f) (%3.3f,%3.3f,%3.3f) (%3.3f,%3.3f,%3.3f) \n", mouseMat.ax, mouseMat.ay, mouseMat.az,  mouseMat.bx, mouseMat.by, mouseMat.bz,   mouseMat.cx, mouseMat.cy, mouseMat.cz );

    GLuint uloc;
    uloc = glGetUniformLocation( shader1->shaderprogram, "modelPos" ); glUniform3fv      (uloc, 1, modelPos );
    //uloc = glGetUniformLocation( shader1->shaderprogram, "modelMat" ); glUniformMatrix3fv(uloc, 1, GL_FALSE, modelMat );
    uloc = glGetUniformLocation( shader1->shaderprogram, "modelMat" ); glUniformMatrix3fv(uloc, 1, GL_FALSE, (float*)&mouseMat );
    uloc = glGetUniformLocation( shader1->shaderprogram, "camMat"   ); glUniformMatrix4fv(uloc, 1, GL_FALSE, camMat   );

    // shading
    if ( render_type == 1 ){
        uloc = glGetUniformLocation( shader1->shaderprogram, "cam_pos"       ); glUniform3fv      (uloc, 1, cam_pos      );
        uloc = glGetUniformLocation( shader1->shaderprogram, "light_pos"     ); glUniform3fv      (uloc, 1, light_pos     );
        uloc = glGetUniformLocation( shader1->shaderprogram, "lightColor"    ); glUniform3fv      (uloc, 1, lightColor    );
        uloc = glGetUniformLocation( shader1->shaderprogram, "diffuseColor"  ); glUniform3fv      (uloc, 1, diffuseColor  );
        uloc = glGetUniformLocation( shader1->shaderprogram, "ambientColor"  ); glUniform3fv      (uloc, 1, ambientColor  );
        uloc = glGetUniformLocation( shader1->shaderprogram, "specularColor" ); glUniform3fv      (uloc, 1, specularColor );
    };

    //uloc = glGetUniformLocation( shader1->shaderprogram, "light_dir"); glUniform3fv(uloc, 1, light_dir  );

    object1->draw();

    SDL_GL_SwapWindow(window);

}
Exemple #6
0
// static
Matrix3f Matrix3f::fromQuat( const Quat4f& q )
{
	Quat4f qq = q.normalized();

	float xx = qq.x * qq.x;
	float yy = qq.y * qq.y;
	float zz = qq.z * qq.z;

	float xy = qq.x * qq.y;
	float zw = qq.z * qq.w;

	float xz = qq.x * qq.z;
	float yw = qq.y * qq.w;

	float yz = qq.y * qq.z;
	float xw = qq.x * qq.w;

	return Matrix3f
	(
		1.0f - 2.0f * ( yy + zz ),		2.0f * ( xy - zw ),				2.0f * ( xz + yw ),
		2.0f * ( xy + zw ),				1.0f - 2.0f * ( xx + zz ),		2.0f * ( yz - xw ),
		2.0f * ( xz - yw ),				2.0f * ( yz + xw ),				1.0f - 2.0f * ( xx + yy )
	);
}
Exemple #7
0
// static
Matrix3f Matrix3f::rotation( const Quat4f& rq )
{
	Quat4f q = rq.normalized();

	float xx = q.x() * q.x();
	float yy = q.y() * q.y();
	float zz = q.z() * q.z();

	float xy = q.x() * q.y();
	float zw = q.z() * q.w();

	float xz = q.x() * q.z();
	float yw = q.y() * q.w();

	float yz = q.y() * q.z();
	float xw = q.x() * q.w();

	return Matrix3f
		(
			1.0f - 2.0f * ( yy + zz ),		2.0f * ( xy - zw ),				2.0f * ( xz + yw ),
			2.0f * ( xy + zw ),				1.0f - 2.0f * ( xx + zz ),		2.0f * ( yz - xw ),
			2.0f * ( xz - yw ),				2.0f * ( yz + xw ),				1.0f - 2.0f * ( xx + yy )
		);
}
// static
Matrix4f Matrix4f::rotation( const Quat4f& q )
{
	Quat4f qq = q.normalized();

	float xx = qq.x() * qq.x();
	float yy = qq.y() * qq.y();
	float zz = qq.z() * qq.z();

	float xy = qq.x() * qq.y();
	float zw = qq.z() * qq.w();

	float xz = qq.x() * qq.z();
	float yw = qq.y() * qq.w();

	float yz = qq.y() * qq.z();
	float xw = qq.x() * qq.w();

	return Matrix4f
	(
		1.0f - 2.0f * ( yy + zz ),		2.0f * ( xy - zw ),				2.0f * ( xz + yw ),				0.0f,
		2.0f * ( xy + zw ),				1.0f - 2.0f * ( xx + zz ),		2.0f * ( yz - xw ),				0.0f,
		2.0f * ( xz - yw ),				2.0f * ( yz + xw ),				1.0f - 2.0f * ( xx + yy ),		0.0f,
		0.0f,							0.0f,							0.0f,							1.0f
	);
}
void QD3D11MultiViewportViewer::mouseMoveEvent( QMouseEvent* event )
{
	Vector2i curPos( event->x(), event->y() );
	Vector2f delta = curPos - m_prevPos;

#if 1

	float pitchSpeed = m_flipMouseUpDown ? m_mousePitchSpeed : -m_mousePitchSpeed;
	const float yawSpeed = 0.005f;
	const float panSpeed = 0.005f;
	const float walkSpeed = -0.005f;

	Matrix3f worldToCamera = m_perspectiveCamera.viewMatrix().getSubmatrix3x3( 0, 0 );
	Matrix3f cameraToWorld = m_perspectiveCamera.inverseViewMatrix().getSubmatrix3x3( 0, 0 );
	
	Vector3f eye = m_perspectiveCamera.eye();
	Vector3f x = m_perspectiveCamera.right();
	Vector3f y = m_perspectiveCamera.up();
	Vector3f z = m_perspectiveCamera.forward();

	// rotate
	if( event->buttons() == Qt::LeftButton )
	{
		// pitch around the local x axis		
		float pitch = pitchSpeed * delta.y;

		Matrix3f pitchMatrix = Matrix3f::rotateX( pitch );		

		y = cameraToWorld * pitchMatrix * worldToCamera * y;
		z = cameraToWorld * pitchMatrix * worldToCamera * z;

		// yaw around the world up vector
		float yaw = yawSpeed * delta.x;

		Matrix3f yawMatrix = m_groundPlaneToWorld * Matrix3f::rotateY( yaw ) * m_worldToGroundPlane;

		x = yawMatrix * x;
		y = yawMatrix * y;
		z = yawMatrix * z;		

		m_perspectiveCamera.setLookAt( eye, eye + z, y );
	}
	// walk
	else if( event->buttons() == Qt::RightButton )
	{
		float dx = panSpeed * delta.x;
		float dz = walkSpeed * delta.y;

		translate( dx, 0, dz );
	}
	// move up/down
	else if( event->buttons() == Qt::MiddleButton )
	{
		float dy = -panSpeed * delta.y;
		translate( 0, dy, 0 );
	}

#else

	if(event->buttons() & Qt::RightButton) //rotate
	{
		float rotSpeed = 0.005f; //radians per pixel
		Quat4f rotation;
		rotation.setAxisAngle(rotSpeed * delta.abs(), Vector3f(-delta[1], -delta[0], 0));
		Matrix3f rotMatrix = Matrix3f::rotation(rotation);
		Matrix3f viewMatrix = m_camera.getViewMatrix().getSubmatrix3x3(0, 0);
		rotMatrix = viewMatrix.transposed() * rotMatrix * viewMatrix;

		Vector3f eye, center, up;
		m_camera.getLookAt(&eye, &center, &up);
		m_camera.setLookAt(center + rotMatrix * (eye - center), center, rotMatrix * up);
	}
	else if(event->buttons() & Qt::LeftButton) //translate
	{
		float speed = 10.f;
		Vector3f screenDelta(delta[0], delta[1], 0);
		screenDelta[0] /= -double(width());
		screenDelta[1] /= double(height());
		Matrix4f iViewProjMatrix = m_camera.getInverseViewProjectionMatrix();
		Vector3f worldDelta = iViewProjMatrix.getSubmatrix3x3(0, 0) * (speed * screenDelta);

		Vector3f eye, center, up;
		m_camera.getLookAt(&eye, &center, &up);
		m_camera.setLookAt(eye + worldDelta, center + worldDelta, up);
	}
#endif

	m_prevPos = curPos;
	update();
}
void draw(){

    long time_start = getCPUticks();

    glClearColor(0.0, 0.0, 1.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT  );

    glEnable( GL_DEPTH_TEST );
    glDepthFunc( GL_LESS );

    long time_0 = getCPUticks();


    //getPerspectiveMatrix( -WIDTH, WIDTH, -HEIGHT, HEIGHT, 1.0, 10.0, camMat );
    getPerspectiveMatrix( -WIDTH, WIDTH, -HEIGHT, HEIGHT, 1.0, 20.0, camMat );

    //Quat4f qCamera_; convert(qCamera,qCamera_);
    qCamera.toMatrix(mouseMat);
    //printf( " (%3.3f,%3.3f,%3.3f,%3.3f) \n", qCamera.x,qCamera.y,qCamera.z,qCamera.w );
    //printf( " (%3.3f,%3.3f,%3.3f,%3.3f) \n", qCamera.x,qCamera.y,qCamera.z,qCamera.w );
    //printf( "mouseMat (%3.3f,%3.3f,%3.3f) (%3.3f,%3.3f,%3.3f) (%3.3f,%3.3f,%3.3f) \n", mouseMat.ax, mouseMat.ay, mouseMat.az,  mouseMat.bx, mouseMat.by, mouseMat.bz,   mouseMat.cx, mouseMat.cy, mouseMat.cz );

    // ============= Terrain

    if(RayTerrain){
        draw_TerrainRayMarch();
    }else{
        draw_TerrainMeshHeightMap();
    }

    long time_1 = getCPUticks();

    // ============= Objects
    glUseProgram(shader1->shaderprogram);

    uloc = glGetUniformLocation( shader1->shaderprogram, "camMat"   ); glUniformMatrix4fv(uloc, 1, GL_FALSE, camMat   );
    uloc = glGetUniformLocation( shader1->shaderprogram, "modelMat" ); glUniformMatrix3fv(uloc, 1, GL_FALSE, (float*)&mouseMat );
    uloc = glGetUniformLocation( shader1->shaderprogram, "modelPos" ); // glUniform3fv      (uloc, 1, modelPos );

    //object1->draw();
    object1->preDraw();
    for(int i=0; i<ninstancs; i++){
        glUniform3fv( uloc, 1, instance_points+i*3 );
        //glDrawArrays( object1->draw_mode, 0, object1->nVert);
        object1->draw_instance();
    }
    object1->afterDraw();

    long time_2 = getCPUticks();

    SDL_GL_SwapWindow(window);

    long time_3 = getCPUticks();


    double Ttot     = (time_3-time_start)*1e-6;
    double Tterrain = (time_1-time_0)*1e-6;
    double Tobject  = (time_2-time_1)*1e-6;
    double Tswap    = (time_3-time_2)*1e-6;

    printf( "Ttot %3.2f terrain %3.2f objects %3.2f swap %3.2f \n", Ttot, Tterrain, Tobject, Tswap );

}
void setup(){

    if ( render_type == 0      ){
        // --- vertex const color
        shader1=new Shader();
        shader1->init( "shaders/basicColor3D_vert.c", "shaders/basicColor3D_frag.c" );
    }else if ( render_type == 1 ){
        // --- shading
        shader1=new Shader();
        shader1->init( "shaders/basicShading3D_vert.c", "shaders/basicShading3D_frag.c" );
        glUseProgram(shader1->shaderprogram);
    };
	//mesh.fromFileOBJ("common_resources/turret.obj");

    int nVert = countVerts( Solids::Icosahedron_nfaces, Solids::Icosahedron_ngons );
    GLfloat * verts   = new GLfloat[nVert*3];
    GLfloat * normals = new GLfloat[nVert*3];
    hardFace( Solids::Icosahedron_nfaces, Solids::Icosahedron_ngons, Solids::Icosahedron_faces, Solids::Icosahedron_verts, verts, normals );

    object1 = new GLObject( );
    object1->nVert    = nVert;
    object1->buffs[0].setup(0,3,GL_FALSE,verts,  'v'); // vertexes
    object1->buffs[1].setup(1,3,GL_FALSE,normals,'n'); // normals
    object1->init();

        // shading
    if ( render_type == 1 ){
        uloc = glGetUniformLocation( shader1->shaderprogram, "cam_pos"       ); glUniform3fv      (uloc, 1, cam_pos      );
        uloc = glGetUniformLocation( shader1->shaderprogram, "light_pos"     ); glUniform3fv      (uloc, 1, light_pos     );
        uloc = glGetUniformLocation( shader1->shaderprogram, "lightColor"    ); glUniform3fv      (uloc, 1, lightColor    );
        uloc = glGetUniformLocation( shader1->shaderprogram, "diffuseColor"  ); glUniform3fv      (uloc, 1, diffuseColor  );
        uloc = glGetUniformLocation( shader1->shaderprogram, "ambientColor"  ); glUniform3fv      (uloc, 1, ambientColor  );
        uloc = glGetUniformLocation( shader1->shaderprogram, "specularColor" ); glUniform3fv      (uloc, 1, specularColor );
    };

    ninstancs = 100; // 30 ms/frame
    //ninstancs = 10000; // 30 ms/frame
    instance_points = new GLfloat[3*ninstancs];
    for (int i=0; i<ninstancs; i++){
        int i3 = 3*i;
        instance_points[i3+0] = randf(-15.0,15.0);
        instance_points[i3+1] = randf(-15.0,15.0);
        instance_points[i3+2] = randf(-60.0,-1000.0);
    }

    // ------------- Terrain
	shader2=new Shader();
	if  ( terrain_mode ){
        if(RayTerrain){ shader2->init( "shaders/terrain_vert.c",  "shaders/terrain_frag.c"  );   }
        else          { shader2->init( "shaders/terrain_vert2.c", "shaders/terrain_frag2.c" );   }
    }else {
        shader2->init( "shaders/plain_vert.c", "shaders/texture_frag.c" );
    };

    // ------------- texture
    const int imgW = 256;
    const int imgH = 256;
    unsigned int imgData [imgW*imgH];
    Vec2d pos,rot,dpos;
    rot.fromAngle( 45454*0.1 );
    for( int iy=0; iy<imgH; iy++ ){
        for( int ix=0; ix<imgW; ix++ ){
            /*
            float r = sin(ix*0.16);
            float g = sin(iy*-0.31);
            float b = sin((ix+iy)*0.1);
            r*=g*b; g=r; b=r;
            r+=1.0f; g+=1.0f; b+=1.0f;
            */
            //8, 2.0, 1.0,  0.5, 0.8, 45454, {100.0,100.0}
            //int n, double scale,  double hscale,  double fdown, double strength, int seed, const Vec2d& pos0
            pos.set(ix*5,iy*5);
            Noise::warpNoise3R( pos, rot, 0.4, 0.4, 6, dpos );
            float r = 1-(dpos.x * dpos.y)*2+0.5;
            //float r = 0.3*sin(ix*0.1)*cos(iy*0.1) + 0.2*cos(ix*0.3)*sin(iy*0.3); r+=0.5;
            float g=r; float b=r;
            imgData[ iy*imgW + ix ] =  ((int)(255*r) <<16) | ((int)(255*g)<<8) | ((int)(255*b));
        }
    }
    glGenTextures  (0, &textureID);    // Create one OpenGL texture
    glBindTexture  (GL_TEXTURE_2D, textureID); // "Bind" the newly created texture : all future texture functions will modify this texture
    glTexImage2D   (GL_TEXTURE_2D, 0,GL_RGBA, imgW, imgH, 0, GL_RGBA, GL_UNSIGNED_BYTE, imgData);   // Give the image to OpenGL
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    if( RayTerrain ){
        object2 = new GLObject( );
        object2->draw_mode = GL_TRIANGLE_STRIP;
        object2->nVert   = 4;
        object2->buffs[0].setup(0,2,GL_FALSE,&QUAD_verts[0],'v');
        object2->init();
    }else{
        float * strip = new GLfloat[Ter_nquads*2*2];
        for(int i=0; i<Ter_nquads; i++){
            int i4 = i<<2;
            float x  = i*Ter_dx-0.5;
            strip[i4  ]=x;          strip[i4+1]=0.0f;
            strip[i4+2]=x*Ter_fsc;  strip[i4+3]=Ter_dz;
        }
        object2 = new GLObject();
        object2->draw_mode = GL_TRIANGLE_STRIP;
        object2->nVert     = Ter_nquads*2;
        object2->buffs[0].setup(0,2,GL_FALSE,strip,'v');
        object2->init();
    }

	qCamera.setOne();


	delay = 1;
}