[Code] Neuen Beispielcode ergänzt
This commit is contained in:
parent
ec4663e293
commit
e12a6f70a1
5 changed files with 283 additions and 10 deletions
|
@ -7,4 +7,14 @@ endif()
|
||||||
find_package(OpenGL REQUIRED)
|
find_package(OpenGL REQUIRED)
|
||||||
find_package(GLUT REQUIRED)
|
find_package(GLUT REQUIRED)
|
||||||
|
|
||||||
add_subdirectory(hello_world)
|
add_executable (hello_world hello_world/helloWorld.c)
|
||||||
|
target_include_directories(hello_world PRIVATE ${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS})
|
||||||
|
target_link_libraries(hello_world ${OPENGL_LIBRARIES} ${GLUT_LIBRARY})
|
||||||
|
|
||||||
|
add_executable (culling box/template_culling.c)
|
||||||
|
target_include_directories(culling PRIVATE ${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS})
|
||||||
|
target_link_libraries(culling ${OPENGL_LIBRARIES} ${GLUT_LIBRARY})
|
||||||
|
|
||||||
|
add_executable (koordinatensystem box/template_koordinatensystemUndPunkte.c)
|
||||||
|
target_include_directories(koordinatensystem PRIVATE ${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS})
|
||||||
|
target_link_libraries(koordinatensystem ${OPENGL_LIBRARIES} ${GLUT_LIBRARY})
|
||||||
|
|
84
Code/box/template_culling.c
Normal file
84
Code/box/template_culling.c
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
#include <GL/glut.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int width = 800;
|
||||||
|
int height = 600;
|
||||||
|
|
||||||
|
void init(int argc, char** argv) {
|
||||||
|
glutInit(&argc, argv);
|
||||||
|
glutInitDisplayMode(GLUT_SINGLE);
|
||||||
|
glutInitWindowSize(width, height);
|
||||||
|
glutInitWindowPosition(100, 100);
|
||||||
|
glViewport(0,0,width,height);
|
||||||
|
glutCreateWindow("Culling");
|
||||||
|
glMatrixMode(GL_PROJECTION);
|
||||||
|
glLoadIdentity();
|
||||||
|
glMatrixMode(GL_MODELVIEW);
|
||||||
|
glOrtho(-2.0, 2.0,-2.0, 2.0, -2.0, 2.0);
|
||||||
|
gluLookAt(0.25,0.5,0.1,0,0,0,0,1,0);
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
glEnable(GL_CULL_FACE);
|
||||||
|
glCullFace(GL_FRONT);
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void display(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
glDepthFunc(GL_LEQUAL);
|
||||||
|
|
||||||
|
glBegin(GL_QUADS);
|
||||||
|
|
||||||
|
glColor3f(1.0f, 0.0f, 0.0f);
|
||||||
|
// FRONT
|
||||||
|
glVertex3f(-0.5f, -0.5f, 0.5f);
|
||||||
|
glVertex3f( 0.5f, -0.5f, 0.5f);
|
||||||
|
glVertex3f( 0.5f, 0.5f, 0.5f);
|
||||||
|
glVertex3f(-0.5f, 0.5f, 0.5f);
|
||||||
|
// BACK
|
||||||
|
glVertex3f(-0.5f, -0.5f, -0.5f);
|
||||||
|
glVertex3f(-0.5f, 0.5f, -0.5f);
|
||||||
|
glVertex3f( 0.5f, 0.5f, -0.5f);
|
||||||
|
glVertex3f( 0.5f, -0.5f, -0.5f);
|
||||||
|
|
||||||
|
glColor3f(0.0f, 1.0f, 0.0f);
|
||||||
|
// LEFT
|
||||||
|
glVertex3f(-0.5f, -0.5f, 0.5f);
|
||||||
|
glVertex3f(-0.5f, 0.5f, 0.5f);
|
||||||
|
glVertex3f(-0.5f, 0.5f, -0.5f);
|
||||||
|
glVertex3f(-0.5f, -0.5f, -0.5f);
|
||||||
|
// RIGHT
|
||||||
|
glVertex3f( 0.5f, -0.5f, -0.5f);
|
||||||
|
glVertex3f( 0.5f, 0.5f, -0.5f);
|
||||||
|
glVertex3f( 0.5f, 0.5f, 0.5f);
|
||||||
|
glVertex3f( 0.5f, -0.5f, 0.5f);
|
||||||
|
|
||||||
|
glColor3f(0.0f, 0.0f, 1.0f);
|
||||||
|
// BOTTOM
|
||||||
|
glVertex3f(-0.5f, -0.5f, 0.5f);
|
||||||
|
glVertex3f(-0.5f, -0.5f, -0.5f);
|
||||||
|
glVertex3f( 0.5f, -0.5f, -0.5f);
|
||||||
|
glVertex3f( 0.5f, -0.5f, 0.5f);
|
||||||
|
glEnd();
|
||||||
|
|
||||||
|
glFlush();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
init(argc, argv);
|
||||||
|
glutDisplayFunc(display);
|
||||||
|
glutMainLoop();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
184
Code/box/template_koordinatensystemUndPunkte.c
Normal file
184
Code/box/template_koordinatensystemUndPunkte.c
Normal file
|
@ -0,0 +1,184 @@
|
||||||
|
#include <GL/glut.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#pragma warning(disable:4996)
|
||||||
|
|
||||||
|
#define PI 3.14159265358979323846
|
||||||
|
|
||||||
|
int width = 800;
|
||||||
|
int height = 600;
|
||||||
|
|
||||||
|
void init(int argc, char** argv) {
|
||||||
|
glutInit(&argc, argv);
|
||||||
|
glutInitDisplayMode(GLUT_SINGLE);
|
||||||
|
glutInitWindowSize(width, height);
|
||||||
|
glutInitWindowPosition(100, 100);
|
||||||
|
glViewport(0, 0, width, height);
|
||||||
|
glutCreateWindow("Programmieruebung Kreis-Plotter");
|
||||||
|
glMatrixMode(GL_PROJECTION);
|
||||||
|
glLoadIdentity();
|
||||||
|
gluOrtho2D(0, width, 0, height);
|
||||||
|
glMatrixMode(GL_MODELVIEW);
|
||||||
|
glEnable(GL_POINT_SMOOTH);
|
||||||
|
glEnable(GL_BLEND);
|
||||||
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
}
|
||||||
|
|
||||||
|
void drawCoordinateSystem(float* origin, int xLength, int yLength, int numTicksX, int numTicksY, float minX, float maxX, float minY, float maxY) {
|
||||||
|
|
||||||
|
float xTickDistance = (float) xLength / (float) numTicksX;
|
||||||
|
float yTickDistance = (float) yLength / (float) numTicksY;
|
||||||
|
|
||||||
|
int k;
|
||||||
|
float j;
|
||||||
|
|
||||||
|
float resX, resY;
|
||||||
|
|
||||||
|
char label[10];
|
||||||
|
|
||||||
|
resX = (maxX - minX) / xLength;
|
||||||
|
resY = (maxY - minY) / yLength;
|
||||||
|
|
||||||
|
glColor3f(0.0, 0.0, 0.0);
|
||||||
|
/* drawing the coordinate system */
|
||||||
|
glBegin(GL_LINES);
|
||||||
|
// x axis
|
||||||
|
glVertex2f(origin[0], origin[1]);
|
||||||
|
glVertex2f(origin[0] + xLength, origin[1]);
|
||||||
|
// y axis
|
||||||
|
glVertex2f(origin[0], origin[1]);
|
||||||
|
glVertex2f(origin[0], origin[1] + yLength);
|
||||||
|
// arrow head at x axis
|
||||||
|
glVertex2f(origin[0] + xLength, origin[1]);
|
||||||
|
glVertex2f(origin[0] + xLength - 15, origin[1] - 10);
|
||||||
|
glVertex2f(origin[0] + xLength, origin[1]);
|
||||||
|
glVertex2f(origin[0] + xLength - 15, origin[1] + 10);
|
||||||
|
// arrow head at y axis
|
||||||
|
glVertex2f(origin[0], origin[1] + yLength);
|
||||||
|
glVertex2f(origin[0] - 10, origin[1] + yLength - 15);
|
||||||
|
glVertex2f(origin[0], origin[1] + yLength);
|
||||||
|
glVertex2f(origin[0] + 10, origin[1] + yLength - 15);
|
||||||
|
glEnd();
|
||||||
|
|
||||||
|
// drawing ticks at x axis
|
||||||
|
for (j = 0; j < xLength; j = j + xTickDistance) {
|
||||||
|
glBegin(GL_LINES);
|
||||||
|
glVertex2f(origin[0] + j, origin[1] - 5);
|
||||||
|
glVertex2f(origin[0] + j, origin[1] + 5);
|
||||||
|
glEnd();
|
||||||
|
sprintf(label, "%5.2f", minX + (j*resX));
|
||||||
|
glRasterPos2i((int) round(origin[0] + j - (8 * 5)), (int) round(origin[1] - 13 - 10));
|
||||||
|
for (k = 0; k < 5; k++) {
|
||||||
|
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, label[k]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// drawing ticks at y axis
|
||||||
|
for (j = 0; j < yLength; j = j + yTickDistance) {
|
||||||
|
glBegin(GL_LINES);
|
||||||
|
glVertex2f(origin[0] - 5, origin[1] + j);
|
||||||
|
glVertex2f(origin[0] + 5, origin[1] + j);
|
||||||
|
glEnd();
|
||||||
|
sprintf(label, "%5.2f", minY + (j*resY));
|
||||||
|
glRasterPos2i((int) round(origin[0] - (8 * 5) - 10), (int) round(origin[1] + j - 13 / 2));
|
||||||
|
for (k = 0; k < 5; k++) {
|
||||||
|
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, label[k]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void drawCircle(float origin[2], float center[2], float radius, GLint numPoints)
|
||||||
|
{
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// - calculate numPoints points on the surface of the circle with given center and radius
|
||||||
|
// hint: see circle equation in parametric form
|
||||||
|
// - position points in the coordinate system starting at given origin
|
||||||
|
// - draw the points by OpenGL commands
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void display(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
int n;
|
||||||
|
int xTickDistance = 50;
|
||||||
|
int yTickDistance = 50;
|
||||||
|
int numPoints = 30;
|
||||||
|
float radius[] = { 50, 100, 150 };
|
||||||
|
float center[] = { 300, 200 };
|
||||||
|
float origin[] = { 100, 100 };
|
||||||
|
GLfloat c[] = { 0.0,0.0,0.0 };
|
||||||
|
int xLength = 600;
|
||||||
|
int yLength = 400;
|
||||||
|
float size = 2;
|
||||||
|
|
||||||
|
|
||||||
|
/* settings for the coordinate system */
|
||||||
|
int numTicksX = 6;
|
||||||
|
int numTicksY = 4;
|
||||||
|
float minX = 0;
|
||||||
|
float maxX = 600;
|
||||||
|
float minY = 0;
|
||||||
|
float maxY = 400;
|
||||||
|
|
||||||
|
|
||||||
|
/* clearing the background color */
|
||||||
|
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
|
||||||
|
drawCoordinateSystem(origin, xLength, yLength, numTicksX, numTicksY, minX, maxX, minY, maxY);
|
||||||
|
|
||||||
|
|
||||||
|
for (n = 0; n < 3; n++) {
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
// - set the point size different for each loop iteration
|
||||||
|
// - set the color different for each loop iteration
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
drawCircle(origin, center, radius[n], numPoints);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
glFlush();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
init(argc, argv);
|
||||||
|
|
||||||
|
glutDisplayFunc(display);
|
||||||
|
glutMainLoop();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
cmake_minimum_required (VERSION 3.8)
|
|
||||||
|
|
||||||
add_executable (hello_world helloWorld.c)
|
|
||||||
target_include_directories(hello_world PRIVATE ${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS} )
|
|
||||||
target_link_libraries(hello_world ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} )
|
|
|
@ -1,8 +1,8 @@
|
||||||
#include <GL/glut.h>
|
#include <GL/glut.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
int width = 640;
|
static int width = 640;
|
||||||
int height = 480;
|
static int height = 480;
|
||||||
|
|
||||||
void init(int argc, char** argv)
|
void init(int argc, char** argv)
|
||||||
{
|
{
|
||||||
|
@ -22,7 +22,7 @@ void init(int argc, char** argv)
|
||||||
void display(void)
|
void display(void)
|
||||||
{
|
{
|
||||||
char* myText = "Hello World!";
|
char* myText = "Hello World!";
|
||||||
int j;
|
unsigned long j;
|
||||||
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
glColor3f(1.0, 0.0, 0.0);
|
glColor3f(1.0, 0.0, 0.0);
|
||||||
|
@ -36,7 +36,7 @@ void display(void)
|
||||||
|
|
||||||
glColor3f(1.0, 1.0, 1.0);
|
glColor3f(1.0, 1.0, 1.0);
|
||||||
glRasterPos2i((width / 2) - (width / 4) + 10, (height / 2) - (height / 4) + 10);
|
glRasterPos2i((width / 2) - (width / 4) + 10, (height / 2) - (height / 4) + 10);
|
||||||
for (j = 0; j < strlen(myText); j++) {
|
for (j = 0ul; j < strlen(myText); j++) {
|
||||||
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, myText[j]);
|
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, myText[j]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue