Skip to content

Instantly share code, notes, and snippets.

@Markonis
Created January 17, 2016 20:59
Show Gist options
  • Save Markonis/1b0c332c97ae62db746b to your computer and use it in GitHub Desktop.
Save Markonis/1b0c332c97ae62db746b to your computer and use it in GitHub Desktop.
#include "StdAfx.h"
#include "GLRenderer.h"
#include "GL\gl.h"
#include "GL\glu.h"
#include "GL\glaux.h"
#include "GL\glut.h"
#include "DImage.h"
#pragma comment(lib, "GL\\glaux.lib")
CGLRenderer::CGLRenderer(void)
{
texture = 0;
angle = 0;
}
CGLRenderer::~CGLRenderer(void)
{
}
bool CGLRenderer::CreateGLContext(CDC* pDC)
{
PIXELFORMATDESCRIPTOR pfd ;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 24;
pfd.iLayerType = PFD_MAIN_PLANE;
int nPixelFormat = ChoosePixelFormat(pDC->m_hDC, &pfd);
if (nPixelFormat == 0) return false;
BOOL bResult = SetPixelFormat (pDC->m_hDC, nPixelFormat, &pfd);
if (!bResult) return false;
m_hrc = wglCreateContext(pDC->m_hDC);
if (!m_hrc) return false;
return true;
}
void CGLRenderer::PrepareScene(CDC *pDC)
{
wglMakeCurrent(pDC->m_hDC, m_hrc);
//---------------------------------
glClearColor(1,1,1,1);
glEnable(GL_DEPTH_TEST);
PrepareLighting();
glPixelStorei(GL_PACK_ALIGNMENT, 4);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
LoadTexture("TSC0.bmp", &texture);
//---------------------------------
wglMakeCurrent(NULL, NULL);
}
void CGLRenderer::PrepareLighting(){
float light1_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
float light1_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
float light1_specular[] = { 1.0, 1.0, 1.0, 1.0 };
float light1_position[] = { 5, 5, 5 };
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light1_ambient);
glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE);
glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular);
glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);
}
void CGLRenderer::DrawScene(CDC *pDC)
{
wglMakeCurrent(pDC->m_hDC, m_hrc);
//---------------------------------
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
DrawScene();
SwapBuffers(pDC->m_hDC);
//---------------------------------
wglMakeCurrent(NULL, NULL);
}
void CGLRenderer::DrawScene()
{
glLoadIdentity();
glPushMatrix();
//---------------------------------
gluLookAt(4, 3, 4, 0, 0, 0, 0, 1, 0);
glRotatef(angle, 0, 1, 0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
DrawPatch(1, 8);
DrawPatch(3, 12);
DrawCylinder(2, 4);
glDisable(GL_TEXTURE_2D);
//---------------------------------
glPopMatrix();
glFlush();
}
void CGLRenderer::LoadTexture(char* fileName, unsigned int* tex)
{
if (*tex == 0)
{
// Alokacija ID-a i kreiranje teksture
glGenTextures(1, tex);
glBindTexture(GL_TEXTURE_2D, *tex);
// Postavljanje parametara teksture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Ucitavanje bitmape
AUX_RGBImageRec *TextureImage;
TextureImage = auxDIBImageLoad(CString(fileName));
// Kopiranje sadrzaja bitmape u teksture
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage->sizeX, TextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);
// Brisanje bitmape
if (TextureImage){
if (TextureImage->data){
free(TextureImage->data);
}
free(TextureImage);
}
}
}
void CGLRenderer::ParamToDec(double x, double y, double R, double* v){
double fi = atan(x);
double t = atan(y * cos(fi));
v[0] = R * cos(t) * sin(fi);
v[1] = R * sin(t);
v[2] = R * cos(t) * cos(fi);
}
void CGLRenderer::DrawPatch(double R, int n){
double x = -1.0, y = -1.0;
double step = 2.0 / n;
double v[3];
for (int i = 0; i < n; i++){
x = -1.0;
for (int j = 0; j < n; j++){
glBegin(GL_QUADS);
ParamToDec(x, y, R, v);
glNormal3d(v[0] / R, v[1] / R, v[2] / R);
glTexCoord2d((x + 1) / 2, (y + 1) / 2);
glVertex3dv(v);
ParamToDec(x + step, y, R, v);
glNormal3d(v[0] / R, v[1] / R, v[2] / R);
glTexCoord2d((x + step + 1) / 2, (y + 1) / 2);
glVertex3dv(v);
ParamToDec(x + step, y + step, R, v);
glNormal3d(v[0] / R, v[1] / R, v[2] / R);
glTexCoord2d((x + step + 1) / 2, (y + step + 1) / 2);
glVertex3dv(v);
ParamToDec(x, y + step, R, v);
glNormal3d(v[0] / R, v[1] / R, v[2] / R);
glTexCoord2d((x + 1) / 2, (y + step + 1) / 2);
glVertex3dv(v);
glEnd();
x += step;
}
y += step;
}
}
void CGLRenderer::CylinderPoint(double R, double y, double angle, double* v, double* n){
v[0] = R * cos(angle);
v[1] = y;
v[2] = R * sin(angle);
n[0] = cos(angle);
n[1] = 0;
n[2] = sin(angle);
Normalize(n);
}
void CGLRenderer::Normalize(double* v)
{
float d = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
v[0] /= d;
v[1] /= d;
v[2] /= d;
}
void CGLRenderer::CalcNormal(double v[3], double w[3], double res[3]){
res[0] = v[1] * w[2] - v[2] * w[1];
res[1] = v[2] * w[0] - v[0] * w[2];
res[2] = v[0] * w[1] - v[1] * w[0];
Normalize(res);
}
void CGLRenderer::DrawCylinder(double R, double h)
{
int n = 24;
double* vertex = new double[3 * n * 4];
double* normal = new double[3 * n * 4];
double* tex = new double[2 * n * 4];
double angle = 0;
double angleStep = 2.0 * PI / n;
double texStep = 1.0 / n;
for (int i = 0; i < n; i++)
{
CylinderPoint(R, -h / 2.0, angle, &vertex[12 * i], &normal[12 * i]);
tex[i * 8] = texStep * i; tex[i * 8 + 1] = 1;
CylinderPoint(R, -h / 2.0, angle + angleStep, &vertex[12 * i + 3], &normal[12 * i + 3]);
tex[i * 8 + 2] = texStep * (i + 1); tex[i * 8 + 3] = 1;
CylinderPoint(R, h / 2.0, angle + angleStep, &vertex[12 * i + 6], &normal[12 * i + 6]);
tex[i * 8 + 4] = texStep * (i + 1); tex[i * 8 + 5] = 0;
CylinderPoint(R, h / 2.0, angle, &vertex[12 * i + 9], &normal[12 * i + 9]);
tex[i * 8 + 6] = texStep * i; tex[i * 8 + 7] = 0;
angle += angleStep;
}
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
//glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_DOUBLE, 0, vertex);
glNormalPointer(GL_DOUBLE, 0, normal);
//glTexCoordPointer(2, GL_DOUBLE, 0, tex);
glDrawArrays(GL_QUADS, 0, 4 * n);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
//glDisableClientState(GL_TEXTURE_COORD_ARRAY);
delete vertex;
delete normal;
delete tex;
}
void CGLRenderer::Reshape(CDC *pDC, int w, int h)
{
wglMakeCurrent(pDC->m_hDC, m_hrc);
//---------------------------------
glViewport(0,0,(GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40, (double)w / (double)h, 0.1, 100);
glMatrixMode(GL_MODELVIEW);
//---------------------------------
wglMakeCurrent(NULL, NULL);
}
void CGLRenderer::DestroyScene(CDC *pDC)
{
wglMakeCurrent(pDC->m_hDC, m_hrc);
glDeleteTextures(1, &texture);
wglMakeCurrent(NULL,NULL);
if(m_hrc)
{
wglDeleteContext(m_hrc);
m_hrc = NULL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment