-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update gl.cpp #2
base: master
Are you sure you want to change the base?
Changes from 12 commits
0c26fb3
179bc07
9f3b46c
a01369f
b53c81a
9ac3f0c
8e2e440
c0d23ed
8195e1d
1810fa1
ed7ac6a
ca935a2
a66153f
3b41ceb
6d897bd
15aced6
f30880e
93eb162
4d149bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,10 +5,17 @@ | |||||
#ifdef _TINSPIRE | ||||||
#include <libndls.h> | ||||||
#else | ||||||
#include <SDL/SDL.h> | ||||||
#include <assert.h> | ||||||
#include <signal.h> | ||||||
static SDL_Surface *scr; | ||||||
#ifdef _WIN32 | ||||||
#include <SDL.h> | ||||||
#include <signal.h> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be in the "not windows" block? |
||||||
#else | ||||||
#include <SDL/SDL.h> | ||||||
#endif | ||||||
SDL_Window* sdl_window; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should all be |
||||||
SDL_Renderer* sdl_renderer; | ||||||
|
||||||
SDL_Texture* sdl_texture; // send to gpu | ||||||
#endif | ||||||
|
||||||
#include "gl.h" | ||||||
|
@@ -62,9 +69,18 @@ void nglInit() | |||||
else | ||||||
lcd_init(SCR_320x240_565); | ||||||
#else | ||||||
SDL_Init(SDL_INIT_VIDEO); | ||||||
scr = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, SDL_SWSURFACE); | ||||||
SDL_CreateWindowAndRenderer(SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN, &sdl_window, &sdl_renderer); | ||||||
|
||||||
sdl_texture = SDL_CreateTexture(sdl_renderer, | ||||||
SDL_PIXELFORMAT_RGB565, | ||||||
SDL_TEXTUREACCESS_STREAMING, | ||||||
SCREEN_WIDTH, SCREEN_HEIGHT); | ||||||
|
||||||
SDL_SetWindowTitle(sdl_window, "nGl"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
#ifndef _WIN32 | ||||||
signal(SIGINT, SIG_DFL); | ||||||
#endif | ||||||
assert(scr); | ||||||
#endif | ||||||
|
||||||
|
@@ -84,7 +100,7 @@ void nglUninit() | |||||
#ifdef _TINSPIRE | ||||||
lcd_init(SCR_TYPE_INVALID); | ||||||
#else | ||||||
//TODO | ||||||
SDL_DestroyRenderer(sdl_renderer); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't there something like |
||||||
#endif | ||||||
} | ||||||
|
||||||
|
@@ -238,10 +254,10 @@ void nglDisplay() | |||||
else | ||||||
lcd_blit(screen, SCR_320x240_565); | ||||||
#else | ||||||
SDL_LockSurface(scr); | ||||||
std::copy(screen, screen + SCREEN_HEIGHT*SCREEN_WIDTH, reinterpret_cast<COLOR*>(scr->pixels)); | ||||||
SDL_UnlockSurface(scr); | ||||||
SDL_UpdateRect(scr, 0, 0, 0, 0); | ||||||
SDL_UpdateTexture(sdl_texture, NULL, screen, SCREEN_WIDTH*sizeof(COLOR)); | ||||||
SDL_RenderClear(sdl_renderer); | ||||||
SDL_RenderCopy(sdl_renderer, sdl_texture, NULL, NULL); | ||||||
SDL_RenderPresent(sdl_renderer); | ||||||
#endif | ||||||
|
||||||
#ifdef FPS_COUNTER | ||||||
|
@@ -354,29 +370,36 @@ GLFix nglZBufferAt(const unsigned int x, const unsigned int y) | |||||
return z_buffer[x + y * SCREEN_WIDTH]; | ||||||
} | ||||||
|
||||||
constexpr GLFix ABS(GLFix a) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This belongs into |
||||||
return a >= GLFix(0) ? a : -a; | ||||||
} | ||||||
|
||||||
//Doesn't interpolate colors even if enabled | ||||||
void nglDrawLine3D(const VERTEX *v1, const VERTEX *v2) | ||||||
{ | ||||||
//TODO: Z-Clipping | ||||||
// Z-Clipping! | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Proper Z clipping would involve only drawing the part of the line which fits into the view (z between |
||||||
if(v1->z < near_plane || v2->z < near_plane) | ||||||
return; | ||||||
|
||||||
VERTEX v1_p = *v1, v2_p = *v2; | ||||||
nglPerspective(&v1_p); | ||||||
Vogtinator marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
nglPerspective(&v2_p); | ||||||
|
||||||
const GLFix diff_x = v2_p.x - v1_p.x; | ||||||
const GLFix dy = (v2_p.y - v1_p.y) / diff_x; | ||||||
const GLFix diff_y = v2_p.y - v1_p.y; | ||||||
|
||||||
const COLOR c = v1_p.c; | ||||||
|
||||||
//Height > width? -> Interpolate X | ||||||
if(dy > GLFix(1) || dy < GLFix(-1)) | ||||||
//if(dy > GLFix(1) || dy < GLFix(-1)) | ||||||
|
||||||
// if check passes diff_y should always be non zero | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can be more confident here:
Suggested change
|
||||||
if (ABS(diff_x) < ABS(diff_y)) | ||||||
{ | ||||||
if(v2_p.y < v1_p.y) | ||||||
std::swap(v1_p, v2_p); | ||||||
|
||||||
const GLFix diff_y = v2_p.y - v1_p.y; | ||||||
|
||||||
const GLFix dx = (v2_p.x - v1_p.x) / diff_y; | ||||||
const GLFix dx = diff_x / diff_y; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it's correct to use those values after the It's easier to just always compute the diffs after the swap though - subtraction is cheap. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||||||
const GLFix dz = (v2_p.z - v1_p.z) / diff_y; | ||||||
|
||||||
int end_y = v2_p.y; | ||||||
|
@@ -394,10 +417,13 @@ void nglDrawLine3D(const VERTEX *v1, const VERTEX *v2) | |||||
} | ||||||
else | ||||||
{ | ||||||
const GLFix dy = diff_x != GLFix(0) ? (diff_y / diff_x) : diff_y; | ||||||
|
||||||
if(v2_p.x < v1_p.x) | ||||||
std::swap(v1_p, v2_p); | ||||||
|
||||||
const GLFix dz = (v2_p.z - v1_p.z) / diff_x; | ||||||
// still need ternary check (for const) since diff_x could be 0 | ||||||
const GLFix dz = diff_x != GLFix(0) ? (v2_p.z - v1_p.z) / diff_x : (v2_p.z - v1_p.z); | ||||||
|
||||||
int end_x = v2_p.x; | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#include <SDL.h>
should actually work for both