OpenGL三角形始终为白色

时间:2018-11-05 05:35:57

标签: c++ opengl

我正在尝试使用openGL渲染三角形,但是无论我做什么,它总是渲染为白色。这是完整的代码:

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <cstring> 
#include<stdlib.h>
#include<string>
#include<tuple>
#include "shader_init_utils_header.h"
#include<iostream>
#include<fstream>
#include <windows.h>
using namespace std;

LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
void EnableOpenGL(HWND hwnd, HDC*, HGLRC*);
void DisableOpenGL(HWND, HDC, HGLRC);


int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX wcex;
HWND hwnd;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL bQuit = FALSE;
float theta = 0.0f;

/* register window class */
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_OWNDC;
wcex.lpfnWndProc = WindowProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "GLSample";
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);;


if (!RegisterClassEx(&wcex))
    return 0;

/* create main window */
hwnd = CreateWindowEx(0,
    "GLSample",
    "OpenGL Sample",
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    256,
    256,
    NULL,
    NULL,
    hInstance,
    NULL);

ShowWindow(hwnd, nCmdShow);

/* enable OpenGL for the window */
EnableOpenGL(hwnd, &hDC, &hRC);

//Init Glew
glewExperimental = GL_TRUE;
glewInit();

//Create vertex array
GLfloat points[] = {
    0.0f, 0.0f, 0.0f,
    1.0f, 0.0f, 0.0f,
    1.0f, 1.0f, 0.0f
};

//Create vbo
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);

//Create vao
GLuint vao = 0;
glGenVertexArrays (1, &vao);
glBindVertexArray(vao);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);

const char* vertex_shader =
    "#version 430\n"
    "in vec3 vp;"
    "out vec4 vc;"
    "void main() {"
    "glPosition = vec4(vp, 1.0);"
    "vc = vec4(0.5, 0.5, 0.5, 1);"
    "};";

const char* fragment_shader =
    "#version 430\n"
    "out vec4 frag_color;"
    "void main() {"
    "frag_color = vec4(0.5, 0.5, 0.5, 1.0);"
    "};";

GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vertex_shader, NULL);
glCompileShader(vs);

GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &fragment_shader, NULL);
glCompileShader(fs);

GLuint sp = glCreateProgram();
glAttachShader(sp, vs);
glAttachShader(sp, fs);
glLinkProgram(sp);


/*
std::ofstream log;
log.open("log.txt");

//Load VS and FS
string vertex_shader_s = readShaderFile("vertex_shader.glsl");
string fragment_shader_s = readShaderFile("fragment_shader.glsl");
const char* vertex_shader = vertex_shader_s.c_str();
const char* fragment_shader = fragment_shader_s.c_str();
log.close();

//Compile and link shaders
GLuint vs, fs;
tie(vs, fs) = compile_shader(vertex_shader, fragment_shader);

//Create shader program
GLuint sp = createShaderProgram(vs, fs);
*/

/* program main loop */
while (!bQuit)
{
    /* check for messages */
    if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
        /* handle or dispatch messages */
        if (msg.message == WM_QUIT)
        {
            bQuit = TRUE;
        }
        else
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    else
    {
        /* OpenGL animation code goes here */

        //glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        glUseProgram(sp);

        glBindVertexArray(vao);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        //glPushMatrix();
        //glRotatef(theta, 0.0f, 0.0f, 1.0f);

        //glBegin(GL_TRIANGLES);

        //glColor3f(1.0f, 0.0f, 0.0f);   glVertex2f(0.0f, 1.0f);
        //glColor3f(0.0f, 1.0f, 0.0f);   glVertex2f(0.87f, -0.5f);
        //glColor3f(0.0f, 0.0f, 1.0f);   glVertex2f(-0.87f, -0.5f);

        //glEnd();

        //glPopMatrix();

        SwapBuffers(hDC);

        //theta += 1.0f;
        Sleep(1);
    }
}

/* shutdown OpenGL */
DisableOpenGL(hwnd, hDC, hRC);

/* destroy the window explicitly */
DestroyWindow(hwnd);

return msg.wParam;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CLOSE:
    PostQuitMessage(0);
    break;

case WM_DESTROY:
    return 0;

case WM_KEYDOWN:
{
    switch (wParam)
    {
    case VK_ESCAPE:
        PostQuitMessage(0);
        break;
    }
}
break;

default:
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

return 0;
}

void EnableOpenGL(HWND hwnd, HDC* hDC, HGLRC* hRC)
{
PIXELFORMATDESCRIPTOR pfd;

int iFormat;

/* get the device context (DC) */
*hDC = GetDC(hwnd);

/* set the pixel format for the DC */
ZeroMemory(&pfd, sizeof(pfd));

pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW |
    PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;

iFormat = ChoosePixelFormat(*hDC, &pfd);

SetPixelFormat(*hDC, iFormat, &pfd);

/* create and enable the render context (RC) */
*hRC = wglCreateContext(*hDC);

wglMakeCurrent(*hDC, *hRC);
}

void DisableOpenGL(HWND hwnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hRC);
ReleaseDC(hwnd, hDC);
}

有人可以指出这里有什么问题吗? 我浏览了一些在线文章,其中提到了vertex_shader和fragment_shader字符串可能是问题,所以我现在内联了他们的代码。

1 个答案:

答案 0 :(得分:1)

顶点着色器位置输出内置变量称为gl_Position,而不是glPosition。注意下划线。