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.
c++ Perspective-correct.cpp
#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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment