Exemple #1
0
///
// Draw triangles using the shader pair created in Init()
//
void gl_rect_draw(GL_STATE_T *p_state)
{
	RectInstanceData *userData = p_state->user_data;
	GLShapeInstanceData *shapeData = &userData->shape;
	GLRectDisplayData *displayData = p_state->rectDisplayData;
	
	mat4x4 projection;
	mat4x4 modelView;
	mat4x4 projection_scaled;
	mat4x4 translation;
	mat4x4 projection_final;
	
	GLfloat vVertices[] = { 0.0f, 0.0f, 0.0f,  // Position 0
		0.0f,  1.0f, 0.0f,  // Position 1
		1.0f,  1.0f, 0.0f,  // Position 2
		1.0f,  0.0f, 0.0f,  // Position 3
	};
	
	GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
	//GLushort indices[] = {1, 0, 3, 0, 2, 0, 1 };
	
	// Use the program object
	glUseProgram ( displayData->programObject );
	
	// Load the vertex position
	glVertexAttribPointer ( displayData->positionLoc, 3, GL_FLOAT,
			       GL_FALSE, 3 * sizeof(GLfloat), vVertices );
	
	glEnableVertexAttribArray ( displayData->positionLoc );

	mat4x4_identity(projection);
	mat4x4_scale_aniso(projection_scaled, projection,
			   2.0/p_state->width,
			   -2.0/p_state->height,
			   1.0);
	mat4x4_translate(translation, -1, 1, 0);
	mat4x4_mul(projection_final, translation, projection_scaled);
	
	mat4x4_translate(translation, shapeData->objectX, shapeData->objectY, 0.0);
	mat4x4_identity(projection);
	mat4x4_scale_aniso(projection_scaled, projection,
			   shapeData->objectWidth,
			   shapeData->objectHeight,
			   1.0);
	mat4x4_mul(modelView, translation, projection_scaled);
	
	glUniformMatrix4fv ( displayData->projectionLoc, 1, GL_FALSE, (GLfloat *)projection_final);
	glUniformMatrix4fv ( displayData->modelViewLoc,  1, GL_FALSE, (GLfloat *)modelView);
	
	glUniform3f ( displayData->colorLoc, userData->red, userData->green, userData->blue );
	
	glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );
	//glDrawElements ( GL_TRIANGLE_STRIP, 6, GL_UNSIGNED_SHORT, indices );
}
Exemple #2
0
static inline void mvp_matrix(mat4x4 model_view_projection_matrix, Params params, mat4x4 view_projection_matrix)
{

    mat4x4 model_matrix;
    mat4x4_identity(model_matrix);

    mat4x4 id;
    mat4x4_identity(id);


    mat4x4_translate(model_matrix, -params.anchor.x, -params.anchor.y, params.anchor.z);

    mat4x4 scaled;
    mat4x4_identity(scaled);
    mat4x4_scale_aniso(scaled, scaled, params.scale.x, -params.scale.y, params.scale.z);


    mat4x4 tmp;
    mat4x4_dup(tmp, model_matrix);

    mat4x4_mul(model_matrix, scaled, tmp);





    mat4x4 rotate;
    mat4x4_dup(rotate, id);
    mat4x4_rotate_Z2(rotate, id, deg_to_radf(-params.rotation));


    mat4x4_dup(tmp, model_matrix);

    mat4x4_mul(model_matrix, rotate, tmp);

    mat4x4_translate_independed(model_matrix, params.position.x, -params.position.y, params.position.z);



    mat4x4 model_matrix3;
    mat4x4_identity(model_matrix3);



    mat4x4 mm;

    mat4x4_mul(mm, model_matrix3, view_projection_matrix);

    mat4x4_mul(model_view_projection_matrix, mm, model_matrix);

    mat4x4_translate_independed(model_view_projection_matrix, 0, -y_offset/view_projection_matrix[3][3], 0);
}
void lndr_gen_jet_mv_matrix(Lander *lander)
{
    if(lander->jetState == JS_OFF)
        return;

    float scale = 0;
    if(lander->jetState == JS_INCREASING || lander->jetState == JS_DECREASING)
        scale = lander->jetFrames / FLAME_GROW_RATE;
    else if(lander->jetState == JS_ON)
    {
        if(lander->jetFrames < 10)
            return;

        lander->jetFrames = 0;

        int rnd = rand() % 6 - 3;
        scale = 1.0 + (0.07 * rnd);
    }

    mat4x4 ident, temp;
    mat4x4_identity(ident);

    mat4x4_translate_in_place(ident, 0, -0.3529412, 0);
    mat4x4_scale_aniso(temp, ident, 1.0, scale, 1.0);
    mat4x4_translate_in_place(temp, 0, 0.3529412, 0);

    mat4x4_transpose(lander->jetMatrix, temp);
}
Exemple #4
0
void Camera_view_projection(Camera* const camera, mat4x4 matrix) {

    mat4x4 position_matrix, orientation_matrix, scale_matrix;

    mat4x4_translate(
        position_matrix,
        -camera->transform.position[0],
        -camera->transform.position[1],
        -camera->transform.position[2]
    );

    quat q;
    quat_conj(q, camera->transform.orientation);
    mat4x4_from_quat(orientation_matrix, q);

    vec3 v = {
        1.0f / camera->transform.scale[0],
        1.0f / camera->transform.scale[1],
        1.0f / camera->transform.scale[2]
    };
    mat4x4 m;
    mat4x4_identity(m);
    mat4x4_scale_aniso(scale_matrix, m, v[0], v[1], v[2]);

    mat4x4_mul(matrix, scale_matrix, orientation_matrix);
    mat4x4_mul(matrix, matrix, position_matrix);

    mat4x4_mul(matrix, camera->projection, matrix);
}
void lndr_gen_mv_matrix(Lander *lander)
{
    mat4x4 ident, temp, trans;
    mat4x4_identity(ident);

    /*
    float glx = 2 / glob_game.ppw;
    float gly = 2 / glob_game.pph;
    */

    /*
    glx *= lander->x;
    gly *= lander->y;

    glx--;
    gly--;
    */

    float glx = lander->x;
    float gly = lander->y;

    mat4x4_scale_aniso(temp, ident, DEFAULT_SCALE, DEFAULT_SCALE, DEFAULT_SCALE);
    
    mat4x4_rotate_Z(temp, temp, lander->rotation);
    mat4x4_translate(trans, glx, gly, 0);
    mat4x4_mul(ident, trans, temp);
    mat4x4_transpose(lander->mvMatrix, ident);
}
Exemple #6
0
static void
recalculate_matrices(struct ball *b)
{
    mat4x4_identity(b->model_matrix);
    mat4x4_translate_in_place(b->model_matrix, b->x, b->y, b->z);
    mat4x4_scale_aniso(b->model_matrix, b->model_matrix, SCALE_FACTOR, SCALE_FACTOR, SCALE_FACTOR);

    mat4x4_invert(b->normal_matrix, b->model_matrix);
    mat4x4_transpose(b->normal_matrix, b->normal_matrix);

}
Exemple #7
0
static void wolf_frame(struct point2 *position, struct vector2 *direction, float angle)
{
	glClearColor(0,0,0,0); 
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 

	mat4x4 project;
	mat4x4_identity(project);
	mat4x4_perspective(project, degrees_to_radians(FOV), 640.0/480.0, 0.1f, 64.0f);

	mat4x4_identity(model_view);
	vec3 eye;
	eye[0] = position->x;
	eye[1] = 0.3;
	eye[2] = position->y;
	vec3 center;
	center[0] = position->x+direction->x;
	center[1] = 0.3;
	center[2] = position->y+direction->y;
	vec3 up;
	up[0] = 0.0;
	up[1] = 1.0;
	up[2] = 0.0;
	mat4x4_look_at(model_view, eye, center, up);

	glMatrixMode(GL_PROJECTION);
	glLoadMatrixf((const GLfloat*) project);

	glMatrixMode(GL_MODELVIEW);
	glLoadMatrixf((const GLfloat*) model_view);

	glEnable(GL_DEPTH_TEST);

	wolf_draw_floor();

	wolf_raycast(position, angle, wolf_draw_wall);
}
Exemple #8
0
void mat4x4_translate_independed(mat4x4 m, float x, float y, float z)
{
    mat4x4 tr;
    mat4x4_identity(tr);

    mat4x4_translate_in_place(tr, x, y, z);


    //mat4x4 model_matrix2_tr;
    //mat4x4_mul(model_matrix2_tr, tr, m);


    mat4x4 m_dup;
    mat4x4_dup(m_dup, m);
    mat4x4_mul(m, tr, m_dup );
}
Primitive* createPrimitive(unsigned int primitiveCount)
{
	Primitive *tmp;
	int i;
	unsigned int len = sizeof(*tmp)*primitiveCount;
	
	assert(primitiveCount > 0);
	
	tmp = malloc(len);
	memset(tmp, 0, len);
	
	for (i = 0; i < primitiveCount; ++i)
		mat4x4_identity(tmp[i].transf);
	
	return tmp;
}
Exemple #10
0
Fichier : rtt.c Projet : needs/anko
void rtt_draw()
{
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, rtt_tex);
	
	mat4x4 id;
	mat4x4_identity(id);
	
	glUseProgram(fx);
	glUniform1i(glGetUniformLocation(fx, "tex"), 0);
	glUniform1f(glGetUniformLocation(fx, "time"), glfwGetTime());
	glUniform1i(glGetUniformLocation(fx, "effect"), rtt_effect);
	glBindVertexArray(rtt_vao);
	glBindBuffer(GL_ARRAY_BUFFER, rtt_vbo);
	glDrawArrays(GL_QUADS, 0, 4);
}
Exemple #11
0
void cubeUpdate(CubeRenderPacket* cube, vec3 rot, float angle, float x, float y, float z, float* view, float* proj)
{
    static mat4x4 temp;
    memcpy(cube->transforms.view, view, 16 * sizeof(float));
    memcpy(cube->transforms.proj, proj, 16 * sizeof(float));

    vec3_norm(rot, rot);

    mat4x4_identity(cube->transforms.world);
    mat4x4_translate_in_place(cube->transforms.world, x, y, z);
    mat4x4_rotate(cube->transforms.world, cube->transforms.world, rot[0], rot[1], rot[2], angle);

    mat4x4_mul(temp, cube->transforms.view, cube->transforms.world);
    mat4x4_mul(cube->transforms.proj_view_world, cube->transforms.proj, temp);
    gfxBindUniformBuffer(cube->ubo, &cube->transforms, sizeof(struct Transforms), 0);
}
Exemple #12
0
int main(void) {
    // set error callback
    glfwSetErrorCallback(error_callback);

    // init environment
    glfwInit();

    // check GLFW and GLES version
    // glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

    // create window context
    GLFWwindow *window = glfwCreateWindow(640, 480, "untitled_1", NULL, NULL);
    glfwSetFramebufferSizeCallback(window, reshape_callback);
    glfwMakeContextCurrent(window);
    glfwSwapInterval(1);

    // load dynamic
    gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);

    // show current OpenGL variables
    printf("GL_VERSION  : %s\n", glGetString(GL_VERSION));
    printf("GL_RENDERER : %s\n", glGetString(GL_RENDERER));

    // shader
    GLuint vertex_shader = create_shader(GL_VERTEX_SHADER, vertex_shader_text);
    GLuint fragment_shader = create_shader(GL_FRAGMENT_SHADER, fragment_shader_text);
    GLuint program = link_program(vertex_shader, fragment_shader);

    // load data from application to gpu
    GLuint vertex_buffer;
    glGenBuffers(1, &vertex_buffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    GLint mvp_location = glGetUniformLocation(program, "MVP");
    GLint vpos_location = glGetAttribLocation(program, "vPosition");
    GLint vcol_location = glGetAttribLocation(program, "vColor");

    glEnableVertexAttribArray((GLuint) vpos_location);
    glVertexAttribPointer((GLuint) vpos_location, 2, GL_FLOAT, GL_FALSE,
                          sizeof(float) * 5, (void *) 0);
    glEnableVertexAttribArray((GLuint) vcol_location);
    glVertexAttribPointer((GLuint) vcol_location, 3, GL_FLOAT, GL_FALSE,
                          sizeof(float) * 5, (void *) (sizeof(float) * 2));

    //
    uint32_t counter = 0;
    float angle = 0;

    // main loop, keep running
    while (!glfwWindowShouldClose(window)) {
        int width, height;
        glfwGetFramebufferSize(window, &width, &height);
        glViewport(0, 0, width, height);
        glClear(GL_COLOR_BUFFER_BIT);

        float ratio = (float) width / height;

        //
        mat4x4 m, p, mvp;
        mat4x4_identity(m);
        mat4x4_rotate_Z(m, m, angle);  // 构建旋转矩阵

        counter += 1;
        if (counter < 100) {
            angle += 0.01;
        } else if ((counter < 200)) {
            angle -= 0.02;
        } else {
            counter = 0;
        }

        //
        mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 1.f, -1.f);  // 处理缩放
        mat4x4_mul(mvp, p, m);  // 组合旋转+缩放

        // apply shader
        glUseProgram(program);

        // Specify the value of a uniform variable for the current program object
        glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat *) mvp);

        // draw
        glDrawArrays(GL_TRIANGLES, 0, 3);

        // window has two buffers, one for display, another for render
        // after render, we need to swap them to display
        glfwSwapBuffers(window);

        // process events
        glfwPollEvents();
    }

    // destroy
    glfwDestroyWindow(window);
    glfwTerminate();

    return 0;
}
void initFace(Faces *face)
{
	assert(NULL != face);
	memset(face, 0, sizeof(*face));
	mat4x4_identity(face->transf);
}
Exemple #14
0
///
// Draw triangles using the shader pair created in Init()
//
void gl_image_draw(GL_STATE_T *p_state)
{
	ImageInstanceData *userData = p_state->user_data;
	GLShapeInstanceData *shapeData = &userData->shape;
	GLImageDisplayData *displayData = p_state->imageDisplayData;
	
	mat4x4 projection;
	mat4x4 modelView;
	mat4x4 projection_scaled;
	mat4x4 translation;
	mat4x4 projection_final;
	
	GLfloat vVertices[] = { 0.0f, 0.0f, 0.0f,  // Position 0
		0.0f,  0.0f,        // TexCoord 0
		0.0f,  1.0f, 0.0f,  // Position 1
		0.0f,  1.0f,        // TexCoord 1
		1.0f,  1.0f, 0.0f,  // Position 2
		1.0f,  1.0f,        // TexCoord 2
		1.0f,  0.0f, 0.0f,  // Position 3
		1.0f,  0.0f         // TexCoord 3
	};
	
	GLfloat texCoords[8];
	TexCoordsForRotation(shapeData->orientation, texCoords);
	vVertices[3] = texCoords[0];
	vVertices[4] = texCoords[1];
	vVertices[8] = texCoords[2];
	vVertices[9] = texCoords[3];
	vVertices[13] = texCoords[4];
	vVertices[14] = texCoords[5];
	vVertices[18] = texCoords[6];
	vVertices[19] = texCoords[7];
	
	GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
	//GLushort indices[] = {1, 0, 3, 0, 2, 0, 1 };
	
	// Use the program object
	glUseProgram ( displayData->programObject );
	
	// Load the vertex position
	glVertexAttribPointer ( displayData->positionLoc, 3, GL_FLOAT,
			       GL_FALSE, 5 * sizeof(GLfloat), vVertices );
	// Load the texture coordinate
	glVertexAttribPointer ( displayData->texCoordLoc, 2, GL_FLOAT,
			       GL_FALSE, 5 * sizeof(GLfloat), &vVertices[3] );
	
	glEnableVertexAttribArray ( displayData->positionLoc );
	glEnableVertexAttribArray ( displayData->texCoordLoc );
	
	// Bind the texture
	glActiveTexture ( GL_TEXTURE0 );
	glBindTexture ( GL_TEXTURE_2D, userData->textureId );
	
	// Set the sampler texture unit to 0
	glUniform1i ( displayData->samplerLoc, 0 );
	
	mat4x4_identity(projection);
	mat4x4_scale_aniso(projection_scaled, projection,
			   2.0/p_state->width,
			   -2.0/p_state->height,
			   1.0);
	mat4x4_translate(translation, -1, 1, 0);
	mat4x4_mul(projection_final, translation, projection_scaled);
	
	mat4x4_translate(translation, shapeData->objectX, shapeData->objectY, 0.0);
	mat4x4_identity(projection);
	mat4x4_scale_aniso(projection_scaled, projection,
			   shapeData->objectWidth,
			   shapeData->objectHeight,
			   1.0);
	mat4x4_mul(modelView, translation, projection_scaled);
	
	glUniformMatrix4fv ( displayData->projectionLoc, 1, GL_FALSE, (GLfloat *)projection_final);
	glUniformMatrix4fv ( displayData->modelViewLoc,  1, GL_FALSE, (GLfloat *)modelView);
	
	glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );
	//glDrawElements ( GL_TRIANGLE_STRIP, 6, GL_UNSIGNED_SHORT, indices );
}
Exemple #15
0
int main(int argc, char** argv)
{
    int count = 0;
    double base;
    GLFWwindow* window;

    srand((unsigned int) time(NULL));

    glfwSetErrorCallback(error_callback);

    if (!glfwInit())
        exit(EXIT_FAILURE);

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

    for (;;)
    {
        int width, height;
        GLFWmonitor* monitor = NULL;
        GLuint vertex_shader, fragment_shader, program, vertex_buffer;
        GLint mvp_location, vpos_location;

        if (count & 1)
        {
            int monitorCount;
            GLFWmonitor** monitors = glfwGetMonitors(&monitorCount);
            monitor = monitors[rand() % monitorCount];
        }

        if (monitor)
        {
            const GLFWvidmode* mode = glfwGetVideoMode(monitor);
            width = mode->width;
            height = mode->height;
        }
        else
        {
            width = 640;
            height = 480;
        }

        base = glfwGetTime();

        window = glfwCreateWindow(width, height, "Window Re-opener", monitor, NULL);
        if (!window)
        {
            glfwTerminate();
            exit(EXIT_FAILURE);
        }

        if (monitor)
        {
            printf("Opening full screen window on monitor %s took %0.3f seconds\n",
                   glfwGetMonitorName(monitor),
                   glfwGetTime() - base);
        }
        else
        {
            printf("Opening regular window took %0.3f seconds\n",
                   glfwGetTime() - base);
        }

        glfwSetWindowCloseCallback(window, window_close_callback);
        glfwSetKeyCallback(window, key_callback);

        glfwMakeContextCurrent(window);
        gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
        glfwSwapInterval(1);

        vertex_shader = glCreateShader(GL_VERTEX_SHADER);
        glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
        glCompileShader(vertex_shader);

        fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
        glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
        glCompileShader(fragment_shader);

        program = glCreateProgram();
        glAttachShader(program, vertex_shader);
        glAttachShader(program, fragment_shader);
        glLinkProgram(program);

        mvp_location = glGetUniformLocation(program, "MVP");
        vpos_location = glGetAttribLocation(program, "vPos");

        glGenBuffers(1, &vertex_buffer);
        glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
        glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

        glEnableVertexAttribArray(vpos_location);
        glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
                              sizeof(vertices[0]), (void*) 0);

        glfwSetTime(0.0);

        while (glfwGetTime() < 5.0)
        {
            float ratio;
            int width, height;
            mat4x4 m, p, mvp;

            glfwGetFramebufferSize(window, &width, &height);
            ratio = width / (float) height;

            glViewport(0, 0, width, height);
            glClear(GL_COLOR_BUFFER_BIT);

            mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 0.f, 1.f);

            mat4x4_identity(m);
            mat4x4_rotate_Z(m, m, (float) glfwGetTime());
            mat4x4_mul(mvp, p, m);

            glUseProgram(program);
            glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
            glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

            glfwSwapBuffers(window);
            glfwPollEvents();

            if (glfwWindowShouldClose(window))
            {
                close_window(window);
                printf("User closed window\n");

                glfwTerminate();
                exit(EXIT_SUCCESS);
            }
        }

        printf("Closing window\n");
        close_window(window);

        count++;
    }

    glfwTerminate();
}
Exemple #16
0
int main(void)
{
    glfwSetErrorCallback(error_callback);

    if (!glfwInit())
        exit(EXIT_FAILURE);

    GLFWmonitor *monitor = glfwGetPrimaryMonitor();
    const GLFWvidmode *mode = glfwGetVideoMode(monitor);
    printf("monitor mode: %d, %d\n", mode->width, mode->height);

    // if DEBUG {
    //     glfw.WindowHint(glfw.OpenGLDebugContext, gl.TRUE)
    // }

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, 1);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow *window = glfwCreateWindow(mode->width, mode->height, "Hialin", NULL, NULL);
    if (!window) {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }

    glfwSetKeyCallback(window, key_callback);

    glfwMakeContextCurrent(window);
    glfwSwapInterval(0);

    p_glBindFragDataLocation = (glBindFragDataLocation_t)glfwGetProcAddress("glBindFragDataLocation");
    if (!p_glBindFragDataLocation) {
        printf("\n failed glBindFragDataLocation");
        glfwTerminate();
        exit(EXIT_FAILURE);
    }
    p_glGenVertexArrays = (glGenVertexArrays_t)glfwGetProcAddress("glGenVertexArrays");
    if (!p_glGenVertexArrays) {
        printf("\n failed glGenVertexArrays");
        glfwTerminate();
        exit(EXIT_FAILURE);
    }
    p_glBindVertexArray = (glBindVertexArray_t)glfwGetProcAddress("glBindVertexArray");
    if (!p_glBindVertexArray) {
        printf("\n failed glBindVertexArray");
        glfwTerminate();
        exit(EXIT_FAILURE);
    }


    GLuint vsh = glCreateShader(GL_VERTEX_SHADER);
    {
        glShaderSource(vsh, 1, &vertex_shader_text, NULL);
        glCompileShader(vsh);
        int result;
        glGetShaderiv(vsh, GL_COMPILE_STATUS, &result );
        if (result == GL_FALSE) {
            int logLength;
            glGetShaderiv(vsh, GL_INFO_LOG_LENGTH, &logLength);
            char log[10*1024] = {0};
            glGetShaderInfoLog(vsh, logLength, NULL, log);
            printf("\nvertex shader compile: %s", log);
            glfwTerminate();
            exit(EXIT_FAILURE);
        }
    }
    glCheck();

    GLuint fsh = glCreateShader(GL_FRAGMENT_SHADER);
    {
        glShaderSource(fsh, 1, &fragment_shader_text, NULL);
        glCompileShader(fsh);
        int result;
        glGetShaderiv(fsh, GL_COMPILE_STATUS, &result);
        if (result == GL_FALSE) {
            int logLength;
            glGetShaderiv(fsh, GL_INFO_LOG_LENGTH, &logLength);
            char log[10*1024] = {0};
            glGetShaderInfoLog(fsh, logLength, NULL, log);
            printf("\nfragment shader compile: %s", log);
            glfwTerminate();
            exit(EXIT_FAILURE);
        }
    }
    glCheck();

    GLuint program = glCreateProgram();
    {
        glAttachShader(program, vsh);
        glAttachShader(program, fsh);
        glLinkProgram(program);
        int result;
        glGetProgramiv(program, GL_LINK_STATUS, &result);
        if (result == GL_FALSE) {
            int logLength;
            glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength);
            char log[10*1024] = {0};
            glGetProgramInfoLog(program, logLength, NULL, log);
            printf("\nprogram link: \n%s", log);
            glfwTerminate();
            exit(EXIT_FAILURE);
        }
    }
    glCheck();
    glUseProgram(program);
    glCheck();

    GLint projectionU = glGetUniformLocation(program, "projection");

    mat4x4 camera;
    vec3 eye = {3, 3, 3}, center = {0, 0, 0}, up = {0, 1, 0};
    mat4x4_look_at(camera, eye, center, up);
    GLint cameraU = glGetUniformLocation(program, "camera");
    glUniformMatrix4fv(cameraU, 1, GL_FALSE, (const float*)camera);
    glCheck();

    mat4x4 model;
    mat4x4_identity(model);
    GLint modelU = glGetUniformLocation(program, "model");
    glUniformMatrix4fv(modelU, 1, GL_FALSE, (const float*)model);
    glCheck();

    GLint texU = glGetUniformLocation(program, "tex");
    glUniform1i(texU, 0);
    p_glBindFragDataLocation(program, 0, "outputColor");
    glCheck();

    // Load the texture
    // char *texturePath = "./Resources/code.png"
    // GLuint texture = MakeTexture(0, texturePath);
    GLuint texture = MakeTexture(0);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture);
    glCheck();

    // Configure the vertex data
    GLuint vao;
    p_glGenVertexArrays(1, &vao);
    p_glBindVertexArray(vao);
    glCheck();

    GLuint vbo;
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices), cube_vertices, GL_STATIC_DRAW);
    glCheck();

    GLuint val = glGetAttribLocation(program, "vert");
    glEnableVertexAttribArray(val);
    glVertexAttribPointer(val, 3, GL_FLOAT, GL_FALSE, 5*4, (const void *)(0*4));
    glCheck();

    GLuint valTC = glGetAttribLocation(program, "vertTexCoord");
    glEnableVertexAttribArray(valTC);
    glVertexAttribPointer(valTC, 2, GL_FLOAT, GL_FALSE, 5*4, (const void *)(3*4));
    glCheck();

    // Configure global settings
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glCheck();

    long time0 = tick();

    float angle = 0.01;

    int width = 0, height = 0;
    int i = 0;
    while (!glfwWindowShouldClose(window))
    {
        int w, h;
        glfwGetFramebufferSize(window, &w, &h);
        if (w != width || h != height) {
            width = w;
            height = h;
            glViewport(0, 0, width, height);
            printf("buffer size: %d %d\n", w, h);
        }
        float ratio = width/(float)height;

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        mat4x4_rotate_Y(model, model, angle);

        mat4x4 projection;
        mat4x4_perspective(projection, 0.785, ratio, 0.1, 10.0); // 45 degrees == 0.785 radians

        glUniformMatrix4fv(projectionU, 1, GL_FALSE, (const float*)projection);
        glUniformMatrix4fv(modelU, 1, GL_FALSE, (const float*)model);

        for (int i = 0; i < 1*1000; i++) {
            glDrawArrays(GL_TRIANGLES, 0, 6*2*3); // 12 triangles
        }

        i++;
        if (i == 100) {
            printf("time for 100 frames: %ld\n", tick()-time0);
        } else if (i == 1000) {
            printf("time for 1000 frames: %ld\n", tick()-time0);
            break;
        }

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwDestroyWindow(window);

    glfwTerminate();
    exit(EXIT_SUCCESS);
}
Exemple #17
0
int main(void)
{
    GLFWwindow* window;
    GLuint vertex_buffer, vertex_shader, fragment_shader, program;
    GLint mvp_location, vpos_location, vcol_location;

    glfwSetErrorCallback(error_callback);

    if (!glfwInit())
        exit(EXIT_FAILURE);

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

    window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }

    glfwSetKeyCallback(window, key_callback);

    glfwMakeContextCurrent(window);
    gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
    glfwSwapInterval(1);

    // NOTE: OpenGL error checks have been omitted for brevity

    glGenBuffers(1, &vertex_buffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    vertex_shader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
    glCompileShader(vertex_shader);

    fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
    glCompileShader(fragment_shader);

    program = glCreateProgram();
    glAttachShader(program, vertex_shader);
    glAttachShader(program, fragment_shader);
    glLinkProgram(program);

    mvp_location = glGetUniformLocation(program, "MVP");
    vpos_location = glGetAttribLocation(program, "vPos");
    vcol_location = glGetAttribLocation(program, "vCol");

    glEnableVertexAttribArray(vpos_location);
    glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
                          sizeof(float) * 5, (void*) 0);
    glEnableVertexAttribArray(vcol_location);
    glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE,
                          sizeof(float) * 5, (void*) (sizeof(float) * 2));

    while (!glfwWindowShouldClose(window))
    {
        float ratio;
        int width, height;
        mat4x4 m, p, mvp;

        glfwGetFramebufferSize(window, &width, &height);
        ratio = width / (float) height;

        glViewport(0, 0, width, height);
        glClear(GL_COLOR_BUFFER_BIT);

        mat4x4_identity(m);
        mat4x4_rotate_Z(m, m, (float) glfwGetTime());
        mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 1.f, -1.f);
        mat4x4_mul(mvp, p, m);

        glUseProgram(program);
        glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
        glDrawArrays(GL_TRIANGLES, 0, 3);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwDestroyWindow(window);

    glfwTerminate();
    exit(EXIT_SUCCESS);
}
Exemple #18
0
int main(void)
{
  Primative *edits[] = {
    primative_create(CUBE, ADDITIVE),
    primative_create(SPHERE, ADDITIVE),
    primative_create(SPHERE, SUBTRACTIVE),
    primative_create(SPHERE, ADDITIVE)
  };

  primative_scale(edits[0], 0.4, 1.0, 1.5);
  primative_translate(edits[1], 0.2, 0, 0);
  primative_translate(edits[2], -0.3, 0, 0);
  primative_translate(edits[3], -0.5, 0, 0);
  primative_scale(edits[3], 0.5, 0.5, 0.5);
  edits[1]->blend = 0.2;

  sdf_args.edits = edits;
  sdf_args.num_edits = 4;

  vec3 origin = {0, 0, 0};
  vec3 halfDim = {1, 1, 1};
  OctTree *tree = octTree_create(origin, halfDim);

  octTree_populate(tree, distance);

  int num_verticies = octTree_count(tree);
  size_t vert_size = sizeof(Point);
  fprintf(stdout, "num verts %d\n", num_verticies);

  int vi = 0;
  Point *verticies = malloc(num_verticies * vert_size);
  octTree_get_points(tree, verticies, &vi);

  GLFWwindow* window;

  mat4x4 ident;
  mat4x4_identity(ident);

  glfwSetErrorCallback(error_callback);

  if (!glfwInit())
    exit(EXIT_FAILURE);

  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

  window = glfwCreateWindow(640, 480, "speed dream", NULL, NULL);
  if (!window)
  {
    glfwTerminate();
    exit(EXIT_FAILURE);
  }

  glfwMakeContextCurrent(window);
  glfwSwapInterval(1);

  glEnable(GL_PROGRAM_POINT_SIZE);
  glEnable(GL_DEPTH_TEST);
  glDepthFunc(GL_LESS);

  glfwSetKeyCallback(window, key_callback);

  mat4x4 proj;
  mat4x4 view;
  mat4x4 matrix;

  mat4x4_identity(view);
  mat4x4_identity(matrix);

  vec3_set(camera.pos, 0.0f, 0.0f, 0.0f);

  // Create Vertex Array Object
  GLuint vao;
  glGenVertexArrays(1, &vao);
  glBindVertexArray(vao);

  // Create a Vertex Buffer Object and copy the vertex data to it
  GLuint vbo;
  glGenBuffers(1, &vbo);

  glBindBuffer(GL_ARRAY_BUFFER, vbo);
  glBufferData(GL_ARRAY_BUFFER, num_verticies * vert_size, verticies, GL_STATIC_DRAW);

  // Create and compile the shaders
  GLuint vertexShader = loadShader(GL_VERTEX_SHADER, DREAM_VERT);
  GLuint fragmentShader = loadShader(GL_FRAGMENT_SHADER, DREAM_FRAG);

  // Link the vertex and fragment shader into a shader program
  GLuint shaderProgram = glCreateProgram();
  glAttachShader(shaderProgram, vertexShader);
  glAttachShader(shaderProgram, fragmentShader);
  glBindFragDataLocation(shaderProgram, 0, "outColor");
  glLinkProgram(shaderProgram);
  glUseProgram(shaderProgram);

  // Specify the layout of the vertex data
  GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
  glEnableVertexAttribArray(posAttrib);
  glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, vert_size, 0);

  GLint normAttrib = glGetAttribLocation(shaderProgram, "normal");
  glEnableVertexAttribArray(normAttrib);
  glVertexAttribPointer(normAttrib, 3, GL_FLOAT, GL_FALSE, vert_size, (void*)(3 * sizeof(GLfloat)));

  GLint colorAttrib = glGetAttribLocation(shaderProgram, "color");
  glEnableVertexAttribArray(colorAttrib);
  glVertexAttribPointer(colorAttrib, 3, GL_FLOAT, GL_FALSE, vert_size, (void*)(6 * sizeof(GLfloat)));

  GLint uniModel = glGetUniformLocation(shaderProgram, "model");
  GLint uniView = glGetUniformLocation(shaderProgram, "view");
  GLint uniProj = glGetUniformLocation(shaderProgram, "proj");

  vec3 lightPos = {3.f, 0.f, 3.f};
  GLint uniLightPos = glGetUniformLocation(shaderProgram, "lightPos");
  glUniform3fv(uniLightPos, 1, (const GLfloat *)&lightPos);

  while (!glfwWindowShouldClose(window))
  {
    float ratio;
    int width, height;

    glfwGetFramebufferSize(window, &width, &height);
    ratio = width / (float) height;

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

    vec4 movement = {0, 0, 0, 1};
    if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS)
      movement[0] -= 0.1;
    if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS)
      movement[0] += 0.1;
    if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS)
      movement[2] += 0.1;
    if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS)
      movement[2] -= 0.1;

    if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
      camera.pitch -= 0.05;
    if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
      camera.pitch += 0.05;
    if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
      camera.yaw += 0.05;
    if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
      camera.yaw -= 0.05;

    mat4x4 pitch_mat;
    mat4x4_identity(pitch_mat);
    mat4x4_rotate_X(pitch_mat, pitch_mat, camera.pitch);

    mat4x4 yaw_mat;
    mat4x4_identity(yaw_mat);
    mat4x4_rotate_Y(yaw_mat, yaw_mat, camera.yaw);

    mat4x4 inv_yaw_mat;
    mat4x4_invert(inv_yaw_mat, yaw_mat);

    vec4 rotated_movement;
    mat4x4_mul_vec4(rotated_movement, inv_yaw_mat, movement);
    camera.pos[0] += rotated_movement[0];
    camera.pos[1] += rotated_movement[1];
    camera.pos[2] += rotated_movement[2];

    mat4x4 translation_mat;
    mat4x4_translate(translation_mat, camera.pos[0], camera.pos[1], camera.pos[2]);

    mat4x4_mul(view, pitch_mat, yaw_mat);
    mat4x4_mul(view, view, translation_mat);
    glUniformMatrix4fv(uniView, 1, GL_FALSE, (const GLfloat *)&view);

    mat4x4_perspective(proj, 60 * M_PI / 180, ratio, 0.1f, 10.0f);
    glUniformMatrix4fv(uniProj, 1, GL_FALSE, (const GLfloat *)&proj);

    glUniformMatrix4fv(uniModel, 1, GL_FALSE, (const GLfloat *)&matrix);

    glDrawArrays(GL_POINTS, 0, num_verticies);

    glfwSwapBuffers(window);
    glfwPollEvents();
  }

  glDeleteProgram(shaderProgram);
  glDeleteShader(fragmentShader);
  glDeleteShader(vertexShader);

  glDeleteBuffers(1, &vbo);
  glDeleteVertexArrays(1, &vao);

  glfwDestroyWindow(window);

  free(verticies);

  glfwTerminate();
  exit(EXIT_SUCCESS);
}