C++ (Qt)#include "singlepoint.h"#include <QGLShader>#include <QGLShaderProgram>#include <QOpenGLVertexArrayObject>#include <QPainter>#include <QDebug> SinglePoint::SinglePoint(QWidget *parent) : QGLWidget(parent){ setWindowTitle("Single Point");} void SinglePoint::initializeGL(){ glClearColor(1.0f, 0.0f, 0.0f, 1.0f); const char * vs_source = "#version 420 core \n" " \n" "void main(void) \n" "{ \n" " gl_Position = vec4(0.0, 0.0, 0.0, 1.0); \n" "} \n"; const char * fs_source = "#version 420 core \n" " \n" "out vec4 color; \n" " \n" "void main(void) \n" "{ \n" " color = vec4(0.0, 0.8, 1.0, 1.0); \n" "} \n"; QGLShader* fs = new QGLShader(QGLShader::Fragment); fs->compileSourceCode(fs_source); QGLShader* vs = new QGLShader(QGLShader::Vertex); vs->compileSourceCode(vs_source); program = new QGLShaderProgram(this); program->addShader(vs); program->addShader(fs); QOpenGLVertexArrayObject* vao = new QOpenGLVertexArrayObject(this); vao->bind();} void SinglePoint::paintGL(){ glClear(GL_COLOR_BUFFER_BIT); program->bind(); glPointSize(40.0f); glDrawArrays(GL_POINTS, 0, 1); program->release();}
C++ (Qt)/* * Copyright © 2012-2013 Graham Sellers * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice (including the next * paragraph) shall be included in all copies or substantial portions of the * Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ #include <sb6.h> class singlepoint_app : public sb6::application{ void init() { static const char title[] = "OpenGL SuperBible - Single Point"; sb6::application::init(); memcpy(info.title, title, sizeof(title)); } virtual void startup() { static const char * vs_source[] = { "#version 420 core \n" " \n" "void main(void) \n" "{ \n" " gl_Position = vec4(0.0, 0.0, 0.0, 1.0); \n" "} \n" }; static const char * fs_source[] = { "#version 420 core \n" " \n" "out vec4 color; \n" " \n" "void main(void) \n" "{ \n" " color = vec4(0.0, 0.8, 1.0, 1.0); \n" "} \n" }; program = glCreateProgram(); GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fs, 1, fs_source, NULL); glCompileShader(fs); GLuint vs = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vs, 1, vs_source, NULL); glCompileShader(vs); glAttachShader(program, vs); glAttachShader(program, fs); glLinkProgram(program); glGenVertexArrays(1, &vao); glBindVertexArray(vao); } virtual void render(double currentTime) { static const GLfloat red[] = { 1.0f, 0.0f, 0.0f, 1.0f }; glClearBufferfv(GL_COLOR, 0, red); glUseProgram(program); glPointSize(40.0f); glDrawArrays(GL_POINTS, 0, 1); } virtual void shutdown() { glDeleteVertexArrays(1, &vao); glDeleteProgram(program); } private: GLuint program; GLuint vao;}; DECLARE_MAIN(singlepoint_app)