예제 #1
0
Point4 Matrix4x4::operator*(const Point4& p) const
{
	Point4 result;

	glusMatrix4x4MultiplyPoint4f(result.p, m, p.p);

	return result;
}
예제 #2
0
파일: main.c 프로젝트: AJ92/OpenGL
GLUSboolean init(GLUSvoid)
{
    GLfloat lightDirection[3] = { 1.0f, 1.0f, 1.0f };
    GLfloat color[4] = { 0.0f, 1.0f, 1.0f, 1.0f };

    GLUStextfile computeSource;

    GLUStextfile vertexSource;
    GLUStextfile fragmentSource;

    GLint i;
    GLfloat matrix[16];

    GLfloat* normals;

    GLfloat distanceRest;
    GLfloat distanceDiagonalRest;

    GLfloat sphereCenter[4] = {0.0f, 0.0f, -0.01f, 1.0f};
    GLfloat sphereRadius = 1.0f;

    glusFileLoadText("../Example40/shader/cloth.comp.glsl", &computeSource);

    glusProgramBuildComputeFromSource(&g_computeProgram, (const GLchar**) &computeSource.text);

    glusFileDestroyText(&computeSource);


    glusFileLoadText("../Example40/shader/cloth.vert.glsl", &vertexSource);
    glusFileLoadText("../Example40/shader/cloth.frag.glsl", &fragmentSource);

    glusProgramBuildFromSource(&g_program, (const GLUSchar**) &vertexSource.text, 0, 0, 0, (const GLUSchar**) &fragmentSource.text);

    glusFileDestroyText(&vertexSource);
    glusFileDestroyText(&fragmentSource);

    //

    g_verticesPerRowLocation = glGetUniformLocation(g_computeProgram.program, "u_verticesPerRow");
    g_deltaTimeLocation = glGetUniformLocation(g_computeProgram.program, "u_deltaTime");
    g_distanceRestLocation = glGetUniformLocation(g_computeProgram.program, "u_distanceRest");
    g_distanceDiagonalRestLocation = glGetUniformLocation(g_computeProgram.program, "u_distanceDiagonalRest");
    g_sphereCenterLocation = glGetUniformLocation(g_computeProgram.program, "u_sphereCenter");
    g_sphereRadiusLocation = glGetUniformLocation(g_computeProgram.program, "u_sphereRadius");


    g_modelViewProjectionMatrixLocation = glGetUniformLocation(g_program.program, "u_modelViewProjectionMatrix");
    g_normalMatrixLocation = glGetUniformLocation(g_program.program, "u_normalMatrix");
    g_lightDirectionLocation = glGetUniformLocation(g_program.program, "u_lightDirection");
    g_colorLocation = glGetUniformLocation(g_program.program, "u_color");

	g_vertexLocation = glGetAttribLocation(g_program.program, "a_vertex");
	g_normalLocation = glGetAttribLocation(g_program.program, "a_normal");

    //

    // Use a helper function to create a grid plane.
    glusShapeCreateRectangularGridPlanef(&g_gridPlane, 2.0f, 2.0f, ROWS, ROWS, GLUS_FALSE);

    // Use x, as only horizontal and vertical springs are used. Adapt this, if diagonal or a non square grid is used.
    distanceRest = g_gridPlane.vertices[4] - g_gridPlane.vertices[0];
    distanceDiagonalRest = sqrtf(2.0f * distanceRest * distanceRest);

    // Rotate by 90 degrees, that the grid is in the x-z-plane.
    glusMatrix4x4Identityf(matrix);
    glusMatrix4x4Translatef(matrix, 0.0f, 1.1f, 0.0f);
    glusMatrix4x4RotateRxf(matrix, -90.0f);
    for (i = 0; i < g_gridPlane.numberVertices; i++)
    {
    	glusMatrix4x4MultiplyPoint4f(&g_gridPlane.vertices[4 * i], matrix, &g_gridPlane.vertices[4 * i]);
    }

    g_numberIndicesPlane = g_gridPlane.numberIndices;

    glGenBuffers(1, &g_indicesVBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, g_gridPlane.numberIndices * sizeof(GLuint), (GLuint*) g_gridPlane.indices, GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    //

    normals = (GLfloat*)malloc(g_gridPlane.numberVertices * 4 * sizeof(GLfloat));

    // Add one more GLfloat channel as padding for std430 layout.
    glusPaddingConvertf(normals, g_gridPlane.normals, 3, 1, g_gridPlane.numberVertices);

    free(g_gridPlane.normals);
    g_gridPlane.normals = normals;

    //

	glGenBuffers(3, g_verticesBuffer);

	glBindBuffer(GL_SHADER_STORAGE_BUFFER, g_verticesBuffer[0]);
	glBufferData(GL_SHADER_STORAGE_BUFFER, g_gridPlane.numberVertices * 4 * sizeof(GLfloat), g_gridPlane.vertices, GL_DYNAMIC_DRAW);

	glBindBuffer(GL_SHADER_STORAGE_BUFFER, g_verticesBuffer[1]);
	glBufferData(GL_SHADER_STORAGE_BUFFER, g_gridPlane.numberVertices * 4 * sizeof(GLfloat), g_gridPlane.vertices, GL_DYNAMIC_DRAW);

	glBindBuffer(GL_SHADER_STORAGE_BUFFER, g_verticesBuffer[2]);
	glBufferData(GL_SHADER_STORAGE_BUFFER, g_gridPlane.numberVertices * 4 * sizeof(GLfloat), 0, GL_DYNAMIC_DRAW);

	glGenBuffers(1, &g_normalsBuffer);

	glBindBuffer(GL_SHADER_STORAGE_BUFFER, g_normalsBuffer);
	glBufferData(GL_SHADER_STORAGE_BUFFER, g_gridPlane.numberVertices * 4 * sizeof(GLfloat), g_gridPlane.normals, GL_DYNAMIC_DRAW);

    //

    glUseProgram(g_program.program);

    glGenVertexArrays(1, &g_vao);
    glBindVertexArray(g_vao);

	glBindBuffer(GL_ARRAY_BUFFER, g_normalsBuffer);
	glVertexAttribPointer(g_normalLocation, 4, GL_FLOAT, GL_FALSE, 0, 0);
	glEnableVertexAttribArray(g_normalLocation);

	glBindBuffer(GL_ARRAY_BUFFER, g_verticesBuffer[2]);
	glVertexAttribPointer(g_vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0);
	glEnableVertexAttribArray(g_vertexLocation);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO);

    glBindVertexArray(0);

    //

    glusVector3Normalizef(lightDirection);
    glUniform3fv(g_lightDirectionLocation, 1, lightDirection);

    glUniform4fv(g_colorLocation, 1, color);

    glUseProgram(0);

    //

    glUseProgram(g_computeProgram.program);

    glUniform1i(g_verticesPerRowLocation, ROWS + 1);

    glUniform1f(g_distanceRestLocation, distanceRest);
    glUniform1f(g_distanceDiagonalRestLocation, distanceDiagonalRest);

    glUniform4fv(g_sphereCenterLocation, 1, sphereCenter);
    glUniform1f(g_sphereRadiusLocation, sphereRadius);

    glUseProgram(0);

    //

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    glClearDepth(1.0f);

    glEnable(GL_DEPTH_TEST);

    //

    if (!initSphere(sphereCenter, sphereRadius, lightDirection))
    {
    	return GLUS_FALSE;
    }

    return GLUS_TRUE;
}
예제 #3
0
파일: main.c 프로젝트: DreamerKing/OpenGL
GLUSboolean update(GLUSfloat time)
{
    static GLfloat angle = 0.0f;

    GLuint primitivesWritten;

    // Field of view

    GLfloat rotationMatrix[16];

    GLfloat positionTextureSpace[4];
    GLfloat directionTextureSpace[3];
    GLfloat leftNormalTextureSpace[3];
    GLfloat rightNormalTextureSpace[3];
    GLfloat backNormalTextureSpace[3];

    GLfloat xzPosition2D[4];

    //

    GLfloat tmvpMatrix[16];

    // Animation update

    g_personView.cameraPosition[0] = -cosf(2.0f * GLUS_PI * angle / TURN_DURATION) * TURN_RADIUS * METERS_TO_VIRTUAL_WORLD_SCALE;
    g_personView.cameraPosition[2] = -sinf(2.0f * GLUS_PI * angle / TURN_DURATION) * TURN_RADIUS * METERS_TO_VIRTUAL_WORLD_SCALE;

    g_personView.cameraDirection[0] = sinf(2.0f * GLUS_PI * angle / TURN_DURATION);
    g_personView.cameraDirection[2] = -cosf(2.0f * GLUS_PI * angle / TURN_DURATION);

    if (g_animationOn)
    {
        angle += time;
    }

    glusMatrix4x4LookAtf(g_viewMatrix, g_activeView->cameraPosition[0], g_activeView->cameraPosition[1], g_activeView->cameraPosition[2], g_activeView->cameraPosition[0] + g_activeView->cameraDirection[0], g_activeView->cameraPosition[1] + g_activeView->cameraDirection[1],
            g_activeView->cameraPosition[2] + g_activeView->cameraDirection[2], g_activeView->cameraUp[0], g_activeView->cameraUp[1], g_activeView->cameraUp[2]);

    glusMatrix4x4Identityf(tmvpMatrix);
    glusMatrix4x4Multiplyf(tmvpMatrix, tmvpMatrix, g_projectionMatrix);
    glusMatrix4x4Multiplyf(tmvpMatrix, tmvpMatrix, g_viewMatrix);
    glusMatrix4x4Multiplyf(tmvpMatrix, tmvpMatrix, g_textureToWorldMatrix);

    // Position

    xzPosition2D[0] = g_personView.cameraPosition[0];
    xzPosition2D[1] = 0.0f;
    xzPosition2D[2] = g_personView.cameraPosition[2];
    xzPosition2D[3] = g_personView.cameraPosition[3];

    glusMatrix4x4MultiplyPoint4f(positionTextureSpace, g_worldToTextureMatrix, xzPosition2D);

    // Direction

    glusMatrix4x4MultiplyVector3f(directionTextureSpace, g_worldToTextureMatrix, g_personView.cameraDirection);

    // Left normal of field of view

    glusMatrix4x4Identityf(rotationMatrix);
    glusMatrix4x4RotateRyf(rotationMatrix, g_personView.fov * (g_width / g_height) / 2.0f + 90.0f);
    glusMatrix4x4MultiplyVector3f(leftNormalTextureSpace, rotationMatrix, g_personView.cameraDirection);
    glusMatrix4x4MultiplyVector3f(leftNormalTextureSpace, g_worldToTextureNormalMatrix, leftNormalTextureSpace);

    // Right normal of field of view

    glusMatrix4x4Identityf(rotationMatrix);
    glusMatrix4x4RotateRyf(rotationMatrix, -g_personView.fov * (g_width / g_height) / 2.0f - 90.0f);
    glusMatrix4x4MultiplyVector3f(rightNormalTextureSpace, rotationMatrix, g_personView.cameraDirection);
    glusMatrix4x4MultiplyVector3f(rightNormalTextureSpace, g_worldToTextureNormalMatrix, rightNormalTextureSpace);

    // Back normal of field of view

    glusMatrix4x4Identityf(rotationMatrix);
    glusMatrix4x4RotateRyf(rotationMatrix, 180.0f);
    glusMatrix4x4MultiplyVector3f(backNormalTextureSpace, rotationMatrix, g_personView.cameraDirection);
    glusMatrix4x4MultiplyVector3f(backNormalTextureSpace, g_worldToTextureNormalMatrix, backNormalTextureSpace);

    // OpenGL stuff

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Pass one.

    // Disable any rasterization
    glEnable(GL_RASTERIZER_DISCARD);

    glUseProgram(g_programPassOne.program);

    glUniform4fv(g_positionTextureSpacePassOneLocation, 1, positionTextureSpace);
    glUniform3fv(g_leftNormalTextureSpacePassOneLocation, 1, leftNormalTextureSpace);
    glUniform3fv(g_rightNormalTextureSpacePassOneLocation, 1, rightNormalTextureSpace);
    glUniform3fv(g_backNormalTextureSpacePassOneLocation, 1, backNormalTextureSpace);

    glBindVertexArray(g_vaoPassOne);

    // Bind to vertices used in render pass two. To this buffer is written.
    glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, g_verticesPassTwoVBO);

    // We need to know, how many primitives are written. So start the query.
    glBeginQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN, g_transformFeedbackQuery);

    // Start the operation ...
    glBeginTransformFeedback(GL_POINTS);

    // ... render the elements ...
    glDrawElements(GL_POINTS, g_sNumPoints * g_tNumPoints, GL_UNSIGNED_INT, 0);

    // ... and stop the operation.
    glEndTransformFeedback();

    // Now, we can also stop the query.
    glEndQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN);

    glDisable(GL_RASTERIZER_DISCARD);

    glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);

    glBindVertexArray(0);

    // Pass two

    glUseProgram(g_shaderProgramPassTwo.program);

    glUniformMatrix4fv(g_tmvpPassTwoLocation, 1, GL_FALSE, tmvpMatrix);
    glUniform4fv(g_positionTextureSpacePassTwoLocation, 1, positionTextureSpace);

    glBindVertexArray(g_vaoPassTwo);

    // Now get the number of primitives written in the first render pass.
    glGetQueryObjectuiv(g_transformFeedbackQuery, GL_QUERY_RESULT, &primitivesWritten);

    // No draw the final terrain.
    glDrawArrays(GL_PATCHES, 0, primitivesWritten);

    return GLUS_TRUE;
}
예제 #4
0
void DebugDrawFactory::createDefaultDebugGeometry() const
{
	LineGeometrySP lineGeometry;

	GLUSline gridPlane;
	glusLineCreateRectangularGridf(&gridPlane, 50.0f, 50.0f, 50, 50);
	lineGeometry = LineGeometrySP(new LineGeometry(gridPlane));
	glusLineDestroyf(&gridPlane);
	LineGeometryManager::getInstance()->setLineGeometry("GridPlane", lineGeometry);

	GLUSshape sphere;
	glusShapeCreateSpheref(&sphere, 1.0f, 16);
	lineGeometry = LineGeometrySP(new LineGeometry(sphere));
	glusShapeDestroyf(&sphere);
	LineGeometryManager::getInstance()->setLineGeometry("Sphere", lineGeometry);

	GLUSshape cone;
	glusShapeCreateConef(&cone, 0.5f, 0.5f, 16, 16);
	lineGeometry = LineGeometrySP(new LineGeometry(cone));
	glusShapeDestroyf(&cone);
	LineGeometryManager::getInstance()->setLineGeometry("Cone", lineGeometry);

	GLUSshape pyramid;
	glusShapeCreateConef(&pyramid, 0.5f, 0.5f, 4, 1);

	// The pyramid gets rotated. So adjust the height to the edge and not the vertex.
	float height = sqrtf(0.125f);
	Matrix4x4 modelMatrix;
	modelMatrix.scale(0.5f / height, 1.0f, 0.5f / height);
	modelMatrix.rotateRzRyRx(0.0f, 45.0f, 0.0f);
	for (uint32_t i = 0; i < pyramid.numberVertices; i++)
	{
		glusMatrix4x4MultiplyPoint4f(&pyramid.vertices[i*4], modelMatrix.getM(), &pyramid.vertices[i*4]);
	}

	lineGeometry = LineGeometrySP(new LineGeometry(pyramid));
	glusShapeDestroyf(&pyramid);
	LineGeometryManager::getInstance()->setLineGeometry("Pyramid", lineGeometry);

	GLUSshape cylinder;
	glusShapeCreateCylinderf(&cylinder, 0.5f, 0.5f, 16);
	lineGeometry = LineGeometrySP(new LineGeometry(cylinder));
	glusShapeDestroyf(&cone);
	LineGeometryManager::getInstance()->setLineGeometry("Cylinder", lineGeometry);

	GLUSline square;
	glusLineCreateSquaref(&square, 1.0f);
	lineGeometry = LineGeometrySP(new LineGeometry(square));
	glusLineDestroyf(&square);
	LineGeometryManager::getInstance()->setLineGeometry("Square", lineGeometry);

	GLUSline circle;
	glusLineCreateCirclef(&circle, 1.0f, 32);
	lineGeometry = LineGeometrySP(new LineGeometry(circle));
	glusLineDestroyf(&circle);
	LineGeometryManager::getInstance()->setLineGeometry("Circle", lineGeometry);

	GLUSline singleLine;
	GLUSfloat origin0[4] = {0.0f, 0.0f, 0.0f, 1.0f};
	GLUSfloat origin1[4] = {0.0f, 0.0f, 0.0f, 1.0f};
	glusLineCreateLinef(&singleLine, origin0, origin1);
	lineGeometry = LineGeometrySP(new LineGeometry(singleLine));
	glusLineDestroyf(&singleLine);
	LineGeometryManager::getInstance()->setLineGeometry("SingleLine", lineGeometry);
}
예제 #5
0
파일: main.c 프로젝트: DreamerKing/OpenGL
GLUSboolean init(GLUSvoid)
{
    GLUSshape sphere;

    GLUStextfile vertexSource;
    GLUStextfile geometrySource;
    GLUStextfile fragmentSource;

    glusFileLoadText("../Example10/shader/dublicate.vert.glsl", &vertexSource);
    glusFileLoadText("../Example10/shader/dublicate.geom.glsl", &geometrySource);
    glusFileLoadText("../Example10/shader/dublicate.frag.glsl", &fragmentSource);

    glusProgramBuildFromSource(&g_program, (const GLUSchar**) &vertexSource.text, 0, 0, (const GLUSchar**) &geometrySource.text, (const GLUSchar**) &fragmentSource.text);

    glusFileDestroyText(&vertexSource);
    glusFileDestroyText(&geometrySource);
    glusFileDestroyText(&fragmentSource);

    //

    g_projectionMatrixLocation = glGetUniformLocation(g_program.program, "u_projectionMatrix");
    g_viewMatrixLocation = glGetUniformLocation(g_program.program, "u_viewMatrix");
    g_normalMatrixLocation = glGetUniformLocation(g_program.program, "u_normalMatrix");
    g_lightPositionLocation = glGetUniformLocation(g_program.program, "u_lightPosition");

    g_vertexLocation = glGetAttribLocation(g_program.program, "a_vertex");
    g_normalLocation = glGetAttribLocation(g_program.program, "a_normal");

    //

    glusShapeCreateSpheref(&sphere, 1.0f, 32);
    g_numberIndicesSphere = sphere.numberIndices;

    glGenBuffers(1, &g_verticesVBO);

    glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO);
    glBufferData(GL_ARRAY_BUFFER, sphere.numberVertices * 4 * sizeof(GLfloat), (GLfloat*) sphere.vertices, GL_STATIC_DRAW);
    glGenBuffers(1, &g_normalsVBO);

    glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO);
    glBufferData(GL_ARRAY_BUFFER, sphere.numberVertices * 3 * sizeof(GLfloat), (GLfloat*) sphere.normals, GL_STATIC_DRAW);
    glGenBuffers(1, &g_indicesVBO);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sphere.numberIndices * sizeof(GLuint), (GLuint*) sphere.indices, GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    glusShapeDestroyf(&sphere);

    //

    glUseProgram(g_program.program);

    glusMatrix4x4LookAtf(g_viewMatrix, 0.0f, 0.0f, 6.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

    glUniformMatrix4fv(g_viewMatrixLocation, 1, GL_FALSE, g_viewMatrix);

    glusMatrix4x4ExtractMatrix3x3f(g_normalMatrix, g_viewMatrix);

    glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, g_normalMatrix);

    glusMatrix4x4MultiplyPoint4f(g_lightPosition, g_viewMatrix, g_lightPosition);

    glUniform4fv(g_lightPositionLocation, 1, g_lightPosition);

    //

    glGenVertexArrays(1, &g_vao);
    glBindVertexArray(g_vao);

    glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO);
    glVertexAttribPointer(g_vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(g_vertexLocation);

    glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO);
    glVertexAttribPointer(g_normalLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(g_normalLocation);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO);

    //

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    glClearDepth(1.0f);

    glEnable(GL_DEPTH_TEST);

    glEnable(GL_CULL_FACE);

    return GLUS_TRUE;
}