[英]OpenGL/SDL - Can't make line load
I've been trying to make a code so that a line appears across the window.我一直在尝试编写代码,以便在窗口中出现一条线。 I've been using c and opengl/sdl.我一直在使用 c 和 opengl/sdl。 But for some reason the code is not making a line in the window.但由于某种原因,代码没有在窗口中划一条线。 I can't figure out what I'm doing wrong:我无法弄清楚我做错了什么:
#include <SDL.h>
#include <stdio.h>
#include <GL/glew.h>
#include <SDL_opengl.h>
int main(int argc, char* argv[])
{
SDL_Window* window;
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);;
window = SDL_CreateWindow("Breakout!", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 400, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
SDL_GLContext context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, context);
SDL_GL_SetSwapInterval(1);
glClearColor(1.f, 1.f, 1.f, 1.f);
glViewport(0, 0, 800, 400);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glDisable(GL_DEPTH_TEST);
// Check that the window was successfully created
if (window == NULL) {
// In the case window was not made
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
SDL_Event windowEvent;
while (1)
{
// Get the next event
SDL_Event event;
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
// Break out of the loop on quit
break;
}
}
//Rendering to the screen
glClear(GL_COLOR_BUFFER_BIT);
glOrtho(0.0, 800, 400, 0.0, -1, 1); // setup a 10x10x2 viewing world
glColor4ub(255, 0, 0, 255);
glBegin(GL_LINES);
glVertex2f(0, 0);
glVertex2f(800, 400);
glEnd();
//Render
SDL_GL_SwapWindow(window);
}
// Close and destroy the window
SDL_DestroyWindow(window);
SDL_GL_DeleteContext(window);
// Clean up
SDL_Quit();
return 0;
}
glOrtho
doesn't just set a matrix. glOrtho
不只是设置一个矩阵。 The function creates an orthographic projection matrix and multiplies the current matrix with the new matrix.该函数创建一个正交投影矩阵,并将当前矩阵与新矩阵相乘。
If you call glOrtho
in the application loop, you must first load the identity matrix with glLoadIdentity
:如果在应用程序循环中调用glOrtho
,则必须首先使用glLoadIdentity
加载单位矩阵:
glLoadIdentity();
glOrtho(0.0, 800, 400, 0.0, -1, 1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.