Reputation: 91
I am following a tutorial to draw a triangle in the screen using OpenGL. I have followed all the steps correctly yet I only get a green screen, without the triangle.
This is my current code. DrawScene is called during the initialization of the application, and ResizeScene every WM_DRAW. My question is: Why is the triangle not showing with my code?
OpenGL version: 4.6
#include "glew.h"
GLuint program;
GLuint VAO, VBO, EBO;
void DrawScene(HWND _hWnd);
void SetupShaders()
{
const char* vertexSource = {
"#version 130 core\n"
"layout (location = 0) in vec3 iPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(iPos, 1.0f);\n"
"}\n"
"\0"
};
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexSource, NULL);
glCompileShader(vertexShader);
const char* fragmentSource = {
"#version 130 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(1.0f, 0.0f, 1.0f, 1.0f);\n"
"}\n"
"\0"
};
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
glCompileShader(fragmentShader);
program = glCreateProgram();
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glBindAttribLocation(program, 0, "iPos");
glLinkProgram(program);
}
void SetupBuffers()
{
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
float vertexData[] = {
0.0f, 0.0f, 0.0f,
10.0f, 10.0f, 0.0f,
10.0f, 0.0f, 0.0f,
0.0f, 10.0f, 0.0f
};
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
unsigned int indexData[] = {
0, 1, 2,
1, 2, 3
};
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexData), indexData, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(float) * 3, (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void SetupScene(HWND& _hWnd)
{
static PIXELFORMATDESCRIPTOR pfd = // pfd Tells Windows How We Want Things To Be
{
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
32, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
16, // 16Bit Z-Buffer (Depth Buffer)
0, // No Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
};
HDC hdc = GetDC(_hWnd);
int pf = ChoosePixelFormat(hdc, &pfd);
SetPixelFormat(hdc, pf, &pfd);
HGLRC hglrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, hglrc);
glewInit();
ShowWindow(_hWnd, SW_SHOW);
SetForegroundWindow(_hWnd);
SetFocus(_hWnd);
SetupShaders();
SetupBuffers();
}
void ResizeScene(HWND& _hWnd, size_t _width, size_t _height)
{
if (!_height)
{
_height = 1;
}
glViewport(0, 0, _width, _height);
DrawScene(_hWnd);
}
void DrawScene(HWND _hWnd)
{
HDC hdc = GetDC(_hWnd);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
glUseProgram(program);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, 0);
SwapBuffers(hdc);
}
Upvotes: 0
Views: 151
Reputation: 67447
You really need to enable the debugging validation layer and check errors, because there's a whole bunch of easily catchable misconceptions in your code:
Don't use glBindAttribLocation
. iPos
is already (correctly) bound to layout location 0, you're just breaking it.
You didn't detach and delete your shader programs after you linked the program, thus leaking the memory.
Don't unbind the vertex and element buffers from your vertex array, that's the whole point of using it in the first place.
Related to 3, don't change the vertex and element buffers of your vertex array every render. You set up a vertex buffer once.
You're telling OpenGL that your index elements are bytes in your glDrawElements
and instead you upload them as 32-bit integers. Be consistent.
Upvotes: 1