Пример #1
0
void setupCameraAlter(ShaderProgram * sp, const Camera * camera)
{
    /* Pointers */
    GLint rotP, viewPosP;

    /* Matrices */
    mat4 rotMatrix;

    setMatrixFromQuaternion(rotMatrix, camera->q);
    transposeMatrix(rotMatrix);

    glUseProgram(sp->p);

    rotP = glGetUniformLocation(sp->p, "transform.rot");
    /* TODO: if (rotP == -1) {} */
    glUniformMatrix4fv(rotP, 1, GL_TRUE, rotMatrix);

    viewPosP = glGetUniformLocation(sp->p, "transform.viewPosition");
    /* TODO: if (viewPosP == -1) {} */
    glUniform3fv(viewPosP, 1, camera->pos);

#ifdef DEBUG
    validateShaderProgram(sp->p);
#endif
    CHECK_OPENGL_ERRORS(__FILE__, __LINE__);
}
Пример #2
0
void checkOpenGLVersion()
{
    GLenum glewInitValue;

    /* For compatinility with OpenGL 3.2+ core context. */
    glewExperimental = GL_TRUE;

    glewInitValue = glewInit();

    /* Avoid GLEW errors, see LINKS: [10]. */
    glGetError();

    if (glewInitValue != GLEW_OK)
    {
        fprintf(stderr, "glewInit failed: %s\n.",
            glewGetErrorString(glewInitValue));
        exit(EXIT_FAILURE);
    }

    if (! glewIsSupported("GL_VERSION_3_3"))
    {
        fprintf(stderr, "No OpenGL 3.3 support. I'm sorry.\n");
        fprintf(stderr, "Your OpenGL version: %s.\n",
            glGetString(GL_VERSION));
        exit(EXIT_FAILURE);
    }

    CHECK_OPENGL_ERRORS(__FILE__, __LINE__);
}
Пример #3
0
void setupOpenGLContext(ContextSize * context)
{
    if (glfwInit() == GL_FALSE)
    {
        fprintf(stderr, "glfwInit failed.\n");
        exit(EXIT_FAILURE);
    }

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

    context->window = glfwCreateWindow(
        context->w, context->h,
        "Wave Simulation", /* window title */
        NULL,  /* NULL to use windowed mode */
        NULL); /* NULL to not share resources with other windows */
    if (context->window == GL_FALSE)
    {
        fprintf(stderr, "glfwOpenWindow failed.\n");
        glfwTerminate();
        exit(EXIT_FAILURE);
    }

    glfwMakeContextCurrent(context->window);

    CHECK_OPENGL_ERRORS(__FILE__, __LINE__);
}
Пример #4
0
Crosshair::Crosshair(int screenWidth, int screenHeight) {
	this->centerX = (float)screenWidth / 2;
	this->centerY = (float)screenHeight / 2;

	this->crosshair = glGenLists(1);
    CHECK_OPENGL_ERRORS(__LINE__);
    assert(this->crosshair != 0);
    glNewList(this->crosshair, GL_COMPILE);
	this->draw();
	glEndList();
}
Пример #5
0
void setupOpenGLState(ContextSize * context, GLboolean vsync)
{
    if (vsync)
    {
        glfwSwapInterval(1);
    }
    else
    {
        glfwSwapInterval(0);
    }

    glViewport(0, 0, context->w, context->h);
    glClearColor(0.0f, 0.20f, 0.40f, 1.0f);

#if 0
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);
#endif

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glDepthRange(0.0f, 1.0f);

    /* No interpolate 'flat' varying variables, i.e. normals. */
    glProvokingVertex(GL_FIRST_VERTEX_CONVENTION);

    /* Perform some rendering with near and far planes clipping off. */
    /* I use small znear value instead, that work better. */
    /* glEnable(GL_DEPTH_CLAMP); */

    /* For correct bilinear interpolation at rib of skybox. */
    /* TODO: glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);*/

#if 0
    glEnable(GL_POINT_SMOOTH);
    glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);

    glEnable(GL_LINE_SMOOTH);
    glLineWidth(1.0f);
    glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);

    glEnable(GL_POLYGON_SMOOTH);
    glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
#endif

    CHECK_OPENGL_ERRORS(__FILE__, __LINE__);
}
Пример #6
0
void setupCamera(ShaderProgram * sp, const Camera * camera)
{
    /* Pointers */
    GLint viewProjP, viewPosP;

    /* Matrices */
    mat4 viewTrMatrix, viewRotMatrix, viewMatrix;
    mat4 projMatrix, viewProjMatrix;

    setPerspectiveMatrix(projMatrix,
        camera->viewAngleY,
        camera->aspect,
        camera->znear,
        camera->zfar);

    setMatrixFromQuaternion(viewRotMatrix, camera->q);

    setTranslationMatrix(viewTrMatrix,
        -camera->pos[0],
        -camera->pos[1],
        -camera->pos[2]);

    setMulMatrix(viewMatrix, viewRotMatrix, viewTrMatrix);

    setMulMatrix(viewProjMatrix, projMatrix, viewMatrix);

    glUseProgram(sp->p);

    viewProjP = glGetUniformLocation(sp->p, "transform.viewProjection");
    /* TODO: if (viewProjP == -1) {} */
    glUniformMatrix4fv(viewProjP, 1, GL_TRUE, viewProjMatrix);

    viewPosP = glGetUniformLocation(sp->p, "transform.viewPosition");
    /* TODO: if (viewPosP == -1) {} */
    glUniform3fv(viewPosP, 1, camera->pos);

#ifdef DEBUG
    validateShaderProgram(sp->p);
#endif
    CHECK_OPENGL_ERRORS(__FILE__, __LINE__);
}
Пример #7
0
GLuint createTextureFromTga(const char * path)
{
    long length;
    char * buffer = getTextFileContent(path, &length);
    const TgaHeader * header = (TgaHeader *) buffer;

    GLint format;
    GLint internalFormat;
    GLuint texture;
    const GLvoid * imgData;

    if (buffer == NULL)
    {
        return 0;
    }

    if (length <= (signed long) sizeof(TgaHeader))
    {
         fprintf(stderr, "Too small TGA file: %s\n", path);
         free(buffer);
         return 0;
    }

    if (header->datatype != 2 ||
        (header->bitperpel != 24 && header->bitperpel != 32))
    {
         fprintf(stderr, "Wrong TGA file format: %s\n", path);
         free(buffer);
         return 0;
    }

    format = (header->bitperpel == 24 ? GL_BGR : GL_BGRA);
    internalFormat = (format == GL_BGR ? GL_RGB8 : GL_RGBA8);
    imgData = (const GLvoid *)
        (buffer + sizeof(TgaHeader) + header->idlength);

    glGenTextures(1, &texture);

    glBindTexture(GL_TEXTURE_2D, texture);

    /* Set 1-byte alignment (for non (2^n)x(2^n) size textures). */
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    glTexImage2D(GL_TEXTURE_2D, 0, internalFormat,
        header->width, header->height, 0, format,
        GL_UNSIGNED_BYTE, imgData);

    free(buffer);

#if 0
    /* Linear filtering */
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    /*glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);*/
#else
    glGenerateMipmap(GL_TEXTURE_2D);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
        GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
#endif

#if 0
    /* No wrap */
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
#else
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
#endif

    CHECK_OPENGL_ERRORS(__FILE__, __LINE__);

    return texture;
}