Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
AG Computergrafik
ss21cg3
Commits
63ca4329
Commit
63ca4329
authored
Apr 26, 2021
by
Bastian Krayer
Browse files
added solution 1
parent
7954f5ae
Changes
3
Hide whitespace changes
Inline
Side-by-side
src/executables/FragezeichenSolution/CMakeLists.txt
0 → 100644
View file @
63ca4329
cmake_minimum_required
(
VERSION 3.1
)
include
(
${
CMAKE_MODULE_PATH
}
/DefaultExecutable.cmake
)
\ No newline at end of file
src/executables/FragezeichenSolution/dependencies.txt
0 → 100644
View file @
63ca4329
CVK_2
\ No newline at end of file
src/executables/FragezeichenSolution/main.cpp
0 → 100644
View file @
63ca4329
#include
<CVK_2/CVK_Framework.h>
#include
<glad/glad.h>
#include
<memory>
#include
<random>
const
int
WIDTH
=
600
;
const
int
HEIGHT
=
600
;
const
int
NUM_POINTS
=
50
;
class
FragezeichenSolution
:
public
CVK
::
GlfwBase
{
public:
FragezeichenSolution
();
// run the actual loop and display
void
display
();
static
void
mouseCallback
(
GLFWwindow
*
window
,
int
button
,
int
action
,
int
mods
);
private:
std
::
vector
<
glm
::
vec3
>
colors
,
points
;
int
selectIndex
=
-
1
;
};
void
mouseCallback
(
GLFWwindow
*
window
,
int
button
,
int
action
,
int
mods
);
int
main
()
{
FragezeichenSolution
demo
;
demo
.
display
();
return
0
;
}
// Implementation
FragezeichenSolution
::
FragezeichenSolution
()
{
m_window
=
glfwCreateWindow
(
WIDTH
,
HEIGHT
,
"Voronoi"
,
nullptr
,
nullptr
);
// Allows us to associate data with a window
// We store a pointer to this object so we can use it in the static methods
glfwSetWindowUserPointer
(
m_window
,
this
);
glfwSetWindowPos
(
m_window
,
600
,
50
);
glfwMakeContextCurrent
(
m_window
);
glfwSetMouseButtonCallback
(
m_window
,
mouseCallback
);
// init opengl
if
(
!
gladLoadGLLoader
((
GLADloadproc
)
glfwGetProcAddress
))
{
std
::
cerr
<<
"Failed to initialize OpenGL context"
<<
std
::
endl
;
throw
std
::
runtime_error
(
"Failed to initialize OpenGL context"
);
}
}
void
FragezeichenSolution
::
mouseCallback
(
GLFWwindow
*
window
,
int
button
,
int
action
,
int
mods
)
{
// Since glfw callbacks come from C, we can only pass static member
// functions. But at the beginning, we store a pointer to our object with
// this window and we can retrieve it. "demo" effectively acts as "this"
// here
auto
demo
=
static_cast
<
FragezeichenSolution
*>
(
glfwGetWindowUserPointer
(
window
));
if
(
action
==
GLFW_RELEASE
)
{
demo
->
selectIndex
=
-
1
;
}
if
(
action
==
GLFW_PRESS
)
{
double
x
,
y
;
glfwGetCursorPos
(
window
,
&
x
,
&
y
);
glm
::
vec2
mousePos
;
mousePos
.
x
=
static_cast
<
int
>
(((
x
/
WIDTH
)
*
2.0
f
)
-
1.0
f
);
mousePos
.
y
=
static_cast
<
int
>
(((((
HEIGHT
-
y
)
/
HEIGHT
)
*
2.0
f
)
-
1.0
f
));
float
minDist
=
100.0
f
;
for
(
int
i
=
0
;
i
<
NUM_POINTS
;
i
++
)
{
glm
::
vec2
point_pos
(
demo
->
points
[
i
]);
float
dist
=
glm
::
length
(
point_pos
-
mousePos
);
if
(
dist
<
minDist
)
{
minDist
=
dist
;
demo
->
selectIndex
=
i
;
}
}
}
}
void
FragezeichenSolution
::
display
()
{
glClearColor
(
1
,
1
,
1
,
1
);
float
coneRadius
=
10.0
f
;
CVK
::
Cone
coneGeometry
(
glm
::
vec3
(
0.0
f
,
0.0
f
,
-
coneRadius
),
glm
::
vec3
(
0.0
f
,
0.0
f
,
0.0
f
),
coneRadius
,
0.0
f
,
100
);
// create points
// random device for generating random numbers
std
::
random_device
gen
;
std
::
uniform_real_distribution
<
float
>
dis
(
-
1.0
f
,
1.0
f
);
std
::
uniform_real_distribution
<
float
>
disColor
(
0.0
f
,
1.0
f
);
for
(
int
i
=
0
;
i
<
NUM_POINTS
;
i
++
)
{
glm
::
vec3
point
;
glm
::
vec3
color
;
point
.
x
=
dis
(
gen
);
point
.
y
=
dis
(
gen
);
point
.
z
=
0.0
f
;
points
.
push_back
(
point
);
for
(
int
j
=
0
;
j
<
3
;
j
++
)
{
color
[
j
]
=
disColor
(
gen
);
}
colors
.
push_back
(
color
);
}
GLuint
vaoHandlePoints
;
glGenVertexArrays
(
1
,
&
vaoHandlePoints
);
glBindVertexArray
(
vaoHandlePoints
);
GLuint
vboHandle
;
glGenBuffers
(
1
,
&
vboHandle
);
// bind the first buffer and store geometry into the VBO
glBindBuffer
(
GL_ARRAY_BUFFER
,
vboHandle
);
glBufferData
(
GL_ARRAY_BUFFER
,
sizeof
(
glm
::
vec3
)
*
points
.
size
(),
&
points
[
0
],
GL_STATIC_DRAW
);
glEnableVertexAttribArray
(
0
);
glVertexAttribPointer
(
0
,
3
,
GL_FLOAT
,
GL_FALSE
,
sizeof
(
glm
::
vec3
),
nullptr
);
glm
::
vec3
black
(
0.0
f
,
0.0
f
,
0.0
f
);
// compile a shader program
CVK
::
ShaderSet
shaderProgram
(
VERTEX_SHADER_BIT
|
FRAGMENT_SHADER_BIT
,
{
CVK
::
State
::
getInstance
()
->
getShaderPath
()
+
"/Fragezeichen/fragezeichen.vert"
,
CVK
::
State
::
getInstance
()
->
getShaderPath
()
+
"/Fragezeichen/fragezeichen.frag"
});
// use the shader program
shaderProgram
.
useProgram
();
GLint
viewMatrixHandle
=
glGetUniformLocation
(
shaderProgram
.
getProgramID
(),
"viewMatrix"
);
GLint
projectionMatrixHandle
=
glGetUniformLocation
(
shaderProgram
.
getProgramID
(),
"projectionMatrix"
);
GLint
modelMatrixHandle
=
glGetUniformLocation
(
shaderProgram
.
getProgramID
(),
"modelMatrix"
);
GLint
colorHandle
=
glGetUniformLocation
(
shaderProgram
.
getProgramID
(),
"color"
);
// setting up the camera parameters
glm
::
mat4
viewMatrix
=
glm
::
lookAt
(
glm
::
vec3
(
0.0
f
,
0.0
f
,
0.001
f
),
glm
::
vec3
(
0.0
f
,
0.0
f
,
0.0
f
),
glm
::
vec3
(
0.0
f
,
1.0
f
,
0.0
f
));
glm
::
mat4
projectionMatrix
=
glm
::
ortho
(
-
1.0
f
,
1.0
f
,
-
1.0
f
,
1.0
f
,
0.0
f
,
10.0
f
);
glm
::
mat4
modelMatrix
=
glm
::
mat4
(
1.0
f
);
glUniformMatrix4fv
(
viewMatrixHandle
,
1
,
GL_FALSE
,
glm
::
value_ptr
(
viewMatrix
));
glUniformMatrix4fv
(
projectionMatrixHandle
,
1
,
GL_FALSE
,
glm
::
value_ptr
(
projectionMatrix
));
glEnable
(
GL_DEPTH_TEST
);
glPointSize
(
5.0
f
);
// renderloop
while
(
!
glfwWindowShouldClose
(
m_window
))
{
glClear
(
GL_COLOR_BUFFER_BIT
|
GL_DEPTH_BUFFER_BIT
);
if
(
selectIndex
>=
0
&&
selectIndex
<
points
.
size
())
{
// get mouse position
double
x
,
y
;
glfwGetCursorPos
(
m_window
,
&
x
,
&
y
);
glm
::
vec2
mousePos
;
mousePos
.
x
=
((
x
/
WIDTH
)
*
2.0
f
)
-
1.0
f
;
mousePos
.
y
=
((((
HEIGHT
-
y
)
/
HEIGHT
)
*
2.0
f
)
-
1.0
f
);
// update selected point
points
[
selectIndex
]
=
glm
::
vec3
(
mousePos
.
x
,
mousePos
.
y
,
0
);
glBindBuffer
(
GL_ARRAY_BUFFER
,
vboHandle
);
glBufferData
(
GL_ARRAY_BUFFER
,
sizeof
(
glm
::
vec3
)
*
points
.
size
(),
points
.
data
(),
GL_STATIC_DRAW
);
}
for
(
int
i
=
0
;
i
<
NUM_POINTS
;
i
++
)
{
// set color
glUniform3fv
(
colorHandle
,
1
,
glm
::
value_ptr
(
colors
[
i
]));
// update modelmatrix
modelMatrix
=
glm
::
translate
(
glm
::
mat4
(
1.0
f
),
points
.
at
(
i
));
glUniformMatrix4fv
(
modelMatrixHandle
,
1
,
GL_FALSE
,
glm
::
value_ptr
(
modelMatrix
));
coneGeometry
.
render
();
}
modelMatrix
=
glm
::
mat4
(
1.0
f
);
glUniformMatrix4fv
(
modelMatrixHandle
,
1
,
GL_FALSE
,
glm
::
value_ptr
(
modelMatrix
));
glUniform3fv
(
colorHandle
,
1
,
glm
::
value_ptr
(
black
));
glBindVertexArray
(
vaoHandlePoints
);
glDrawArrays
(
GL_POINTS
,
0
,
points
.
size
());
// show what's been drawn
glfwSwapBuffers
(
m_window
);
glfwPollEvents
();
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment