2 Commits

4 changed files with 79 additions and 122 deletions

View File

@@ -4,9 +4,10 @@ CFLAGS = -I.
DEPS = -lglew32 -lopengl32 -lglu32 -lgdi32 DEPS = -lglew32 -lopengl32 -lglu32 -lgdi32
LINK = -L. -DGLEW_STATIC LINK = -L. -DGLEW_STATIC
OUTPUT = output.exe OUTPUT = output.exe
CPPSTD = c++17
default: default:
$(CC) -g *.cpp $(CFLAGS) $(DEPS) $(LINK) -static -static-libgcc -fno-keep-inline-dllexport -o $(OUTPUT) $(CC) -g *.cpp $(CFLAGS) $(DEPS) $(LINK) -std=$(CPPSTD) -static -static-libgcc -fno-keep-inline-dllexport -o $(OUTPUT)
run: default run: default
$(OUTPUT) $(OUTPUT)

View File

@@ -15,11 +15,15 @@ bool loadOBJ(const char* path, std::vector <glm::vec3>& out_vertices, std::vecto
return false; return false;
} }
int line = 0; unsigned int line = 0;
unsigned int vs = 0;
unsigned int vts = 0;
unsigned int vns = 0;
unsigned int fs = 0;
char lineHeader[128];
while (1) { while (1) {
char lineHeader[128];
// read the first word of the line // read the first word of the line
int res = fscanf(file, "%s", lineHeader); int res = fscanf(file, "%s", lineHeader);
if (res == EOF) break; // EOF = End Of File. Quit the loop. if (res == EOF) break; // EOF = End Of File. Quit the loop.
@@ -30,23 +34,27 @@ bool loadOBJ(const char* path, std::vector <glm::vec3>& out_vertices, std::vecto
glm::vec3 vertex; glm::vec3 vertex;
fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z); fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z);
temp_vertices.push_back(vertex); temp_vertices.push_back(vertex);
vs++;
} else if (strcmp(lineHeader, "vt") == 0) { } else if (strcmp(lineHeader, "vt") == 0) {
glm::vec2 uv; glm::vec2 uv;
fscanf(file, "%f %f\n", &uv.x, &uv.y); fscanf(file, "%f %f\n", &uv.x, &uv.y);
temp_uvs.push_back(uv); temp_uvs.push_back(uv);
vts++;
} else if (strcmp(lineHeader, "vn") == 0) { } else if (strcmp(lineHeader, "vn") == 0) {
glm::vec3 normal; glm::vec3 normal;
fscanf(file, "%f %f %f\n", &normal.x, &normal.y, &normal.z); fscanf(file, "%f %f %f\n", &normal.x, &normal.y, &normal.z);
temp_normals.push_back(normal); temp_normals.push_back(normal);
vns++;
} else if (strcmp(lineHeader, "f") == 0) { } else if (strcmp(lineHeader, "f") == 0) {
unsigned int vertexIndex[3], uvIndex[3], normalIndex[3]; unsigned int vertexIndex[3], uvIndex[3], normalIndex[3];
int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vertexIndex[0], &uvIndex[0], &normalIndex[0], &vertexIndex[1], &uvIndex[1], &normalIndex[1], &vertexIndex[2], &uvIndex[2], &normalIndex[2]); int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vertexIndex[0], &uvIndex[0], &normalIndex[0], &vertexIndex[1], &uvIndex[1], &normalIndex[1], &vertexIndex[2], &uvIndex[2], &normalIndex[2]);
fs++;
if (matches != 9){ if (matches != 9){
printf("File can't be read by our simple parser. Try exporting with other options (%d matches on line %d)\n", matches, line); printf("File can't be read by our simple parser. Try exporting with other options (%d matches on line %d)\n", matches, line);
@@ -64,6 +72,9 @@ bool loadOBJ(const char* path, std::vector <glm::vec3>& out_vertices, std::vecto
normalIndices.push_back(normalIndex[2]); normalIndices.push_back(normalIndex[2]);
} }
line++;
}
// For each vertex of each triangle // For each vertex of each triangle
for (unsigned int i = 0; i < vertexIndices.size(); i++) { for (unsigned int i = 0; i < vertexIndices.size(); i++) {
unsigned int vertexIndex = vertexIndices[i]; unsigned int vertexIndex = vertexIndices[i];
@@ -79,9 +90,9 @@ bool loadOBJ(const char* path, std::vector <glm::vec3>& out_vertices, std::vecto
out_normals.push_back(vertex3); out_normals.push_back(vertex3);
} }
line++; fclose(file);
} printf("(loadOBJ.cpp) Loaded file with %d v, %d vt, %d vn, %d f. %d lines total.\n", vs, vts, vns, fs, line);
return true; return true;
} }

149
main.cpp
View File

@@ -67,7 +67,6 @@ BOOL APIENTRY AboutDlgProc(HWND hDlg, UINT message, UINT wParam, LONG lParam);
void SetDCPixelFormat(HDC hDC); void SetDCPixelFormat(HDC hDC);
int polygonmode = 0; int polygonmode = 0;
char loadCount = 0;
// Change viewing volume and viewport. Called when window is resized // Change viewing volume and viewport. Called when window is resized
void ChangeSize(GLsizei w, GLsizei h) { void ChangeSize(GLsizei w, GLsizei h) {
@@ -100,47 +99,6 @@ void ChangeSize(GLsizei w, GLsizei h) {
glLoadIdentity(); glLoadIdentity();
} }
void SetupRC() {
// Light values and coordinates
//GLfloat ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f };
//GLfloat diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f };
//GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
//GLfloat lightPos[] = { 0.0f, 150.0f, 150.0f, 1.0f };
//GLfloat specref[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glEnable(GL_DEPTH_TEST); // Hidden surface removal
glFrontFace(GL_CCW); // Counter clock-wise polygons face out
//glEnable(GL_CULL_FACE); // Do not calculate inside of jet
// Enable lighting
//glEnable(GL_LIGHTING);
// Setup and enable light 0
//glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
//glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
//glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
//glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
//glEnable(GL_LIGHT0);
// Enable color tracking
//glEnable(GL_COLOR_MATERIAL);
// Set Material properties to follow glColor values
//glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
// All materials hereafter have full specular reflectivity
// with a high shine
//glMaterialfv(GL_FRONT, GL_SPECULAR, specref);
//glMateriali(GL_FRONT, GL_SHININESS, 128);
// White background
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
// Black brush
glColor3f(0.0, 0.0, 0.0);
}
// LoadBitmapFile // LoadBitmapFile
// opis: ładuje mapę bitową z pliku i zwraca jej adres. // opis: ładuje mapę bitową z pliku i zwraca jej adres.
// Wypełnia strukturę nagłówka. // Wypełnia strukturę nagłówka.
@@ -239,14 +197,45 @@ GLuint vertexbuffer;
GLuint uvbuffer; GLuint uvbuffer;
std::time_t lastTime = std::time(nullptr); std::time_t lastTime = std::time(nullptr);
void setup(){ void SetupRC() {
// załaduj model // Light values and coordinates
//GLfloat ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f };
//GLfloat diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f };
//GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
//GLfloat lightPos[] = { 0.0f, 150.0f, 150.0f, 1.0f };
//GLfloat specref[] = { 1.0f, 1.0f, 1.0f, 1.0f };
// if(!glfwInit()) {
// fprintf(stderr, "Failed to initialize GLFW\n"); glEnable(GL_DEPTH_TEST); // Hidden surface removal
// getchar(); glFrontFace(GL_CCW); // Counter clock-wise polygons face out
// return; glEnable(GL_CULL_FACE); // Do not calculate inside of jet // !!! znacząco poprawia wydajność
// }
// Enable lighting
//glEnable(GL_LIGHTING);
// Setup and enable light 0
//glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
//glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
//glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
//glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
//glEnable(GL_LIGHT0);
// Enable color tracking
//glEnable(GL_COLOR_MATERIAL);
// Set Material properties to follow glColor values
//glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
// All materials hereafter have full specular reflectivity
// with a high shine
//glMaterialfv(GL_FRONT, GL_SPECULAR, specref);
//glMateriali(GL_FRONT, GL_SHININESS, 128);
// White background
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
// Black brush
glColor3f(0.0, 0.0, 0.0);
// Initialize GLEW // Initialize GLEW
timestampedCout("Inicjalizowanie GLEW..."); timestampedCout("Inicjalizowanie GLEW...");
@@ -260,44 +249,34 @@ void setup(){
timestampedCout("Zainicjalizowano GLEW."); timestampedCout("Zainicjalizowano GLEW.");
/* */ // Załaduj model z pliku .obj
// Read our .obj file
// TODO: zmierzyć czas ładowania łazika w cyklach procesora/mikrosekundach // TODO: zmierzyć czas ładowania łazika w cyklach procesora/mikrosekundach
timestampedCout("Ladowanie lazika..."); timestampedCout("Ladowanie lazika...");
bool res = loadOBJ("res/models/lazik3.obj", vertices, uvs, normals); bool res = loadOBJ("res/models/lazik3.obj", vertices, uvs, normals);
// bool res = loadOBJ("res/models/suzanne.obj", vertices, uvs, normals); // bool res = loadOBJ("res/models/suzanne.obj", vertices, uvs, normals);
if (res) { if (res) timestampedCout("Pomyslnie zaladowano model lazika.")
timestampedCout("Pomyslnie zaladowano model lazika.");
}
else timestampedCout("Nie udalo sie zaladowac modelu lazika."); else timestampedCout("Nie udalo sie zaladowac modelu lazika.");
/* */
glGenBuffers(1, &vertexbuffer); glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
// timestampedCout("rozmiar vertexbuffer: " << vertices.size() * sizeof(glm::vec3));
glGenBuffers(1, &uvbuffer); glGenBuffers(1, &uvbuffer);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer); glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec2), &uvs[0], GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec2), &uvs[0], GL_STATIC_DRAW);
// timestampedCout("rozmiar uvbuffer: " << uvs.size() * sizeof(glm::vec2));
loadCount++;
//glClearColor(0.0f, 0.0f, 0.4f, 0.0f); //glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
} }
void RenderScene(void) { void RenderScene(void) {
if (loadCount == 0) setup();
//float normal[3]; // Storage for calculated surface normal //float normal[3]; // Storage for calculated surface normal
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Save the matrix state and do the rotations // Save the matrix state and do the rotations
glPushMatrix(); glPushMatrix();
glRotatef(xRot, 1.0f, 0.0f, 0.0f); glRotatef(xRot, 1.0f, 0.0f, 0.0f);
@@ -310,25 +289,10 @@ void RenderScene(void) {
// 0, 1, 0 // probably glm::vec3(0,1,0), but (0,-1,0) would make you looking upside-down, which can be great too // 0, 1, 0 // probably glm::vec3(0,1,0), but (0,-1,0) would make you looking upside-down, which can be great too
// ); // );
/////////////////////////////////////////////////////////////////
// MIEJSCE NA KOD OPENGL DO TWORZENIA WLASNYCH SCEN: //
/////////////////////////////////////////////////////////////////
//szescian();
//Sposób na odróżnienie "przedniej" i "tylniej" ściany wielokąta:
//glPolygonMode(GL_BACK,GL_LINE);
//walec(40, 40);
//szescian();
switch (polygonmode) { switch (polygonmode) {
case 0:
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
break;
case 1: case 1:
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
break; break;
default: default:
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
} }
@@ -346,7 +310,7 @@ void RenderScene(void) {
// // Set our "myTextureSampler" sampler to use Texture Unit 0 // // Set our "myTextureSampler" sampler to use Texture Unit 0
// glUniform1i(TextureID, 0); // glUniform1i(TextureID, 0);
// 1rst attribute buffer : vertices // 1st attribute buffer : vertices
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer( glVertexAttribPointer(
@@ -370,7 +334,7 @@ void RenderScene(void) {
(void*)0 // array buffer offset (void*)0 // array buffer offset
); );
// Draw the triangle! // Draw vertices
glDrawArrays(GL_TRIANGLES, 0, vertices.size()); glDrawArrays(GL_TRIANGLES, 0, vertices.size());
glDisableVertexAttribArray(0); glDisableVertexAttribArray(0);
@@ -387,29 +351,14 @@ void RenderScene(void) {
nowy.create(25, 10, 0, 10); nowy.create(25, 10, 0, 10);
*/ */
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
/*
szescian nowy2;
nowy2.create(0, 10, 0, 10);
szescian nowy3;
nowy3.create(0, 20, -10, 10);
*/
//Uzyskanie siatki:
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
//Wyrysowanie prostokata: //Wyrysowanie prostokata:
//glRectd(-10.0,-10.0,20.0,20.0); //glRectd(-10.0,-10.0,20.0,20.0);
///////////////////////////////////////////////////////////////// glPopMatrix(); // wymagane
///////////////////////////////////////////////////////////////// glMatrixMode(GL_MODELVIEW); // zmniejsza zużycie GPU
/////////////////////////////////////////////////////////////////
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
// Flush drawing commands // Flush drawing commands
glFlush(); // glFlush();
} }
// If necessary, creates a 3-3-2 palette for the device context listed. // If necessary, creates a 3-3-2 palette for the device context listed.
@@ -626,7 +575,6 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
ChangeSize(LOWORD(lParam), HIWORD(lParam)); ChangeSize(LOWORD(lParam), HIWORD(lParam));
break; break;
// The painting function. This message sent by Windows // The painting function. This message sent by Windows
// whenever the screen needs updating. // whenever the screen needs updating.
case WM_PAINT: case WM_PAINT:
@@ -757,9 +705,6 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
return (0L); return (0L);
} }
// Dialog procedure. // Dialog procedure.
BOOL APIENTRY AboutDlgProc(HWND hDlg, UINT message, UINT wParam, LONG lParam) { BOOL APIENTRY AboutDlgProc(HWND hDlg, UINT message, UINT wParam, LONG lParam) {

Binary file not shown.