Skip to content

Commit 249b124

Browse files
author
David Eränen
committed
Committed files.
0 parents  commit 249b124

8 files changed

+323
-0
lines changed

AlgoVis.pro

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
TEMPLATE = app
2+
CONFIG += console
3+
CONFIG -= app_bundle
4+
CONFIG -= qt
5+
6+
SOURCES += \
7+
main.cpp \
8+
GLVis.cpp \
9+
GLUtils.cpp
10+
11+
QMAKE_CXXFLAGS += -std=c++11
12+
13+
CONFIG(debug, debug|release): QMAKE_CXXFLAGS += -D_DEBUG
14+
15+
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../build-EasyGeom-Desktop-Debug/release/ -lEasyGeom
16+
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../build-EasyGeom-Desktop-Debug/debug/ -lEasyGeom
17+
else:unix: LIBS += -L$$PWD/../build-EasyGeom-Desktop-Debug/ -lEasyGeom
18+
19+
INCLUDEPATH += $$PWD/../EasyGeom
20+
DEPENDPATH += $$PWD/../EasyGeom
21+
22+
mac: INCLUDEPATH += /Library/Frameworks/SDL2.framework/Headers
23+
mac: INCLUDEPATH += /System/Library/Frameworks/OpenGL.framework/Headers
24+
25+
win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../build-EasyGeom-Desktop-Debug/release/libEasyGeom.a
26+
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../build-EasyGeom-Desktop-Debug/debug/libEasyGeom.a
27+
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../build-EasyGeom-Desktop-Debug/release/EasyGeom.lib
28+
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../build-EasyGeom-Desktop-Debug/debug/EasyGeom.lib
29+
else:unix: PRE_TARGETDEPS += $$PWD/../build-EasyGeom-Desktop-Debug/libEasyGeom.a
30+
31+
mac: LIBS += -F/Library/Frameworks -framework SDL2
32+
else:unix|win32: LIBS += -lSDL2
33+
34+
mac: LIBS += -framework OpenGL
35+
else:unix|win32: LIBS += -lOpenGL
36+
37+
HEADERS += \
38+
GLVis.h \
39+
GLUtils.h
40+
41+
OTHER_FILES += \
42+
triangles.frag \
43+
triangles.vert

GLUtils.cpp

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#include "GLUtils.h"
2+
3+
#include <iostream>
4+
5+
static const GLchar*
6+
ReadShader( const char* filename )
7+
{
8+
#ifdef WIN32
9+
FILE* infile;
10+
fopen_s( &infile, filename, "rb" );
11+
#else
12+
FILE* infile = fopen( filename, "rb" );
13+
#endif // WIN32
14+
15+
if ( !infile ) {
16+
#ifdef _DEBUG
17+
std::cerr << "Unable to open file '" << filename << "'." << std::endl;
18+
#endif /* DEBUG */
19+
return NULL;
20+
}
21+
22+
fseek( infile, 0, SEEK_END );
23+
int len = ftell( infile );
24+
fseek( infile, 0, SEEK_SET );
25+
26+
GLchar* source = new GLchar[len+1];
27+
28+
fread( source, 1, len, infile );
29+
fclose( infile );
30+
31+
source[len] = 0;
32+
33+
return const_cast<const GLchar*>(source);
34+
}
35+
36+
//----------------------------------------------------------------------------
37+
38+
GLuint
39+
LoadShaders( ShaderInfo* shaders )
40+
{
41+
if ( shaders == NULL ) { return 0; }
42+
43+
GLuint program = glCreateProgram();
44+
45+
ShaderInfo* entry = shaders;
46+
while ( entry->type != GL_NONE ) {
47+
GLuint shader = glCreateShader( entry->type );
48+
49+
entry->shader = shader;
50+
51+
const GLchar* source = ReadShader( entry->filename );
52+
if ( source == NULL ) {
53+
for ( entry = shaders; entry->type != GL_NONE; ++entry ) {
54+
glDeleteShader( entry->shader );
55+
entry->shader = 0;
56+
}
57+
58+
return 0;
59+
}
60+
61+
glShaderSource( shader, 1, &source, NULL );
62+
delete [] source;
63+
64+
glCompileShader( shader );
65+
66+
GLint compiled;
67+
glGetShaderiv( shader, GL_COMPILE_STATUS, &compiled );
68+
if ( !compiled ) {
69+
#ifdef _DEBUG
70+
GLsizei len;
71+
glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &len );
72+
73+
GLchar* log = new GLchar[len+1];
74+
glGetShaderInfoLog( shader, len, &len, log );
75+
std::cerr << "Shader compilation failed: " << log << std::endl;
76+
delete [] log;
77+
#endif /* DEBUG */
78+
79+
return 0;
80+
}
81+
82+
glAttachShader( program, shader );
83+
84+
++entry;
85+
}
86+
87+
glLinkProgram( program );
88+
89+
GLint linked;
90+
glGetProgramiv( program, GL_LINK_STATUS, &linked );
91+
if ( !linked ) {
92+
#ifdef _DEBUG
93+
GLsizei len;
94+
glGetProgramiv( program, GL_INFO_LOG_LENGTH, &len );
95+
96+
GLchar* log = new GLchar[len+1];
97+
glGetProgramInfoLog( program, len, &len, log );
98+
std::cerr << "Shader linking failed: " << log << std::endl;
99+
delete [] log;
100+
#endif /* DEBUG */
101+
102+
for ( entry = shaders; entry->type != GL_NONE; ++entry ) {
103+
glDeleteShader( entry->shader );
104+
entry->shader = 0;
105+
}
106+
107+
return 0;
108+
}
109+
110+
return program;
111+
}

