Пример #1
0
/*****************************************************************************
 * reshape()
 *****************************************************************************/
void reshape( GLFWwindow* window, int w, int h )
{
   mat4x4 projection, view;

   glViewport( 0, 0, (GLsizei)w, (GLsizei)h );

   glMatrixMode( GL_PROJECTION );
   mat4x4_perspective( projection,
                       2.f * (float) atan2( RADIUS, 200.f ),
                       (float)w / (float)h,
                       1.f, VIEW_SCENE_DIST );
   glLoadMatrixf((const GLfloat*) projection);

   glMatrixMode( GL_MODELVIEW );
   {
      vec3 eye = { 0.f, 0.f, VIEW_SCENE_DIST };
      vec3 center = { 0.f, 0.f, 0.f };
      vec3 up = { 0.f, -1.f, 0.f };
      mat4x4_look_at( view, eye, center, up );
   }
   glLoadMatrixf((const GLfloat*) view);
}
Пример #2
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);
}
Пример #3
0
static inline void _set_view(game *g) {
    mat4x4_look_at(g->d.view, g->d.eye, g->d.center, g->d.up);
}
Пример #4
0
static inline void _zoom_view(game *g) {
    if (!g->d.ortho) {
        mat4x4_look_at(g->d.view, g->d.eye_zoom, g->d.center, g->d.up);
    }
}
Пример #5
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);
}