1kingja / graphics Goto Github PK
View Code? Open in Web Editor NEWCode for my OpenGL graphics on YouTube
Code for my OpenGL graphics on YouTube
From the OpenGLGraphics Playlist - Video 36 - OpenGL Perspective Projection
I've checked the code several times, can't find any difference. I even copied some code from this repository over mine, to be shure. But still same result. Just a black window.
I use qmake, GNUmake, g++
everything else worked flawlessly up to this point. Hope Someone can see what I don't see right now or has an idea why It's not working with my setup or recent library versions maybe etc.
Cheers.
edit: with or without this:
(60.0f * glm::pi<GLfloat>() ) / 180.0f
nohing changed.
update: The code from the next video, using the fullTrasnsformMatrix works and the above
glm::pi() function is needed for the correct viewing frustum. But why is the other code not yielding any results. Hope someone can clear that up for me.
/*** vertex_shader.glsl ***/
#version 430
in layout(location=0) vec3 position;
in layout(location=1) vec3 vertexColor;
uniform mat4 modelTransformMatrix;
uniform mat4 projectionMatrix;
out vec3 theColor;
void main()
{
vec4 v = vec4(position, 1.0);
vec4 newPosition = modelTransformMatrix * v;
vec4 projectedPosition = projectionMatrix * newPosition;
gl_Position = projectedPosition;
theColor = vertexColor;
}
/*** fragmen_shader.glsl ***/
#version 430
in vec3 theColor;
out vec4 daColor;
void main()
{
daColor = vec4(theColor, 1.0);
}
/*** MeGLWindow.cpp ***/
#include <GL/glew.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/constants.hpp>
#include <iostream>
#include <fstream>
#include "MeGLWindow.h"
#include "primitives/Vertex.h"
#include "primitives/ShapeGenerator.h"
using namespace std;
using glm::vec3;
using glm::mat4;
const uint NUM_VERICES_PER_TRI = 3;
const uint NUM_FLOATS_PER_VERTICE = 6;
const uint VERTEX_BYTE_SIZE = NUM_FLOATS_PER_VERTICE * sizeof(float);
GLuint programID;
GLuint numIndices;
void sendDataToOpenGL()
{
ShapeData shape = ShapeGenerator::makeCube();
GLuint vertexBufferID;
glGenBuffers(1, &vertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
glBufferData(GL_ARRAY_BUFFER, shape.vertexBufferSize(), shape.vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 6, 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 6, (char*)(sizeof(float) * 3));
GLuint indexArrayBufferID;
glGenBuffers(1, &indexArrayBufferID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexArrayBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, shape.indexBufferSize(), shape.indices, GL_STATIC_DRAW);
numIndices = shape.numIndices;
shape.cleanup();
}
void MeGLWindow::paintGL()
{
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glViewport(0, 0, width(), height());
mat4 modelTransformMatrix = glm::translate(mat4(), vec3(0.0f, 0.0f, -3.0f));
mat4 projectionMatrix = glm::perspective((60.0f * glm::pi<GLfloat>() ) / 180.0f, ((float)width()) / height(), 0.1f, 10.0f);
GLint modelTransformMatrixUniformLocation =
glGetUniformLocation(programID, "modelTransformMatrix");
GLint projectionMatrixUniformLocation =
glGetUniformLocation(programID, "projectionMatrix");
glUniformMatrix4fv(modelTransformMatrixUniformLocation, 1,
GL_FALSE, &modelTransformMatrix[0][0]);
glUniformMatrix4fv(projectionMatrixUniformLocation, 1,
GL_FALSE, &projectionMatrix[0][0]);
glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, 0);
}
bool checkStatus(
GLuint objectID,
PFNGLGETSHADERIVPROC objectPropertyGetterFunc,
PFNGLGETSHADERINFOLOGPROC getInfoLogFunc,
GLenum statusType)
{
GLint status;
objectPropertyGetterFunc(objectID, statusType, &status);
if (status != GL_TRUE)
{
GLint infoLogLength;
objectPropertyGetterFunc(objectID, GL_INFO_LOG_LENGTH, &infoLogLength);
GLchar* buffer = new GLchar[infoLogLength];
GLsizei bufferSize;
getInfoLogFunc(objectID, infoLogLength, &bufferSize, buffer);
cout << buffer << endl;
delete[] buffer;
return false;
}
return true;
}
bool checkShaderStatus(GLuint shaderID)
{
return checkStatus(shaderID, glGetShaderiv, glGetShaderInfoLog, GL_COMPILE_STATUS);
}
bool checkProgramStatus(GLuint programID)
{
return checkStatus(programID, glGetProgramiv, glGetProgramInfoLog, GL_LINK_STATUS);
}
string readShaderCode(const char* fileName)
{
ifstream meInput(fileName);
if ( ! meInput.good())
{
cout << "File failed to load..." << fileName;
exit(1);
}
return std::string(
std::istreambuf_iterator<char>(meInput),
std::istreambuf_iterator<char>());
}
void installShaders()
{
GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
const GLchar* adapter[1];
string temp = readShaderCode("vertex_shader.glsl");
adapter[0] = temp.c_str();
glShaderSource(vertexShaderID, 1, adapter, 0);
temp = readShaderCode("fragment_shader.glsl");
adapter[0] = temp.c_str();
glShaderSource(fragmentShaderID, 1, adapter, 0);
glCompileShader(vertexShaderID);
glCompileShader(fragmentShaderID);
if( ! checkShaderStatus(vertexShaderID) || ! checkShaderStatus(fragmentShaderID))
return;
programID = glCreateProgram();
glAttachShader(programID, vertexShaderID);
glAttachShader(programID, fragmentShaderID);
glLinkProgram(programID);
if ( ! checkProgramStatus(programID))
return;
glUseProgram(programID);
}
void MeGLWindow::initializeGL()
{
glewInit();
glEnable(GL_DEPTH_TEST);
sendDataToOpenGL();
installShaders();
}
/*** MeGLWindow.h ***/
#ifndef ME_GL_WINDOW_H
#define ME_GL_WINDOW_H
#include <qt/QtOpenGL/QGLWidget>
class MeGLWindow : public QGLWidget
{
protected:
void initializeGL();
void paintGL();
public:
};
#endif // ME_GL_WINDOW_H
/*** ShapeGenerator.cpp ***/
#include <glm/glm.hpp>
#include <cstring> // Included this for memcpy()
#include "ShapeGenerator.h"
#include "Vertex.h"
using glm::vec3;
#define NUM_ARRAY_ELEMENTS(a) sizeof(a) / sizeof(*a)
ShapeData ShapeGenerator::makeTriangle()
{
ShapeData ret;
Vertex myTri[] =
{
glm::vec3(+0.0f, +1.0f, +0.0f),
glm::vec3(+1.0f, +0.0f, +0.0f),
glm::vec3(-1.0f, -1.0f, +0.0f),
glm::vec3(+0.0f, +1.0f, +0.0f),
glm::vec3(+1.0f, -1.0f, +0.0f),
glm::vec3(+0.0f, +0.0f, +1.0f),
};
ret.numVertices = NUM_ARRAY_ELEMENTS(myTri);
ret.vertices = new Vertex[ret.numVertices];
memcpy(ret.vertices, myTri, sizeof(myTri));
GLushort indices[] = { 0, 1, 2 };
ret.numIndices = NUM_ARRAY_ELEMENTS(indices);
ret.indices = new GLushort[ret.numIndices];
memcpy(ret.indices, indices, sizeof(indices));
return ret;
}
ShapeData ShapeGenerator::makeCube() {
ShapeData ret;
Vertex stackVerts[] = {
vec3(-1.0f, +1.0f, +1.0f), // 0
vec3(+1.0f, +0.0f, +0.0f), // Color
vec3(+1.0f, +1.0f, +1.0f), // 1
vec3(+0.0f, +1.0f, +0.0f), // Color
vec3(+1.0f, +1.0f, -1.0f), // 2
vec3(+0.0f, +0.0f, +1.0f), // Color
vec3(-1.0f, +1.0f, -1.0f), // 3
vec3(+1.0f, +1.0f, +1.0f), // Color
vec3(-1.0f, +1.0f, -1.0f), // 4
vec3(+1.0f, +0.0f, +1.0f), // Color
vec3(+1.0f, +1.0f, -1.0f), // 5
vec3(+0.0f, +0.5f, +0.2f), // Color
vec3(+1.0f, -1.0f, -1.0f), // 6
vec3(+0.8f, +0.6f, +0.4f), // Color
vec3(-1.0f, -1.0f, -1.0f), // 7
vec3(+0.3f, +1.0f, +0.5f), // Color
vec3(+1.0f, +1.0f, -1.0f), // 8
vec3(+0.2f, +0.5f, +0.2f), // Color
vec3(+1.0f, +1.0f, +1.0f), // 9
vec3(+0.9f, +0.3f, +0.7f), // Color
vec3(+1.0f, -1.0f, +1.0f), // 10
vec3(+0.3f, +0.7f, +0.5f), // Color
vec3(+1.0f, -1.0f, -1.0f), // 11
vec3(+0.5f, +0.7f, +0.5f), // Color
vec3(-1.0f, +1.0f, +1.0f), // 12
vec3(+0.7f, +0.8f, +0.2f), // Color
vec3(-1.0f, +1.0f, -1.0f), // 13
vec3(+0.5f, +0.7f, +0.3f), // Color
vec3(-1.0f, -1.0f, -1.0f), // 14
vec3(+0.4f, +0.7f, +0.7f), // Color
vec3(-1.0f, -1.0f, +1.0f), // 15
vec3(+0.2f, +0.5f, +1.0f), // Color
vec3(+1.0f, +1.0f, +1.0f), // 16
vec3(+0.6f, +1.0f, +0.7f), // Color
vec3(-1.0f, +1.0f, +1.0f), // 17
vec3(+0.6f, +0.4f, +0.8f), // Color
vec3(-1.0f, -1.0f, +1.0f), // 18
vec3(+0.2f, +0.8f, +0.7f), // Color
vec3(+1.0f, -1.0f, +1.0f), // 19
vec3(+0.2f, +0.7f, +1.0f), // Color
vec3(+1.0f, -1.0f, -1.0f), // 20
vec3(+0.8f, +0.3f, +0.7f), // Color
vec3(-1.0f, -1.0f, -1.0f), // 21
vec3(+0.8f, +0.9f, +0.5f), // Color
vec3(-1.0f, -1.0f, +1.0f), // 22
vec3(+0.5f, +0.8f, +0.5f), // Color
vec3(+1.0f, -1.0f, +1.0f), // 23
vec3(+0.9f, +1.0f, +0.2f), // Color
};
ret.numVertices = NUM_ARRAY_ELEMENTS(stackVerts);
ret.vertices = new Vertex[ret.numVertices];
memcpy(ret.vertices, stackVerts, sizeof(stackVerts));
unsigned short stackIndices[] = {
0, 1, 2, 0, 2, 3, // Top
4, 5, 6, 4, 6, 7, // Front
8, 9, 10, 8, 10, 11, // Right
12, 13, 14, 12, 14, 15, // Left
16, 17, 18, 16, 18, 19, // Back
20, 22, 21, 20, 23, 22, // Bottom
};
ret.numIndices = NUM_ARRAY_ELEMENTS(stackIndices);
ret.indices = new GLushort[ret.numIndices];
memcpy(ret.indices, stackIndices, sizeof(stackIndices));
return ret;
}
/*** ShapeGenerator.h ***/
#pragma once
#include "ShapeData.h"
class ShapeGenerator
{
public:
static ShapeData makeTriangle();
static ShapeData makeCube();
};
/*** ShapeData.h ***/
#pragma once
#include <GL/glew.h>
#include "Vertex.h"
class Vertex;
struct ShapeData
{
ShapeData() : vertices(0), numVertices(0), indices(0), numIndices(0) {}
Vertex* vertices;
GLuint numVertices;
GLushort* indices;
GLuint numIndices;
GLsizeiptr vertexBufferSize() const
{
return numVertices * sizeof(Vertex);
}
GLsizeiptr indexBufferSize() const
{
return numIndices * sizeof(GLushort);
}
void cleanup()
{
delete [] vertices;
delete [] indices;
numVertices = numIndices = 0;
}
};
/*** Vertex.h ***/
#pragma once
#include <glm/glm.hpp>
struct Vertex
{
glm::vec3 position;
glm::vec3 color;
};
# GraphicsPad.pro
# Automatically generated by qmake (3.1)
TEMPLATE = app
TARGET = GraphicsPad
INCLUDEPATH += .
LIBS += -lGLEW
QT += widgets opengl
# The following define makes your compiler warn you if you use any
# feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
# Input
HEADERS += MeGLWindow.h primitives/ShapeGenerator.h primitives/ShapeData.h primitives/Vertex.h
SOURCES += main.cpp MeGLWindow.cpp primitives/ShapeGenerator.cpp
Where are the definitions for the height() and width() functions that you call in glViewport() and glm::perspective()?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.