Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
GraPa
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kai Westerkamp
GraPa
Commits
26c7c733
Commit
26c7c733
authored
Oct 29, 2015
by
Kai Westerkamp
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added Shader
Fixed Rotation Todo: Shader MAterial Gouraut Shading
parent
546c71b8
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
117 additions
and
28 deletions
+117
-28
cubewidget.cpp
A1/hellocube/cubewidget.cpp
+60
-28
cubewidget.h
A1/hellocube/cubewidget.h
+4
-0
hellocube.pro
A1/hellocube/hellocube.pro
+4
-0
hellocube.qrc
A1/hellocube/hellocube.qrc
+2
-0
phong.frag
A1/hellocube/phong.frag
+28
-0
phong.vert
A1/hellocube/phong.vert
+19
-0
No files found.
A1/hellocube/cubewidget.cpp
View file @
26c7c733
...
@@ -22,6 +22,7 @@ void CubeWidget::initializeGL ()
...
@@ -22,6 +22,7 @@ void CubeWidget::initializeGL ()
glEnable
(
GL_DEPTH_TEST
);
glEnable
(
GL_DEPTH_TEST
);
tesselation
=
0
;
tesselation
=
0
;
prepareShader
();
// enable default shading
// enable default shading
home
();
home
();
showFlat
();
showFlat
();
...
@@ -30,13 +31,40 @@ void CubeWidget::initializeGL ()
...
@@ -30,13 +31,40 @@ void CubeWidget::initializeGL ()
glEnable
(
GL_LIGHT0
);
glEnable
(
GL_LIGHT0
);
static
GLfloat
lightPosition
[
4
]
=
{
0.5
,
0.0
,
2.0
,
1.0
};
static
GLfloat
lightPosition
[
4
]
=
{
0.5
,
0.0
,
2.0
,
1.0
};
glLightfv
(
GL_LIGHT0
,
GL_POSITION
,
lightPosition
);
glLightfv
(
GL_LIGHT0
,
GL_POSITION
,
lightPosition
);
GLfloat
white
[]
=
{
1.0
,
1.0
,
1.0
};
glLightfv
(
GL_LIGHT0
,
GL_DIFFUSE
,
white
);
glLightfv
(
GL_LIGHT0
,
GL_SPECULAR
,
white
);
}
void
CubeWidget
::
prepareShader
()
{
QGLShader
*
vertex
=
new
QGLShader
(
QGLShader
::
Vertex
);
if
(
!
vertex
->
compileSourceFile
(
QLatin1String
(
":/phong.vert"
)))
qCritical
()
<<
"Vertex Shader failed"
<<
vertex
->
log
();
QGLShader
*
fragment
=
new
QGLShader
(
QGLShader
::
Fragment
);
if
(
!
fragment
->
compileSourceFile
(
QLatin1String
(
":/phong.frag"
)))
qCritical
()
<<
"Fragment Shader failed"
<<
fragment
->
log
();
shader
=
new
QGLShaderProgram
(
this
);
shader
->
addShader
(
vertex
);
shader
->
addShader
(
fragment
);
if
(
!
shader
->
link
())
qCritical
()
<<
"Linking failed"
<<
shader
->
log
();
}
}
void
CubeWidget
::
setMaterial
(
GLfloat
*
color
)
void
CubeWidget
::
setMaterial
(
GLfloat
*
color
)
{
{
glMaterialfv
(
GL_FRONT
,
GL_AMBIENT
,
color
);
glMaterialfv
(
GL_FRONT
,
GL_DIFFUSE
,
color
);
glMaterialfv
(
GL_FRONT
,
GL_DIFFUSE
,
color
);
glMaterialfv
(
GL_FRONT
,
GL_SPECULAR
,
color
);
glMaterialfv
(
GL_FRONT
,
GL_SPECULAR
,
color
);
glMaterialfv
(
GL_FRONT
,
GL_SHININESS
,
color
);
glMaterialfv
(
GL_FRONT
,
GL_SHININESS
,
new
GLfloat
(
50.0
));
}
}
void
CubeWidget
::
paintGL
(
)
void
CubeWidget
::
paintGL
(
)
...
@@ -50,19 +78,15 @@ void CubeWidget::paintGL ( )
...
@@ -50,19 +78,15 @@ void CubeWidget::paintGL ( )
mat
.
rotate
(
*
rotation
);
mat
.
rotate
(
*
rotation
);
glMultMatrixf
(
mat
.
data
());
glMultMatrixf
(
mat
.
data
());
GLfloat
red
[]
=
{
1.0
,
0.0
,
0.0
};
GLfloat
red
[]
=
{
1.0
,
0.0
,
0.0
};
GLfloat
green
[]
=
{
0.0
,
1.0
,
0.0
};
GLfloat
green
[]
=
{
0.0
,
1.0
,
0.0
};
GLfloat
blue
[]
=
{
0.0
,
0.0
,
1.0
};
GLfloat
blue
[]
=
{
0.0
,
0.0
,
1.0
};
GLfloat
cyan
[]
=
{
0.0
,
1.0
,
1.0
};
GLfloat
cyan
[]
=
{
0.0
,
1.0
,
1.0
};
GLfloat
magenta
[]
=
{
1.0
,
0.0
,
1.0
};
GLfloat
magenta
[]
=
{
1.0
,
0.0
,
1.0
};
GLfloat
yellow
[]
=
{
1.0
,
1.0
,
0.0
};
GLfloat
yellow
[]
=
{
1.0
,
1.0
,
0.0
};
glBegin
(
GL_QUADS
);
float
increment
=
1.0
/
(
pow
(
2
,
tesselation
));
float
increment
=
1.0
/
(
pow
(
2
,
tesselation
));
glBegin
(
GL_QUADS
);
setMaterial
(
red
);
setMaterial
(
red
);
for
(
float
x
=
-
0.5
;
x
<
0.5
;
x
+=
increment
)
{
for
(
float
x
=
-
0.5
;
x
<
0.5
;
x
+=
increment
)
{
for
(
float
y
=
-
0.5
;
y
<
0.5
;
y
+=
increment
)
{
for
(
float
y
=
-
0.5
;
y
<
0.5
;
y
+=
increment
)
{
...
@@ -72,7 +96,9 @@ void CubeWidget::paintGL ( )
...
@@ -72,7 +96,9 @@ void CubeWidget::paintGL ( )
glVertex3f
(
x
+
increment
,
0.5
f
,
y
+
increment
);
glVertex3f
(
x
+
increment
,
0.5
f
,
y
+
increment
);
}
}
}
}
glEnd
();
glBegin
(
GL_QUADS
);
setMaterial
(
magenta
);
setMaterial
(
magenta
);
for
(
float
x
=
-
0.5
;
x
<
0.5
;
x
+=
increment
)
{
for
(
float
x
=
-
0.5
;
x
<
0.5
;
x
+=
increment
)
{
for
(
float
y
=
-
0.5
;
y
<
0.5
;
y
+=
increment
)
{
for
(
float
y
=
-
0.5
;
y
<
0.5
;
y
+=
increment
)
{
...
@@ -82,7 +108,9 @@ void CubeWidget::paintGL ( )
...
@@ -82,7 +108,9 @@ void CubeWidget::paintGL ( )
glVertex3f
(
x
+
increment
,
-
0.5
f
,
y
);
glVertex3f
(
x
+
increment
,
-
0.5
f
,
y
);
}
}
}
}
glEnd
();
glBegin
(
GL_QUADS
);
setMaterial
(
green
);
setMaterial
(
green
);
for
(
float
x
=
-
0.5
;
x
<
0.5
;
x
+=
increment
)
{
for
(
float
x
=
-
0.5
;
x
<
0.5
;
x
+=
increment
)
{
for
(
float
y
=
-
0.5
;
y
<
0.5
;
y
+=
increment
)
{
for
(
float
y
=
-
0.5
;
y
<
0.5
;
y
+=
increment
)
{
...
@@ -92,7 +120,9 @@ void CubeWidget::paintGL ( )
...
@@ -92,7 +120,9 @@ void CubeWidget::paintGL ( )
glVertex3f
(
x
+
increment
,
y
,
0.5
f
);
glVertex3f
(
x
+
increment
,
y
,
0.5
f
);
}
}
}
}
glEnd
();
glBegin
(
GL_QUADS
);
setMaterial
(
yellow
);
setMaterial
(
yellow
);
for
(
float
x
=
-
0.5
;
x
<
0.5
;
x
+=
increment
)
{
for
(
float
x
=
-
0.5
;
x
<
0.5
;
x
+=
increment
)
{
for
(
float
y
=
-
0.5
;
y
<
0.5
;
y
+=
increment
)
{
for
(
float
y
=
-
0.5
;
y
<
0.5
;
y
+=
increment
)
{
...
@@ -102,8 +132,9 @@ void CubeWidget::paintGL ( )
...
@@ -102,8 +132,9 @@ void CubeWidget::paintGL ( )
glVertex3f
(
x
+
increment
,
y
+
increment
,
-
0.5
f
);
glVertex3f
(
x
+
increment
,
y
+
increment
,
-
0.5
f
);
}
}
}
}
glEnd
();
glBegin
(
GL_QUADS
);
setMaterial
(
blue
);
setMaterial
(
blue
);
for
(
float
x
=
-
0.5
;
x
<
0.5
;
x
+=
increment
)
{
for
(
float
x
=
-
0.5
;
x
<
0.5
;
x
+=
increment
)
{
for
(
float
y
=
-
0.5
;
y
<
0.5
;
y
+=
increment
)
{
for
(
float
y
=
-
0.5
;
y
<
0.5
;
y
+=
increment
)
{
...
@@ -113,7 +144,8 @@ void CubeWidget::paintGL ( )
...
@@ -113,7 +144,8 @@ void CubeWidget::paintGL ( )
glVertex3f
(
-
0.5
f
,
x
,
y
+
increment
);
glVertex3f
(
-
0.5
f
,
x
,
y
+
increment
);
}
}
}
}
glEnd
();
glBegin
(
GL_QUADS
);
setMaterial
(
cyan
);
setMaterial
(
cyan
);
for
(
float
x
=
-
0.5
;
x
<
0.5
;
x
+=
increment
)
{
for
(
float
x
=
-
0.5
;
x
<
0.5
;
x
+=
increment
)
{
for
(
float
y
=
-
0.5
;
y
<
0.5
;
y
+=
increment
)
{
for
(
float
y
=
-
0.5
;
y
<
0.5
;
y
+=
increment
)
{
...
@@ -158,9 +190,9 @@ void CubeWidget::showWireframe()
...
@@ -158,9 +190,9 @@ void CubeWidget::showWireframe()
qDebug
(
"show Wireframe"
);
qDebug
(
"show Wireframe"
);
glShadeModel
(
GL_FLAT
);
glShadeModel
(
GL_FLAT
);
// glDisable(GL_CULL_FACE);
glPolygonMode
(
GL_FRONT_AND_BACK
,
GL_LINE
);
glPolygonMode
(
GL_FRONT_AND_BACK
,
GL_LINE
);
shader
->
release
();
updateGL
();
updateGL
();
}
}
...
@@ -168,8 +200,8 @@ void CubeWidget::showFlat()
...
@@ -168,8 +200,8 @@ void CubeWidget::showFlat()
{
{
qDebug
(
"show Flat"
);
qDebug
(
"show Flat"
);
glShadeModel
(
GL_FLAT
);
glShadeModel
(
GL_FLAT
);
// glEnable(GL_CULL_FACE);
glPolygonMode
(
GL_FRONT_AND_BACK
,
GL_FILL
);
glPolygonMode
(
GL_FRONT_AND_BACK
,
GL_FILL
);
shader
->
release
();
updateGL
();
updateGL
();
}
}
...
@@ -177,15 +209,17 @@ void CubeWidget::showGouraut()
...
@@ -177,15 +209,17 @@ void CubeWidget::showGouraut()
{
{
qDebug
(
"show Gouraut"
);
qDebug
(
"show Gouraut"
);
glShadeModel
(
GL_SMOOTH
);
glShadeModel
(
GL_SMOOTH
);
// glEnable(GL_CULL_FACE);
glPolygonMode
(
GL_FRONT_AND_BACK
,
GL_FILL
);
glPolygonMode
(
GL_FRONT_AND_BACK
,
GL_FILL
);
shader
->
release
();
updateGL
();
updateGL
();
}
}
void
CubeWidget
::
showPhong
()
void
CubeWidget
::
showPhong
()
{
{
qDebug
(
"show Phong"
);
qDebug
(
"show Phong"
);
showFlat
();
glPolygonMode
(
GL_FRONT_AND_BACK
,
GL_FILL
);
shader
->
bind
();
updateGL
();
}
}
...
@@ -206,11 +240,10 @@ void CubeWidget::home()
...
@@ -206,11 +240,10 @@ void CubeWidget::home()
void
CubeWidget
::
mousePressEvent
(
QMouseEvent
*
event
)
void
CubeWidget
::
mousePressEvent
(
QMouseEvent
*
event
)
{
{
// if (event->isAccepted())
// if (event->isAccepted())
// return;
// return;
if
(
event
->
buttons
()
&
Qt
::
LeftButton
)
{
if
(
event
->
buttons
()
&
Qt
::
LeftButton
)
{
qDebug
(
"press"
);
lastPos
=
trackballPoint
(
event
->
pos
().
x
(),
event
->
pos
().
y
());
lastPos
=
trackballPoint
(
event
->
pos
().
x
(),
event
->
pos
().
y
());
...
@@ -226,11 +259,10 @@ void CubeWidget::mouseMoveEvent(QMouseEvent *event )
...
@@ -226,11 +259,10 @@ void CubeWidget::mouseMoveEvent(QMouseEvent *event )
//http://web.cse.ohio-state.edu/~hwshen/781/Site/Slides_files/trackball.pdf
//http://web.cse.ohio-state.edu/~hwshen/781/Site/Slides_files/trackball.pdf
//http://doc.qt.io/qt-5/qtwidgets-graphicsview-boxes-example.html
//http://doc.qt.io/qt-5/qtwidgets-graphicsview-boxes-example.html
// if (event->isAccepted())
// if (event->isAccepted())
// return;
// return;
if
(
event
->
buttons
()
&
Qt
::
LeftButton
)
{
if
(
event
->
buttons
()
&
Qt
::
LeftButton
)
{
qDebug
(
"move"
);
move
(
trackballPoint
(
event
->
pos
().
x
(),
event
->
pos
().
y
()));
move
(
trackballPoint
(
event
->
pos
().
x
(),
event
->
pos
().
y
()));
//m_trackBalls[0].move(pixelPosToViewPos(event->scenePos()), m_trackBalls[2].rotation().conjugate());
//m_trackBalls[0].move(pixelPosToViewPos(event->scenePos()), m_trackBalls[2].rotation().conjugate());
...
@@ -242,25 +274,25 @@ void CubeWidget::mouseMoveEvent(QMouseEvent *event )
...
@@ -242,25 +274,25 @@ void CubeWidget::mouseMoveEvent(QMouseEvent *event )
void
CubeWidget
::
move
(
QVector3D
*
newPos
)
void
CubeWidget
::
move
(
QVector3D
*
newPos
)
{
{
QVector3D
axis
=
QVector3D
::
crossProduct
(
*
lastPos
,
*
newPos
);
QVector3D
axis
=
QVector3D
::
crossProduct
(
*
lastPos
,
*
newPos
);
//warum so besser
//warum so besser
float
angle
=
180
/
M_PI
*
std
::
asin
(
std
::
sqrt
(
QVector3D
::
dotProduct
(
axis
,
axis
)));
float
angle
=
180
/
M_PI
*
std
::
asin
(
std
::
sqrt
(
QVector3D
::
dotProduct
(
axis
,
axis
)));
axis
.
normalize
();
axis
.
normalize
();
axis
=
rotation
->
conjugate
().
rotatedVector
(
axis
);
//
axis = rotation->conjugate().rotatedVector(axis);
QQuaternion
newRot
=
QQuaternion
::
fromAxisAndAngle
(
axis
,
angle
)
*
*
rotation
;
QQuaternion
newRot
=
QQuaternion
::
fromAxisAndAngle
(
axis
,
angle
)
*
*
rotation
;
rotation
=
new
QQuaternion
(
newRot
.
toVector4D
());
rotation
=
new
QQuaternion
(
newRot
.
toVector4D
());
lastPos
=
newPos
;
lastPos
=
newPos
;
updateGL
();
updateGL
();
}
}
QVector3D
*
CubeWidget
::
trackballPoint
(
int
x
,
int
y
){
QVector3D
*
CubeWidget
::
trackballPoint
(
int
x
,
int
y
){
float
xo
,
yo
,
zo
;
float
xo
,
yo
,
zo
;
qDebug
()
<<
"x:"
<<
x
<<
" y:"
<<
y
;
//
qDebug()<<"x:"<< x << " y:"<<y;
xo
=
((
2.0
*
x
)
-
width
())
/
height
();
xo
=
((
2.0
*
x
)
-
width
())
/
height
();
yo
=
(
height
()
-
(
2.0
*
y
))
/
width
();
yo
=
(
height
()
-
(
2.0
*
y
))
/
width
();
...
@@ -271,7 +303,7 @@ QVector3D* CubeWidget::trackballPoint(int x, int y){
...
@@ -271,7 +303,7 @@ QVector3D* CubeWidget::trackballPoint(int x, int y){
QVector3D
*
pos
=
new
QVector3D
(
xo
,
yo
,
zo
);
QVector3D
*
pos
=
new
QVector3D
(
xo
,
yo
,
zo
);
pos
->
normalize
();
pos
->
normalize
();
qDebug
()
<<
"x:"
<<
xo
<<
" y:"
<<
yo
<<
" z:"
<<
zo
;
//
qDebug()<<"x:"<< xo << " y:"<<yo<<" z:"<<zo;
return
pos
;
return
pos
;
}
}
...
...
A1/hellocube/cubewidget.h
View file @
26c7c733
...
@@ -46,6 +46,10 @@ private:
...
@@ -46,6 +46,10 @@ private:
QVector3D
*
lastPos
;
QVector3D
*
lastPos
;
bool
mousDown
=
false
;
bool
mousDown
=
false
;
QGLShaderProgram
*
shader
;
void
prepareShader
();
void
perspective
(
GLdouble
fovy
,
GLdouble
aspect
,
GLdouble
zNear
,
GLdouble
zFar
);
void
perspective
(
GLdouble
fovy
,
GLdouble
aspect
,
GLdouble
zNear
,
GLdouble
zFar
);
void
setMaterial
(
GLfloat
*
color
);
void
setMaterial
(
GLfloat
*
color
);
void
move
(
QVector3D
*
newPos
);
void
move
(
QVector3D
*
newPos
);
...
...
A1/hellocube/hellocube.pro
View file @
26c7c733
...
@@ -20,3 +20,7 @@ HEADERS += mainwindow.h \
...
@@ -20,3 +20,7 @@ HEADERS += mainwindow.h \
RESOURCES
+=
\
RESOURCES
+=
\
hellocube
.
qrc
hellocube
.
qrc
DISTFILES
+=
\
phong
.
frag
\
phong
.
vert
A1/hellocube/hellocube.qrc
View file @
26c7c733
...
@@ -5,5 +5,7 @@
...
@@ -5,5 +5,7 @@
<file>img/gouraud.png</file>
<file>img/gouraud.png</file>
<file>img/phong.png</file>
<file>img/phong.png</file>
<file>img/wireframe.png</file>
<file>img/wireframe.png</file>
<file>phong.frag</file>
<file>phong.vert</file>
</qresource>
</qresource>
</RCC>
</RCC>
A1/hellocube/phong.frag
0 → 100644
View file @
26c7c733
uniform
gl_LightSourceParameters
gl_LightSource
[
gl_MaxLights
];
uniform
gl_MaterialParameters
gl_FrontMaterial
;
uniform
gl_LightModelProducts
gl_FrontLightModelProduct
;
varying
vec3
position
;
varying
vec3
normal
;
void
main
(
void
)
{
vec3
N
=
normalize
(
normal
);
vec3
L
=
normalize
(
vec3
(
gl_LightSource
[
0
].
position
)
-
position
);
vec3
V
=
normalize
(
-
position
.
xyz
);
vec3
R
=
reflect
(
-
L
,
N
);
vec4
diffuse
=
vec4
(
max
(
dot
(
L
,
N
),
0
.
0
));
vec4
specular
=
vec4
(
pow
(
max
(
dot
(
R
,
V
),
0
.
0
),
gl_FrontMaterial
.
shininess
));
vec4
Iamb
=
gl_FrontLightProduct
[
0
].
ambient
;
vec4
Idiff
=
gl_FrontLightProduct
[
0
].
diffuse
*
diffuse
;
Idiff
=
clamp
(
Idiff
,
0
.
0
,
1
.
0
);
vec4
Ispec
=
gl_FrontLightProduct
[
0
].
specular
*
specular
;
Ispec
=
clamp
(
Idiff
,
0
.
0
,
1
.
0
);
vec4
color
=
/*gl_FrontLightModelProduct.sceneColor +*/
Iamb
+
Idiff
+
Ispec
;
gl_FragColor
=
vec4
(
color
);
}
A1/hellocube/phong.vert
0 → 100644
View file @
26c7c733
attribute
vec4
gl_Vertex
;
attribute
vec3
gl_Normal
;
uniform
mat4
gl_ModelViewProjectionMatrix
;
uniform
mat4
gl_ModelViewMatrix
;
uniform
mat3
gl_NormalMatrix
;
varying
vec3
position
;
varying
vec3
normal
;
void
main
(
void
)
{
normal
=
normalize
(
gl_NormalMatrix
*
gl_Normal
);
position
=
vec3
(
gl_ModelViewMatrix
*
gl_Vertex
);
gl_Position
=
gl_ModelViewProjectionMatrix
*
gl_Vertex
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a 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