Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <iostream> #include <cstdlib> #include <stdio.h> #include <stdlib.h> #include <vector> #include <GL/glew.h> #include <GLFW/glfw3.h> #include <glm/glm.hpp> #include "ShaderUtils.h" #include "Shape.h" #include "ShapeGenerator.h" class Window{ GLFWwindow* glfwWindow; GLuint shaderProgram; std::vector<Shape*> shapes; GLFWwindow* createGLFWwindow(int width, int height, std::string title); void adjustViewport(); void sendDataToOpenGL(); void useShaders(); void mainLoop(); void quit(); public: Window(int width, int height, std::string title); }; |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
#include "Window.h" Window::Window(int width, int height, std::string title){ if(glfwInit() != GL_TRUE){ std::cout << "Failed to initialize GLFW!" << std::endl; glfwTerminate(); //TODO quit execution } glfwWindow = createGLFWwindow(width, height, title); if(glfwWindow == NULL) { //TODO quit execution }; glClearColor(0.0f, 0.0f, 0.1f, 1.0f); useShaders(); sendDataToOpenGL(); mainLoop(); quit(); } void Window::useShaders(){ // create Shader GLuint vertexShader = ShaderUtils::loadShader("../res/shaders/VertexShader.vs", GL_VERTEX_SHADER); GLuint fragmentShader = ShaderUtils::loadShader("../res/shaders/FragmentShader.fs", GL_FRAGMENT_SHADER); shaderProgram = ShaderUtils::linkShaderProgram(vertexShader, fragmentShader, 0); glUseProgram(shaderProgram); } void Window::sendDataToOpenGL(){ // Location of variable "position" (VertexShader) in VAO GLint positionLoc = glGetAttribLocation(shaderProgram, "position"); GLint colorLoc = glGetAttribLocation(shaderProgram, "color"); Shape* arrow = ShapeGenerator::createArrow(positionLoc, colorLoc); shapes.push_back(arrow); Shape* tri = ShapeGenerator::createTriangle(positionLoc, colorLoc); //shapes.push_back(tri); } void Window::mainLoop(){ // main Loop while(!glfwWindowShouldClose(glfwWindow)){ adjustViewport(); glClear(GL_COLOR_BUFFER_BIT); for(auto itr = shapes.begin(); itr != shapes.end(); ++itr){ (*itr)->draw(); } glfwSwapBuffers(glfwWindow); glfwPollEvents(); } } void Window::quit(){ glfwDestroyWindow(glfwWindow); glfwTerminate(); for(auto itr = shapes.begin(); itr != shapes.end(); ++itr){ delete *itr; } } void Window::adjustViewport(){ GLint height, width; glfwGetWindowSize(glfwWindow, &width, &height); glViewport(0, 0, width, height); } GLFWwindow* Window::createGLFWwindow(int width, int height, std::string title){ glfwWindowHint(GLFW_SAMPLES, 4); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); GLFWwindow* window = glfwCreateWindow(width, height, title.c_str(), NULL, NULL); if(window == NULL){ std::cout << "Window could not be created!" << std::endl; glfwTerminate(); return NULL; } // Context creation... glfwMakeContextCurrent(window); // ...before glew initialisation ;) glewExperimental=GL_TRUE; GLenum err = glewInit(); if(err != GLEW_OK){ std::cout << "Failed to Init GLEW!" << std::endl; std::cout << glewGetErrorString(err) << std::endl; glfwTerminate(); return NULL; } std::cout << glGetString(GL_VERSION) << std::endl; return window; } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#pragma once #include <GL/glew.h> #include <glm/glm.hpp> struct Vertex{ glm::vec3 position; glm::vec3 color; }; class Shape{ GLuint VBOID, EBOID, VAOID; GLuint numElements; GLint positionLoc, colorLoc; void generateVAO(); public: Shape(); Shape(GLuint pVBOID, GLuint pEBOID, GLuint pNumElements, GLint pPositionLoc, GLint pColorLoc); ~Shape(); void draw(); }; |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
#include "Shape.h" #include "Window.h" #include <iostream> Shape::Shape(GLuint pVBOID, GLuint pEBOID, GLuint pNumElements, GLint pPositionLoc, GLint pColorLoc): VBOID(pVBOID), EBOID(pEBOID), numElements(pNumElements), positionLoc(pPositionLoc), colorLoc(pColorLoc){ generateVAO(); } void Shape::generateVAO(){ glGenVertexArrays(1, &VAOID); glBindVertexArray(VBOID); // Location of variable "position" (VertexShader) in VAO glEnableVertexAttribArray(positionLoc); glEnableVertexAttribArray(colorLoc); glBindBuffer(GL_ARRAY_BUFFER, VBOID); // Variable Location, Elements per Vertex, Datatype, normalized, stride, startposition glVertexAttribPointer(positionLoc, 3, GL_FLOAT, GL_FALSE, 6*sizeof(GLfloat), 0); glVertexAttribPointer(colorLoc, 3, GL_FLOAT, GL_FALSE, 6*sizeof(GLfloat), (void*)(3 * sizeof(float))); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); } Shape::~Shape(){ glDeleteBuffers(1, &VBOID); glDeleteBuffers(1, &EBOID); glDeleteBuffers(1, &VAOID); } void Shape::draw(){ std::cout << "1" << std::endl; glBindVertexArray(VBOID); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBOID); std::cout << "2" << std::endl; glDrawElements(GL_TRIANGLES, 3 * numElements, GL_UNSIGNED_SHORT, NULL); std::cout << "3" << std::endl; glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindVertexArray(0); } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 |
#pragma once #include <GL/glew.h> #include "Shape.h" class ShapeGenerator{ public: static Shape* createTriangle(GLint positionLoc, GLint colorLoc); static Shape* createArrow(GLint positionLoc, GLint colorLoc); }; |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
#include "ShapeGenerator.h" Shape* ShapeGenerator::createTriangle(GLint positionLoc, GLint colorLoc){ GLuint VBOID, EBOID; glGenBuffers(1, &VBOID); glGenBuffers(1, &EBOID); Vertex vertecies[] = { glm::vec3(+0.00f, +0.75f, +0.00f), glm::vec3(+1.0f, +0.0f, +0.0f), glm::vec3(-0.75f, -0.75f, +0.00f), glm::vec3(+0.0f, +1.0f, +0.0f), glm::vec3(+0.75f, -0.75f, +0.00f), glm::vec3(+0.0f, +0.0f, +1.0f), }; GLushort indexdata[] = { 0, 1, 2 }; glBindBuffer(GL_ARRAY_BUFFER, VBOID); glBufferData(GL_ARRAY_BUFFER, sizeof(vertecies), vertecies, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBOID); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexdata), indexdata, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); return new Shape(VBOID, EBOID, 1, positionLoc, colorLoc); } Shape* ShapeGenerator::createArrow(GLint positionLoc, GLint colorLoc){ GLuint VBOID, EBOID; glGenBuffers(1, &VBOID); Vertex vertecies[] = { glm::vec3(+0.00f, +0.75f, +0.0f), // 0 glm::vec3(+0.0f, +0.0f, +1.0f), glm::vec3(-0.50f, +0.25f, +0.0f), // 1 glm::vec3(+0.5f, +0.0f, +0.5f), glm::vec3(-0.25f, +0.25f, +0.0f), // 2 glm::vec3(+0.5f, +0.0f, +0.5f), glm::vec3(-0.25f, -0.50f, +0.0f), // 3 glm::vec3(+1.0f, +0.0f, +0.0f), glm::vec3(+0.25f, -0.50f, +0.0f), // 4 glm::vec3(+1.0f, +0.0f, +0.0f), glm::vec3(+0.25f, +0.25f, +0.0f), // 5 glm::vec3(+0.5f, +0.0f, +0.5f), glm::vec3(+0.50f, +0.25f, +0.0f), // 6 glm::vec3(+0.5f, +0.0f, +0.5f), }; glBindBuffer(GL_ARRAY_BUFFER, VBOID); glBufferData(GL_ARRAY_BUFFER, sizeof(vertecies), vertecies, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); GLushort indexdata[] = { 0, 1, 2, 0, 2, 5, 0, 5, 6, 2, 3, 5, 3, 4, 5 }; glGenBuffers(1, &EBOID); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBOID); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexdata), indexdata, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); Shape* ret = new Shape(VBOID, EBOID, 5, positionLoc, colorLoc); return ret; } |
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
C-/C++-Quelltext |
|
1 |
glDrawElements(GL_TRIANGLES, 3 * numElements, GL_UNSIGNED_SHORT, NULL); |
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von »Gnoccy« (09.07.2014, 18:45)
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Die Sache mit dem "Count" hat mich auch schon häufig ziemlich verwirrt.
Die Doku verwendet den Begriff "Elements" was wirklich sehr uneindeutig ist. Ein Element ist aber eben kein Primitiv.
So gibt "Count" NICHT nicht die Anzahl der Primitiven an, sondern die Anzahl Indices oder Vertices(in der Regel und wenn man mehrfach indizierte auch mehrfach zählt). Besonders verwirrend ist auch, dass es unter DirectX, soweit ich mich erinnere, auch genau anders war.
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
C-/C++-Quelltext |
|
1 |
glBindVertexArray(VBOID); |
Werbeanzeige