Skip to content

Instantly share code, notes, and snippets.

@quinor
Last active August 29, 2015 14:03
Show Gist options
  • Save quinor/1d598c1d64bfa54b71c5 to your computer and use it in GitHub Desktop.
Save quinor/1d598c1d64bfa54b71c5 to your computer and use it in GitHub Desktop.
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
#include <cstdio>
#include <vector>
#include <array>
#include <cstring>
using namespace sf;
using namespace std;
vector<array<float, 3> > vertices;
vector<array<float, 3> > normals;
vector<array<int, 2> > triangles;
int main (int, char** argv)
{
vertices.push_back({0,0,0});
normals.push_back({0,0,0});
FILE * file = fopen(argv[1], "r");
char header[128];
float x, y, z;
int a, b, c, d, e, f;
while (fscanf(file, "%s ", header)!=EOF)
{
if (strcmp(header, "v")==0)
{
fscanf(file, "%f %f %f\n", &x, &y, &z);
vertices.push_back({x, y, z});
}
else if (strcmp(header, "vn")==0)
{
fscanf(file, "%f %f %f\n", &x, &y, &z);
normals.push_back({x, y, z});
}
else if (strcmp(header, "f")==0)
{
fscanf(file, "%d//%d %d//%d %d//%d\n", &a, &b, &c, &d, &e, &f);
triangles.push_back({a, b});
triangles.push_back({c, d});
triangles.push_back({e, f});
}
}
RenderWindow window(VideoMode(640,640), "OpenGL");
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_NORMALIZE);
GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
glEnable(GL_LIGHT0);
GLfloat lightColor1[] = {0.5f, 0.2f, 0.2f, 1.0f};
GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f};
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);
glEnable(GL_LIGHT1);
while(window.isOpen())
{
Event ev;
while(window.pollEvent(ev))
{
if (ev.type==Event::Closed)
{
window.close();
break;
}
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0,0,0.1,1);
glColor3f(1, 1, 0.7);
glRotatef(0.7, 0, 1, 0);
glBegin(GL_TRIANGLES);
for (auto q : triangles)
{
a=q[0];
b=q[1];
glNormal3f(normals[b][0], normals[b][1], normals[b][2]);
glVertex3f(vertices[a][0], vertices[a][1], vertices[a][2]);
}
glEnd();
window.display();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment