Skip to content

Instantly share code, notes, and snippets.

@danicomas
Last active October 4, 2020 09:07
Show Gist options
  • Save danicomas/a1f5a0e6849b3ac8b51c169c2c030e37 to your computer and use it in GitHub Desktop.
Save danicomas/a1f5a0e6849b3ac8b51c169c2c030e37 to your computer and use it in GitHub Desktop.

Revisions

  1. danicomas revised this gist Oct 4, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions perspective-correct.cpp
    Original file line number Diff line number Diff line change
    @@ -176,9 +176,9 @@ void InitOpenGL()
    glEnable(GL_DEPTH_TEST);
    loadShaders();
    loadExternalTextures();
    //textureId = loadTexture("lego.bmp");
    //textureId = loadTexture("lego.bmp");

    //glBindTexture (GL_TEXTURE_2D, textureId);
    //glBindTexture (GL_TEXTURE_2D, textureId);

    glEnable(GL_NORMALIZE);
    glShadeModel(GL_SMOOTH);
  2. danicomas revised this gist Oct 4, 2020. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions perspective-correct.cpp
    Original file line number Diff line number Diff line change
    @@ -99,7 +99,7 @@ void loadExternalTextures()
    void loadShaders()
    {
    GLuint program = LoadShader("vertex.vs", "fragment.frag");
    glUseProgram(program);
    glUseProgram(program);
    }

    void display()
    @@ -174,8 +174,8 @@ void InitOpenGL()
    glMatrixMode(GL_MODELVIEW);

    glEnable(GL_DEPTH_TEST);
    loadShaders();
    loadExternalTextures();
    loadShaders();
    loadExternalTextures();
    //textureId = loadTexture("lego.bmp");

    //glBindTexture (GL_TEXTURE_2D, textureId);
  3. danicomas renamed this gist Oct 4, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. danicomas revised this gist Oct 4, 2020. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    #ifdef WIN32
    #include <windows.h>
    #endif
    @@ -206,4 +205,4 @@ int main (int argc, char **argv)

    ExitGlut();
    return 0;
    }
    }
  5. danicomas revised this gist Oct 4, 2020. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@

    #ifdef WIN32
    #include <windows.h>
    #endif
    @@ -205,4 +206,4 @@ int main (int argc, char **argv)

    ExitGlut();
    return 0;
    }
    }
  6. danicomas created this gist Oct 4, 2020.
    208 changes: 208 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,208 @@
    #ifdef WIN32
    #include <windows.h>
    #endif

    #include <stdlib.h>
    #include <iostream>
    #include <fstream>
    #include "GLShader.hpp"


    #ifdef __APPLE__
    #include <GLUT/glut.h>
    #include <OpenGL/gl.h>
    #include <OpenGL/glu.h>

    #else
    #include <GL/glut.h>
    #include <GL/glu.h>
    #include <GL/gl.h>
    #endif

    using namespace std;

    void menuCallback(int);
    void setCamera(void);
    void drawScene(void);

    static int win = 0;

    //static GLuint textureId = 0;
    static unsigned int texture[2];

    // Struct of bitmap file.
    struct BitMapFile
    {
    int sizeX;
    int sizeY;
    unsigned char *data;
    };

    // Routine to read a bitmap file.
    // Works only for uncompressed bmp files of 24-bit color.
    BitMapFile *getBMPData(string filename)
    {
    BitMapFile *bmp = new BitMapFile;
    unsigned int size, offset, headerSize;

    // Read input file name.
    ifstream infile(filename.c_str(), ios::binary);

    // Get the starting point of the image data.
    infile.seekg(10);
    infile.read((char *) &offset, 4);

    // Get the header size of the bitmap.
    infile.read((char *) &headerSize,4);

    // Get width and height values in the bitmap header.
    infile.seekg(18);
    infile.read( (char *) &bmp->sizeX, 4);
    infile.read( (char *) &bmp->sizeY, 4);

    // Allocate buffer for the image.
    size = bmp->sizeX * bmp->sizeY * 24;
    bmp->data = new unsigned char[size];

    // Read bitmap data.
    infile.seekg(offset);
    infile.read((char *) bmp->data , size);

    return bmp;
    }

    // Load external textures.
    void loadExternalTextures()
    {
    // Local storage for bmp image data.
    BitMapFile *image[1];

    // Load the texture.
    image[0] = getBMPData("lego.bmp");

    // Activate texture index texture[0].
    glBindTexture(GL_TEXTURE_2D, texture[0]);

    // Set texture parameters for wrapping.
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    // Set texture parameters for filtering.
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    // Specify an image as the texture to be bound with the currently active texture index.
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image[0]->sizeX, image[0]->sizeY, 0,
    GL_RGB, GL_UNSIGNED_BYTE, image[0]->data);
    }

    void loadShaders()
    {
    GLuint program = LoadShader("vertex.vs", "fragment.frag");
    glUseProgram(program);
    }

    void display()
    {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glPushMatrix();
    glTranslatef(0.0f, 10.0f, 0.0f);
    glPopMatrix();

    setCamera();

    drawScene();

    glutSwapBuffers();
    }


    void drawScene()
    {
    // Draw Trapezoid (Perspective-correct Problem)
    glPushMatrix();
    glBegin(GL_POLYGON);

    glTexCoord2f(1,0);
    glVertex3f(4, 0, -4);
    glTexCoord2f(1, 1);
    glVertex3f(2, 4, -4);
    glTexCoord2f(0, 1);
    glVertex3f(-2, 4, -4);
    glTexCoord2f(0,0);
    glVertex3f(-4, 0, -4);

    glEnd();
    glPopMatrix();

    }

    void idle()
    {
    glutPostRedisplay();
    }

    void setCamera()
    {
    glTranslatef(0.0f,-0.75f, -2.0f);
    glRotatef(0.0f, 0.0f, 1.0f, 0.0f);
    }

    void CreateGlutWindow()
    {
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
    glutInitWindowPosition (10, 10);
    glutInitWindowSize (512, 512);
    win = glutCreateWindow ("Comas, Daniel");
    }

    void CreateGlutCallbacks()
    {
    glutDisplayFunc(display);
    glutIdleFunc(idle);
    }

    void InitOpenGL()
    {
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90.0, 1.0, 0.1, 100);
    glMatrixMode(GL_MODELVIEW);

    glEnable(GL_DEPTH_TEST);
    loadShaders();
    loadExternalTextures();
    //textureId = loadTexture("lego.bmp");

    //glBindTexture (GL_TEXTURE_2D, textureId);

    glEnable(GL_NORMALIZE);
    glShadeModel(GL_SMOOTH);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

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

    void ExitGlut()
    {
    glutDestroyWindow(win);
    exit(0);
    }

    int main (int argc, char **argv)
    {
    glutInit(&argc, argv);
    CreateGlutWindow();
    CreateGlutCallbacks();
    InitOpenGL();

    glutMainLoop();

    ExitGlut();
    return 0;
    }