GLUtils.h

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
#include "OpenGL/gl3.h"
4+
5+
#define BUFFER_OFFSET(x) ((const void*) (x))
6+
7+
typedef struct {
8+
GLenum type;
9+
const char* filename;
10+
GLuint shader;
11+
} ShaderInfo;
12+
13+
GLuint LoadShaders( ShaderInfo* );

GLVis.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "GLVis.h"
2+
#include <iostream>
3+
4+
bool GLVis::InitGLVis()
5+
{
6+
std::cout << "GLVis initialized!" << std::endl;
7+
return true;
8+
}

GLVis.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#define GLVis_main \
4+
SecondaryMain(); \
5+
int main(int argc, char** argv) { \
6+
GLVis::InitGLVis(); \
7+
SecondaryMain(); \
8+
} \
9+
int SecondaryMain
10+
11+
namespace GLVis {
12+
13+
bool InitGLVis();
14+
15+
}

main.cpp

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#include <OpenGL/gl3.h>
2+
#include <OpenGL/gl3ext.h>
3+
4+
#include <SDL.h>
5+
6+
#include <iostream>
7+
#include <cstdlib>
8+
9+
#include "GLVis.h"
10+
#include "GLUtils.h"
11+
12+
void init();
13+
14+
enum VAO_IDs { Triangles, NumVAOs };
15+
enum VBO_IDs { ArrayBuffer, NumVBOs };
16+
enum Attrib_IDs { vPosition = 0 };
17+
18+
GLuint VAOs[NumVAOs];
19+
GLuint VBOs[NumVBOs];
20+
21+
const GLuint NumVertices = 6;
22+
23+
int GLVis_main()
24+
{
25+
for(float f = 0.0f; f <= 1.0f; f += 0.1)
26+
{
27+
std::cout << f << std::endl;
28+
}
29+
30+
if( SDL_Init(SDL_INIT_VIDEO) )
31+
{
32+
std::cerr << "SDL_Init Error: "
33+
<< SDL_GetError() << std::endl;
34+
return 1;
35+
}
36+
37+
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
38+
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
39+
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
40+
41+
SDL_Window* window =
42+
SDL_CreateWindow( "Hello World!", 100, 100, 640, 480, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN );
43+
44+
if( window == nullptr ) {
45+
std::cout << "SDL_CreateWindow Error: "
46+
<< SDL_GetError() << std::endl;
47+
return 1;
48+
}
49+
50+
SDL_GLContext glContext = SDL_GL_CreateContext(window);
51+
if( glContext == nullptr ) {
52+
std::cout << "SDL_GL_CreateContext Error: "
53+
<< SDL_GetError() << std::endl;
54+
return 1;
55+
}
56+
57+
init();
58+
59+
bool bQuit = false;
60+
while( !bQuit )
61+
{
62+
SDL_Event e;
63+
while( SDL_PollEvent(&e) )
64+
{
65+
switch(e.type)
66+
{
67+
case SDL_QUIT: { bQuit = true; break; }
68+
}
69+
}
70+
71+
glClearColor(1.0, 0.0, 0.0, 1.0);
72+
glClear(GL_COLOR_BUFFER_BIT);
73+
74+
glBindVertexArray(VAOs[Triangles]);
75+
glDrawArrays(GL_TRIANGLES, 0, NumVertices);
76+
77+
SDL_GL_SwapWindow(window);
78+
}
79+
80+
SDL_DestroyWindow(window);
81+
82+
atexit( SDL_Quit );
83+
return 0;
84+
}
85+
86+
void init()
87+
{
88+
glGenVertexArrays(NumVAOs, VAOs);
89+
glBindVertexArray(VAOs[Triangles]);
90+
91+
GLfloat vertices[NumVertices][2] = {
92+
{ -0.90, -0.90 }, // Triangle 1
93+
{ 0.85, -0.90 },
94+
{ -0.90, 0.85 },
95+
{ 0.90, -0.85 }, // Triangle 2
96+
{ 0.90, 0.90 },
97+
{ -0.85, 0.90 }
98+
};
99+
100+
glGenBuffers(NumVBOs, VBOs);
101+
glBindBuffer(GL_ARRAY_BUFFER, VBOs[ArrayBuffer]);
102+
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
103+
104+
ShaderInfo shaders[] = {
105+
{ GL_VERTEX_SHADER, "triangles.vert" },
106+
{ GL_FRAGMENT_SHADER, "triangles.frag" },
107+
{ GL_NONE, nullptr }
108+
};
109+
110+
GLuint program = LoadShaders(shaders);
111+
glUseProgram(program);
112+
113+
glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
114+
glEnableVertexAttribArray(vPosition);
115+
}

triangles.frag

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#version 150 core
2+
3+
out vec4 fColor;
4+
5+
void
6+
main()
7+
{
8+
fColor = vec4(0.0, 0.0, 1.0, 1.0);
9+
}

triangles.vert

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#version 150 core
2+
3+
layout(location = 0) in vec4 vPosition;
4+
5+
void
6+
main()
7+
{
8+
gl_Position = vPosition;
9+
}

0 commit comments

Comments
 (0)